Only send domoticz changed values

Sat, 10 Jun 2017 23:43:56 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 10 Jun 2017 23:43:56 +0200
changeset 515
7ab5cf2afc0c
parent 514
f5a00ad53329
child 516
65299c11774b

Only send domoticz changed values

thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/mqtt.h file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
--- a/thermferm/mqtt.c	Sat Jun 10 13:00:14 2017 +0200
+++ b/thermferm/mqtt.c	Sat Jun 10 23:43:56 2017 +0200
@@ -178,19 +178,20 @@
 
 
 
-void pub_domoticz_temp(int idx, char *value) {
+void pub_domoticz_temp(int idx, int value) {
     char	*dload = NULL;
-    char	sidx[10];
+    char	sidx[10], sval[20];
 
     if (idx == 0)
 	return;
 
     sprintf(sidx, "%d", idx);
+    sprintf(sval, "%.3f", value / 1000.0);
 
     dload = xstrcpy((char *)"{\"command\":\"udevice\",\"idx\":");
     dload = xstrcat(dload, sidx);
     dload = xstrcat(dload, (char *)",\"nvalue\":0,\"svalue\":\"");
-    dload = xstrcat(dload, value);
+    dload = xstrcat(dload, sval);
     dload = xstrcat(dload, (char *)"\"}");
     publisher(mosq, (char *)"domoticz/in", dload, false);
     free(dload);
@@ -254,7 +255,6 @@
         sprintf(buf, "%.3f", unit->air_temperature / 1000.0);
         payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_temp(unit->air_idx, buf);      
     } else {
 	payload = xstrcat(payload, (char *)"\",\"air\":null");
     }
@@ -268,7 +268,6 @@
         sprintf(buf, "%.3f", unit->beer_temperature / 1000.0);
         payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_temp(unit->beer_idx, buf);
     } else {
 	payload = xstrcat(payload, (char *)",\"beer\":null");
     }
@@ -280,7 +279,6 @@
 	sprintf(buf, "%d", unit->heater_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->heater_idx, unit->heater_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"heater\":null");
     }
@@ -292,7 +290,6 @@
 	sprintf(buf, "%d", unit->cooler_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"cooler\":null");
     }
@@ -304,7 +301,6 @@
 	sprintf(buf, "%d", unit->fan_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->fan_idx, unit->fan_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"fan\":null");
     }
@@ -316,7 +312,6 @@
 	sprintf(buf, "%d", unit->door_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->door_idx, unit->door_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"door\":null");
     }
@@ -328,7 +323,6 @@
 	sprintf(buf, "%d", unit->light_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->light_idx, unit->light_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"light\":null");
     }
@@ -340,7 +334,6 @@
 	sprintf(buf, "%d", unit->psu_state);
 	payload = xstrcat(payload, buf);
 	payload = xstrcat(payload, (char *)"}");
-	pub_domoticz_output(unit->psu_idx, unit->psu_state);
     } else {
 	payload = xstrcat(payload, (char *)",\"psu\":null");
     }
--- a/thermferm/mqtt.h	Sat Jun 10 13:00:14 2017 +0200
+++ b/thermferm/mqtt.h	Sat Jun 10 23:43:56 2017 +0200
@@ -12,6 +12,8 @@
 /*
  * Public functions
  */
+void pub_domoticz_temp(int, int);
+void pub_domoticz_output(int, int);
 void mqtt_connect(void);
 void mqtt_disconnect(void);
 void publishDData(units_list *);
--- a/thermferm/thermferm.c	Sat Jun 10 13:00:14 2017 +0200
+++ b/thermferm/thermferm.c	Sat Jun 10 23:43:56 2017 +0200
@@ -1233,8 +1233,10 @@
 			deviation = 40000;
 			if ((unit->air_temperature == 0) ||
 			    (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) {
-			    if (unit->air_temperature != temp)
+			    if (unit->air_temperature != temp) {
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
+				pub_domoticz_temp(unit->air_idx, temp);
+			    }
 			    unit->air_temperature = temp;
 			    unit->air_state = 0;
 			} else {
@@ -1253,8 +1255,10 @@
 			deviation = 40000;
 			if ((unit->beer_temperature == 0) ||
 			    (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) {
-			    if (unit->beer_temperature != temp)
+			    if (unit->beer_temperature != temp) {
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
+				pub_domoticz_temp(unit->beer_idx, temp);
+			    }
     			    unit->beer_temperature = temp;
 			    unit->beer_state = 0;
 			} else {
@@ -1277,12 +1281,14 @@
 			    if (unit->door_state == 0) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name);
 			    	unit->door_state = 1;
+				pub_domoticz_output(unit->door_idx, unit->door_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			} else {
 			    if (unit->door_state) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name);
 			    	unit->door_state = 0;
+				pub_domoticz_output(unit->door_idx, unit->door_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			}
@@ -1303,12 +1309,14 @@
 			    if (unit->psu_state == 0) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name);
 				unit->psu_state = 1;
+				pub_domoticz_output(unit->psu_idx, unit->psu_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			} else {
 			    if (unit->psu_state) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name);
 				unit->psu_state = 0;
+				pub_domoticz_output(unit->psu_idx, unit->psu_state);
 				unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    }
 			}
@@ -1651,6 +1659,7 @@
 				if (unit->heater_state != power) {
 				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power);
 				    unit->heater_state = power;
+				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
 				    if (unit->heater_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				}
@@ -1662,15 +1671,19 @@
 				if (unit->heater_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name);
 				    unit->heater_state = 0;
+				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
 				    if (unit->heater_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				}
 			    }
 			}
-			if (unit->door_state)
+			if (unit->door_state) {
 			    device_out(unit->heater_address, unit->heater_state);
-			else
+			    pub_domoticz_output(unit->heater_idx, unit->heater_state);
+			} else {
 			    device_out(unit->heater_address, 0);
+			    pub_domoticz_output(unit->heater_idx, 0);
+			}
 		    }
 
 		    if (unit->cooler_address && ! unit->heater_state) {
@@ -1682,6 +1695,7 @@
 				if (unit->cooler_state != power) {
 				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power);
 				    unit->cooler_state = power;
+				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
 				    if (unit->cooler_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				}
@@ -1693,15 +1707,19 @@
 				if (unit->cooler_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name);
 				    unit->cooler_state = 0;
+				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
 				    if (unit->cooler_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				}
 			    }
 			}
-			if (unit->door_state)
+			if (unit->door_state) {
 			    device_out(unit->cooler_address, unit->cooler_state);
-			else
+			    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
+			} else {
 			    device_out(unit->cooler_address, 0);
+			    pub_domoticz_output(unit->cooler_idx, 0);
+			}
 		    }
 		    if (debug)
 			fprintf(stdout, "Final: PIDheat=%.2f PWRheat=%d  PIDcool=%.2f PWRcool=%d\n", 
@@ -1719,6 +1737,7 @@
 			    	if (! unit->fan_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name);
 				    unit->fan_state = 100;
+				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
 				    if (unit->fan_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 			    	}
@@ -1730,6 +1749,7 @@
 			    	if (unit->fan_state) {
 				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name);
 			    	    unit->fan_state = 0;
+				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
 				    if (unit->fan_address)
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
 				}
@@ -1912,8 +1932,11 @@
 	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;
 	device_out(unit->heater_address, unit->heater_state);
+	pub_domoticz_output(unit->heater_idx, unit->heater_state);
 	device_out(unit->cooler_address, unit->cooler_state);
+	pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
 	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);

mercurial