Mercurial > templog
comparison py/sensor_ds18b20.py @ 447:8fdf86ea41e7
few updates, seems to run
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 15 Dec 2012 23:49:08 +0800 |
parents | bca470d153fd |
children | d686b111dab4 |
comparison
equal
deleted
inserted
replaced
446:d8ff29036740 | 447:8fdf86ea41e7 |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python2.7 |
2 | 2 |
3 import gevent | 3 import gevent |
4 import gevent.threadpool | |
4 import config | 5 import config |
5 import re | 6 import re |
6 from utils import L,W,E,EX | 7 from utils import L,W,E,EX |
7 | 8 |
8 class DS18B20s(gevent.Greenlet): | 9 class DS18B20s(gevent.Greenlet): |
10 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) | 11 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) |
11 | 12 |
12 def __init__(self, server): | 13 def __init__(self, server): |
13 gevent.Greenlet.__init__(self) | 14 gevent.Greenlet.__init__(self) |
14 self.server = server | 15 self.server = server |
15 # XXX set up paths | 16 self.readthread = gevent.threadpool.ThreadPool(1) |
16 # XXX set up drain etc | 17 self.master_dir = config.SENSOR_BASE_DIR |
17 | 18 |
18 def _run(self): | 19 def _run(self): |
19 while True: | 20 while True: |
20 self.do() | 21 self.do() |
21 gevent.sleep(config.SENSOR_SLEEP) | 22 gevent.sleep(config.SENSOR_SLEEP) |
22 | 23 |
23 def sensor_path(self, s): | 24 def read_wait(self, f): |
24 return os.path.join(self.master_dir, s) | 25 # handles a blocking file read with a gevent threadpool. A |
26 # real python thread performs the read while other gevent | |
27 # greenlets keep running. | |
28 # the ds18b20 takes ~750ms to read, which is noticable | |
29 # interactively. | |
30 return self.readthread.apply(lambda: f.read) | |
25 | 31 |
26 def do_sensor_name(self, s, contents = None): | 32 def do_sensor(self, s, contents = None): |
33 """ contents can be set by the caller for testing """ | |
27 try: | 34 try: |
28 if contents is None: | 35 if contents is None: |
29 fn = os.path.join(self.sensor_path(s), 'w1_slave') | 36 fn = os.path.join(self.master_dir, s, 'w1_slave') |
30 f = open(fn, 'r') | 37 f = open(fn, 'r') |
31 contents = f.read() | 38 contents = self.read_wait(f) |
32 match = self.THERM_RE.match(contents) | 39 match = self.THERM_RE.match(contents) |
33 if match is None: | 40 if match is None: |
34 return None | 41 return None |
35 temp = int(match.groups(1)[0]) | 42 temp = int(match.groups(1)[0]) |
36 return temp / 1000.0 | 43 return temp / 1000.0 |
47 | 54 |
48 self.server.add_reading(vals) | 55 self.server.add_reading(vals) |
49 | 56 |
50 def sensor_names(self): | 57 def sensor_names(self): |
51 """ Returns a sequence of sensorname """ | 58 """ Returns a sequence of sensorname """ |
52 return [d for d in os.listdir(self.master_dir) if | 59 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") |
53 os.stat(sensor_path(d)).st_mode & stat.S_ISDIR] | 60 return open(slaves_path, 'r').split() |
54 | 61 |
55 def wort_name(self): | 62 def wort_name(self): |
56 return config.WORT_NAME | 63 return config.WORT_NAME |
57 | 64 |
58 def fridge_name(self): | 65 def fridge_name(self): |