bmsd/mqtt.c

changeset 747
b6fbe6821468
parent 679
48f8f3fce7c0
equal deleted inserted replaced
746:44d929ff268e 747:b6fbe6821468
1 /***************************************************************************** 1 /*****************************************************************************
2 * Copyright (C) 2017-2020 2 * Copyright (C) 2017-2021
3 * 3 *
4 * Michiel Broek <mbroek at mbse dot eu> 4 * Michiel Broek <mbroek at mbse dot eu>
5 * 5 *
6 * This file is part of the bms (Brewery Management System) 6 * This file is part of the bms (Brewery Management System)
7 * 7 *
38 int mqtt_qos = 0; 38 int mqtt_qos = 0;
39 int mqtt_status = STATUS_CONNECTING; 39 int mqtt_status = STATUS_CONNECTING;
40 int mqtt_mid_sent = 0; 40 int mqtt_mid_sent = 0;
41 int mqtt_last_mid = -1; 41 int mqtt_last_mid = -1;
42 int mqtt_last_mid_sent = -1; 42 int mqtt_last_mid_sent = -1;
43 int mqtt_connected = TRUE; 43 int mqtt_connected = true;
44 int mqtt_disconnect_sent = FALSE; 44 int mqtt_disconnect_sent = false;
45 int mqtt_connect_lost = FALSE; 45 int mqtt_connect_lost = false;
46 int mqtt_my_shutdown = FALSE; 46 int mqtt_my_shutdown = false;
47 int mqtt_use = FALSE; 47 int mqtt_use = false;
48 int keepalive = 60; 48 int keepalive = 60;
49 unsigned int max_inflight = 20; 49 unsigned int max_inflight = 20;
50 struct mosquitto *mosq = NULL; 50 struct mosquitto *mosq = NULL;
51 char *state = NULL; 51 char *state = NULL;
52 char my_hostname[256]; 52 char my_hostname[256];
86 void my_connect_callback(struct mosquitto *my_mosq, void *obj, int result) 86 void my_connect_callback(struct mosquitto *my_mosq, void *obj, int result)
87 { 87 {
88 char *topic = NULL; 88 char *topic = NULL;
89 89
90 if (mqtt_connect_lost) { 90 if (mqtt_connect_lost) {
91 mqtt_connect_lost = FALSE; 91 mqtt_connect_lost = false;
92 syslog(LOG_NOTICE, "MQTT: reconnect: %s", mosquitto_connack_string(result)); 92 syslog(LOG_NOTICE, "MQTT: reconnect: %s", mosquitto_connack_string(result));
93 } 93 }
94 94
95 if (!result) { 95 if (!result) {
96 topic = topic_base((char *)"NCMD"); // TODO: do we need this?? 96 topic = topic_base((char *)"NCMD"); // TODO: do we need this??
117 117
118 void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc) 118 void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc)
119 { 119 {
120 if (mqtt_my_shutdown) { 120 if (mqtt_my_shutdown) {
121 syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host); 121 syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host);
122 mqtt_connected = FALSE; 122 mqtt_connected = false;
123 } else { 123 } else {
124 /* 124 /*
125 * The remote server was brought down. We must keep running 125 * The remote server was brought down. We must keep running
126 */ 126 */
127 syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host); 127 syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host);
128 mqtt_connect_lost = TRUE; 128 mqtt_connect_lost = true;
129 } 129 }
130 } 130 }
131 131
132 132
133 133
298 * Enforce maximum client id length of 23 characters 298 * Enforce maximum client id length of 23 characters
299 */ 299 */
300 id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; 300 id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0';
301 } 301 }
302 302
303 mosq = mosquitto_new(id, TRUE, NULL); 303 mosq = mosquitto_new(id, true, NULL);
304 if (!mosq) { 304 if (!mosq) {
305 switch(errno) { 305 switch(errno) {
306 case ENOMEM: 306 case ENOMEM:
307 syslog(LOG_NOTICE, "MQTT: mosquitto_new: Out of memory"); 307 syslog(LOG_NOTICE, "MQTT: mosquitto_new: Out of memory");
308 break; 308 break;
368 } 368 }
369 mosquitto_lib_cleanup(); 369 mosquitto_lib_cleanup();
370 syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker."); 370 syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker.");
371 return 4; 371 return 4;
372 } else { 372 } else {
373 mqtt_use = TRUE; 373 mqtt_use = true;
374 syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port); 374 syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port);
375 375
376 /* 376 /*
377 * Initialise is complete, report our presence state 377 * Initialise is complete, report our presence state
378 */ 378 */
403 publisher(mosq, topic, NULL, true); 403 publisher(mosq, topic, NULL, true);
404 free(topic); 404 free(topic);
405 topic = NULL; 405 topic = NULL;
406 mqtt_last_mid = mqtt_mid_sent; 406 mqtt_last_mid = mqtt_mid_sent;
407 mqtt_status = STATUS_WAITING; 407 mqtt_status = STATUS_WAITING;
408 mqtt_my_shutdown = TRUE; 408 mqtt_my_shutdown = true;
409 409
410 do { 410 do {
411 if (mqtt_status == STATUS_WAITING) { 411 if (mqtt_status == STATUS_WAITING) {
412 if (debug) 412 if (debug)
413 fprintf(stdout, (char *)"Waiting\n"); 413 fprintf(stdout, (char *)"Waiting\n");
414 if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == FALSE) { 414 if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == false) {
415 mosquitto_disconnect(mosq); 415 mosquitto_disconnect(mosq);
416 mqtt_disconnect_sent = TRUE; 416 mqtt_disconnect_sent = true;
417 } 417 }
418 usleep(100000); 418 usleep(100000);
419 } 419 }
420 rc = MOSQ_ERR_SUCCESS; 420 rc = MOSQ_ERR_SUCCESS;
421 } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected); 421 } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected);
422 422
423 mosquitto_loop_stop(mosq, FALSE); 423 mosquitto_loop_stop(mosq, false);
424 mosquitto_destroy(mosq); 424 mosquitto_destroy(mosq);
425 mosquitto_lib_cleanup(); 425 mosquitto_lib_cleanup();
426 mqtt_use = FALSE; 426 mqtt_use = false;
427 mqtt_status = STATUS_CONNECTING; 427 mqtt_status = STATUS_CONNECTING;
428 mqtt_mid_sent = 0; 428 mqtt_mid_sent = 0;
429 mqtt_last_mid = -1; 429 mqtt_last_mid = -1;
430 mqtt_last_mid_sent = -1; 430 mqtt_last_mid_sent = -1;
431 mqtt_connected = TRUE; 431 mqtt_connected = true;
432 mqtt_disconnect_sent = FALSE; 432 mqtt_disconnect_sent = false;
433 mqtt_connect_lost = FALSE; 433 mqtt_connect_lost = false;
434 mqtt_my_shutdown = FALSE; 434 mqtt_my_shutdown = false;
435 syslog(LOG_NOTICE, "MQTT: disconnected"); 435 syslog(LOG_NOTICE, "MQTT: disconnected");
436 } 436 }
437 } 437 }
438 438
439 439

mercurial