thermferm/thermferm.c

changeset 46
000399c64d3f
parent 44
f37d73940699
child 48
d8c64f81b192
equal deleted inserted replaced
45:053c4657105f 46:000399c64d3f
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/ 21 *****************************************************************************/
22 22
23 #include "../lib/mbselib.h" 23 #include "../lib/mbselib.h"
24 #include "thermferm.h" 24 #include "thermferm.h"
25 #include "mosquitto.h"
26 #include "sensors.h" 25 #include "sensors.h"
27 #include "server.h" 26 #include "server.h"
28 27
29 #ifdef HAVE_WIRINGPI_H 28 #ifdef HAVE_WIRINGPI_H
30 29
46 45
47 46
48 int server(void); 47 int server(void);
49 void help(void); 48 void help(void);
50 void die(int); 49 void die(int);
50 void sendRCswitch(char *, int);
51 void stopLCD(void);
52 void stopRCswitch(void);
53
51 54
52 55
53 void help(void) 56 void help(void)
54 { 57 {
55 fprintf(stdout, "mbsePi-apps thermferm v%s starting\n\n", VERSION); 58 fprintf(stdout, "mbsePi-apps thermferm v%s starting\n\n", VERSION);
71 break; 74 break;
72 default: syslog(LOG_NOTICE, "die() on signal %d", onsig); 75 default: syslog(LOG_NOTICE, "die() on signal %d", onsig);
73 } 76 }
74 77
75 my_shutdown = true; 78 my_shutdown = true;
79 }
80
81
82
83 void sendRCswitch(char *address, int state)
84 {
85 char *cmd = NULL;
86 int rc;
87
88 cmd = xstrcpy(address);
89 if (state)
90 cmd = xstrcat(cmd, (char *)",1");
91 else
92 cmd = xstrcat(cmd, (char *)",0");
93 rc = toggleSwitch(cmd);
94 if (debug)
95 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
96 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
97 free(cmd);
76 } 98 }
77 99
78 100
79 101
80 void stopLCD(void) 102 void stopLCD(void)
242 int server(void) 264 int server(void)
243 { 265 {
244 char buf[1024]; 266 char buf[1024];
245 w1_therm *tmp1, *old1; 267 w1_therm *tmp1, *old1;
246 rc_switch *tmp2, *old2; 268 rc_switch *tmp2, *old2;
247 int rc, run = 0, temp; 269 int rc, run = 1, temp;
248
249 270
250 if (lockprog((char *)"thermferm")) { 271 if (lockprog((char *)"thermferm")) {
251 syslog(LOG_NOTICE, "Can't lock"); 272 syslog(LOG_NOTICE, "Can't lock");
252 return 1; 273 return 1;
253 } 274 }
254
255 my_mosquitto_init();
256 275
257 rc = piThreadCreate(my_sensors_loop); 276 rc = piThreadCreate(my_sensors_loop);
258 if (rc) { 277 if (rc) {
259 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); 278 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc);
260 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); 279 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc);
270 logger((char *)"thermferm.log", (char *)"thermferm", buf); 289 logger((char *)"thermferm.log", (char *)"thermferm", buf);
271 290
272 do { 291 do {
273 lcdupdate = FALSE; 292 lcdupdate = FALSE;
274 293
275 run = my_mosquitto_loop(); 294 if (my_shutdown)
295 run = 0;
276 296
277 tmp1 = Config.w1therms; 297 tmp1 = Config.w1therms;
278 tmp2 = Config.rcswitch; 298 tmp2 = Config.rcswitch;
279 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { 299 if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) {
280 my_mosquitto_switch(tmp2->address, 0);
281 coolerA = 0; 300 coolerA = 0;
282 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); 301 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
302 sendRCswitch(tmp2->address, 0);
283 lcdupdate = TRUE; 303 lcdupdate = TRUE;
284 } 304 }
285 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { 305 if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) {
286 my_mosquitto_switch(tmp2->address, 1);
287 coolerA = 1; 306 coolerA = 1;
288 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); 307 syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
308 sendRCswitch(tmp2->address, 1);
289 lcdupdate = TRUE; 309 lcdupdate = TRUE;
290 } 310 }
291 old1 = tmp1->next; 311 old1 = tmp1->next;
292 tmp1 = old1; 312 tmp1 = old1;
293 old2 = tmp2->next; 313 old2 = tmp2->next;
294 tmp2 = old2; 314 tmp2 = old2;
295 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { 315 if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) {
296 my_mosquitto_switch(tmp2->address, 0);
297 coolerB = 0; 316 coolerB = 0;
298 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); 317 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0));
318 sendRCswitch(tmp2->address, 0);
299 lcdupdate = TRUE; 319 lcdupdate = TRUE;
300 } 320 }
301 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { 321 if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) {
302 my_mosquitto_switch(tmp2->address, 1);
303 coolerB = 1; 322 coolerB = 1;
304 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); 323 syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0));
324 sendRCswitch(tmp2->address, 1);
305 lcdupdate = TRUE; 325 lcdupdate = TRUE;
306 } 326 }
307 327
308 if (run && lcdupdate) { 328 if (run && lcdupdate) {
309 lcdPosition(lcdHandle, 0, 0); 329 lcdPosition(lcdHandle, 0, 0);
334 354
335 if (Config.tx433 != -1) { 355 if (Config.tx433 != -1) {
336 stopRCswitch(); 356 stopRCswitch();
337 } 357 }
338 358
339 my_mosquitto_exit();
340 stopLCD(); 359 stopLCD();
341 disableTransmit(); 360 disableTransmit();
342 361
343 ulockprog((char *)"thermferm"); 362 ulockprog((char *)"thermferm");
344 363

mercurial