Threads partly working via new devices interface

Thu, 31 Jul 2014 23:39:11 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 31 Jul 2014 23:39:11 +0200
changeset 164
f16def8472ba
parent 163
3d813570a5e3
child 165
e97829d0f8f9

Threads partly working via new devices interface

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/units.c file | annotate | diff | comparison | revisions
--- 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);
     }
--- a/thermferm/thermferm.c	Thu Jul 31 22:15:22 2014 +0200
+++ b/thermferm/thermferm.c	Thu Jul 31 23:39:11 2014 +0200
@@ -52,7 +52,7 @@
 #endif
 int			lcdupdate;
 #ifndef HAVE_WIRINGPI_H
-pthread_t		threads[3];
+pthread_t		threads[4];
 #endif
 extern const char       UNITMODE[5][8];
 
@@ -255,6 +255,20 @@
     }
 
 #ifdef HAVE_WIRINGPI_H
+    rc = piThreadCreate(my_devices_loop);
+#else
+    rc = pthread_create(&threads[t], NULL, my_devices_loop, (void *)t );
+#endif
+    if (rc) {
+	fprintf(stderr, "my_devices_loop thread didn't start rc=%d\n", rc);
+	syslog(LOG_NOTICE, "my_devices_loop thread didn't start rc=%d", rc);
+#ifndef HAVE_WIRINGPI_H
+    } else {
+	t++;
+#endif
+    }
+
+#ifdef HAVE_WIRINGPI_H
     rc = piThreadCreate(my_sensors_loop);
 #else
     rc = pthread_create(&threads[t], NULL, my_sensors_loop, (void *)t );
--- a/thermferm/units.c	Thu Jul 31 22:15:22 2014 +0200
+++ b/thermferm/units.c	Thu Jul 31 23:39:11 2014 +0200
@@ -35,54 +35,18 @@
 
 int read_w1_28(char *address, int *val)
 {
-    char 	*device, line[60], *p = NULL;
-    FILE	*fp;
-    int		rc = 0;
-
-    device = xstrcpy((char *)"/sys/bus/w1/devices/");
-    device = xstrcat(device, address);
-    device = xstrcat(device, (char *)"/w1_slave");
+    devices_list	*device;
+    int			tmp;
 
-    /*
-     * 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", val);
-	    if (rc != 1) {
-		syslog(LOG_NOTICE, "sensor %s data parse error", address);
-		rc = 3;
-	    } else {
-		rc = 0;
-	    }
-	} else {
-	    syslog(LOG_NOTICE, "sensor %s CRC error", address);
-	    rc = 1;
+    for (device = Config.devices; device; device = device->next) {
+	if (strcmp(address, device->uuid) == 0) {
+	    tmp = device->value;
+	    *val = tmp;
+	    return device->present;
 	}
-	fclose(fp);
-    } else {
-	syslog(LOG_NOTICE, "sensor %s is missing", address);
-	rc = 2;
     }
 
-    free(device);
-    device = NULL;
-
-    return rc;
+    return 1;
 }
 
 
@@ -139,7 +103,7 @@
 
 	    if (unit->air_address) {
 		rc = read_w1_28(unit->air_address, &temp);
-		if (rc == 0) {
+		if (rc == 2) {
 		    /*
 		     * It is possible to have read errors or extreme values.
 		     * This can happen with bad connections so we compare the
@@ -159,7 +123,7 @@
 			    fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp);
 			}
 		    }
-		} else if (rc == 2) {
+		} else if (rc == 3) {
 		    unit->air_state = 1;
 		} else {
 		    unit->air_state = 2;
@@ -170,7 +134,7 @@
 
 	    if (unit->beer_address) {
 		rc = read_w1_28(unit->beer_address, &temp);
-		if (rc == 0) {
+		if (rc == 2) {
 		    deviation = 20000;
 		    if ((unit->beer_temperature == 0) ||
 		    	(unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) {
@@ -182,7 +146,7 @@
 			    fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp);
 		    	}
 		    }
-		} else if (rc == 2) {
+		} else if (rc == 3) {
 		    unit->beer_state = 1;
 		} else {
 		    unit->beer_state = 2;

mercurial