Mercurial > templog
annotate py/sensor_ds18b20.py @ 452:25bdf95f0e29
config for sensor IDs
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 20 Dec 2012 22:26:18 +0800 |
parents | e99559bf188f |
children | d03157c7ad60 |
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 |
449
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
22 def do(self): |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
23 vals = {} |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
24 for n in self.sensor_names(): |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
25 value = self.do_sensor(n) |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
26 if value is not None: |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
27 vals[n] = value |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
28 |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
29 itemp = self.do_internal() |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
30 if itemp: |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
31 vals['internal'] = itemp |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
32 |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
33 self.server.add_reading(vals) |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
34 |
443 | 35 def _run(self): |
36 while True: | |
37 self.do() | |
38 gevent.sleep(config.SENSOR_SLEEP) | |
39 | |
447 | 40 def read_wait(self, f): |
41 # handles a blocking file read with a gevent threadpool. A | |
42 # real python thread performs the read while other gevent | |
43 # greenlets keep running. | |
44 # the ds18b20 takes ~750ms to read, which is noticable | |
45 # interactively. | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 return self.readthread.apply(f.read) |
439 | 47 |
447 | 48 def do_sensor(self, s, contents = None): |
49 """ 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
|
50 D("dosensor %s" % s) |
443 | 51 try: |
52 if contents is None: | |
447 | 53 fn = os.path.join(self.master_dir, s, 'w1_slave') |
443 | 54 f = open(fn, 'r') |
447 | 55 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
|
56 |
443 | 57 match = self.THERM_RE.match(contents) |
58 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
|
59 D("no match") |
443 | 60 return None |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
61 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
|
62 D("returning %f" % temp) |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
63 return temp |
443 | 64 except Exception, e: |
65 EX("Problem reading sensor '%s': %s" % (s, str(e))) | |
66 return None | |
439 | 67 |
449
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
68 def do_internal(self): |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
69 try: |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
70 return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0 |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
71 except Exception, e: |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
72 EX("Problem reading internal sensor: %s" % str(e)) |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
73 return None |
e99559bf188f
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
448
diff
changeset
|
74 |
443 | 75 |
76 def sensor_names(self): | |
77 """ Returns a sequence of sensorname """ | |
447 | 78 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
|
79 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
|
80 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
|
81 return names |
439 | 82 |
83 def wort_name(self): | |
443 | 84 return config.WORT_NAME |
439 | 85 |
86 def fridge_name(self): | |
443 | 87 return config.FRIDGE_NAME |