thermferm/devices.c

changeset 240
6bdda35b4a13
parent 239
793c09ece542
child 242
d3fe84ece349
--- a/thermferm/devices.c	Thu Aug 14 22:02:33 2014 +0200
+++ b/thermferm/devices.c	Thu Aug 14 22:43:41 2014 +0200
@@ -178,7 +178,7 @@
     now = time(NULL);
 
     for (device = Config.devices; device; device = device->next) {
-	if (strcmp(uuid, device->uuid) == 0) {
+	if (! strcmp(uuid, device->uuid)) {
 	    /*
 	     * Execute command if different then the old value. But also
 	     * every 2 minutes because commands can have temporary
@@ -189,18 +189,20 @@
 #ifdef HAVE_WIRINGPI_H
 		rc = 0;
 		if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) {
-	            enableTransmit(device->gpiopin);
 		    snprintf(buf, 39, "%s,%d", device->address, value ? 1:0);
 		    for (i = 0; i < strlen(buf); i++)
 			if (buf[i] == '-')
-			    buf[i] = ',';	
+			    buf[i] = ',';
+	    	    enableTransmit(device->gpiopin);	    
 		    rc = toggleSwitch(buf);
+		    disableTransmit();
 		    syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
 		    if (debug)
 			fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc);
-		    disableTransmit();
+		    piLock(LOCK_DEVICES);
                     device->value = value;
 		    device->timestamp = time(NULL);
+		    piUnlock(LOCK_DEVICES);
 		    return rc;
 	        }
 
@@ -436,7 +438,13 @@
 			    if ((fp = fopen(addr, "r"))) {
 				if (device->present != DEVPRESENT_YES) {
 				     syslog(LOG_WARNING, "sensor %s is back", device->address);
+#ifdef HAVE_WIRINGPI_H
+				     piLock(LOCK_DEVICES);
+#endif
 				     device->present = DEVPRESENT_YES;
+#ifdef HAVE_WIRINGPI_H
+				     piUnlock(LOCK_DEVICES);
+#endif
 				}
 				/*
 				 * The output looks like:
@@ -452,19 +460,37 @@
 				    strtok(line, (char *)"=");
 				    p = strtok(NULL, (char *)"=");
 				    rc = sscanf(p, "%d", &temp);
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);
+#endif
 				    if ((rc == 1) && (device->value != temp)) {
 					device->value = temp;
 				    	device->timestamp = time(NULL);
 				    }
+#ifdef HAVE_WIRINGPI_H
+				    piUnlock(LOCK_DEVICES);
+#endif
 				} else {
 				    syslog(LOG_WARNING, "sensor %s CRC error", device->address);
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);                
+#endif
 				    device->present = DEVPRESENT_ERROR;
+#ifdef HAVE_WIRINGPI_H
+				    piUnlock(LOCK_DEVICES);              
+#endif
 				}
 				fclose(fp);
 			    } else {
 				if (device->present != DEVPRESENT_NO) {
 				    syslog(LOG_WARNING, "sensor %s is missing", device->address);
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);                
+#endif
 				    device->present = DEVPRESENT_NO;
+#ifdef HAVE_WIRINGPI_H          
+				    piUnlock(LOCK_DEVICES);              
+#endif
 				}
 			    }
 			    free(addr);
@@ -484,6 +510,7 @@
 			    dht11_last = now;
 			}
 			if (device->subdevice == 0) {
+			    piLock(LOCK_DEVICES);
 			    if (dht11_valid) {
 				device->value = dht11_temperature * 1000;
 				device->timestamp = time(NULL);
@@ -491,7 +518,9 @@
 			    } else {
 				device->present = DEVPRESENT_ERROR;
 			    }
+			    piUnlock(LOCK_DEVICES);
 			} else if (device->subdevice == 1) {
+			    piLock(LOCK_DEVICES);
 			    if (dht11_valid) {
 				device->value = dht11_humidity * 1000;
 				device->timestamp = time(NULL);
@@ -499,6 +528,7 @@
 			    } else {
 				device->present = DEVPRESENT_ERROR;
 			    }
+			    piUnlock(LOCK_DEVICES);
 			}
 			break;
 #endif

mercurial