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