Mon, 26 May 2014 23:25:48 +0200
More configuration changes. Writing settings implemented.
thermferm/rdconfig.c | file | annotate | diff | comparison | revisions | |
thermferm/server.c | file | annotate | diff | comparison | revisions | |
thermferm/thermferm.h | file | annotate | diff | comparison | revisions |
--- a/thermferm/rdconfig.c Sun May 25 23:29:07 2014 +0200 +++ b/thermferm/rdconfig.c Mon May 26 23:25:48 2014 +0200 @@ -37,6 +37,8 @@ #ifdef HAVE_WIRINGPI_H static int getrcs(char **); #endif +static int getuch(char **); +static int getfloat(char **); //static int getbyt(char **); //static int gethex(char **); @@ -47,12 +49,22 @@ * System configuration table */ key_list keytab[] = { - {(char *)"w1therm", getw1, (char **)&Config.w1therms}, - {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, - {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, - {(char *)"tx433", getint, (char **)&Config.tx433}, - {(char *)"rcswitch", getrcs, (char **)&Config.rcswitch}, - {NULL, NULL, NULL} + {(char *)"w1therm", getw1, (char **)&Config.w1therms}, + {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, + {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, + {(char *)"tx433", getint, (char **)&Config.tx433}, + {(char *)"rcswitch", getrcs, (char **)&Config.rcswitch}, + {(char *)"cs_mode", getuch, (char **)&Config.cs_mode}, + {(char *)"cs_beerSet", getfloat, (char **)&Config.cs_beerSet}, + {(char *)"cs_fridgeSet", getfloat, (char **)&Config.cs_fridgeSet}, + {(char *)"cs_heatEstimator", getfloat, (char **)&Config.cs_heatEstimator}, + {(char *)"cs_coolEstimator", getfloat, (char **)&Config.cs_coolEstimator}, + {(char *)"cc_tempFormat", getuch, (char **)&Config.cc_tempFormat}, + {(char *)"cc_tempSetMin", getfloat, (char **)&Config.cc_tempSetMin}, + {(char *)"cc_tempSetMax", getfloat, (char **)&Config.cc_tempSetMax}, + {(char *)"cc_idleRangeH", getfloat, (char **)&Config.cc_idleRangeH}, + {(char *)"cc_idleRangeL", getfloat, (char **)&Config.cc_idleRangeL}, + {NULL, NULL, NULL} }; @@ -96,6 +108,7 @@ Config.rcswitch = NULL; defaultControlSettings(); + defaultControlConstants(); } @@ -149,6 +162,24 @@ } fprintf(fp, "\n"); + fprintf(fp, "# Control Settings.\n"); + fprintf(fp, "#\n"); + fprintf(fp, "cs_mode %c\n", Config.cs_mode); + fprintf(fp, "cs_beerSet %.1f\n", Config.cs_beerSet); + fprintf(fp, "cs_fridgeSet %.1f\n", Config.cs_fridgeSet); + fprintf(fp, "cs_heatEstimator %.1f\n", Config.cs_heatEstimator); + fprintf(fp, "cs_coolEstimator %.1f\n", Config.cs_coolEstimator); + fprintf(fp, "\n"); + + fprintf(fp, "# Control Constants.\n"); + fprintf(fp, "#\n"); + fprintf(fp, "cc_tempFormat %c\n", Config.cc_tempFormat); + fprintf(fp, "cc_tempSetMin %.1f\n", Config.cc_tempSetMin); + fprintf(fp, "cc_tempSetMax %.1f\n", Config.cc_tempSetMax); + fprintf(fp, "cc_idleRangeH %.1f\n", Config.cc_idleRangeH); + fprintf(fp, "cc_idleRangeL %.1f\n", Config.cc_idleRangeL); + fprintf(fp, "\n"); + fprintf(fp, "# End of generated configuration\n"); fclose(fp); syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc); @@ -362,6 +393,41 @@ #endif + +static int getuch(char **dest) +{ + if (debug) + syslog(LOG_NOTICE, "rdconfig: getuch: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); + + if (isalnum(v[0])) { + *((unsigned char*)dest) = v[0]; + } else { + syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad character", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); + } + return 0; +} + + + +static int getfloat(char **dest) +{ + float val = 0.0; + int rc; + + if (debug) + syslog(LOG_NOTICE, "rdconfig: getfloat: %s(%d): %s %s", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); + + rc = sscanf(v, "%f", &val); + if (rc != 1) { + syslog(LOG_NOTICE, "rdconfig: %s(%d): %s %s - bad float value", mypath, linecnt, MBSE_SS(k), MBSE_SS(v)); + return 1; + } + *((float*)dest) = val; + + return 0; +} + + /* static int getbyt(char **dest) {
--- 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
--- a/thermferm/thermferm.h Sun May 25 23:29:07 2014 +0200 +++ b/thermferm/thermferm.h Mon May 26 23:25:48 2014 +0200 @@ -84,6 +84,12 @@ float cs_fridgeSet; /* fridge temperature */ float cs_heatEstimator; float cs_coolEstimator; + /* ControlConstants */ + unsigned char cc_tempFormat; + float cc_tempSetMin; + float cc_tempSetMax; + float cc_idleRangeH; + float cc_idleRangeL; } sys_config; @@ -163,6 +169,7 @@ /* server.c */ void defaultControlSettings(void); +void defaultControlConstants(void); PI_THREAD (my_server_loop); #endif