annotate py/sensor_ds18b20.py @ 476:06de0f2ffa26

workaround for invalid wort time
author Matt Johnston <matt@ucc.asn.au>
date Thu, 25 Jul 2013 08:05:40 +0800
parents 435631e6d612
children 77c2a9caca3d
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
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
3 import os
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
4 import re
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
5
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
6 import gevent
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
7 import gevent.threadpool
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
8
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
9 import config
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
10 from utils import D,L,W,E,EX
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
11
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
12 class DS18B20s(gevent.Greenlet):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
13
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
14 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
15
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
16 def __init__(self, server):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
17 gevent.Greenlet.__init__(self)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
18 self.server = server
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
19 self.readthread = gevent.threadpool.ThreadPool(1)
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
20 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
21
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
22 def do(self):
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
23 vals = {}
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
24 for n in self.sensor_names():
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
25 value = self.do_sensor(n)
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
26 if value is not None:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
27 vals[n] = value
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
28
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
29 itemp = self.do_internal()
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
30 if itemp:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
31 vals['internal'] = itemp
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
32
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
33 self.server.add_reading(vals)
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
34
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
35 def _run(self):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
36 while True:
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
37 self.do()
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
38 gevent.sleep(config.SENSOR_SLEEP)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
39
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
40 def read_wait(self, f):
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
41 # 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
42 # 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
43 # greenlets keep running.
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
44 # 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
45 # interactively.
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
46 return self.readthread.apply(f.read)
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
48 def do_sensor(self, s, contents = None):
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
49 """ 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
50 try:
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
51 if contents is None:
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
52 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
53 f = open(fn, 'r')
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
54 contents = self.read_wait(f)
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
55
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
56 match = self.THERM_RE.match(contents)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
57 if match is None:
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
58 D("no match")
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
59 return None
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
60 temp = int(match.groups(1)[0]) / 1000.0
474
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
61 if temp > 80:
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
62 E("Problem reading sensor '%s': %f" % (s, temp))
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
63 return None
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
64 return temp
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
65 except Exception, e:
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
66 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
67 return None
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
69 def do_internal(self):
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
70 try:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
71 return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
72 except Exception, e:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
73 EX("Problem reading internal sensor: %s" % str(e))
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
74 return None
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
75
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
76
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
77 def sensor_names(self):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
78 """ Returns a sequence of sensorname """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
79 slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
80 names = open(slaves_path, 'r').read().split()
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
81 return names
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 def wort_name(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
84 return config.WORT_NAME
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 def fridge_name(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
87 return config.FRIDGE_NAME