thermferm/server.c

changeset 693
3518c07737d8
parent 689
f94b525f7563
child 696
fe042f9484ac
--- a/thermferm/server.c	Sat Apr 20 11:48:20 2024 +0200
+++ b/thermferm/server.c	Mon Apr 22 11:33:04 2024 +0200
@@ -36,7 +36,6 @@
 extern sys_config       Config;
 extern const char	UNITMODE[5][8];
 extern const char	UNITSTAGE[4][12];
-extern const char	TEMPSTATE[3][8];
 extern const char	DEVTYPE[8][6];
 extern const char	DEVPRESENT[4][6];
 extern const char	DEVDIR[7][11];
@@ -652,13 +651,12 @@
 	srv_send(s, (char *)"213 Global Settings record follows:");
 	srv_send(s, (char *)"RELEASE,%s", VERSION);
 	srv_send(s, (char *)"NAME,%s", Config.name);
-	srv_send(s, (char *)"PORT,%d", Config.my_port);
-	srv_send(s, (char *)"TEMPFORMAT,%c", Config.tempFormat);
-	srv_send(s, (char *)"TEMP_ADDRESS,%s", Config.temp_address);
-	srv_send(s, (char *)"TEMP_STATE,%s", TEMPSTATE[Config.temp_state]);
+	srv_send(s, (char *)"PORT,%d", Config.server_port);
+	srv_send(s, (char *)"TEMP_UUID,%s", Config.temp_uuid);
+	srv_send(s, (char *)"TEMP_STATE,%s", DEVPRESENT[Config.temp_state]);
 	srv_send(s, (char *)"TEMP_VALUE,%.1f", Config.temp_value / 1000.0);
-	srv_send(s, (char *)"HUM_ADDRESS,%s", Config.hum_address);
-	srv_send(s, (char *)"HUM_STATE,%s", TEMPSTATE[Config.hum_state]);
+	srv_send(s, (char *)"HUM_UUID,%s", Config.hum_uuid);
+	srv_send(s, (char *)"HUM_STATE,%s", DEVPRESENT[Config.hum_state]);
 	srv_send(s, (char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0);
 	srv_send(s, (char *)"TEMP_HUM_IDX,%d", Config.temp_hum_idx);
 	srv_send(s, (char *)"LCD_COLS,%d", Config.lcd_cols);
@@ -714,43 +712,36 @@
 
 		    } else if (val && (strcmp(kwd, (char *)"PORT") == 0)) {
 			if (sscanf(val, "%d", &ival) == 1) {
-			    if (Config.my_port != ival)
-			    	syslog(LOG_NOTICE, "Global port %d to %d", Config.my_port, ival);
-			    Config.my_port = ival;
-			}
-
-		    } else if (val && (strcmp(kwd, (char *)"TEMPFORMAT") == 0)) {
-			if ((val[0] == 'C') || (val[0] == 'F')) {
-			    if (Config.tempFormat != val[0])
-				syslog(LOG_NOTICE, "Global port %c to %c", Config.tempFormat, val[0]);
-			    Config.tempFormat = val[0];
+			    if (Config.server_port != ival)
+			    	syslog(LOG_NOTICE, "Global server port %d to %d", Config.server_port, ival);
+			    Config.server_port = ival;
 			}
 
-		    } else if (strcmp(kwd, (char *)"TEMP_ADDRESS") == 0) {
-			if (val && Config.temp_address && (strcmp(val, Config.temp_address)))
-			    syslog(LOG_NOTICE, "Global temperature address `%s' to `%s'", Config.temp_address, val);
-		        if (Config.temp_address) {
-			    device_count(FALSE, Config.temp_address);
-			    free(Config.temp_address);
+		    } else if (strcmp(kwd, (char *)"TEMP_UUID") == 0) {
+			if (val && Config.temp_uuid && (strcmp(val, Config.temp_uuid)))
+			    syslog(LOG_NOTICE, "Global temperature uuid `%s' to `%s'", Config.temp_uuid, val);
+		        if (Config.temp_uuid) {
+			    device_count(FALSE, Config.temp_uuid);
+			    free(Config.temp_uuid);
 			}
 			if (val) {
-			    Config.temp_address = xstrcpy(val);
-			    device_count(TRUE, Config.temp_address);
+			    Config.temp_uuid = xstrcpy(val);
+			    device_count(TRUE, Config.temp_uuid);
 			} else
-			    Config.temp_address = NULL;
+			    Config.temp_uuid = NULL;
 
-		    } else if (strcmp(kwd, (char *)"HUM_ADDRESS") == 0) {
-			if (val && Config.hum_address && (strcmp(val, Config.hum_address)))
-			    syslog(LOG_NOTICE, "Global humidity address `%s' to `%s'", Config.hum_address, val);
-			if (Config.hum_address) {
-			    device_count(FALSE, Config.hum_address);
-			    free(Config.hum_address);
+		    } else if (strcmp(kwd, (char *)"HUM_UUID") == 0) {
+			if (val && Config.hum_uuid && (strcmp(val, Config.hum_uuid)))
+			    syslog(LOG_NOTICE, "Global humidity uuid `%s' to `%s'", Config.hum_uuid, val);
+			if (Config.hum_uuid) {
+			    device_count(FALSE, Config.hum_uuid);
+			    free(Config.hum_uuid);
 			}
 			if (val) {
-			    Config.hum_address = xstrcpy(val);
-			    device_count(TRUE, Config.hum_address);
+			    Config.hum_uuid = xstrcpy(val);
+			    device_count(TRUE, Config.hum_uuid);
 			} else
-			    Config.hum_address = NULL;
+			    Config.hum_uuid = NULL;
 
 		    } else if (val && (strcmp(kwd, (char *)"TEMP_HUM_IDX") == 0)) {
 			if (sscanf(val, "%d", &ival) == 1) {
@@ -1402,6 +1393,52 @@
 	return 0;
     }
 
+    if (strcmp(opt, (char *)"JSON") == 0) {
+        char    *payload = NULL, *payloadu = NULL;
+        bool    comma = false;
+
+        if (param == NULL) {
+            srv_send(s, (char *)"212 Units json list follows:");
+            payload = xstrcpy((char *)"[");
+            for (unit = Config.units; unit; unit = unit->next) {
+                if (comma)
+                    payload = xstrcat(payload, (char *)",");
+                payloadu = unit_data(unit, true);
+		payload = xstrcat(payload, payloadu);
+                comma = true;
+                free(payloadu);
+                payloadu = NULL;
+            }
+            payload = xstrcat(payload, (char *)"]");
+            srv_send(s, payload);
+            srv_send(s, (char *)".");
+            free(payload);
+            payload = NULL;
+            return 0;
+        } else {
+            syslog(LOG_NOTICE, "UNIT JSON %s", param);
+            for (unit = Config.units; unit; unit = unit->next) {
+                if (strcmp(param, unit->uuid) == 0) {
+                    srv_send(s, (char *)"213 Unit json data follows:");
+                    payload = xstrcpy((char *)"{\"type\":\"fermenter\",\"unit\":\"");
+                    payload = xstrcat(payload, unit->alias);
+                    payload = xstrcat(payload, (char *)"\",\"metric\":");
+                    payloadu = unit_data(unit, false);
+                    payload = xstrcat(payload, payloadu);
+                    payload = xstrcat(payload, (char *)"}");
+                    srv_send(s, payload);
+                    free(payload);
+                    free(payloadu);
+                    payload = payloadu = NULL;
+                    srv_send(s, (char *)".");
+                    return 0;
+                }
+            }
+            srv_send(s, (char *)"440 No such unit");
+            return 0;
+        }
+    }
+
     if (param == NULL) {
 	srv_send(s, (char *)"502 Parameter missing");
 	return 0;
@@ -1429,7 +1466,7 @@
 			unit->profile_duration = unit->profile_totalsteps = 0;
 	unit->profile_steps = NULL;
 	unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
-	unit->air_state = unit->beer_state = unit->chiller_state = 1;
+	unit->air_state = unit->beer_state = unit->chiller_state = DEVPRESENT_NO;
 	unit->air_temperature = unit->beer_temperature = unit->chiller_temperature = 20000;
 	unit->beer_set_lo = unit->beer_set_hi = unit->fridge_set_lo = unit->fridge_set_hi = unit->profile_inittemp_lo = unit->profile_inittemp_hi =20.0;
 	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = \
@@ -1512,16 +1549,16 @@
 		srv_send(s, (char *)"STAGE,%s", UNITSTAGE[unit->stage]);
 		srv_send(s, (char *)"VOLUME,%2f", unit->volume);
 		srv_send(s, (char *)"AIR_ADDRESS,%s", unit->air_address);
-		srv_send(s, (char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
+		srv_send(s, (char *)"AIR_STATE,%s", DEVPRESENT[unit->air_state]);
 		srv_send(s, (char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0);
 		srv_send(s, (char *)"AIR_IDX,%d", unit->air_idx);
 		srv_send(s, (char *)"BEER_ADDRESS,%s", MBSE_SS(unit->beer_address));
 		srv_send(s, (char *)"BEER_ADDRESS2,%s", MBSE_SS(unit->beer_address2));
-		srv_send(s, (char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]);
+		srv_send(s, (char *)"BEER_STATE,%s", DEVPRESENT[unit->beer_state]);
 		srv_send(s, (char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0);
 		srv_send(s, (char *)"BEER_IDX,%d", unit->beer_idx);
 		srv_send(s, (char *)"CHILLER_ADDRESS,%s", MBSE_SS(unit->chiller_address));
-		srv_send(s, (char *)"CHILLER_STATE,%s", TEMPSTATE[unit->chiller_state]);
+		srv_send(s, (char *)"CHILLER_STATE,%s", DEVPRESENT[unit->chiller_state]);
 		srv_send(s, (char *)"CHILLER_TEMPERATURE,%.3f", unit->chiller_temperature / 1000.0);
 		srv_send(s, (char *)"CHILLER_IDX,%d", unit->chiller_idx);
 		srv_send(s, (char *)"HEATER_ADDRESS,%s", unit->heater_address);
@@ -1602,32 +1639,6 @@
 	return 0;
     }
 
-    if (strcmp(opt, (char *)"JSON") == 0) {
-	syslog(LOG_NOTICE, "UNIT JSON %s", param);
-        for (unit = Config.units; unit; unit = unit->next) {
-            if (strcmp(param, unit->uuid) == 0) {
-		char	*payload, *payloadu;
-
-		srv_send(s, (char *)"213 Unit json data follows:");
-
-		payload = xstrcpy((char *)"{\"type\":\"fermenter\",\"unit\":\"");
-		payload = xstrcat(payload, unit->alias);
-		payload = xstrcat(payload, (char *)"\",\"metric\":");
-		payloadu = unit_data(unit, false);
-		payload = xstrcat(payload, payloadu);
-		payload = xstrcat(payload, (char *)"}");
-		srv_send(s, payload);
-		free(payload);
-		free(payloadu);
-		payload = payloadu = NULL;
-		srv_send(s, (char *)".");
-		return 0;
-	    }
-	}
-	srv_send(s, (char *)"440 No such unit");
-	return 0;
-    }
-
     if (strcmp(opt, (char *)"PUT") == 0) {
 	/*
 	 * Block main process
@@ -2272,7 +2283,7 @@
     memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
     myaddr_in.sin_family = AF_INET;
     myaddr_in.sin_addr.s_addr = INADDR_ANY;
-    myaddr_in.sin_port = htons(Config.my_port);
+    myaddr_in.sin_port = htons(Config.server_port);
 
     ls = socket(AF_INET, SOCK_STREAM, 0);
     if (ls == -1) {

mercurial