annotate py/sensor_ds18b20.py @ 148:b32e5a11a4cb

few updates, seems to run
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Dec 2012 23:49:08 +0800
parents 482d7852b511
children d686b111dab4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
3 import gevent
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
4 import gevent.threadpool
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
5 import config
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
6 import re
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
7 from utils import L,W,E,EX
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
8
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
9 class DS18B20s(gevent.Greenlet):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
10
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
11 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
13 def __init__(self, server):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
14 gevent.Greenlet.__init__(self)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
15 self.server = server
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
16 self.readthread = gevent.threadpool.ThreadPool(1)
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
17 self.master_dir = config.SENSOR_BASE_DIR
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
18
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
19 def _run(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
20 while True:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
21 self.do()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22 gevent.sleep(config.SENSOR_SLEEP)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
23
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
24 def read_wait(self, f):
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
25 # handles a blocking file read with a gevent threadpool. A
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
26 # real python thread performs the read while other gevent
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
27 # greenlets keep running.
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
28 # the ds18b20 takes ~750ms to read, which is noticable
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
29 # interactively.
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
30 return self.readthread.apply(lambda: f.read)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
32 def do_sensor(self, s, contents = None):
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
33 """ contents can be set by the caller for testing """
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
34 try:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
35 if contents is None:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
36 fn = os.path.join(self.master_dir, s, 'w1_slave')
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
37 f = open(fn, 'r')
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
38 contents = self.read_wait(f)
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
39 match = self.THERM_RE.match(contents)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
40 if match is None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
41 return None
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
42 temp = int(match.groups(1)[0])
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
43 return temp / 1000.0
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
44 except Exception, e:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
45 EX("Problem reading sensor '%s': %s" % (s, str(e)))
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
46 return None
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
48 def do(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
49 vals = {}
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
50 for n in self.sensor_names():
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
51 value = do_sensor(n)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
52 if value is not None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
53 vals[n] = value
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
54
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
55 self.server.add_reading(vals)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
56
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
57 def sensor_names(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
58 """ Returns a sequence of sensorname """
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
59 slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
60 return open(slaves_path, 'r').split()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 def wort_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
63 return config.WORT_NAME
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 def fridge_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
66 return config.FRIDGE_NAME