thermferm/devices.c

changeset 164
f16def8472ba
parent 163
3d813570a5e3
child 166
c31ea86fec43
--- a/thermferm/devices.c	Thu Jul 31 22:15:22 2014 +0200
+++ b/thermferm/devices.c	Thu Jul 31 23:39:11 2014 +0200
@@ -195,9 +195,9 @@
 #endif
 {
     devices_list	*device;
-//    char		line[60], *p = NULL;
-  //  FILE		*fp;
-//    int			temp, rc, deviation;
+    char		*addr = NULL, line[60], *p = NULL;
+    FILE		*fp;
+    int			temp, rc;
 
     syslog(LOG_NOTICE, "Thread my_devices_loop started");
     if (debug)
@@ -209,79 +209,60 @@
     for (;;) {
 
     	/*
-    	 * Here send our 1-wire sensors values
+    	 * Process all devices.
     	 */
     	for (device = Config.devices; device; device = device->next) {
 
 	    if (my_shutdown)
 		break;
 
-	    /*
-	     * Build path to the on-wire sensor
-	     */
-//	    device = xstrcpy((char *)"/sys/bus/w1/devices/");
-//	    device = xstrcat(device, tmp1->master);
-//	    device = xstrcat(device, (char *)"/");
-//	    device = xstrcat(device, tmp1->name);
-//	    device = xstrcat(device, (char *)"/w1_slave");
-
-	    /*
-	     * Read sensor data
-	     */
-//	    if ((fp = fopen(device, "r"))) {
-		/*
-		 * The output looks like:
-		 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
-		 * 72 01 4b 46 7f ff 0e 10 57 t=23125
-		 */
-//		fgets(line, 50, fp);
-//		line[strlen(line)-1] = '\0';
-//		if ((line[36] == 'Y') && (line[37] == 'E')) {
-		    /*
-		     * CRC is Ok, continue
-		     */
-//		    fgets(line, 50, fp);
-//		    line[strlen(line)-1] = '\0';
-//		    strtok(line, (char *)"=");
-//		    p = strtok(NULL, (char *)"=");
-//		    rc = sscanf(p, "%d", &temp);
-//		    if ((rc == 1) && (tmp1->lastval != temp)) {
-			/*
-			 * It is possible to have read errors or extreme values.
-			 * This can happen with bad connections so we compare the
-			 * value with the previous one. If the difference is too
-			 * much, we don't send that value. That also means that if
-			 * the next value is ok again, it will be marked invalid too.
-			 * Maximum error is 20 degrees for now.
-			 */
-//			deviation = 20000;
-//			if ( (tmp1->lastval == 0) ||
-//			     (tmp1->lastval && (temp > (tmp1->lastval - deviation)) && (temp < (tmp1->lastval + deviation))) ) {
-			    /*
-			     * Temperature is changed and valid, set flag.
-			     */
-//			    tmp1->update = TRUE;
-//			} else {
-//			    syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
-//			    if (debug) {
-//				fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, tmp1->lastval, temp);
-//			    }
-//			}
-//			tmp1->lastval = temp;
-//		    }
-//		} else {
-//		    syslog(LOG_NOTICE, "sensor %s/%s CRC error", tmp1->master, tmp1->name);
-//		}
-//		fclose(fp);
-//		tmp1->present = 1;
-//	    } else {
-//		tmp1->present = 0;
-//		if (debug)
-//		    printf("sensor %s is missing\n", tmp1->name);
-//	    }
-
-//	    free(device);
-//	    device = NULL;
+	    switch (device->type) {
+		case DEVTYPE_W1:
+			if (strncmp(device->address, (char *)"28", 2) == 0) {
+			    /* Read DS18B20 sensor */
+			    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
+			    addr = xstrcat(addr, device->address);
+			    addr = xstrcat(addr, (char *)"/w1_slave");
+			    if ((fp = fopen(addr, "r"))) {
+				if (device->present != DEVPRESENT_YES) {
+				     syslog(LOG_NOTICE, "sensor %s is back", device->address);
+				     device->present = DEVPRESENT_YES;
+				}
+				/*
+				 * The output looks like:
+				 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
+				 * 72 01 4b 46 7f ff 0e 10 57 t=23125
+				 */
+				fgets(line, 50, fp);
+				line[strlen(line)-1] = '\0';
+				if ((line[36] == 'Y') && (line[37] == 'E')) {
+				    /* CRC is Ok, continue */
+				    fgets(line, 50, fp);
+				    line[strlen(line)-1] = '\0';
+				    strtok(line, (char *)"=");
+				    p = strtok(NULL, (char *)"=");
+				    rc = sscanf(p, "%d", &temp);
+				    if ((rc == 1) && (device->value != temp)) {
+					device->value = temp;
+				    	device->timestamp = time(NULL);
+				    }
+				} else {
+				    syslog(LOG_NOTICE, "sensor %s CRC error", device->address);
+				    device->present = DEVPRESENT_ERROR;
+				}
+			    } else {
+				if (device->present != DEVPRESENT_NO) {
+				    syslog(LOG_NOTICE, "sensor %s is missing", device->address);
+				    device->present = DEVPRESENT_NO;
+				}
+			    }
+			    free(addr);
+			    addr = NULL;
+			}
+			break;
+		default:
+			break;
+	    }
     	}
 	usleep(10000);
     }

mercurial