Mercurial > templog
comparison py/sensor_ds18b20.py @ 149:d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 16 Dec 2012 22:14:46 +0800 |
parents | b32e5a11a4cb |
children | e114b38c8a55 |
comparison
equal
deleted
inserted
replaced
148:b32e5a11a4cb | 149:d686b111dab4 |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python2.7 |
2 | |
3 import os | |
4 import re | |
2 | 5 |
3 import gevent | 6 import gevent |
4 import gevent.threadpool | 7 import gevent.threadpool |
8 | |
5 import config | 9 import config |
6 import re | 10 from utils import D,L,W,E,EX |
7 from utils import L,W,E,EX | |
8 | 11 |
9 class DS18B20s(gevent.Greenlet): | 12 class DS18B20s(gevent.Greenlet): |
10 | 13 |
11 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) | 14 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) |
12 | 15 |
25 # handles a blocking file read with a gevent threadpool. A | 28 # handles a blocking file read with a gevent threadpool. A |
26 # real python thread performs the read while other gevent | 29 # real python thread performs the read while other gevent |
27 # greenlets keep running. | 30 # greenlets keep running. |
28 # the ds18b20 takes ~750ms to read, which is noticable | 31 # the ds18b20 takes ~750ms to read, which is noticable |
29 # interactively. | 32 # interactively. |
30 return self.readthread.apply(lambda: f.read) | 33 return self.readthread.apply(f.read) |
31 | 34 |
32 def do_sensor(self, s, contents = None): | 35 def do_sensor(self, s, contents = None): |
33 """ contents can be set by the caller for testing """ | 36 """ contents can be set by the caller for testing """ |
37 D("dosensor %s" % s) | |
34 try: | 38 try: |
35 if contents is None: | 39 if contents is None: |
36 fn = os.path.join(self.master_dir, s, 'w1_slave') | 40 fn = os.path.join(self.master_dir, s, 'w1_slave') |
37 f = open(fn, 'r') | 41 f = open(fn, 'r') |
38 contents = self.read_wait(f) | 42 contents = self.read_wait(f) |
43 | |
39 match = self.THERM_RE.match(contents) | 44 match = self.THERM_RE.match(contents) |
40 if match is None: | 45 if match is None: |
46 D("no match") | |
41 return None | 47 return None |
42 temp = int(match.groups(1)[0]) | 48 temp = int(match.groups(1)[0]) / 1000.0 |
43 return temp / 1000.0 | 49 D("returning %f" % temp) |
50 return temp | |
44 except Exception, e: | 51 except Exception, e: |
45 EX("Problem reading sensor '%s': %s" % (s, str(e))) | 52 EX("Problem reading sensor '%s': %s" % (s, str(e))) |
46 return None | 53 return None |
47 | 54 |
48 def do(self): | 55 def do(self): |
49 vals = {} | 56 vals = {} |
50 for n in self.sensor_names(): | 57 for n in self.sensor_names(): |
51 value = do_sensor(n) | 58 value = self.do_sensor(n) |
52 if value is not None: | 59 if value is not None: |
53 vals[n] = value | 60 vals[n] = value |
54 | 61 |
55 self.server.add_reading(vals) | 62 self.server.add_reading(vals) |
56 | 63 |
57 def sensor_names(self): | 64 def sensor_names(self): |
58 """ Returns a sequence of sensorname """ | 65 """ Returns a sequence of sensorname """ |
59 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") | 66 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") |
60 return open(slaves_path, 'r').split() | 67 names = open(slaves_path, 'r').read().split() |
68 D("returning names %s" % names) | |
69 return names | |
61 | 70 |
62 def wort_name(self): | 71 def wort_name(self): |
63 return config.WORT_NAME | 72 return config.WORT_NAME |
64 | 73 |
65 def fridge_name(self): | 74 def fridge_name(self): |