--- 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;