thermferm/thermferm.c

changeset 554
ab9f22ab57b5
parent 550
04c942cded91
child 556
c2ee7f9a069e
--- a/thermferm/thermferm.c	Wed Jul 25 20:08:13 2018 +0200
+++ b/thermferm/thermferm.c	Tue Jul 31 16:42:11 2018 +0200
@@ -188,7 +188,7 @@
 #endif
 				slcdPuts(slcdHandle, "Choose unit:");
 				slcdPosition(slcdHandle, 0, 1);
-				slcdPuts(slcdHandle, current_unit->name);
+				slcdPuts(slcdHandle, current_unit->alias);
 				break;
 
 	case MENU_MODE_OFF:	show_mode();
@@ -394,7 +394,7 @@
 {
     current_unit->mqtt_flag |= MQTT_FLAG_DATA;
     if ((current_unit->mode == UNITMODE_OFF) && (mode != UNITMODE_OFF)) {
-	initlog(current_unit->name);
+	initlog(current_unit->product_code, current_unit->product_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;
@@ -1082,29 +1082,40 @@
 	unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
 	if (unit->mode == UNITMODE_PROFILE) {
 	    if (!unit->profile)
-		syslog(LOG_NOTICE, "Starting unit `%s' in profile mode, no profile defined.", unit->name);
+		syslog(LOG_NOTICE, "Starting unit `%s' in profile mode, no profile defined.", unit->alias);
 	    else {
-	    	syslog(LOG_NOTICE, "Starting unit `%s' in profile state %s.", unit->name, PROFSTATE[unit->prof_state]);
+	    	syslog(LOG_NOTICE, "Starting unit `%s' in profile state %s.", unit->alias, PROFSTATE[unit->prof_state]);
 	    }
 	} else if (unit->mode == UNITMODE_BEER) {
-	    syslog(LOG_NOTICE, "Starting unit `%s' beer cooler at %.1f degrees", unit->name, unit->beer_set);
+	    syslog(LOG_NOTICE, "Starting unit `%s' beer cooler at %.1f degrees", unit->alias, unit->beer_set);
 	} else if (unit->mode == UNITMODE_FRIDGE) {
-	    syslog(LOG_NOTICE, "Starting unit `%s' as refridgerator at %.1f degrees", unit->name, unit->fridge_set);
+	    syslog(LOG_NOTICE, "Starting unit `%s' as refridgerator at %.1f degrees", unit->alias, unit->fridge_set);
 	} else if (unit->mode == UNITMODE_NONE) {
-	    syslog(LOG_NOTICE, "Starting unit `%s' in inactive state", unit->name);
+	    syslog(LOG_NOTICE, "Starting unit `%s' in inactive state", unit->alias);
 	} else {
-	    syslog(LOG_NOTICE, "Starting unit `%s' in off state", unit->name);
+	    syslog(LOG_NOTICE, "Starting unit `%s' in off state", unit->alias);
 	}
 
 	/*
 	 * Initialize logfile
 	 */
 	if (unit->mode != UNITMODE_OFF) {
-	    initlog(unit->name);
+	    initlog(unit->product_code, unit->product_name);
 	}
     }
+    printf("Units started\n");
 #ifdef HAVE_MOSQUITTO_H
     publishDBirthAll();
+    printf("Birth all done\n");
+
+    for (unit = Config.units; unit; unit = unit->next) {
+	if (unit->mode != UNITMODE_OFF) {
+    	    unit->event_msg = xstrcpy((char *)"Startup");
+            publishDLog(unit);
+	    free(unit->event_msg);
+	    unit->event_msg = NULL;
+	}
+    }
 #endif
 
 #ifdef HAVE_WIRINGPI_H
@@ -1331,7 +1342,7 @@
 		    if (rc == DEVPRESENT_YES) {
 			if (temp) {
 			    if (unit->door_state == 0) {
-			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name);
+			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->alias);
 			    	unit->door_state = 1;
 #ifdef HAVE_MOSQUITTO_H
 				pub_domoticz_output(unit->door_idx, unit->door_state);
@@ -1340,7 +1351,7 @@
 			    }
 			} else {
 			    if (unit->door_state) {
-			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name);
+			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->alias);
 			    	unit->door_state = 0;
 #ifdef HAVE_MOSQUITTO_H
 				pub_domoticz_output(unit->door_idx, unit->door_state);
@@ -1369,7 +1380,7 @@
 		    if (rc == DEVPRESENT_YES) {
 			if (temp) {
 			    if (unit->psu_state == 0) {
-				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name);
+				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->alias);
 				unit->psu_state = 1;
 #ifdef HAVE_MOSQUITTO_H
 				pub_domoticz_output(unit->psu_idx, unit->psu_state);
@@ -1378,7 +1389,7 @@
 			    }
 			} else {
 			    if (unit->psu_state) {
-				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name);
+				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->alias);
 				unit->psu_state = 0;
 #ifdef HAVE_MOSQUITTO_H
 				pub_domoticz_output(unit->psu_idx, unit->psu_state);
@@ -1615,7 +1626,7 @@
 			    unit->light_wait--;
 			} else {
 			    unit->light_state = 0;
-			    syslog(LOG_NOTICE, "Unit `%s' lights On => Off", unit->name);
+			    syslog(LOG_NOTICE, "Unit `%s' lights On => Off", unit->alias);
 			    unit->mqtt_flag |= MQTT_FLAG_DATA;
 			}
 		    }
@@ -1623,7 +1634,7 @@
 			unit->light_wait = unit->light_delay;   /* No delay to turn lights on   */
 			unit->light_state = 1;
 			unit->mqtt_flag |= MQTT_FLAG_DATA;
-			syslog(LOG_NOTICE, "Unit `%s' lights Off => On", unit->name);
+			syslog(LOG_NOTICE, "Unit `%s' lights Off => On", unit->alias);
 		    }
 		    device_out(unit->light_address, unit->light_state);
 		}
@@ -1736,12 +1747,14 @@
 			    } else {
 				int	power = round(unit->PID_heat->OutP);
 				if (unit->heater_state != power) {
-				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power);
+				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->alias, unit->heater_state, power);
 				    unit->heater_state = power;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
-				    if (unit->heater_address)
+				    if (unit->heater_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+					unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 				}
 			    }
@@ -1750,12 +1763,14 @@
 				unit->heater_wait--;
 			    } else {
 				if (unit->heater_state) {
-				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name);
+				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->alias);
 				    unit->heater_state = 0;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->heater_idx, unit->heater_state);
-				    if (unit->heater_address)
+				    if (unit->heater_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+					unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 				}
 			    }
@@ -1774,12 +1789,14 @@
 			    } else {
 				int	power = round(unit->PID_cool->OutP); 
 				if (unit->cooler_state != power) {
-				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power);
+				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->alias, unit->cooler_state, power);
 				    unit->cooler_state = power;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
-				    if (unit->cooler_address)
+				    if (unit->cooler_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+				    	unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 				}
 			    }
@@ -1788,12 +1805,14 @@
 				unit->cooler_wait--;
 			    } else {
 				if (unit->cooler_state) {
-				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name);
+				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->alias);
 				    unit->cooler_state = 0;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->cooler_idx, unit->cooler_state);
-				    if (unit->cooler_address)
+				    if (unit->cooler_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+					unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 				}
 			    }
@@ -1819,12 +1838,14 @@
 			    	unit->fan_wait++;
 			    } else {
 			    	if (! unit->fan_state) {
-				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name);
+				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->alias);
 				    unit->fan_state = 100;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
-				    if (unit->fan_address)
+				    if (unit->fan_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+					unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 			    	}
 			    }
@@ -1833,12 +1854,14 @@
 				unit->fan_wait--;
 			    } else {
 			    	if (unit->fan_state) {
-				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name);
+				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->alias);
 			    	    unit->fan_state = 0;
 #ifdef HAVE_MOSQUITTO_H
 				    pub_domoticz_output(unit->fan_idx, unit->fan_state);
-				    if (unit->fan_address)
+				    if (unit->fan_address) {
 					unit->mqtt_flag |= MQTT_FLAG_DATA;
+					unit->mqtt_flag |= MQTT_FLAG_DLOG;
+				    }
 #endif
 				}
 			    }
@@ -1889,7 +1912,7 @@
 		 * Write 4 rows to the LCD to display the unit state
 		 */
 		lcd_buf_write(row++, "Unit %d: %s          ", LCDunit, UNITMODE[unit->mode]);
-		lcd_buf_write(row++, "%s                   ", unit->name);
+		lcd_buf_write(row++, "%s                   ", unit->product_name);
 		lcd_buf_write(row++, "%c%5.1f\2 A%6.2f\1    ", LCDstatC, LCDspH, LCDair);
 		lcd_buf_write(row++, "%c%5.1f\2 B%6.2f\1    ", LCDstatH, LCDspL, LCDbeer);
 #ifdef HAVE_WIRINGPI_H
@@ -1914,6 +1937,13 @@
 			publishDDeath(unit);
 			unit->mqtt_flag &= ~MQTT_FLAG_DEATH;
 		    }
+		    if (unit->mqtt_flag & MQTT_FLAG_DLOG) {
+			publishDLog(unit);
+			if (unit->event_msg)
+			    free(unit->event_msg);
+			unit->event_msg = NULL;
+			unit->mqtt_flag &= ~MQTT_FLAG_DLOG;
+		    }
 		}
 #endif
 
@@ -1921,7 +1951,7 @@
 		 * Handle changed alarms
 		 */
 		if (unit->alarm_flag != unit->alarm_last) {
-		    syslog(LOG_NOTICE, "Unit `%s' Alarm %d => %d", unit->name, unit->alarm_last, unit->alarm_flag);
+		    syslog(LOG_NOTICE, "Unit `%s' Alarm %d => %d", unit->alias, unit->alarm_last, unit->alarm_flag);
 		    unit->alarm_last = unit->alarm_flag;
 		}
 	    } /* for units */
@@ -1997,7 +2027,9 @@
 					unit->beer_temperature / 1000.0, 
 					target_lo, heater, cooler, fan, door, use_heater, use_cooler, use_fan, room_temp, target_hi,
 					unit->chiller_temperature / 1000.0);
-			filename = xstrcpy(unit->name);
+			filename = xstrcpy(unit->product_code);
+			filename = xstrcat(filename, (char *)" ");
+			filename = xstrcat(filename, unit->product_name);
 			filename = xstrcat(filename, (char *)".log");
 			logger(filename, buf);
 			free(filename);
@@ -2006,13 +2038,20 @@
 		}
 
 		minutes++;
+		if ((minutes % 5) == 0) {
+		    for (unit = Config.units; unit; unit = unit->next) {
+			if (unit->mode != UNITMODE_OFF) {
+			    unit->mqtt_flag |= MQTT_FLAG_DLOG;
+			}
+		    }
+		}
 		if (minutes == 60) {
 		    minutes = 0;
 		    /*
 		     * Log usage counters every hour
 		     */
 		    for (unit = Config.units; unit; unit = unit->next) {
-		    	syslog(LOG_NOTICE, "Unit `%s' usage heater=%d cooler=%d fan=%d", unit->name, unit->heater_usage, unit->cooler_usage, unit->fan_usage);
+		    	syslog(LOG_NOTICE, "Unit `%s' usage heater=%d cooler=%d fan=%d", unit->alias, unit->heater_usage, unit->cooler_usage, unit->fan_usage);
 		    }
 		}
 
@@ -2063,9 +2102,13 @@
 	    unit->mqtt_flag = MQTT_FLAG_DATA;
 	    publishDData(unit);
 	    publishDDeath(unit);
+	    unit->event_msg = xstrcpy((char *)"Shutdown");
+	    publishDLog(unit);
+	    free(unit->event_msg);
+	    unit->event_msg = NULL;
 	}
 #endif
-	syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]);
+	syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->alias, UNITMODE[unit->mode]);
     }
 
     usleep(100000);

mercurial