thermferm/thermferm.c

changeset 506
cdcd07bbee30
parent 504
862de87f9f89
child 507
80344f433a78
--- a/thermferm/thermferm.c	Sat Apr 29 17:07:36 2017 +0200
+++ b/thermferm/thermferm.c	Mon May 08 16:26:02 2017 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014-2016
+ * Copyright (C) 2014-2017
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -1065,44 +1065,19 @@
 	/*
 	 * Safety, turn everything off
 	 */
-	unit->mqtt_flag = MQTT_FLAG_MODE;
+	unit->mqtt_flag = 0;
 	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0;
 	unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
-	mqtt_publish_int(unit->alias, (char *)"state", (unit->mode != UNITMODE_OFF) ? 1 : 0);
-	if (unit->name)
-	    mqtt_publish_str(unit->alias, (char *)"name", unit->name);
-	else
-	    mqtt_publish_clear(unit->alias, (char *)"name");
-	if (unit->heater_address)
-	    mqtt_publish_int(unit->alias, (char *)"heater", 0);
-	else
-	    mqtt_publish_clear(unit->alias, (char *)"heater");
-	if (unit->cooler_address)
-	    mqtt_publish_int(unit->alias, (char *)"cooler", 0);
-	else
-	    mqtt_publish_clear(unit->alias, (char *)"cooler");
-	if (unit->fan_address)
-	    mqtt_publish_int(unit->alias, (char *)"fan", 0);
-	else
-	    mqtt_publish_clear(unit->alias, (char *)"fan");
-	if (unit->air_address)
-	    unit->mqtt_flag |= MQTT_FLAG_AIR;
-	if (unit->beer_address)
-	    unit->mqtt_flag |= MQTT_FLAG_BEER;
 	if (unit->mode == UNITMODE_PROFILE) {
 	    if (!unit->profile)
 		syslog(LOG_NOTICE, "Starting unit `%s' in profile mode, no profile defined.", unit->name);
 	    else {
 	    	syslog(LOG_NOTICE, "Starting unit `%s' in profile state %s.", unit->name, PROFSTATE[unit->prof_state]);
-		unit->mqtt_flag |= MQTT_FLAG_SP;
-		unit->mqtt_flag |= MQTT_FLAG_PROFILE;
 	    }
 	} else if (unit->mode == UNITMODE_BEER) {
 	    syslog(LOG_NOTICE, "Starting unit `%s' beer cooler at %.1f degrees", unit->name, unit->beer_set);
-	    unit->mqtt_flag |= MQTT_FLAG_SP;
 	} else if (unit->mode == UNITMODE_FRIDGE) {
 	    syslog(LOG_NOTICE, "Starting unit `%s' as refridgerator at %.1f degrees", unit->name, unit->fridge_set);
-	    unit->mqtt_flag |= MQTT_FLAG_SP;
 	} else if (unit->mode == UNITMODE_NONE) {
 	    syslog(LOG_NOTICE, "Starting unit `%s' in inactive state", unit->name);
 	} else {
@@ -1184,11 +1159,12 @@
 #ifdef HAVE_WIRINGPI_H
 	    piUnlock(LOCK_LCD);
 #endif
+	    bool updateHT = false;
             if (Config.temp_address) {
 		rc = device_in(Config.temp_address, &temp);
 		if (rc == DEVPRESENT_YES) {
 		    if (Config.temp_value != temp)
-			mqtt_publish_float((char *)"room", (char *)"temperature", temp / 1000.0, 1);
+			updateHT = true;
 		    Config.temp_value = temp;
 		    Config.temp_state = 0;
 #ifdef HAVE_WIRINGPI_H
@@ -1218,7 +1194,7 @@
 		rc = device_in(Config.hum_address, &temp);
 		if (rc == DEVPRESENT_YES) {
 		    if (Config.hum_value != temp)
-			mqtt_publish_float((char *)"room", (char *)"humidity", temp / 1000.0, 1);
+			updateHT = true;
 		    Config.hum_value = temp;
 		    Config.hum_state = 0;
 #ifdef HAVE_WIRINGPI_H
@@ -1235,10 +1211,14 @@
 		}
 	    }
 	    row++;
+	    if (updateHT)
+		publishNData(false, MQTT_NODE_HT);
 
 	    LCDunit = 0;
 	    for (unit = Config.units; unit; unit = unit->next) {
 		LCDunit++;
+		unit->mqtt_flag = 0;
+
 		if (unit->air_address) {
 		    rc = device_in(unit->air_address, &temp);
 		    if (rc == DEVPRESENT_YES) {
@@ -1297,13 +1277,13 @@
 			    if (unit->door_state == 0) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name);
 			    	unit->door_state = 1;
-				mqtt_publish_str(unit->alias, (char *)"door", (char *)"closed");
+				unit->mqtt_flag |= MQTT_FLAG_DOOR;
 			    }
 			} else {
 			    if (unit->door_state) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name);
 			    	unit->door_state = 0;
-				mqtt_publish_str(unit->alias, (char *)"door", (char *)"open");
+				unit->mqtt_flag |= MQTT_FLAG_DOOR;
 			    }
 			}
 		    } else {
@@ -1323,13 +1303,13 @@
 			    if (unit->psu_state == 0) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name);
 				unit->psu_state = 1;
-				mqtt_publish_str(unit->alias, (char *)"12volt", (char *)"on");
+				unit->mqtt_flag |= MQTT_FLAG_PSU;
 			    }
 			} else {
 			    if (unit->psu_state) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name);
 				unit->psu_state = 0;
-				mqtt_publish_str(unit->alias, (char *)"12volt", (char *)"off");
+				unit->mqtt_flag |= MQTT_FLAG_PSU;
 			    }
 			}
 		    } else {
@@ -1410,7 +1390,7 @@
 							}
 						    } else {
 						    	/*
-						    	 * This method works if the unit has no cooling or if the profile allowd the
+						    	 * This method works if the unit has no cooling or if the profile allowed the
 						    	 * beer temperature to rise freely.
 						    	 */
 						    	if ((unit->beer_temperature / 1000.0) <  (unit->prof_peak_abs - 0.5)) {
@@ -1561,11 +1541,13 @@
 			} else {
 			    unit->light_state = 0;
 			    syslog(LOG_NOTICE, "Unit `%s' lights On => Off", unit->name);
+			    unit->mqtt_flag |= MQTT_FLAG_LIGHT;
 			}
 		    }
 		    if (!unit->door_state && !unit->light_state) {
 			unit->light_wait = unit->light_delay;   /* No delay to turn lights on   */
 			unit->light_state = 1;
+			unit->mqtt_flag |= MQTT_FLAG_LIGHT;
 			syslog(LOG_NOTICE, "Unit `%s' lights Off => On", unit->name);
 		    }
 		    device_out(unit->light_address, unit->light_state);
@@ -1670,7 +1652,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power);
 				    unit->heater_state = power;
 				    if (unit->heater_address)
-					mqtt_publish_int(unit->alias, (char *)"heater", unit->heater_state);
+					unit->mqtt_flag |= MQTT_FLAG_HEATER;
 				}
 			    }
 			} else {
@@ -1681,7 +1663,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name);
 				    unit->heater_state = 0;
 				    if (unit->heater_address)
-					mqtt_publish_int(unit->alias, (char *)"heater", 0);
+					unit->mqtt_flag |= MQTT_FLAG_HEATER;
 				}
 			    }
 			}
@@ -1701,7 +1683,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power);
 				    unit->cooler_state = power;
 				    if (unit->cooler_address)
-					mqtt_publish_int(unit->alias, (char *)"cooler", unit->cooler_state);
+					unit->mqtt_flag |= MQTT_FLAG_COOLER;
 				}
 			    }
 			} else {
@@ -1712,7 +1694,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name);
 				    unit->cooler_state = 0;
 				    if (unit->cooler_address)
-					mqtt_publish_int(unit->alias, (char *)"cooler", 0);
+					unit->mqtt_flag |= MQTT_FLAG_COOLER;
 				}
 			    }
 			}
@@ -1738,7 +1720,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name);
 				    unit->fan_state = 100;
 				    if (unit->fan_address)
-					mqtt_publish_int(unit->alias, (char *)"fan", 100);
+					unit->mqtt_flag |= MQTT_FLAG_FAN;
 			    	}
 			    }
 			} else {
@@ -1749,7 +1731,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name);
 			    	    unit->fan_state = 0;
 				    if (unit->fan_address)
-					mqtt_publish_int(unit->alias, (char *)"fan", 0);
+					unit->mqtt_flag |= MQTT_FLAG_FAN;
 				}
 			    }
 			}
@@ -1809,33 +1791,9 @@
 		/*
 		 * Publish MQTT messages set in flag
 		 */
-		if (unit->mqtt_flag & MQTT_FLAG_SP) {
-		    mqtt_publish_float(unit->alias, (char *)"setpoint/high", LCDspH, 1);
-		    mqtt_publish_float(unit->alias, (char *)"setpoint/low", LCDspL, 1);
-		}
-		if (unit->mqtt_flag & MQTT_FLAG_AIR) {
-		    mqtt_publish_float(unit->alias, (char *)"air/temperature", unit->air_temperature / 1000.0, 3);
-		}
-		if (unit->mqtt_flag & MQTT_FLAG_BEER) {
-		    mqtt_publish_float(unit->alias, (char *)"beer/temperature", unit->beer_temperature / 1000.0, 3);
-		}
-		if (unit->mqtt_flag & MQTT_FLAG_MODE) {
-		    mqtt_publish_str(unit->alias, (char *)"mode", (char *)UNITMODE[unit->mode]);
+		if (unit->mqtt_flag) {
+		    publishDData(unit);
 		}
-		if (unit->mqtt_flag & MQTT_FLAG_PROFILE) {
-		    mqtt_publish_str(unit->alias, (char *)"profile/uuid", unit->profile);
-		    mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[unit->prof_state]);
-		    for (profile = Config.profiles; profile; profile = profile->next) {
-			if (strcmp(unit->profile, profile->uuid) == 0) {
-			    mqtt_publish_str(unit->alias, (char *)"profile/name", profile->name);
-			    mqtt_publish_int(unit->alias, (char *)"profile/fridgemode", profile->fridge_mode);
-			}
-		    }
-		}
-		if (unit->mqtt_flag & MQTT_FLAG_PERCENT) {
-		    mqtt_publish_int(unit->alias, (char *)"profile/percent", unit->prof_percent);
-		}
-		unit->mqtt_flag = 0;
 	    } /* for units */
 
 #ifdef HAVE_WIRINGPI_H
@@ -1948,40 +1906,6 @@
      * Stop units processing in a neat way
      */
     for (unit = Config.units; unit; unit = unit->next) {
-
-        if (unit->mode != UNITMODE_OFF) {
-	    if (unit->heater_address) {
-		mqtt_publish_int(unit->alias, (char *)"heater", 0);
-		mqtt_publish_clear(unit->alias, (char *)"heater");
-	    }
-	    if (unit->cooler_address) {
-		mqtt_publish_int(unit->alias, (char *)"cooler", 0);
-		mqtt_publish_clear(unit->alias, (char *)"cooler");
-	    }
-	    if (unit->fan_address) {
-		mqtt_publish_int(unit->alias, (char *)"fan", 0);
-		mqtt_publish_clear(unit->alias, (char *)"fan");
-	    }
-	    mqtt_publish_int(unit->alias, (char *)"state", 0);
-	    mqtt_publish_clear(unit->alias, (char *)"state");
-	}
-	mqtt_publish_clear(unit->alias, (char *)"air/temperature");
-	mqtt_publish_clear(unit->alias, (char *)"air");
-	mqtt_publish_clear(unit->alias, (char *)"beer/temperature");
-	mqtt_publish_clear(unit->alias, (char *)"beer");
-	mqtt_publish_clear(unit->alias, (char *)"setpoint/high");
-	mqtt_publish_clear(unit->alias, (char *)"setpoint/low");
-	mqtt_publish_clear(unit->alias, (char *)"setpoint");
-	mqtt_publish_clear(unit->alias, (char *)"door");
-	mqtt_publish_clear(unit->alias, (char *)"name");
-	mqtt_publish_clear(unit->alias, (char *)"mode");
-	mqtt_publish_clear(unit->alias, (char *)"12volt");
-	mqtt_publish_clear(unit->alias, (char *)"profile/uuid");
-	mqtt_publish_clear(unit->alias, (char *)"profile/state");
-	mqtt_publish_clear(unit->alias, (char *)"profile/name");
-	mqtt_publish_clear(unit->alias, (char *)"profile/fridgemode");
-	mqtt_publish_clear(unit->alias, (char *)"profile/percent");
-
 	/*
 	 * Turn everything off
 	 */
@@ -1991,6 +1915,8 @@
 	device_out(unit->cooler_address, unit->cooler_state);
 	device_out(unit->fan_address, unit->fan_state);
 	device_out(unit->light_address, unit->light_state);
+	unit->mqtt_flag = MQTT_FLAG_HEATER | MQTT_FLAG_COOLER | MQTT_FLAG_FAN | MQTT_FLAG_LIGHT;
+	publishDData(unit);
 	syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]);
     }
 

mercurial