Mercurial > templog
annotate py/sensor_ds18b20.py @ 179:30390852cb5d
sort sensor lines
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 30 Jan 2014 22:56:05 +0800 |
parents | 256505f98c4d |
children | 33ed66a7ea0b |
rev | line source |
---|---|
141 | 1 #!/usr/bin/env python2.7 |
2 | |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
3 import os |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
4 import re |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
5 |
144 | 6 import gevent |
148 | 7 import gevent.threadpool |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
8 |
144 | 9 import config |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
10 from utils import D,L,W,E,EX |
144 | 11 |
12 class DS18B20s(gevent.Greenlet): | |
13 | |
14 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) | |
141 | 15 |
144 | 16 def __init__(self, server): |
17 gevent.Greenlet.__init__(self) | |
18 self.server = server | |
148 | 19 self.readthread = gevent.threadpool.ThreadPool(1) |
20 self.master_dir = config.SENSOR_BASE_DIR | |
144 | 21 |
151
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
22 def do(self): |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
23 vals = {} |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
24 for n in self.sensor_names(): |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
25 value = self.do_sensor(n) |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
26 if value is not None: |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
27 vals[n] = value |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
28 |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
29 itemp = self.do_internal() |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
30 if itemp: |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
31 vals['internal'] = itemp |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
32 |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
33 self.server.add_reading(vals) |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
34 |
144 | 35 def _run(self): |
36 while True: | |
37 self.do() | |
38 gevent.sleep(config.SENSOR_SLEEP) | |
39 | |
148 | 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. | |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
46 return self.readthread.apply(f.read) |
141 | 47 |
148 | 48 def do_sensor(self, s, contents = None): |
49 """ contents can be set by the caller for testing """ | |
144 | 50 try: |
51 if contents is None: | |
148 | 52 fn = os.path.join(self.master_dir, s, 'w1_slave') |
144 | 53 f = open(fn, 'r') |
148 | 54 contents = self.read_wait(f) |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
55 |
144 | 56 match = self.THERM_RE.match(contents) |
57 if match is None: | |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
58 D("no match") |
144 | 59 return None |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
60 temp = int(match.groups(1)[0]) / 1000.0 |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
61 return temp |
144 | 62 except Exception, e: |
63 EX("Problem reading sensor '%s': %s" % (s, str(e))) | |
64 return None | |
141 | 65 |
151
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
66 def do_internal(self): |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
67 try: |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
68 return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0 |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
69 except Exception, e: |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
70 EX("Problem reading internal sensor: %s" % str(e)) |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
71 return None |
e114b38c8a55
internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents:
149
diff
changeset
|
72 |
144 | 73 |
74 def sensor_names(self): | |
75 """ Returns a sequence of sensorname """ | |
148 | 76 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") |
149
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
77 names = open(slaves_path, 'r').read().split() |
d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
148
diff
changeset
|
78 return names |
141 | 79 |
80 def wort_name(self): | |
144 | 81 return config.WORT_NAME |
141 | 82 |
83 def fridge_name(self): | |
144 | 84 return config.FRIDGE_NAME |