thermferm/server.c

changeset 533
49580ca85ab7
parent 529
00b45f2aaac3
child 534
92b546d4a839
--- 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);
+				}
+			    }
 			}
 		    }
 		}

mercurial