thermferm/server.c

changeset 54
c06190a58f22
parent 53
37623517e0ef
child 58
e8e7b46b705b
--- a/thermferm/server.c	Sun May 25 23:29:07 2014 +0200
+++ b/thermferm/server.c	Mon May 26 23:25:48 2014 +0200
@@ -39,11 +39,6 @@
 #define SS_BUFSIZE      1024
 #define SS_TIMEOUT      300
 
-unsigned char		cc_tempFormat = 'C';
-float			cc_tempSetMin = 1.0;
-float			cc_tempSetMax = 30.0;
-float			cc_idleRangeH = 1.000;
-float			cc_idleRangeL = -1.000;
 
 float			cv_beerDiff = 0.0;
 
@@ -62,11 +57,11 @@
 
 void defaultControlConstants(void)
 {
-	cc_tempFormat = 'C';
-	cc_tempSetMin = 1.0;
-	cc_tempSetMax = 30.0;
-	cc_idleRangeH = 1.000;
-	cc_idleRangeL = -1.000;
+	Config.cc_tempFormat = 'C';
+	Config.cc_tempSetMin = 1.0;
+	Config.cc_tempSetMax = 30.0;
+	Config.cc_idleRangeH = 1.000;
+	Config.cc_idleRangeL = -1.000;
 }
 
 
@@ -117,7 +112,7 @@
 
 void cmd_server(void)
 {
-    char                *inp, buf[SS_BUFSIZE], obuf[SS_BUFSIZE];
+    char                *inp, *p, *q, buf[SS_BUFSIZE], obuf[SS_BUFSIZE];
     int                 i, rc, rlen;
     socklen_t           fromlen;
     float		newtemp;
@@ -168,7 +163,7 @@
 		srv_send("%.1f", Config.cs_beerSet);
 	    } else if (strncmp(buf, "getControlConstants", 19) == 0) {
 		srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", 
-			cc_tempFormat, cc_tempSetMin, cc_tempSetMax, cc_idleRangeH, cc_idleRangeL );
+			Config.cc_tempFormat, Config.cc_tempSetMin, Config.cc_tempSetMax, Config.cc_idleRangeH, Config.cc_idleRangeL );
 	    } else if (strncmp(buf, "getControlSettings", 18) == 0) {
 		srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", 
 			Config.cs_mode, Config.cs_beerSet, Config.cs_fridgeSet, Config.cs_heatEstimator, Config.cs_coolEstimator);
@@ -186,13 +181,13 @@
 		if (debug)
 		    fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
 		if (rc == 1) {
-		    if ((cc_tempSetMin <= newtemp) && (newtemp <= cc_tempSetMax)) {
+		    if ((Config.cc_tempSetMin <= newtemp) && (newtemp <= Config.cc_tempSetMax)) {
 		    	syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp);
 		    	srv_send("ack");
 		    	Config.cs_mode = 'b';
 		    	Config.cs_beerSet = newtemp;
 		    } else {
-			syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, cc_tempSetMin, cc_tempSetMax);
+			syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.cc_tempSetMin, Config.cc_tempSetMax);
 			srv_send("err");
 		    }
 		} else {
@@ -206,13 +201,13 @@
 		if (debug)
 		    fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc);
 		if (rc == 1) {
-		    if ((cc_tempSetMin <= newtemp) && (newtemp <= cc_tempSetMax)) {
+		    if ((Config.cc_tempSetMin <= newtemp) && (newtemp <= Config.cc_tempSetMax)) {
 			syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp);
 		    	srv_send("ack");
 		    	Config.cs_mode = 'f';
 		    	Config.cs_fridgeSet = newtemp;
 		    } else {
-			syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, cc_tempSetMin, cc_tempSetMax);
+			syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.cc_tempSetMin, Config.cc_tempSetMax);
 			srv_send("err");
 		    }
 		} else {
@@ -226,10 +221,35 @@
 		syslog(LOG_NOTICE, "Notification: Temperature control disabled");
 		Config.cs_mode = 'o';
 		srv_send("ack");
-	    } else if (strncmp(buf, "setParameters", 13) == 0) {
+	    } else if (strncmp(buf, "setParameters=", 14) == 0) {
+		inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */
 		if (debug)
-		    fprintf(stdout, "FIXME:\n");
-		srv_send("ack");
+		    fprintf(stdout, "setParameters: %s\n", inp);
+		strtok(inp, (char *)"\"");
+		p = strtok(NULL, (char *)"\"");
+		q = strtok(NULL, (char *)":,}");
+		if (strcmp(p, (char *)"tempSetMin") == 0) {
+		    rc = sscanf(q, "%f", &newtemp);
+		    if (rc == 1) {
+			syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp);
+			Config.cc_tempSetMin = newtemp;
+			srv_send("ack");
+		    } else {
+			srv_send("ERR");
+		    }
+		} else if (strcmp(p, (char *)"tempSetMax") == 0) {
+		    rc = sscanf(q, "%f", &newtemp);
+		    if (rc == 1) {
+			syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp);
+			Config.cc_tempSetMax = newtemp;
+			srv_send("ack");
+		    } else {
+			srv_send("ERR");
+		    }
+		} else {
+		    fprintf(stdout, "p='%s' q='%s' inp='%s'\n", p, q, inp);
+		    srv_send("ERR");
+		}
 	// stopScript
 	// quit
 	// eraseLogs

mercurial