summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2015-02-15 15:34:14 +0000
committerroot <root@beaglebone.local.sublab.org>2015-02-15 15:34:14 +0000
commit1e53abeb9ed447d99df3d93f116d00572bb0f9f3 (patch)
treed39e1bdaa7be8430fda8a8fdff04036d08bb1b3b
parent9897d29a2c01bb45f7694d17dac9e1dbd4e70c18 (diff)
envmon: make error handling less fnord-y
-rw-r--r--envmon/onewire-sysfs.py43
-rwxr-xr-xenvmon/onewire2graphite.py2
2 files changed, 33 insertions, 12 deletions
diff --git a/envmon/onewire-sysfs.py b/envmon/onewire-sysfs.py
index 5623483..c10e83f 100644
--- a/envmon/onewire-sysfs.py
+++ b/envmon/onewire-sysfs.py
@@ -3,12 +3,19 @@ Onewire interface using w1 sysfs
"""
import os
+import errno
import re
import sys
import time
class OnewireException(Exception):
pass
+class SensorNotPresent(OnewireException):
+ pass
+class ReadFailed(OnewireException):
+ pass
+class OutOfRange(OnewireException):
+ pass
_sensors = {}
with open(os.path.join(os.path.realpath(os.path.dirname(__file__)), 'onewire-sensors.txt'), 'r') as sensors_file:
@@ -25,33 +32,45 @@ class SensorFacade(object):
self._addr = addr
self._name = name
- def get_temperature(self):
+ def _path(self):
addr = self._addr.replace('.', '-').lower()
- path = '/sys/devices/w1_bus_master1/{0}/w1_slave'.format(addr)
+ return '/sys/devices/w1_bus_master1/{0}/w1_slave'.format(addr)
+
+ def get_temperature(self):
+ path = self._path()
+
+ try: os.stat(path)
+ except OSError, ose:
+ if ose.errno == errno.ENOENT:
+ raise SensorNotPresent, path
+ else:
+ raise
with open(path, 'r') as w1_file:
w1_data = w1_file.read()
+ if w1_data.strip() == '':
+ raise ReadFailed, path
+
match = re.search(r'\st=(\d+)', w1_data)
temp = float(match.group(1)) / 1000
return temp
@property
def temperature(self):
- for i in range(1,3):
+ for i in range(2, 0,-1):
try:
rv = self.get_temperature()
- except Exception:
+ 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)
- continue
-
- if rv > 65:
- continue
- if rv < -25:
- continue
- return rv
- raise OnewireException("Bus error for %s" % self._name)
def sensor(name):
if name not in _sensors:
diff --git a/envmon/onewire2graphite.py b/envmon/onewire2graphite.py
index 73e6b8d..46bc94e 100755
--- a/envmon/onewire2graphite.py
+++ b/envmon/onewire2graphite.py
@@ -19,6 +19,8 @@ while True:
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)