Versie 0.6.3. MQTT device berichten alleen als een fermenter ingeschakeld is. MQTT fermenter birth en death berichhten als een fementer in of uitgeschakeld wordt. MQTT node death bericht bij normaal afsluiten van de daemon. Alle MQTT persistent berichten worden nu goed opgeruikmd.

Sat, 14 Jul 2018 17:21:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Jul 2018 17:21:25 +0200
changeset 533
49580ca85ab7
parent 532
34bf9f389749
child 534
92b546d4a839

Versie 0.6.3. MQTT device berichten alleen als een fermenter ingeschakeld is. MQTT fermenter birth en death berichhten als een fementer in of uitgeschakeld wordt. MQTT node death bericht bij normaal afsluiten van de daemon. Alle MQTT persistent berichten worden nu goed opgeruikmd.

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/mqtt.h file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/configure	Fri Aug 18 17:45:14 2017 +0200
+++ b/configure	Sat Jul 14 17:21:25 2018 +0200
@@ -2035,9 +2035,9 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.6.2"
-COPYRIGHT="Copyright (C) 2014-2017 Michiel Broek, All Rights Reserved"
-CYEARS="2014-2017"
+VERSION="0.6.3"
+COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
+CYEARS="2014-2018"
 
 
 
--- a/configure.ac	Fri Aug 18 17:45:14 2017 +0200
+++ b/configure.ac	Sat Jul 14 17:21:25 2018 +0200
@@ -8,9 +8,9 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.6.2"
-COPYRIGHT="Copyright (C) 2014-2017 Michiel Broek, All Rights Reserved"
-CYEARS="2014-2017"
+VERSION="0.6.3"
+COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
+CYEARS="2014-2018"
 AC_SUBST(PACKAGE)
 AC_SUBST(VERSION)
 AC_SUBST(COPYRIGHT)
--- 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 <mbroek at mbse dot eu>
  *
@@ -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;
--- a/thermferm/mqtt.h	Fri Aug 18 17:45:14 2017 +0200
+++ b/thermferm/mqtt.h	Sat Jul 14 17:21:25 2018 +0200
@@ -16,7 +16,30 @@
 void pub_domoticz_output(int, int);
 void mqtt_connect(void);
 void mqtt_disconnect(void);
-void publishDData(units_list *);
-void publishNData(bool, int);
+
+/**
+ * @brief Publish unit data.
+ * @param unit Unit data structure.
+ */
+void publishDData(units_list *unit);
+
+/**
+ * @brief Publish unit birth.
+ * @param unit Unit data structure.
+ */
+void publishDBirth(units_list *unit);
+
+/**
+ * @brief Publish death of a unit.
+ * @param unit Unit data structure.
+ */
+void publishDDeath(units_list *unit);
+
+/**
+ * @brief Publish Node data or birth message.
+ * @param birth If true send a birh message, else a regular data update.
+ * @param flag Bit flag to enable control messages.
+ */
+void publishNData(bool birth, int flag);
 
 #endif
--- a/thermferm/server.c	Fri Aug 18 17:45:14 2017 +0200
+++ b/thermferm/server.c	Sat Jul 14 17:21:25 2018 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2008-2017
+ * Copyright (C) 2008-2018
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -2099,6 +2099,7 @@
 		srv_send((char *)"NAME,%s", unit->name);
 		srv_send((char *)"UUID,%s", unit->uuid);
 		srv_send((char *)"ALIAS,%s", unit->alias);
+		srv_send((char *)"MODE,%s", UNITMODE[unit->mode]);
 		srv_send((char *)"VOLUME,%2f", unit->volume);
 		srv_send((char *)"AIR_ADDRESS,%s", unit->air_address);
 		srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
@@ -2154,7 +2155,6 @@
 		srv_send((char *)"PSU_ADDRESS,%s", unit->psu_address);
 		srv_send((char *)"PSU_STATE,%d", unit->psu_state);
 		srv_send((char *)"PSU_IDX,%d", unit->psu_idx);
-		srv_send((char *)"MODE,%s", UNITMODE[unit->mode]);
 		srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set);
 		srv_send((char *)"BEER_SET,%.1f", unit->beer_set);
 		srv_send((char *)"PROFILE,%s", unit->profile);
@@ -2484,12 +2484,16 @@
 			    } else if (val && (strcmp(kwd, (char *)"MODE") == 0)) {
 				for (i = 0; i < 5; i++) {
 				    if (strcmp(val, UNITMODE[i]) == 0) {
+					unit->mqtt_flag |= MQTT_FLAG_DATA;
 					/* Initialize log if the unit is turned on */
-					if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF))
+					if ((unit->mode == UNITMODE_OFF) && (i != UNITMODE_OFF)) {
 					    initlog(unit->name);
+					    unit->mqtt_flag |= MQTT_FLAG_BIRTH;
+					} else if ((unit->mode != UNITMODE_OFF) && (i == UNITMODE_OFF)) {
+					    unit->mqtt_flag |= MQTT_FLAG_DEATH;
+					}
 					syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
 					unit->mode = i;
-					unit->mqtt_flag |= MQTT_FLAG_DATA;
 					/* Allways turn everything off after a mode change */
 					unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
 					unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
@@ -2681,8 +2685,18 @@
 				}
 
 			    }
-			    if (unit->mqtt_flag)
-				publishDData(unit);
+			    if (unit->mqtt_flag) {
+				    if (debug)
+					    fprintf(stdout, "flag value %d\n", unit->mqtt_flag);
+				if (unit->mqtt_flag & MQTT_FLAG_BIRTH) {
+				    publishDBirth(unit);
+				} else {
+				    publishDData(unit);
+				}
+				if (unit->mqtt_flag & MQTT_FLAG_DEATH) {
+				    publishDDeath(unit);
+				}
+			    }
 			}
 		    }
 		}
--- a/thermferm/thermferm.c	Fri Aug 18 17:45:14 2017 +0200
+++ b/thermferm/thermferm.c	Sat Jul 14 17:21:25 2018 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014-2017
+ * Copyright (C) 2014-2018
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -391,11 +391,15 @@
  */
 void change_mode(int mode)
 {
-    if ((current_unit->mode == UNITMODE_OFF) && (mode != UNITMODE_OFF))
+    current_unit->mqtt_flag |= MQTT_FLAG_DATA;
+    if ((current_unit->mode == UNITMODE_OFF) && (mode != UNITMODE_OFF)) {
 	initlog(current_unit->name);
+	current_unit->mqtt_flag |= MQTT_FLAG_BIRTH;
+    } else if ((current_unit->mode != UNITMODE_OFF) && (mode == UNITMODE_OFF)) {
+	current_unit->mqtt_flag |= MQTT_FLAG_DEATH;
+    }
     syslog(LOG_NOTICE, "Mode from %s to %s via panel interface", UNITMODE[current_unit->mode], UNITMODE[mode]);
     current_unit->mode = mode;
-    current_unit->mqtt_flag |= MQTT_FLAG_DATA;
     /* Allways turn everything off after a mode change */
     current_unit->PID_cool->OutP = current_unit->PID_heat->OutP = 0.0;
     current_unit->PID_cool->Mode = current_unit->PID_heat->Mode = PID_MODE_NONE;
@@ -1217,7 +1221,8 @@
 	    LCDunit = 0;
 	    for (unit = Config.units; unit; unit = unit->next) {
 		LCDunit++;
-		unit->mqtt_flag = unit->alarm_flag = 0;
+		unit->mqtt_flag &= ~MQTT_FLAG_DATA;
+		unit->alarm_flag = 0;
 
 		if (unit->air_address) {
 		    rc = device_in(unit->air_address, &temp);
@@ -1850,7 +1855,17 @@
 		 * Publish MQTT messages set in flag
 		 */
 		if (unit->mqtt_flag) {
-		    publishDData(unit);
+		    if (unit->mqtt_flag & MQTT_FLAG_BIRTH) {
+			publishDBirth(unit);
+			unit->mqtt_flag &= ~MQTT_FLAG_BIRTH;
+		    } else {
+			publishDData(unit);
+			unit->mqtt_flag &= ~MQTT_FLAG_DATA;
+		    }
+		    if (unit->mqtt_flag & MQTT_FLAG_DEATH) {
+			publishDDeath(unit);
+			unit->mqtt_flag &= ~MQTT_FLAG_DEATH;
+		    }
 		}
 
 		/*
@@ -1984,8 +1999,14 @@
 	device_out(unit->fan_address, unit->fan_state);
 	pub_domoticz_output(unit->fan_idx, unit->fan_state);
 	device_out(unit->light_address, unit->light_state);
-	unit->mqtt_flag = MQTT_FLAG_DATA;
-	publishDData(unit);
+	if (unit->mode != UNITMODE_OFF) {
+	    /*
+	     * If unit ia active, publish we are dying.
+	     */
+	    unit->mqtt_flag = MQTT_FLAG_DATA;
+	    publishDData(unit);
+	    publishDDeath(unit);
+	}
 	syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]);
     }
 
--- a/thermferm/thermferm.h	Fri Aug 18 17:45:14 2017 +0200
+++ b/thermferm/thermferm.h	Sat Jul 14 17:21:25 2018 +0200
@@ -206,6 +206,8 @@
 #define	UNITMODE_PROFILE	4		/* Unit runs in profile mode	*/
 
 #define	MQTT_FLAG_DATA		0x0001		/* Show updated data values	*/
+#define	MQTT_FLAG_BIRTH		0x0002		/* Show birth instead of data	*/
+#define MQTT_FLAG_DEATH		0x0004		/* Show death of a unit		*/
 
 #define	MQTT_NODE_CONTROL	0x0001		/* Show node control		*/
 

mercurial