annotate py/sensor_ds18b20.py @ 144:482d7852b511

a bit more, and some tests
author Matt Johnston <matt@ucc.asn.au>
date Mon, 26 Nov 2012 23:21:03 +0800
parents 4755e6f9a5b8
children b32e5a11a4cb
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
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
4 import config
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
5 import re
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
6 from utils import L,W,E,EX
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
7
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
8 class DS18B20s(gevent.Greenlet):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
9
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
10 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
11
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
12 def __init__(self, server):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
13 gevent.Greenlet.__init__(self)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
14 self.server = server
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
15 # XXX set up paths
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
16 # XXX set up drain etc
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
17
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
18 def _run(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
19 while True:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
20 self.do()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
21 gevent.sleep(config.SENSOR_SLEEP)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
23 def sensor_path(self, s):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
24 return os.path.join(self.master_dir, s)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
26 def do_sensor_name(self, s, contents = None):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
27 try:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
28 if contents is None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
29 fn = os.path.join(self.sensor_path(s), 'w1_slave')
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
30 f = open(fn, 'r')
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
31 contents = f.read()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
32 match = self.THERM_RE.match(contents)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
33 if match is None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
34 return None
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
35 temp = int(match.groups(1)[0])
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
36 return temp / 1000.0
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
37 except Exception, e:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
38 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
39 return None
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
41 def do(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
42 vals = {}
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
43 for n in self.sensor_names():
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
44 value = do_sensor(n)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
45 if value is not None:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
46 vals[n] = value
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
47
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
48 self.server.add_reading(vals)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
49
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
50 def sensor_names(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
51 """ Returns a sequence of sensorname """
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
52 return [d for d in os.listdir(self.master_dir) if
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
53 os.stat(sensor_path(d)).st_mode & stat.S_ISDIR]
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 def wort_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
56 return config.WORT_NAME
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 def fridge_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
59 return config.FRIDGE_NAME