annotate py/sensor_ds18b20.py @ 252:3e6f82347eab

uwsgi bits
author Matt Johnston <matt@ucc.asn.au>
date Sat, 06 Jun 2015 15:34:03 +0800
parents 185621f47040
children 659953f2ee03
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
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
5 import asyncio
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
6 import concurrent.futures
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
7
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
8 import config
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
9 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
10
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
11 class DS18B20s(object):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
12
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
13 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
14
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
15 def __init__(self, server):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
16 self.server = server
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
17 self.readthread = concurrent.futures.ThreadPoolExecutor(max_workers=1)
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
18 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
19
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
20 @asyncio.coroutine
151
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
21 def do(self):
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
22 vals = {}
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
23 for n in self.sensor_names():
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
24 value = yield from self.do_sensor(n)
151
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
25 if value is not None:
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
26 vals[n] = value
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
27
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
28 itemp = self.do_internal()
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
29 if itemp:
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
30 vals['internal'] = itemp
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
31
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
32 self.server.add_reading(vals)
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
33
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
34 @asyncio.coroutine
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
35 def run(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
36 while True:
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
37 yield from self.do()
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
38 yield from self.server.sleep(config.SENSOR_SLEEP)
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
39
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
40 @asyncio.coroutine
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
41 def read_wait(self, f):
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
42 # handles a blocking file read with a threadpool. A
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
43 # real python thread performs the read while other
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
44 # asyncio tasks keep running.
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
45 # 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
46 # interactively.
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
47 loop = asyncio.get_event_loop()
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
48 yield from loop.run_in_executor(self.readthread, f.read)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
50 @asyncio.coroutine
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
51 def do_sensor(self, s, contents = None):
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
52 """ contents can be set by the caller for testing """
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
53 try:
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
54 if contents is None:
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
55 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
56 f = open(fn, 'r')
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
57 contents = yield from 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
58
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
59 match = self.THERM_RE.match(contents)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
60 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
61 D("no match")
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
62 return None
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
63 temp = int(match.groups(1)[0]) / 1000.0
175
33ed66a7ea0b Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
64 if temp > 80:
33ed66a7ea0b Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
65 E("Problem reading sensor '%s': %f" % (s, temp))
33ed66a7ea0b Ignore temp>80
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
66 return None
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
67 return temp
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
68 except Exception as e:
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
69 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
70 return None
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
151
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
72 def do_internal(self):
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
73 try:
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
74 return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
75 except Exception as e:
151
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
76 EX("Problem reading internal sensor: %s" % str(e))
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
77 return None
e114b38c8a55 internal temperature sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
78
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
79
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
80 def sensor_names(self):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
81 """ Returns a sequence of sensorname """
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
82 slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
178
77c2a9caca3d a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 175
diff changeset
83 contents = open(slaves_path, 'r').read()
77c2a9caca3d a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 175
diff changeset
84 if 'not found' in contents:
77c2a9caca3d a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 175
diff changeset
85 E("No W1 sensors found")
77c2a9caca3d a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 175
diff changeset
86 return []
77c2a9caca3d a few fixes
Matt Johnston <matt@ucc.asn.au>
parents: 175
diff changeset
87 names = contents.split()
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
88 return names
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 def wort_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
91 return config.WORT_NAME
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 def fridge_name(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
94 return config.FRIDGE_NAME