annotate py/sensor_ds18b20.py @ 149:d686b111dab4

working better. logging works properly, cleanup fridge.off() happens.
author Matt Johnston <matt@ucc.asn.au>
date Sun, 16 Dec 2012 22:14:46 +0800
parents b32e5a11a4cb
children e114b38c8a55
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
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
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
6 import gevent
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
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
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
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
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
11
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
12 class DS18B20s(gevent.Greenlet):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
13
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
14 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
15
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
16 def __init__(self, server):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
17 gevent.Greenlet.__init__(self)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
18 self.server = server
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
19 self.readthread = gevent.threadpool.ThreadPool(1)
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
20 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
21
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22 def _run(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
23 while True:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
24 self.do()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
25 gevent.sleep(config.SENSOR_SLEEP)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
26
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
27 def read_wait(self, f):
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
28 # 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
29 # 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
30 # greenlets keep running.
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
31 # 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
32 # interactively.
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
33 return self.readthread.apply(f.read)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
35 def do_sensor(self, s, contents = None):
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
36 """ contents can be set by the caller for testing """
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
37 D("dosensor %s" % s)
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
38 try:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
39 if contents is None:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
40 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
41 f = open(fn, 'r')
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
42 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
43
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
44 match = self.THERM_RE.match(contents)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
45 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
46 D("no match")
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
47 return None
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
48 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
49 D("returning %f" % temp)
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
50 return temp
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
51 except Exception, e:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
52 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
53 return None
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
55 def do(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
56 vals = {}
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
57 for n in self.sensor_names():
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
58 value = self.do_sensor(n)
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
59 if value is not None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
60 vals[n] = value
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
61
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
62 self.server.add_reading(vals)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
63
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
64 def sensor_names(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
65 """ Returns a sequence of sensorname """
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
66 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
67 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
68 D("returning names %s" % names)
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
69 return names
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 def wort_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
72 return config.WORT_NAME
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 def fridge_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
75 return config.FRIDGE_NAME