thermferm/thermferm.c

changeset 46
000399c64d3f
parent 44
f37d73940699
child 48
d8c64f81b192
--- 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();
 

mercurial