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