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