Mercurial > templog
comparison py/sensor_ds18b20.py @ 443:bca470d153fd
a bit more, and some tests
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 26 Nov 2012 23:21:03 +0800 |
parents | 31ac84425a2d |
children | b32e5a11a4cb |
comparison
equal
deleted
inserted
replaced
442:02318c9660cd | 443:bca470d153fd |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python2.7 |
2 | 2 |
3 class DS18B20s(object): | 3 import gevent |
4 import config | |
5 import re | |
6 from utils import L,W,E,EX | |
4 | 7 |
5 def __init__(self): | 8 class DS18B20s(gevent.Greenlet): |
6 # query the bus | |
7 pass | |
8 | 9 |
9 def get_sensors(self): | 10 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) |
11 | |
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) | |
25 | |
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 | |
40 | |
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): | |
10 """ Returns a sequence of sensorname """ | 51 """ Returns a sequence of sensorname """ |
11 pass | 52 return [d for d in os.listdir(self.master_dir) if |
12 | 53 os.stat(sensor_path(d)).st_mode & stat.S_ISDIR] |
13 def read(self): | |
14 """ Returns a map of sensorname->temperature """ | |
15 pass | |
16 | 54 |
17 def wort_name(self): | 55 def wort_name(self): |
18 pass | 56 return config.WORT_NAME |
19 | 57 |
20 def fridge_name(self): | 58 def fridge_name(self): |
21 pass | 59 return config.FRIDGE_NAME |