thermferm/server.c

changeset 306
97602274eb58
parent 303
19a9a3912d03
child 310
53774295e14a
--- a/thermferm/server.c	Wed Jan 14 13:20:16 2015 +0100
+++ b/thermferm/server.c	Sat Feb 07 23:12:55 2015 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2008-2014
+ * Copyright (C) 2008-2015
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -1378,9 +1378,15 @@
 		if (current->fan_address)
 		    free(current->fan_address);
 		current->fan_address = NULL;
+		if (current->light_address)
+		    free(current->light_address);
+		current->light_address = NULL;
 		if (current->door_address)
 		    free(current->door_address);
 		current->door_address = NULL;
+		if (current->psu_address)
+		    free(current->psu_address);
+		current->psu_address = NULL;
 		if (current->profile)
 		    free(current->profile);
 		current->profile = NULL;
@@ -1409,6 +1415,12 @@
 		if (current->door_address)
 		    free(current->door_address);
 		current->door_address = NULL;
+		if (current->light_address)
+		    free(current->light_address);
+		current->light_address = NULL;
+		if (current->psu_address)
+		    free(current->psu_address);
+		current->psu_address = NULL;
 		if (current->profile)
 		    free(current->profile);
 		current->profile = NULL;
@@ -1475,15 +1487,18 @@
 	uuid_unparse(uu, unit->uuid);
 	unit->name = xstrcpy(param);
 	unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \
-			    unit->fan_address = unit->door_address = unit->profile = NULL;
+			    unit->fan_address = unit->door_address = unit->light_address = \
+			    unit->psu_address = unit->profile = NULL;
 	unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
 	unit->air_state = unit->beer_state = 1;
 	unit->air_temperature = unit->beer_temperature = 20000;
 	unit->beer_set = unit->fridge_set = 20.0;
-	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0;
-	unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20;     /* 5 minutes delay */
-	unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0;
-	unit->heater_usage = unit->cooler_usage = unit->fan_usage = 0;
+	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \
+			     unit->light_state = unit->psu_state = unit->prof_state = 0;
+	unit->heater_delay = unit->cooler_delay = unit->fan_delay = 20;	/* 5 minutes delay	*/
+	unit->light_delay = 1;						/* 15 seconds delay	*/
+	unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
+	unit->heater_usage = unit->cooler_usage = unit->fan_usage = unit->light_usage = 0;
 	unit->temp_set_min = 1.0;
 	unit->temp_set_max = 30.0;
 	unit->idle_rangeH = 1.0;
@@ -1574,8 +1589,14 @@
 		srv_send((char *)"FAN_STATE,%d", unit->fan_state);
 		srv_send((char *)"FAN_DELAY,%d", unit->fan_delay);
 		srv_send((char *)"FAN_USAGE,%d", unit->fan_usage);
+		srv_send((char *)"LIGHT_ADDRESS,%s", unit->light_address);
+		srv_send((char *)"LIGHT_STATE,%d", unit->light_state);
+		srv_send((char *)"LIGHT_DELAY,%d", unit->light_delay);
+		srv_send((char *)"LIGHT_USAGE,%d", unit->light_usage);
 		srv_send((char *)"DOOR_ADDRESS,%s", unit->door_address);
 		srv_send((char *)"DOOR_STATE,%d", unit->door_state);
+		srv_send((char *)"PSU_ADDRESS,%s", unit->psu_address);
+		srv_send((char *)"PSU_STATE,%d", unit->psu_state);
 		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);
@@ -1765,6 +1786,33 @@
 				    unit->fan_delay = ival;
 				}
 
+			    } else if (strcmp(kwd, (char *)"LIGHT_ADDRESS") == 0) {
+				if (val && unit->light_address && (strcmp(val, unit->light_address)))
+				    syslog(LOG_NOTICE, "Fermenter unit %s light address `%s' to `%s'", unit->uuid, unit->light_address, val);
+				if (unit->light_address) {
+				    device_count(FALSE, unit->light_address);
+				    free(unit->light_address);
+				}
+				if (val) {
+				    unit->light_address = xstrcpy(val);
+				    device_count(TRUE, unit->light_address);
+				} else
+				    unit->light_address = NULL;
+
+			    } else if (val && (strcmp(kwd, (char *)"LIGHT_STATE") == 0)) {
+				if ((sscanf(val, "%d", &ival) == 1) && ((ival == 0) || (ival == 100))) {
+				    if (unit->light_state != ival)
+					syslog(LOG_NOTICE, "Fermenter unit %s light state %d to %d", unit->uuid, unit->light_state, ival);
+				    unit->light_state = ival;
+				}
+
+			    } else if (val && (strcmp(kwd, (char *)"LIGHT_DELAY") == 0)) {
+				if (sscanf(val, "%d", &ival) == 1) {
+				    if (unit->light_delay != ival)
+					syslog(LOG_NOTICE, "Fermenter unit %s light delay %d to %d", unit->uuid, unit->light_delay, ival);
+				    unit->light_delay = ival;
+				}
+
 			    } else if (strcmp(kwd, (char *)"DOOR_ADDRESS") == 0) {
 				if (val && unit->door_address && (strcmp(val, unit->door_address)))
 				    syslog(LOG_NOTICE, "Fermenter unit %s door address `%s' to `%s'", unit->uuid, unit->door_address, val);
@@ -1778,6 +1826,19 @@
 				} else
 				    unit->door_address = NULL;
 
+			    } else if (strcmp(kwd, (char *)"PSU_ADDRESS") == 0) {
+				if (val && unit->psu_address && (strcmp(val, unit->psu_address)))
+				    syslog(LOG_NOTICE, "Fermenter unit %s psu address `%s' to `%s'", unit->uuid, unit->psu_address, val);
+				if (unit->psu_address) {
+				    device_count(FALSE, unit->psu_address);
+				    free(unit->psu_address);
+				}
+				if (val) {
+				    unit->psu_address = xstrcpy(val);
+				    device_count(TRUE, unit->psu_address);
+				} else
+				    unit->psu_address = NULL;
+
 			    } else if (val && (strcmp(kwd, (char *)"MODE") == 0)) {
 				for (i = 0; i < 5; i++) {
 				    if (strcmp(val, UNITMODE[i]) == 0) {
@@ -1788,11 +1849,12 @@
 					unit->mode = i;
 					/* Allways turn everything off after a mode change */
 					unit->PID_I_err = unit->PID_err_old = 0.0;
-					unit->heater_state = unit->cooler_state = unit->fan_state = 0;
-					unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0;
+					unit->heater_state = unit->cooler_state = unit->fan_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);
 					device_out(unit->cooler_address, unit->cooler_state);
 					device_out(unit->fan_address, unit->fan_state);
+					device_out(unit->light_address, unit->light_state);
 					if (unit->mode == UNITMODE_PROFILE) {
 					    /*
 					     * Set a sane default until it will be overruled by the
@@ -1836,11 +1898,12 @@
 				     * Reset all output devices
 				     */
 				    unit->PID_I_err = unit->PID_err_old = 0.0;
-				    unit->heater_state = unit->cooler_state = unit->fan_state = 0;
-				    unit->heater_wait = unit->cooler_wait = unit->fan_wait = 0;
+				    unit->heater_state = unit->cooler_state = unit->fan_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);
 				    device_out(unit->cooler_address, unit->cooler_state);
 				    device_out(unit->fan_address, unit->fan_state);
+				    device_out(unit->light_address, unit->light_state);
 				}
 
 			    } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) {

mercurial