439
|
1 #!/usr/bin/env python2.7 |
|
2 |
443
|
3 import gevent |
|
4 import config |
|
5 import re |
|
6 from utils import L,W,E,EX |
|
7 |
|
8 class DS18B20s(gevent.Greenlet): |
|
9 |
|
10 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) |
439
|
11 |
443
|
12 def __init__(self, server): |
|
13 gevent.Greenlet.__init__(self) |
|
14 self.server = server |
|
15 # XXX set up paths |
|
16 # XXX set up drain etc |
|
17 |
|
18 def _run(self): |
|
19 while True: |
|
20 self.do() |
|
21 gevent.sleep(config.SENSOR_SLEEP) |
|
22 |
|
23 def sensor_path(self, s): |
|
24 return os.path.join(self.master_dir, s) |
439
|
25 |
443
|
26 def do_sensor_name(self, s, contents = None): |
|
27 try: |
|
28 if contents is None: |
|
29 fn = os.path.join(self.sensor_path(s), 'w1_slave') |
|
30 f = open(fn, 'r') |
|
31 contents = f.read() |
|
32 match = self.THERM_RE.match(contents) |
|
33 if match is None: |
|
34 return None |
|
35 temp = int(match.groups(1)[0]) |
|
36 return temp / 1000.0 |
|
37 except Exception, e: |
|
38 EX("Problem reading sensor '%s': %s" % (s, str(e))) |
|
39 return None |
439
|
40 |
443
|
41 def do(self): |
|
42 vals = {} |
|
43 for n in self.sensor_names(): |
|
44 value = do_sensor(n) |
|
45 if value is not None: |
|
46 vals[n] = value |
|
47 |
|
48 self.server.add_reading(vals) |
|
49 |
|
50 def sensor_names(self): |
|
51 """ Returns a sequence of sensorname """ |
|
52 return [d for d in os.listdir(self.master_dir) if |
|
53 os.stat(sensor_path(d)).st_mode & stat.S_ISDIR] |
439
|
54 |
|
55 def wort_name(self): |
443
|
56 return config.WORT_NAME |
439
|
57 |
|
58 def fridge_name(self): |
443
|
59 return config.FRIDGE_NAME |