Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit e296064

Browse files
committed
flexible topics adaption from topics directory
1 parent d4e2970 commit e296064

File tree

4 files changed

+201
-21
lines changed

4 files changed

+201
-21
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import axios from "./http/axios.js";
77
import * as mqttUtils from "./mqtt/utils.js";
88
import * as mqttLogger from "./mqtt/logger.js";
99

10-
const subTopics = JSON.parse(fs.readFileSync("./sub_topics.json"));
10+
const topics = mqttUtils.parseTopicsDirectory();
1111

1212
const client = mqtt.connect(mqttUtils.formatConnectionOpts(config.mqtt));
1313

@@ -16,7 +16,7 @@ console.log(client.options);
1616
client.on("connect", () => {
1717
mqttLogger.connect();
1818

19-
client.subscribe(subTopics, (err, grant) => {
19+
client.subscribe(topics, (err, grant) => {
2020
if (err) mqttLogger.subscriptionError(err);
2121
mqttLogger.subscribed(grant);
2222
});

mqtt/utils.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as fs from "fs";
2+
13
const formatConnectionOpts = (obj) => {
24
return {
35
protocol: obj.protocol,
@@ -12,4 +14,21 @@ const formatConnectionOpts = (obj) => {
1214
};
1315
};
1416

15-
export { formatConnectionOpts };
17+
/**
18+
* @returns {array}
19+
*/
20+
const parseTopicsDirectory = () => {
21+
const topicsDirectory = JSON.parse(
22+
fs.readFileSync("./topics_directory.json")
23+
);
24+
25+
let topics = topicsDirectory.map((entry) => {
26+
return `"${entry.topic}": {"qos": ${entry.qos}}`
27+
.replace("%u", "+")
28+
.replace("%d", "+");
29+
});
30+
31+
return JSON.parse("{" + topics.join(",") + "}");
32+
};
33+
34+
export { formatConnectionOpts, parseTopicsDirectory };

sub_topics.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

topics_directory.json

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
[
2+
{
3+
"topic": "%u/%d/sensor/OWM/actualWeather",
4+
"qos": 2,
5+
"frequency_event": "1h",
6+
"retain": true,
7+
"description": "Fichier JSON sur les données météorologique actuel",
8+
"type": "json",
9+
"format": {
10+
"humidity": ["required", "numeric", "min:0", "max:100"],
11+
"rainfall": ["required", "numeric", "min:0", "max:1000"],
12+
"pressure": ["required", "numeric", "min:800", "max:1200"],
13+
"temperature": ["required", "numeric", "min:-100", "max:100"],
14+
"wind_speed": ["required", "numeric", "min:0", "max:1000"]
15+
}
16+
},
17+
{
18+
"topic": "%u/%d/sensor/OWM/dailyWeather",
19+
"qos": 2,
20+
"frequency_event": "24h",
21+
"retain": false,
22+
"description": "Fichier JSON sur les données météorologique du jour",
23+
"type": "json",
24+
"format": {
25+
"humidity": ["required", "numeric", "min:0", "max:100"],
26+
"rainfall": ["required", "numeric", "min:0", "max:1000"],
27+
"pressure": ["required", "numeric", "min:800", "max:1200"],
28+
"temperature": ["required", "numeric", "min:-100", "max:100"],
29+
"temperature_max": ["required", "numeric", "min:-100", "max:100"],
30+
"temperature_min": ["required", "numeric", "min:-100", "max:100"],
31+
"wind_speed": ["required", "numeric", "min:0", "max:100"]
32+
}
33+
},
34+
{
35+
"topic": "%u/%d/actuator/irrignnov_V1/state",
36+
"qos": 2,
37+
"frequency_event": "event",
38+
"retain": true,
39+
"description": "Etat de l'irrigation",
40+
"type": "boolean",
41+
"format": ["required", "boolean"]
42+
},
43+
{
44+
"topic": "%u/%d/actuator/irrignnov_V1/last_irrigation_begin",
45+
"qos": 2,
46+
"frequency_event": "event",
47+
"retain": false,
48+
"description": "Heure du début de la dernière irrigation",
49+
"type": "integer",
50+
"format": ["required", "integer", "min:1"]
51+
},
52+
{
53+
"topic": "%u/%d/actuator/irrignnov_V1/last_irrigation_end",
54+
"qos": 2,
55+
"frequency_event": "event",
56+
"retain": false,
57+
"description": "Heure de la fin de la dernière irrigation",
58+
"type": "integer",
59+
"format": ["required", "integer", "min:1"]
60+
},
61+
{
62+
"topic": "%u/%d/actuator/irrignnov_V1/method",
63+
"qos": 2,
64+
"frequency_event": "event",
65+
"retain": true,
66+
"description": "Sélection de la méthode d'irrigation (2 : goutte à goutte, 1: asperseur, 0 : désactivé l'irrigation)",
67+
"type": "integer",
68+
"format": ["required", "integer", "in:0,1,2"]
69+
},
70+
{
71+
"topic": "%u/%d/actuator/irrignnov_V1/kc",
72+
"qos": 2,
73+
"frequency_event": "event",
74+
"retain": true,
75+
"description": "Valeur du coefficient cultural",
76+
"type": "float",
77+
"format": ["required", "numeric", "min:0", "max:2"]
78+
},
79+
{
80+
"topic": "%u/%d/actuator/irrignnov_V1/drip",
81+
"qos": 2,
82+
"frequency_event": "event",
83+
"retain": true,
84+
"description": "Vecteur de 3 valeurs pour le paramétrage goutte à goutte",
85+
"type": "json",
86+
"format": [
87+
["required", "numeric", "min:0", "max:100"],
88+
["required", "integer", "min:0", "max:1000"],
89+
["required", "integer", "min:0", "max:1000"]
90+
]
91+
},
92+
{
93+
"topic": "%u/%d/actuator/irrignnov_V1/sprinkler",
94+
"qos": 2,
95+
"frequency_event": "event",
96+
"retain": true,
97+
"description": "Vecteur de 4 valeurs pour le paramétrage asperseur",
98+
"type": "json",
99+
"format": [
100+
["required", "numeric", "min:0", "max:100"],
101+
["required", "integer", "min:0", "max:10000"],
102+
["required", "integer", "min:0", "max:10000"],
103+
["required", "numeric", "min:0", "max:20"]
104+
]
105+
},
106+
{
107+
"topic": "%u/%d/actuator/irrignnov_V1/time",
108+
"qos": 2,
109+
"frequency_event": "event",
110+
"retain": true,
111+
"description": "Heure d'irrigation",
112+
"type": "string",
113+
"format": ["required", "date_format:hh:mm"]
114+
},
115+
{
116+
"topic": "%u/%d/actuator/irrignnov_V1/frequence",
117+
"qos": 2,
118+
"frequency_event": "event",
119+
"retain": true,
120+
"description": "Fréquence d'irrigation ( en jour(s) )",
121+
"type": "integer",
122+
"format": ["required", "integer"]
123+
},
124+
{
125+
"topic": "%u/%d/global/crop",
126+
"qos": 2,
127+
"frequency_event": "event",
128+
"retain": true,
129+
"description": "Nom de la culture actuel",
130+
"type": "string",
131+
"format": ["required", "string"]
132+
},
133+
{
134+
"topic": "%u/%d/global/position",
135+
"qos": 2,
136+
"frequency_event": "event",
137+
"retain": true,
138+
"description": "Position du champ Vecteur de 2 (Latitude et Longitude)",
139+
"type": "json",
140+
"format": [
141+
["required", "numeric"],
142+
["required", "numeric"]
143+
]
144+
},
145+
{
146+
"topic": "%u/%d/global/area",
147+
"qos": 2,
148+
"frequency_event": "event",
149+
"retain": true,
150+
"description": "Surface de la parcelle en m²",
151+
"type": "float",
152+
"format": ["required", "numeric", "min:0", "max:1000000"]
153+
},
154+
{
155+
"topic": "%u/%d/sensor/irrignnov_V1/etm",
156+
"qos": 2,
157+
"frequency_event": "24h",
158+
"retain": false,
159+
"description": "Valeur de l'évapotranspiration du jour",
160+
"type": "float",
161+
"format": ["required", "numeric", "min:-1000", "max:1000"]
162+
},
163+
{
164+
"topic": "%u/%d/sensor/irrignnov_V1/prediction",
165+
"qos": 2,
166+
"frequency_event": "1h",
167+
"retain": true,
168+
"description": "Fichier JSON des prévisions d'irrigation de la semaines",
169+
"type": "json",
170+
"format": {
171+
"ET0": ["required"],
172+
"ET0.*": ["required", "numeric", "min:-1000", "max:1000"],
173+
"ETM": ["required"],
174+
"ETM.*": ["required", "numeric", "min:-1000", "max:1000"],
175+
"rain": ["required"],
176+
"rain.*": ["required", "numeric", "min:0", "max:1000"]
177+
}
178+
}
179+
]

0 commit comments

Comments
 (0)