# HG changeset patch # User Michiel Broek # Date 1400618416 -7200 # Node ID 000399c64d3f53a6967c69b517d69649d4eeced3 # Parent 053c4657105fecf586f594ff299781e0f7e909dd More client/server communications diff -r 053c4657105f -r 000399c64d3f thermferm/server.c --- a/thermferm/server.c Tue May 20 17:07:32 2014 +0200 +++ b/thermferm/server.c Tue May 20 22:40:16 2014 +0200 @@ -23,7 +23,7 @@ #include "../lib/mbselib.h" #include "server.h" - +#include "thermferm.h" extern bool my_shutdown; extern bool debug; @@ -32,12 +32,6 @@ extern sys_config Config; extern int clients; -/* beer settings */ -float cs_beerSet = 20.0; -float cs_fridgeSet = 20.0; -unsigned char cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ - - int s; /* connected socket */ int ls; /* listen socket */ @@ -50,6 +44,36 @@ #define SS_TIMEOUT 300 + +void defaultControlsettings(void) +{ + beer->cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ + beer->cs_beerSet = 20.0; + beer->cs_fridgeSet = 20.0; + beer->cs_heatEstimator = 0.2; + beer->cs_coolEstimator = 5; +} + + + +void defaultControlConstants(void) +{ + beer->cc_tempFormat = 'C'; + beer->cc_tempSetMin = 1.0; + beer->cc_tempSetMax = 30.0; + beer->cc_idleRangeH = 1.000; + beer->cc_idleRangeL = -1.000; +} + + + +void defaultControlVariables(void) +{ + beer->cv_beerDiff = 0.0; +} + + + /* * Send message to client */ @@ -95,10 +119,11 @@ void cmd_server(void) { - char *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; + char *inp, *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; int i, rc, rlen, timer; socklen_t fromlen; struct pollfd pfd[1]; + float newtemp; /* * Close listen socket @@ -172,7 +197,7 @@ if (strncmp(buf, "ack", 3) == 0) { srv_send((char *)"ack"); } else if (strncmp(buf, "lcd", 3) == 0) { - sprintf(obuf, "[\"12345678901234567890\", \"12345678901234567890\", \"12345678901234567890\", \"12345678901234567890\"]"); + sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); for (i = 0; i < 20; i++) { obuf[i+2] = lcdbuf[lcdHandle][i][0]; obuf[i+26] = lcdbuf[lcdHandle][i][1]; @@ -181,9 +206,43 @@ } srv_send(obuf); } else if (strncmp(buf, "getMode", 7) == 0) { - srv_send("%c", cs_mode); + srv_send("%c", beer->cs_mode); + } else if (strncmp(buf, "getFridge", 9) == 0) { + srv_send("%.1f", beer->cs_fridgeSet); + } else if (strncmp(buf, "getBeer", 7) == 0) { + srv_send("%.1f", beer->cs_beerSet); + } else if (strncmp(buf, "getControlConstants", 19) == 0) { + srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", + beer->cc_tempFormat, beer->cc_tempSetMin, beer->cc_tempSetMax, beer->cc_idleRangeH, beer->cc_idleRangeL ); } else if (strncmp(buf, "getControlSettings", 18) == 0) { - srv_send("{ mode:%c, \"beerSet\":\"%.1f\", \"fridgeSet\":\"%.1f\", \"heatEstimator\":\"0.2\", \"coolEstimator\":\"5\" }", cs_mode, cs_beerSet, cs_fridgeSet); + srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", + beer->cs_mode, beer->cs_beerSet, beer->cs_fridgeSet, beer->cs_heatEstimator, beer->cs_coolEstimator); + } else if (strncmp(buf, "getControlVariables", 19) == 0) { + srv_send("{ \"beerDiff\":%.2f }", beer->cv_beerDiff); + } else if (strncmp(buf, "setBeer=", 8) == 0) { + inp = xstrcpy(buf+8); + rc = sscanf(inp, "%f", &newtemp); + if (debug) + fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); + if (rc == 1) { + srv_send("ack"); + beer->cs_mode = 'b'; + beer->cs_beerSet = newtemp; + } else { + srv_send("err"); + } + } else if (strncmp(buf, "setFridge=", 10) == 0) { + inp = xstrcpy(buf+10); + rc = sscanf(inp, "%f", &newtemp); + if (debug) + fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); + if (rc == 1) { + srv_send("ack"); + beer->cs_mode = 'f'; + beer->cs_fridgeSet = newtemp; + } else { + srv_send("err"); + } } else { if (debug) fprintf(stdout, "unknown command \"%s\"\n", buf); @@ -231,6 +290,10 @@ if (debug) fprintf(stdout, "Thread my_server_loop started\n"); + defaultControlsettings(); + defaultControlConstants(); + defaultControlVariables(); + memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); myaddr_in.sin_family = AF_INET; diff -r 053c4657105f -r 000399c64d3f thermferm/thermferm.c --- a/thermferm/thermferm.c Tue May 20 17:07:32 2014 +0200 +++ b/thermferm/thermferm.c Tue May 20 22:40:16 2014 +0200 @@ -22,7 +22,6 @@ #include "../lib/mbselib.h" #include "thermferm.h" -#include "mosquitto.h" #include "sensors.h" #include "server.h" @@ -48,6 +47,10 @@ int server(void); void help(void); void die(int); +void sendRCswitch(char *, int); +void stopLCD(void); +void stopRCswitch(void); + void help(void) @@ -77,6 +80,25 @@ +void sendRCswitch(char *address, int state) +{ + char *cmd = NULL; + int rc; + + cmd = xstrcpy(address); + if (state) + cmd = xstrcat(cmd, (char *)",1"); + else + cmd = xstrcat(cmd, (char *)",0"); + rc = toggleSwitch(cmd); + if (debug) + fprintf(stdout, "Switch %s rc=%d\n", cmd, rc); + syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); + free(cmd); +} + + + void stopLCD(void) { mb_lcdClear(lcdHandle); @@ -244,16 +266,13 @@ char buf[1024]; w1_therm *tmp1, *old1; rc_switch *tmp2, *old2; - int rc, run = 0, temp; - + int rc, run = 1, temp; if (lockprog((char *)"thermferm")) { syslog(LOG_NOTICE, "Can't lock"); return 1; } - my_mosquitto_init(); - rc = piThreadCreate(my_sensors_loop); if (rc) { fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); @@ -272,20 +291,21 @@ do { lcdupdate = FALSE; - run = my_mosquitto_loop(); + if (my_shutdown) + run = 0; tmp1 = Config.w1therms; tmp2 = Config.rcswitch; if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { - my_mosquitto_switch(tmp2->address, 0); coolerA = 0; syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); + sendRCswitch(tmp2->address, 0); lcdupdate = TRUE; } if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { - my_mosquitto_switch(tmp2->address, 1); coolerA = 1; syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); + sendRCswitch(tmp2->address, 1); lcdupdate = TRUE; } old1 = tmp1->next; @@ -293,15 +313,15 @@ old2 = tmp2->next; tmp2 = old2; if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { - my_mosquitto_switch(tmp2->address, 0); coolerB = 0; syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); + sendRCswitch(tmp2->address, 0); lcdupdate = TRUE; } if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { - my_mosquitto_switch(tmp2->address, 1); coolerB = 1; syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); + sendRCswitch(tmp2->address, 1); lcdupdate = TRUE; } @@ -336,7 +356,6 @@ stopRCswitch(); } - my_mosquitto_exit(); stopLCD(); disableTransmit(); diff -r 053c4657105f -r 000399c64d3f thermferm/thermferm.h --- a/thermferm/thermferm.h Tue May 20 17:07:32 2014 +0200 +++ b/thermferm/thermferm.h Tue May 20 22:40:16 2014 +0200 @@ -6,4 +6,26 @@ #define FALSE 0 +/* + * IPC shared memory + */ +struct _beer { + float cs_beerSet; + float cs_fridgeSet; + unsigned char cs_mode; /* o = Off, f = fridge, b = beer, p = profile-run */ + float cs_heatEstimator; + float cs_coolEstimator; + + unsigned char cc_tempFormat; + float cc_tempSetMin; + float cc_tempSetMax; + float cc_idleRangeH; + float cc_idleRangeL; + + float cv_beerDiff; +}; + +struct _beer *beer; + + #endif