diff -r 34bf9f389749 -r 49580ca85ab7 thermferm/mqtt.c --- a/thermferm/mqtt.c Fri Aug 18 17:45:14 2017 +0200 +++ b/thermferm/mqtt.c Sat Jul 14 17:21:25 2018 +0200 @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (C) 2016-2017 + * Copyright (C) 2016-2018 * * Michiel Broek * @@ -419,7 +419,11 @@ -void publishDBirth(void) +/** + * @brief Publish DBIRTH for all active units. If there are no active units, don't + * publish anything. This function should be called at program start. + */ +void publishDBirthAll(void) { char *payload = NULL; units_list *unit; @@ -428,13 +432,17 @@ payload = payload_header(); payload = xstrcat(payload, (char *)"{\"units\":["); for (unit = Config.units; unit; unit = unit->next) { - if (comma) - payload = xstrcat(payload, (char *)","); - payload = xstrcat(payload, unit_data(unit, true)); - comma = TRUE; + if (unit->mode != UNITMODE_OFF) { + if (comma) + payload = xstrcat(payload, (char *)","); + payload = xstrcat(payload, unit_data(unit, true)); + comma = TRUE; + } } - payload = xstrcat(payload, (char *)"]}}"); - publisher(mosq, topic_base((char *)"DBIRTH"), payload, true); + if (comma) { // Only publish if there is at least one unit active. + payload = xstrcat(payload, (char *)"]}}"); + publisher(mosq, topic_base((char *)"DBIRTH"), payload, true); + } free(payload); payload = NULL; } @@ -465,6 +473,53 @@ +void publishDBirth(units_list *unit) +{ +#ifdef HAVE_MOSQUITTO_H + + char *payload = NULL, *topic = NULL; + + if (mqtt_use) { + payload = payload_header(); + payload = xstrcat(payload, unit_data(unit, true)); + payload = xstrcat(payload, (char *)"}"); + topic = xstrcat(topic_base((char *)"DBIRTH"), (char *)"/"); + topic = xstrcat(topic, unit->alias); + publisher(mosq, topic, payload, true); + free(payload); + payload = NULL; + free(topic); + topic = NULL; + } +#endif +} + + + +void publishDDeath(units_list *unit) +{ +#ifdef HAVE_MOSQUITTO_H + + char *topic = NULL; + + if (mqtt_use) { + // First delete presistent DBIRTH topic + topic = xstrcat(topic_base((char *)"DBIRTH"), (char *)"/"); + topic = xstrcat(topic, unit->alias); + publisher(mosq, topic, NULL, true); + free(topic); + topic = NULL; + topic = xstrcat(topic_base((char *)"DDEATH"), (char *)"/"); + topic = xstrcat(topic, unit->alias); + publisher(mosq, topic, NULL, true); + free(topic); + topic = NULL; + } +#endif +} + + + void publishNData(bool birth, int flag) { #ifdef HAVE_MOSQUITTO_H @@ -550,15 +605,6 @@ -void publishBirth(void) -{ - publishNData(true, 0); - publishDBirth(); -} - - - - void mqtt_connect(void) { #ifdef HAVE_MOSQUITTO_H @@ -630,7 +676,8 @@ * Initialise is complete, report our presence state */ mosquitto_loop_start(mosq); - publishBirth(); + publishNData(true, 0); + publishDBirthAll(); } #endif } @@ -648,8 +695,8 @@ * After that, remove the retained topic. */ syslog(LOG_NOTICE, "MQTT disconnecting"); - publisher(mosq, topic_base((char *)"DBIRTH"), NULL, true); publisher(mosq, topic_base((char *)"NBIRTH"), NULL, true); + publisher(mosq, topic_base((char *)"NDEATH"), NULL, true); mqtt_last_mid = mqtt_mid_sent; mqtt_status = STATUS_WAITING; mqtt_my_shutdown = TRUE;