annotate py/sensor_ds18b20.py @ 544:b0b0c7e7133c

merge from server
author Matt Johnston <matt@ucc.asn.au>
date Wed, 20 May 2015 00:03:53 +0800
parents 3a514fbb88eb
children 26eee8591f61
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
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
5 import asyncio
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
6 import concurrent.futures
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
7
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
8 import config
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
9 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
10
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
11 class SensorDS18B20(object):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
12
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
13 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
14
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
15 def __init__(self, server):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
16 self.server = server
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
17 self.readthread = concurrent.futures.ThreadPoolExecutor(max_workers=1)
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
18 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
19
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
20 @asyncio.coroutine
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
21 def do(self):
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
22 vals = {}
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
23 for n in self.sensor_names():
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
24 value = yield from self.do_sensor(n)
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
25 if value is not None:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
26 vals[n] = value
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
27
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
28 itemp = self.do_internal()
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
29 if itemp:
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
30 vals['internal'] = itemp
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
31
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
32 self.server.add_reading(vals)
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
33
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
34 @asyncio.coroutine
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
35 def run(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
36 while True:
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
37 yield from self.do()
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
38 yield from self.server.sleep(config.SENSOR_SLEEP)
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
39
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
40
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
41 @asyncio.coroutine
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
42 def read_wait(self, f):
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
43 # handles a blocking file read with a threadpool. A
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
44 # real python thread performs the read while other
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
45 # asyncio tasks keep running.
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
46 # 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
47 # interactively.
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
48 loop = asyncio.get_event_loop()
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
49 return loop.run_in_executor(None, f.read)
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
51 @asyncio.coroutine
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
52 def do_sensor(self, s, contents = None):
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
53 """ 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
54 try:
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
55 if contents is None:
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
56 fn = os.path.join(self.master_dir, s, 'w1_slave')
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
57 with open(fn, 'r') as f:
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
58 contents = yield from 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
59
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
60 match = self.THERM_RE.match(contents)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
61 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
62 D("no match")
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
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 temp = int(match.groups(1)[0]) / 1000.0
474
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
65 if temp > 80:
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
66 E("Problem reading sensor '%s': %f" % (s, temp))
435631e6d612 Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
67 return None
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
68 return temp
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
69 except Exception as e:
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
70 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
71 return None
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
73 def do_internal(self):
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
74 try:
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
75 with open(config.INTERNAL_TEMPERATURE, 'r') as f:
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
76 return int(f.read()) / 1000.0
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
77 except Exception as e:
449
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
78 EX("Problem reading internal sensor: %s" % str(e))
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
79 return None
e99559bf188f internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
80
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
81
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
82 def sensor_names(self):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
83 """ Returns a sequence of sensorname """
447
8fdf86ea41e7 few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
84 slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
535
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
85 with open(slaves_path, 'r') as f:
3a514fbb88eb ds18b20 works
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
86 contents = f.read()
477
d05b1612e19e a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 474
diff changeset
87 if 'not found' in contents:
d05b1612e19e a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 474
diff changeset
88 E("No W1 sensors found")
d05b1612e19e a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 474
diff changeset
89 return []
d05b1612e19e a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 474
diff changeset
90 names = contents.split()
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 447
diff changeset
91 return names
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 def wort_name(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
94 return config.WORT_NAME
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 def fridge_name(self):
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
97 return config.FRIDGE_NAME