diff options
author | root <root@beaglebone.local.sublab.org> | 2016-01-21 08:47:52 +0000 |
---|---|---|
committer | root <root@beaglebone.local.sublab.org> | 2016-01-21 08:47:52 +0000 |
commit | 252068641357a34456e866e486542b02490eacbe (patch) | |
tree | 7b92f9cecda52edb753516eb9d2207b8b087ba33 | |
parent | a9f65e0ee3934e11aaa64ff58e7571b4b95b45ca (diff) |
intermediate code foo
-rw-r--r-- | cethcan/cethcan.json | 2 | ||||
-rw-r--r-- | envmon/onewire-sysfs.py | 45 | ||||
-rwxr-xr-x | envmon/onewire2graphite.py | 50 |
3 files changed, 67 insertions, 30 deletions
diff --git a/cethcan/cethcan.json b/cethcan/cethcan.json index 7aedf31..8998550 100644 --- a/cethcan/cethcan.json +++ b/cethcan/cethcan.json @@ -24,6 +24,8 @@ "dali.hacklab.east.inner", "dali.hacklab.west.outer" ]} + { "addr": 1100, "name": "dali.flur.wc" }, + { "addr": 1151, "name": "dali.werkstatt.doppel" }, ], "beans": [ { "addr": 256, "name": "toilet.lock", "values": [ "open", "closed" ] }, diff --git a/envmon/onewire-sysfs.py b/envmon/onewire-sysfs.py index c10e83f..d889768 100644 --- a/envmon/onewire-sysfs.py +++ b/envmon/onewire-sysfs.py @@ -7,6 +7,7 @@ import errno import re import sys import time +import random class OnewireException(Exception): pass @@ -31,6 +32,9 @@ class SensorFacade(object): def __init__(self, addr, name): self._addr = addr self._name = name + self._cached = None + self._cachets = None + self._backoff = None def _path(self): addr = self._addr.replace('.', '-').lower() @@ -58,19 +62,34 @@ class SensorFacade(object): @property def temperature(self): - for i in range(2, 0,-1): - try: - rv = self.get_temperature() - if rv > 65 or rv < -25: - raise OutOfRange, (self._path(), rv) - return rv - except SensorNotPresent: - raise - except Exception, e: - if i == 1: - raise - sys.excepthook(*sys.exc_info()) - time.sleep(0.5) + if self._cachets > time.time() - 50: + return self._cached + if self._backoff > time.time(): + if self._cachets > time.time() - 250: + return self._cached + return None + + try: + rv = self.get_temperature() + if rv > 65 or rv < -25: + raise OutOfRange, (self._path(), rv) + self._cached = rv + self._cachets = time.time() + self._backoff = None + print '\033[32m', self._name, 'read', self._cached, '\033[m' + return self._cached + except SensorNotPresent: + self._backoff = time.time() + 2 + random.expovariate(1./3.) * 2 + print '\033[31m', self._name, 'SensorNotPresent back-off', self._backoff - time.time(), '\033[m' + if self._cachets > time.time() - 250: + return self._cached + raise + except OutOfRange: + self._backoff = time.time() + 15 + random.expovariate(1./4.) * 10 + print '\033[33m', self._name, 'OutOfRange back-off', self._backoff - time.time(), '\033[m' + if self._cachets > time.time() - 250: + return self._cached + raise def sensor(name): if name not in _sensors: diff --git a/envmon/onewire2graphite.py b/envmon/onewire2graphite.py index 46bc94e..4b7743e 100755 --- a/envmon/onewire2graphite.py +++ b/envmon/onewire2graphite.py @@ -6,27 +6,43 @@ import sys import onewire import socket -CARBON_SERVER = 'sonar.local.sublab.org' +CARBON_SERVER = '2a02:238:f02a:8e2f:1:67:7261:7068' # 'sonar.local.sublab.org' CARBON_PORT = 2003 -prefix = "envmon.beaglebone_local_sublab_org." +prefix = "sublab.beaglebone.env." -sock = socket.socket() +sock = socket.socket(socket.AF_INET6) sock.connect((CARBON_SERVER, CARBON_PORT)) +sensors = onewire.sensors() +sensobj = dict([(sensor, onewire.sensor(sensor)) for sensor in sensors]) +vals = {} + while True: - message = '' - for sensor in onewire.sensors(): - try: - message += "%s %f %d\n" % (prefix + sensor, onewire.sensor(sensor).temperature, time.time()) - except onewire.SensorNotPresent, e: - print >>sys.stderr, 'SensorNotPresent:', str(e) - except Exception: - print >>sys.stderr, "On %s: Could not retrieve temperature for '%s':" % ( - time.strftime('%a, %d %b %Y %T %z'), sensor) - sys.excepthook(*sys.exc_info()) - print >>sys.stderr, '========================================' - if message: - print message + nextslot = time.time() + 65 + nextslot = nextslot - (nextslot % 60) + + while nextslot - time.time() > 5.0: + for sensor in sensors: + try: + vals.setdefault(sensor, None) + vals[sensor] = sensobj[sensor].temperature + except onewire.SensorNotPresent, e: + print >>sys.stderr, sensor, 'SensorNotPresent:', str(e) + except onewire.OutOfRange, e: + # print >>sys.stderr, sensor, 'OutOfRange:', str(e) + pass + except Exception: + print >>sys.stderr, "On %s: Could not retrieve temperature for '%s':" % ( + time.strftime('%a, %d %b %Y %T %z'), sensor) + sys.excepthook(*sys.exc_info()) + print >>sys.stderr, '========================================' + time.sleep(0.5) + + time.sleep(max(nextslot - time.time(), 0)) + goodvals = [sensor for sensor in sensors if vals.get(sensor, None) is not None] + print '%d of %d sensors have data' % (len(goodvals), len(sensors)) + message = ''.join(["%s %f %d\n" % (prefix + sensor, vals[sensor], nextslot) for sensor in goodvals]) + if message != '': + # print message sock.sendall(message) - time.sleep(300-len(onewire.sensors())) |