Sun, 15 Dec 2019 16:49:29 +0100
Fix a small bug in mon_nodes view. More relaxed check nodes timeout, now 5 seconds instead of 0,1 second.
0 | 1 | /***************************************************************************** |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
2 | * Copyright (C) 2017-2019 |
0 | 3 | * |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the bms (Brewery Management System) | |
7 | * | |
8 | * This is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * bms is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free | |
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | *****************************************************************************/ | |
22 | ||
23 | #include "bms.h" | |
24 | #include "xutil.h" | |
25 | #include "mqtt.h" | |
26 | #include "nodes.h" | |
27 | #include "fermenters.h" | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
28 | #include "co2meters.h" |
567
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
29 | #include "ispindels.h" |
0 | 30 | |
31 | ||
32 | extern sys_config Config; | |
33 | extern int debug; | |
34 | ||
35 | ||
36 | ||
37 | /* Global variables for use in callbacks. */ | |
38 | int mqtt_qos = 0; | |
39 | int mqtt_status = STATUS_CONNECTING; | |
40 | int mqtt_mid_sent = 0; | |
41 | int mqtt_last_mid = -1; | |
42 | int mqtt_last_mid_sent = -1; | |
43 | int mqtt_connected = TRUE; | |
44 | int mqtt_disconnect_sent = FALSE; | |
45 | int mqtt_connect_lost = FALSE; | |
46 | int mqtt_my_shutdown = FALSE; | |
47 | int mqtt_use = FALSE; | |
48 | int keepalive = 60; | |
49 | unsigned int max_inflight = 20; | |
50 | struct mosquitto *mosq = NULL; | |
51 | char *state = NULL; | |
52 | char my_hostname[256]; | |
53 | int Sequence = 0; | |
54 | ||
55 | ||
56 | char *payload_header(void) | |
57 | { | |
58 | static char *tmp; | |
59 | char buf[128]; | |
60 | ||
61 | tmp = xstrcpy((char *)"{\"timestamp\":"); | |
62 | sprintf(buf, "%ld", time(NULL)); | |
63 | tmp = xstrcat(tmp, buf); | |
64 | tmp = xstrcat(tmp, (char *)",\"seq\":"); | |
65 | sprintf(buf, "%d", Sequence++); | |
66 | tmp = xstrcat(tmp, buf); | |
67 | tmp = xstrcat(tmp, (char *)",\"metric\":"); | |
68 | return tmp; | |
69 | } | |
70 | ||
71 | ||
72 | ||
73 | char *topic_base(char *msgtype) | |
74 | { | |
75 | static char *tmp; | |
76 | ||
77 | tmp = xstrcpy((char *)"mbv1.0/brewery/"); | |
78 | tmp = xstrcat(tmp, msgtype); | |
79 | tmp = xstrcat(tmp, (char *)"/"); | |
80 | tmp = xstrcat(tmp, my_hostname); | |
81 | return tmp; | |
82 | } | |
83 | ||
84 | ||
85 | ||
86 | void my_connect_callback(struct mosquitto *my_mosq, void *obj, int result) | |
87 | { | |
88 | char *topic = NULL; | |
89 | ||
90 | if (mqtt_connect_lost) { | |
91 | mqtt_connect_lost = FALSE; | |
92 | syslog(LOG_NOTICE, "MQTT: reconnect: %s", mosquitto_connack_string(result)); | |
93 | } | |
94 | ||
95 | if (!result) { | |
96 | topic = topic_base((char *)"NCMD"); // TODO: do we need this?? | |
97 | topic = xstrcat(topic, (char *)"/#"); | |
98 | mosquitto_subscribe(mosq, NULL, topic, 0); | |
99 | free(topic); | |
501
9c41e865144a
Accept NBIRTH messages from co2meter modules.
Michiel Broek <mbroek@mbse.eu>
parents:
75
diff
changeset
|
100 | topic = xstrcpy((char *)"mbv1.0/fermenters/#"); // Subscribe to fermenter messages. |
0 | 101 | mosquitto_subscribe(mosq, NULL, topic, 0); |
102 | free(topic); | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
103 | topic = xstrcpy((char *)"mbv1.0/co2meters/#"); // Subscribe to co2meter messages. |
75
1a3c6480e057
Added support for brewcontrol nodes
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
104 | mosquitto_subscribe(mosq, NULL, topic, 0); |
1a3c6480e057
Added support for brewcontrol nodes
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
105 | free(topic); |
572
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
567
diff
changeset
|
106 | topic = xstrcpy((char *)"ispindel/#"); // Subscribe to ispindel messages. |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
567
diff
changeset
|
107 | mosquitto_subscribe(mosq, NULL, topic, 0); |
7a03181d29a3
Version 0.3.27 More code for iSpindels. All online/offline data in the database is now handles as integers. Nodes timeout use the configured interval time from the nodes.
Michiel Broek <mbroek@mbse.eu>
parents:
567
diff
changeset
|
108 | free(topic); |
0 | 109 | topic = NULL; |
110 | mqtt_status = STATUS_CONNACK_RECVD; | |
111 | } else { | |
112 | syslog(LOG_NOTICE, "MQTT: my_connect_callback: %s\n", mosquitto_connack_string(result)); | |
113 | } | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc) | |
119 | { | |
120 | if (mqtt_my_shutdown) { | |
121 | syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host); | |
122 | mqtt_connected = FALSE; | |
123 | } else { | |
124 | /* | |
125 | * The remote server was brought down. We must keep running | |
126 | */ | |
127 | syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host); | |
128 | mqtt_connect_lost = TRUE; | |
129 | } | |
130 | } | |
131 | ||
132 | ||
133 | ||
134 | void my_publish_callback(struct mosquitto *my_mosq, void *obj, int mid) | |
135 | { | |
136 | mqtt_last_mid_sent = mid; | |
137 | } | |
138 | ||
139 | ||
140 | ||
141 | void my_subscribe_callback(struct mosquitto *my_mosq, void *userdata, int mid, int qos_count, const int *granted_qos) | |
142 | { | |
143 | int i; | |
144 | ||
145 | syslog(LOG_NOTICE, "Subscribed (mid: %d): %d", mid, granted_qos[0]); | |
146 | for (i = 1; i < qos_count; i++) { | |
147 | syslog(LOG_NOTICE, " %d", granted_qos[i]); | |
148 | } | |
149 | } | |
150 | ||
151 | ||
152 | ||
153 | void my_log_callback(struct mosquitto *my_mosq, void *obj, int level, const char *str) | |
154 | { | |
155 | syslog(LOG_NOTICE, "MQTT: %s", str); | |
156 | if (debug) | |
157 | fprintf(stdout, "MQTT: %s\n", str); | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | void my_message_callback(struct mosquitto *my_mosq, void *userdata, const struct mosquitto_message *message) | |
163 | { | |
164 | if (message->payloadlen) { | |
165 | // TODO: process subscribed topics here. | |
166 | if (strstr(message->topic, (char *)"NBIRTH") || strstr(message->topic, (char *)"NDATA")) { | |
167 | node_birth_data(message->topic, (char *)message->payload); | |
168 | return; | |
169 | } | |
170 | if (strstr(message->topic, (char *)"fermenters") && (strstr(message->topic, (char *)"DBIRTH") || strstr(message->topic, (char *)"DDATA"))) { | |
171 | fermenter_birth_data(message->topic, (char *)message->payload); | |
172 | return; | |
173 | } | |
174 | if (strstr(message->topic, (char *)"fermenters") && strstr(message->topic, (char *)"DLOG")) { | |
175 | fermenter_log(message->topic, (char *)message->payload); | |
176 | return; | |
177 | } | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
178 | if (strstr(message->topic, (char *)"co2meters") && strstr(message->topic, (char *)"DBIRTH")) { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
179 | co2meter_birth_data(message->topic, (char *)message->payload); |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
180 | return; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
181 | } |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
182 | if (strstr(message->topic, (char *)"co2meters") && strstr(message->topic, (char *)"DLOG")) { |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
183 | co2meter_log(message->topic, (char *)message->payload); |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
184 | return; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
185 | } |
567
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
186 | if (strstr(message->topic, (char *)"ispindel")) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
187 | ispindel_mqtt(message->topic, (char *)message->payload); |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
188 | } |
0 | 189 | syslog(LOG_NOTICE, "MQTT: message callback %s :: %d", message->topic, message->payloadlen); |
190 | } else { | |
191 | if (strstr(message->topic, (char *)"NBIRTH")) { | |
192 | // Ignore ?? | |
193 | fprintf(stdout, "MQTT: %s NULL\n", message->topic); | |
194 | return; | |
195 | } | |
196 | if (strstr(message->topic, (char *)"NDEATH")) { | |
197 | node_death(message->topic); | |
198 | return; | |
199 | } | |
200 | if (strstr(message->topic, (char *)"fermenters") && strstr(message->topic, (char *)"DDEATH")) { | |
201 | fermenter_death(message->topic); | |
202 | return; | |
203 | } | |
204 | syslog(LOG_NOTICE, "MQTT: message callback %s (null)", message->topic); | |
205 | } | |
206 | } | |
207 | ||
208 | ||
209 | ||
210 | void publisher(struct mosquitto *my_mosq, char *topic, char *payload, bool retain) | |
211 | { | |
212 | // publish the data | |
213 | if (payload) | |
214 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, strlen(payload), payload, mqtt_qos, retain); | |
215 | else | |
216 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, 0, NULL, mqtt_qos, retain); | |
217 | } | |
218 | ||
219 | ||
220 | ||
221 | void publishNData(bool birth, int flag) | |
222 | { | |
223 | char *topic = NULL, *payload = NULL; | |
224 | struct utsname ubuf; | |
225 | bool comma = false; | |
226 | ||
227 | payload = payload_header(); | |
228 | payload = xstrcat(payload, (char *)"{"); | |
229 | ||
230 | if (birth || flag & MQTT_NODE_CONTROL) { | |
231 | payload = xstrcat(payload, (char *)"\"nodecontrol\":{\"reboot\":false,\"rebirth\":false,\"nextserver\":false,\"scanrate\":3000}"); | |
232 | comma = true; | |
233 | } | |
234 | ||
235 | if (birth) { | |
236 | if (comma) | |
237 | payload = xstrcat(payload, (char *)","); | |
238 | payload = xstrcat(payload, (char *)"\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\"Unknown\""); | |
239 | if (uname(&ubuf) == 0) { | |
240 | payload = xstrcat(payload, (char *)",\"os\":\""); | |
241 | payload = xstrcat(payload, ubuf.sysname); | |
242 | payload = xstrcat(payload, (char *)"\",\"os_version\":\""); | |
243 | payload = xstrcat(payload, ubuf.release); | |
244 | payload = xstrcat(payload, (char *)"\""); | |
245 | } else { | |
246 | payload = xstrcat(payload, (char *)",\"os\":\"Unknown\",\"os_version\":\"Unknown\""); | |
247 | } | |
248 | ||
249 | payload = xstrcat(payload, (char *)",\"FW\":\""); | |
250 | payload = xstrcat(payload, (char *)VERSION); | |
251 | payload = xstrcat(payload, (char *)"\"}"); | |
252 | comma = true; | |
253 | } | |
254 | payload = xstrcat(payload, (char *)"}}"); | |
255 | ||
256 | if (birth) { | |
257 | topic = topic_base((char *)"NBIRTH"); | |
258 | publisher(mosq, topic, payload, true); | |
259 | } else { | |
260 | topic = topic_base((char *)"NDATA"); | |
261 | publisher(mosq, topic, payload, false); | |
262 | } | |
263 | ||
264 | free(payload); | |
265 | payload = NULL; | |
266 | free(topic); | |
267 | topic = NULL; | |
268 | } | |
269 | ||
270 | ||
271 | ||
272 | int mqtt_connect(void) | |
273 | { | |
274 | char *id = NULL, *topic = NULL; | |
275 | char err[1024]; | |
276 | int rc; | |
277 | ||
278 | /* | |
279 | * Initialize mosquitto communication | |
280 | */ | |
281 | gethostname(my_hostname, 255); | |
282 | mosquitto_lib_init(); | |
283 | id = xstrcpy((char *)"bmsd/"); | |
284 | id = xstrcat(id, my_hostname); | |
285 | if (strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { | |
286 | /* | |
287 | * Enforce maximum client id length of 23 characters | |
288 | */ | |
289 | id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; | |
290 | } | |
291 | ||
292 | mosq = mosquitto_new(id, TRUE, NULL); | |
293 | if (!mosq) { | |
294 | switch(errno) { | |
295 | case ENOMEM: | |
296 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Out of memory"); | |
297 | break; | |
298 | case EINVAL: | |
299 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Invalid id"); | |
300 | break; | |
301 | } | |
302 | mosquitto_lib_cleanup(); | |
303 | return 1; | |
304 | } | |
305 | free(id); | |
306 | id = NULL; | |
307 | ||
308 | /* | |
309 | * Set our will | |
310 | */ | |
311 | topic = topic_base((char *)"NDEATH"); | |
312 | if ((rc = mosquitto_will_set(mosq, topic, 0, NULL, mqtt_qos, false))) { | |
313 | if (rc > MOSQ_ERR_SUCCESS) | |
314 | syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: %s", mosquitto_strerror(rc)); | |
315 | mosquitto_lib_cleanup(); | |
316 | return 2; | |
317 | } | |
318 | free(topic); | |
319 | topic = NULL; | |
320 | ||
321 | if (debug) | |
322 | mosquitto_log_callback_set(mosq, my_log_callback); | |
323 | ||
324 | /* | |
325 | * Username/Password | |
326 | */ | |
327 | if (Config.mqtt_user) { | |
328 | if (Config.mqtt_pass) { | |
329 | rc = mosquitto_username_pw_set(mosq, Config.mqtt_user, Config.mqtt_pass); | |
330 | } else { | |
331 | rc = mosquitto_username_pw_set(mosq, Config.mqtt_user, NULL); | |
332 | } | |
333 | if (rc == MOSQ_ERR_INVAL) { | |
334 | syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: input parameters invalid"); | |
335 | } else if (rc == MOSQ_ERR_NOMEM) { | |
336 | syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of Memory"); | |
337 | } | |
338 | if (rc != MOSQ_ERR_SUCCESS) { | |
339 | mosquitto_lib_cleanup(); | |
340 | return 3; | |
341 | } | |
342 | } | |
343 | ||
344 | mosquitto_max_inflight_messages_set(mosq, max_inflight); | |
345 | mosquitto_connect_callback_set(mosq, my_connect_callback); | |
346 | mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); | |
347 | mosquitto_publish_callback_set(mosq, my_publish_callback); | |
348 | mosquitto_message_callback_set(mosq, my_message_callback); | |
349 | mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); | |
350 | ||
351 | if ((rc = mosquitto_connect(mosq, Config.mqtt_host, Config.mqtt_port, keepalive))) { | |
352 | if (rc == MOSQ_ERR_ERRNO) { | |
353 | strerror_r(errno, err, 1024); | |
354 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: error: %s", err); | |
355 | } else { | |
356 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: unable to connect (%d)", rc); | |
357 | } | |
358 | mosquitto_lib_cleanup(); | |
359 | syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker."); | |
360 | return 4; | |
361 | } else { | |
362 | mqtt_use = TRUE; | |
363 | syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port); | |
364 | ||
365 | /* | |
366 | * Initialise is complete, report our presence state | |
367 | */ | |
368 | mosquitto_loop_start(mosq); | |
369 | publishNData(true, 0); | |
370 | } | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | ||
376 | ||
377 | void mqtt_disconnect(void) | |
378 | { | |
379 | int rc; | |
380 | char *topic = NULL; | |
381 | ||
382 | if (mqtt_use) { | |
383 | /* | |
384 | * Final publish 0 to clients/<hostname>/bmsd/state | |
385 | * After that, remove the retained topic. | |
386 | */ | |
387 | syslog(LOG_NOTICE, "MQTT disconnecting"); | |
388 | topic = topic_base((char *)"NBIRTH"); | |
389 | publisher(mosq, topic, NULL, true); | |
390 | free(topic); | |
391 | topic = topic_base((char *)"NDEATH"); | |
392 | publisher(mosq, topic, NULL, true); | |
393 | free(topic); | |
394 | topic = NULL; | |
395 | mqtt_last_mid = mqtt_mid_sent; | |
396 | mqtt_status = STATUS_WAITING; | |
397 | mqtt_my_shutdown = TRUE; | |
398 | ||
399 | do { | |
400 | if (mqtt_status == STATUS_WAITING) { | |
401 | if (debug) | |
402 | fprintf(stdout, (char *)"Waiting\n"); | |
403 | if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == FALSE) { | |
404 | mosquitto_disconnect(mosq); | |
405 | mqtt_disconnect_sent = TRUE; | |
406 | } | |
407 | usleep(100000); | |
408 | } | |
409 | rc = MOSQ_ERR_SUCCESS; | |
410 | } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected); | |
411 | ||
412 | mosquitto_loop_stop(mosq, FALSE); | |
413 | mosquitto_destroy(mosq); | |
414 | mosquitto_lib_cleanup(); | |
415 | mqtt_use = FALSE; | |
416 | mqtt_status = STATUS_CONNECTING; | |
417 | mqtt_mid_sent = 0; | |
418 | mqtt_last_mid = -1; | |
419 | mqtt_last_mid_sent = -1; | |
420 | mqtt_connected = TRUE; | |
421 | mqtt_disconnect_sent = FALSE; | |
422 | mqtt_connect_lost = FALSE; | |
423 | mqtt_my_shutdown = FALSE; | |
424 | syslog(LOG_NOTICE, "MQTT: disconnected"); | |
425 | } | |
426 | } | |
427 | ||
428 |