Sat, 14 Dec 2019 21:04:47 +0100
Added iSpindel monitor screen design.
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); |
0 | 106 | topic = NULL; |
107 | mqtt_status = STATUS_CONNACK_RECVD; | |
108 | } else { | |
109 | syslog(LOG_NOTICE, "MQTT: my_connect_callback: %s\n", mosquitto_connack_string(result)); | |
110 | } | |
111 | } | |
112 | ||
113 | ||
114 | ||
115 | void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc) | |
116 | { | |
117 | if (mqtt_my_shutdown) { | |
118 | syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host); | |
119 | mqtt_connected = FALSE; | |
120 | } else { | |
121 | /* | |
122 | * The remote server was brought down. We must keep running | |
123 | */ | |
124 | syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host); | |
125 | mqtt_connect_lost = TRUE; | |
126 | } | |
127 | } | |
128 | ||
129 | ||
130 | ||
131 | void my_publish_callback(struct mosquitto *my_mosq, void *obj, int mid) | |
132 | { | |
133 | mqtt_last_mid_sent = mid; | |
134 | } | |
135 | ||
136 | ||
137 | ||
138 | void my_subscribe_callback(struct mosquitto *my_mosq, void *userdata, int mid, int qos_count, const int *granted_qos) | |
139 | { | |
140 | int i; | |
141 | ||
142 | syslog(LOG_NOTICE, "Subscribed (mid: %d): %d", mid, granted_qos[0]); | |
143 | for (i = 1; i < qos_count; i++) { | |
144 | syslog(LOG_NOTICE, " %d", granted_qos[i]); | |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | ||
150 | void my_log_callback(struct mosquitto *my_mosq, void *obj, int level, const char *str) | |
151 | { | |
152 | syslog(LOG_NOTICE, "MQTT: %s", str); | |
153 | if (debug) | |
154 | fprintf(stdout, "MQTT: %s\n", str); | |
155 | } | |
156 | ||
157 | ||
158 | ||
159 | void my_message_callback(struct mosquitto *my_mosq, void *userdata, const struct mosquitto_message *message) | |
160 | { | |
161 | if (message->payloadlen) { | |
162 | // TODO: process subscribed topics here. | |
163 | if (strstr(message->topic, (char *)"NBIRTH") || strstr(message->topic, (char *)"NDATA")) { | |
164 | node_birth_data(message->topic, (char *)message->payload); | |
165 | return; | |
166 | } | |
167 | if (strstr(message->topic, (char *)"fermenters") && (strstr(message->topic, (char *)"DBIRTH") || strstr(message->topic, (char *)"DDATA"))) { | |
168 | fermenter_birth_data(message->topic, (char *)message->payload); | |
169 | return; | |
170 | } | |
171 | if (strstr(message->topic, (char *)"fermenters") && strstr(message->topic, (char *)"DLOG")) { | |
172 | fermenter_log(message->topic, (char *)message->payload); | |
173 | return; | |
174 | } | |
502
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
175 | 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
|
176 | 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
|
177 | return; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
178 | } |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
179 | 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
|
180 | co2meter_log(message->topic, (char *)message->payload); |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
181 | return; |
a8a6901b5a99
Added CO2 meter units to the daemon.
Michiel Broek <mbroek@mbse.eu>
parents:
501
diff
changeset
|
182 | } |
567
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
183 | if (strstr(message->topic, (char *)"ispindel")) { |
6bf0afc33e70
Initial code for iSpindel support in the daemon
Michiel Broek <mbroek@mbse.eu>
parents:
502
diff
changeset
|
184 | 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
|
185 | } |
0 | 186 | syslog(LOG_NOTICE, "MQTT: message callback %s :: %d", message->topic, message->payloadlen); |
187 | } else { | |
188 | if (strstr(message->topic, (char *)"NBIRTH")) { | |
189 | // Ignore ?? | |
190 | fprintf(stdout, "MQTT: %s NULL\n", message->topic); | |
191 | return; | |
192 | } | |
193 | if (strstr(message->topic, (char *)"NDEATH")) { | |
194 | node_death(message->topic); | |
195 | return; | |
196 | } | |
197 | if (strstr(message->topic, (char *)"fermenters") && strstr(message->topic, (char *)"DDEATH")) { | |
198 | fermenter_death(message->topic); | |
199 | return; | |
200 | } | |
201 | syslog(LOG_NOTICE, "MQTT: message callback %s (null)", message->topic); | |
202 | } | |
203 | } | |
204 | ||
205 | ||
206 | ||
207 | void publisher(struct mosquitto *my_mosq, char *topic, char *payload, bool retain) | |
208 | { | |
209 | // publish the data | |
210 | if (payload) | |
211 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, strlen(payload), payload, mqtt_qos, retain); | |
212 | else | |
213 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, 0, NULL, mqtt_qos, retain); | |
214 | } | |
215 | ||
216 | ||
217 | ||
218 | void publishNData(bool birth, int flag) | |
219 | { | |
220 | char *topic = NULL, *payload = NULL; | |
221 | struct utsname ubuf; | |
222 | bool comma = false; | |
223 | ||
224 | payload = payload_header(); | |
225 | payload = xstrcat(payload, (char *)"{"); | |
226 | ||
227 | if (birth || flag & MQTT_NODE_CONTROL) { | |
228 | payload = xstrcat(payload, (char *)"\"nodecontrol\":{\"reboot\":false,\"rebirth\":false,\"nextserver\":false,\"scanrate\":3000}"); | |
229 | comma = true; | |
230 | } | |
231 | ||
232 | if (birth) { | |
233 | if (comma) | |
234 | payload = xstrcat(payload, (char *)","); | |
235 | payload = xstrcat(payload, (char *)"\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\"Unknown\""); | |
236 | if (uname(&ubuf) == 0) { | |
237 | payload = xstrcat(payload, (char *)",\"os\":\""); | |
238 | payload = xstrcat(payload, ubuf.sysname); | |
239 | payload = xstrcat(payload, (char *)"\",\"os_version\":\""); | |
240 | payload = xstrcat(payload, ubuf.release); | |
241 | payload = xstrcat(payload, (char *)"\""); | |
242 | } else { | |
243 | payload = xstrcat(payload, (char *)",\"os\":\"Unknown\",\"os_version\":\"Unknown\""); | |
244 | } | |
245 | ||
246 | payload = xstrcat(payload, (char *)",\"FW\":\""); | |
247 | payload = xstrcat(payload, (char *)VERSION); | |
248 | payload = xstrcat(payload, (char *)"\"}"); | |
249 | comma = true; | |
250 | } | |
251 | payload = xstrcat(payload, (char *)"}}"); | |
252 | ||
253 | if (birth) { | |
254 | topic = topic_base((char *)"NBIRTH"); | |
255 | publisher(mosq, topic, payload, true); | |
256 | } else { | |
257 | topic = topic_base((char *)"NDATA"); | |
258 | publisher(mosq, topic, payload, false); | |
259 | } | |
260 | ||
261 | free(payload); | |
262 | payload = NULL; | |
263 | free(topic); | |
264 | topic = NULL; | |
265 | } | |
266 | ||
267 | ||
268 | ||
269 | int mqtt_connect(void) | |
270 | { | |
271 | char *id = NULL, *topic = NULL; | |
272 | char err[1024]; | |
273 | int rc; | |
274 | ||
275 | /* | |
276 | * Initialize mosquitto communication | |
277 | */ | |
278 | gethostname(my_hostname, 255); | |
279 | mosquitto_lib_init(); | |
280 | id = xstrcpy((char *)"bmsd/"); | |
281 | id = xstrcat(id, my_hostname); | |
282 | if (strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { | |
283 | /* | |
284 | * Enforce maximum client id length of 23 characters | |
285 | */ | |
286 | id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; | |
287 | } | |
288 | ||
289 | mosq = mosquitto_new(id, TRUE, NULL); | |
290 | if (!mosq) { | |
291 | switch(errno) { | |
292 | case ENOMEM: | |
293 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Out of memory"); | |
294 | break; | |
295 | case EINVAL: | |
296 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Invalid id"); | |
297 | break; | |
298 | } | |
299 | mosquitto_lib_cleanup(); | |
300 | return 1; | |
301 | } | |
302 | free(id); | |
303 | id = NULL; | |
304 | ||
305 | /* | |
306 | * Set our will | |
307 | */ | |
308 | topic = topic_base((char *)"NDEATH"); | |
309 | if ((rc = mosquitto_will_set(mosq, topic, 0, NULL, mqtt_qos, false))) { | |
310 | if (rc > MOSQ_ERR_SUCCESS) | |
311 | syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: %s", mosquitto_strerror(rc)); | |
312 | mosquitto_lib_cleanup(); | |
313 | return 2; | |
314 | } | |
315 | free(topic); | |
316 | topic = NULL; | |
317 | ||
318 | if (debug) | |
319 | mosquitto_log_callback_set(mosq, my_log_callback); | |
320 | ||
321 | /* | |
322 | * Username/Password | |
323 | */ | |
324 | if (Config.mqtt_user) { | |
325 | if (Config.mqtt_pass) { | |
326 | rc = mosquitto_username_pw_set(mosq, Config.mqtt_user, Config.mqtt_pass); | |
327 | } else { | |
328 | rc = mosquitto_username_pw_set(mosq, Config.mqtt_user, NULL); | |
329 | } | |
330 | if (rc == MOSQ_ERR_INVAL) { | |
331 | syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: input parameters invalid"); | |
332 | } else if (rc == MOSQ_ERR_NOMEM) { | |
333 | syslog(LOG_NOTICE, "MQTT: mosquitto_username_pw_set: Out of Memory"); | |
334 | } | |
335 | if (rc != MOSQ_ERR_SUCCESS) { | |
336 | mosquitto_lib_cleanup(); | |
337 | return 3; | |
338 | } | |
339 | } | |
340 | ||
341 | mosquitto_max_inflight_messages_set(mosq, max_inflight); | |
342 | mosquitto_connect_callback_set(mosq, my_connect_callback); | |
343 | mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); | |
344 | mosquitto_publish_callback_set(mosq, my_publish_callback); | |
345 | mosquitto_message_callback_set(mosq, my_message_callback); | |
346 | mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); | |
347 | ||
348 | if ((rc = mosquitto_connect(mosq, Config.mqtt_host, Config.mqtt_port, keepalive))) { | |
349 | if (rc == MOSQ_ERR_ERRNO) { | |
350 | strerror_r(errno, err, 1024); | |
351 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: error: %s", err); | |
352 | } else { | |
353 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: unable to connect (%d)", rc); | |
354 | } | |
355 | mosquitto_lib_cleanup(); | |
356 | syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker."); | |
357 | return 4; | |
358 | } else { | |
359 | mqtt_use = TRUE; | |
360 | syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port); | |
361 | ||
362 | /* | |
363 | * Initialise is complete, report our presence state | |
364 | */ | |
365 | mosquitto_loop_start(mosq); | |
366 | publishNData(true, 0); | |
367 | } | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | ||
373 | ||
374 | void mqtt_disconnect(void) | |
375 | { | |
376 | int rc; | |
377 | char *topic = NULL; | |
378 | ||
379 | if (mqtt_use) { | |
380 | /* | |
381 | * Final publish 0 to clients/<hostname>/bmsd/state | |
382 | * After that, remove the retained topic. | |
383 | */ | |
384 | syslog(LOG_NOTICE, "MQTT disconnecting"); | |
385 | topic = topic_base((char *)"NBIRTH"); | |
386 | publisher(mosq, topic, NULL, true); | |
387 | free(topic); | |
388 | topic = topic_base((char *)"NDEATH"); | |
389 | publisher(mosq, topic, NULL, true); | |
390 | free(topic); | |
391 | topic = NULL; | |
392 | mqtt_last_mid = mqtt_mid_sent; | |
393 | mqtt_status = STATUS_WAITING; | |
394 | mqtt_my_shutdown = TRUE; | |
395 | ||
396 | do { | |
397 | if (mqtt_status == STATUS_WAITING) { | |
398 | if (debug) | |
399 | fprintf(stdout, (char *)"Waiting\n"); | |
400 | if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == FALSE) { | |
401 | mosquitto_disconnect(mosq); | |
402 | mqtt_disconnect_sent = TRUE; | |
403 | } | |
404 | usleep(100000); | |
405 | } | |
406 | rc = MOSQ_ERR_SUCCESS; | |
407 | } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected); | |
408 | ||
409 | mosquitto_loop_stop(mosq, FALSE); | |
410 | mosquitto_destroy(mosq); | |
411 | mosquitto_lib_cleanup(); | |
412 | mqtt_use = FALSE; | |
413 | mqtt_status = STATUS_CONNECTING; | |
414 | mqtt_mid_sent = 0; | |
415 | mqtt_last_mid = -1; | |
416 | mqtt_last_mid_sent = -1; | |
417 | mqtt_connected = TRUE; | |
418 | mqtt_disconnect_sent = FALSE; | |
419 | mqtt_connect_lost = FALSE; | |
420 | mqtt_my_shutdown = FALSE; | |
421 | syslog(LOG_NOTICE, "MQTT: disconnected"); | |
422 | } | |
423 | } | |
424 | ||
425 |