# HG changeset patch # User Michiel Broek # Date 1401053347 -7200 # Node ID 37623517e0ef37e458e38c21f806ae7a2b1e841c # Parent 4387a6b11eb3139882c97525b906f6d7c530a1ab Added writing configuration diff -r 4387a6b11eb3 -r 37623517e0ef thermferm/rdconfig.c --- a/thermferm/rdconfig.c Sun May 25 22:37:23 2014 +0200 +++ b/thermferm/rdconfig.c Sun May 25 23:29:07 2014 +0200 @@ -47,16 +47,11 @@ * System configuration table */ key_list keytab[] = { - {(char *)"mosq_host", getstr, &Config.mosq_host}, - {(char *)"mosq_port", getint, (char **)&Config.mosq_port}, {(char *)"w1therm", getw1, (char **)&Config.w1therms}, -#ifdef HAVE_WIRINGPI_H {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, - {(char *)"rx433", getint, (char **)&Config.rx433}, {(char *)"tx433", getint, (char **)&Config.tx433}, {(char *)"rcswitch", getrcs, (char **)&Config.rcswitch}, -#endif {NULL, NULL, NULL} }; @@ -65,19 +60,12 @@ void killconfig(void) { w1_therm *tmp1, *old1; -#ifdef HAVE_WIRINGPI_H rc_switch *tmp2, *old2; -#endif if (Config.name) free(Config.name); Config.name = NULL; - if (Config.mosq_host) - free(Config.mosq_host); - Config.mosq_host= (char *)"localhost"; - Config.mosq_port = 1883; - for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { old1 = tmp1->next; if (tmp1->master) @@ -91,10 +79,8 @@ Config.w1therms = NULL; Config.my_port = 6554; -#ifdef HAVE_WIRINGPI_H Config.lcd_cols = 16; Config.lcd_rows = 2; - Config.rx433 = -1; Config.tx433 = -1; for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) { @@ -109,7 +95,67 @@ } Config.rcswitch = NULL; -#endif + defaultControlSettings(); +} + + + +int wrconfig(char *config) +{ + int rc = 0; + FILE *fp; + w1_therm *tmp1; + rc_switch *tmp2; + + mypath = xstrcpy(getenv((char *)"HOME")); + mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); + mypath = xstrcat(mypath, config); + + if (debug) + fprintf(stdout, "Writing %s\n", mypath); + + if ((fp = fopen(mypath, "w")) == NULL) { + syslog(LOG_NOTICE, "could not rewrite %s", mypath); + return 1; + } + + fprintf(fp, "# Configuration file for thermferm %s\n", VERSION); + fprintf(fp, "\n"); + + fprintf(fp, "# Radio controllers 433 MHz.\n"); + fprintf(fp, "#\n"); + fprintf(fp, "tx433 %d\n", Config.tx433); + fprintf(fp, "\n"); + + fprintf(fp, "# LCD display\n"); + fprintf(fp, "#\n"); + fprintf(fp, "lcd_cols %d\n", Config.lcd_cols); + fprintf(fp, "lcd_rows %d\n", Config.lcd_rows); + fprintf(fp, "\n"); + + fprintf(fp, "# DS18B20 temperature sensors on the 1-wire bus.\n"); + fprintf(fp, "#\n"); + fprintf(fp, "# kwd master bus name alias\n"); + for (tmp1 = Config.w1therms; tmp1; tmp1 = tmp1->next) { + fprintf(fp, "w1therm %s %d %s %s\n", tmp1->master, tmp1->bus, tmp1->name, tmp1->alias); + } + fprintf(fp, "\n"); + + fprintf(fp, "# RC switches that we want to control.\n"); + fprintf(fp, "#\n"); + fprintf(fp, "# kwd address alias\n"); + for (tmp2 = Config.rcswitch; tmp2; tmp2 = tmp2->next) { + fprintf(fp, "rcswitch %s %s\n", tmp2->address, tmp2->alias); + } + fprintf(fp, "\n"); + + fprintf(fp, "# End of generated configuration\n"); + fclose(fp); + syslog(LOG_NOTICE, "Written %s rc=%d", mypath, rc); + free(mypath); + mypath = NULL; + + return rc; } diff -r 4387a6b11eb3 -r 37623517e0ef thermferm/server.c --- a/thermferm/server.c Sun May 25 22:37:23 2014 +0200 +++ b/thermferm/server.c Sun May 25 23:29:07 2014 +0200 @@ -39,12 +39,6 @@ #define SS_BUFSIZE 1024 #define SS_TIMEOUT 300 -unsigned char cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ -float cs_beerSet = 20.0; -float cs_fridgeSet = 20.0; -float cs_heatEstimator = 0.2; -float cs_coolEstimator = 5; - unsigned char cc_tempFormat = 'C'; float cc_tempSetMin = 1.0; float cc_tempSetMax = 30.0; @@ -57,11 +51,11 @@ void defaultControlSettings(void) { - cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ - cs_beerSet = 20.0; - cs_fridgeSet = 20.0; - cs_heatEstimator = 0.2; - cs_coolEstimator = 5; + Config.cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ + Config.cs_beerSet = 20.0; + Config.cs_fridgeSet = 20.0; + Config.cs_heatEstimator = 0.2; + Config.cs_coolEstimator = 5; } @@ -167,17 +161,17 @@ } srv_send(obuf); } else if (strncmp(buf, "getMode", 7) == 0) { - srv_send("%c", cs_mode); + srv_send("%c", Config.cs_mode); } else if (strncmp(buf, "getFridge", 9) == 0) { - srv_send("%.1f", cs_fridgeSet); + srv_send("%.1f", Config.cs_fridgeSet); } else if (strncmp(buf, "getBeer", 7) == 0) { - srv_send("%.1f", cs_beerSet); + 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 ); } else if (strncmp(buf, "getControlSettings", 18) == 0) { srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", - cs_mode, cs_beerSet, cs_fridgeSet, cs_heatEstimator, cs_coolEstimator); + Config.cs_mode, Config.cs_beerSet, Config.cs_fridgeSet, Config.cs_heatEstimator, Config.cs_coolEstimator); } else if (strncmp(buf, "getControlVariables", 19) == 0) { srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { @@ -195,8 +189,8 @@ if ((cc_tempSetMin <= newtemp) && (newtemp <= cc_tempSetMax)) { syslog(LOG_NOTICE, "Beer temperature set to %.1f degrees in web interface", newtemp); srv_send("ack"); - cs_mode = 'b'; - cs_beerSet = newtemp; + 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); srv_send("err"); @@ -215,8 +209,8 @@ if ((cc_tempSetMin <= newtemp) && (newtemp <= cc_tempSetMax)) { syslog(LOG_NOTICE, "Fridge temperature set to %.1f degrees in web interface", newtemp); srv_send("ack"); - cs_mode = 'f'; - cs_fridgeSet = newtemp; + 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); srv_send("err"); @@ -230,7 +224,7 @@ if (debug) fprintf(stdout, "temperature control disabled\n"); syslog(LOG_NOTICE, "Notification: Temperature control disabled"); - cs_mode = 'o'; + Config.cs_mode = 'o'; srv_send("ack"); } else if (strncmp(buf, "setParameters", 13) == 0) { if (debug) diff -r 4387a6b11eb3 -r 37623517e0ef thermferm/thermferm.c --- a/thermferm/thermferm.c Sun May 25 22:37:23 2014 +0200 +++ b/thermferm/thermferm.c Sun May 25 23:29:07 2014 +0200 @@ -366,6 +366,8 @@ stopLCD(); disableTransmit(); + wrconfig((char *)"thermferm.conf"); + ulockprog((char *)"thermferm"); if (debug) diff -r 4387a6b11eb3 -r 37623517e0ef thermferm/thermferm.h --- a/thermferm/thermferm.h Sun May 25 22:37:23 2014 +0200 +++ b/thermferm/thermferm.h Sun May 25 23:29:07 2014 +0200 @@ -72,22 +72,24 @@ typedef struct _sys_config { char *name; /* Configuration name */ - char *mosq_host; /* mosquitto server hostname */ - int mosq_port; /* mosquitto server port */ int my_port; /* my client/server port */ w1_therm *w1therms; /* 1-wire temp sensors */ -#ifdef HAVE_WIRINGPI_H int lcd_cols; /* LCD display columns */ int lcd_rows; /* LCD display rows */ - int rx433; /* 433 MHz receiver pin */ int tx433; /* 433 MHz transmitter pin */ rc_switch *rcswitch; /* 433 MHz RC Power switches */ -#endif + /* ControlSettings: */ + unsigned char cs_mode; /* mode */ + float cs_beerSet; /* beer temperature */ + float cs_fridgeSet; /* fridge temperature */ + float cs_heatEstimator; + float cs_coolEstimator; } sys_config; void killconfig(void); int rdconfig(char *); +int wrconfig(char *); /* lock.c */ @@ -156,7 +158,11 @@ void dht11Read(void); void dht11Init(int, int, int); + PI_THREAD (my_sensors_loop); + +/* server.c */ +void defaultControlSettings(void); PI_THREAD (my_server_loop); #endif