Mercurial > templog
annotate py/sensor_ds18b20.py @ 579:4a6e75ed2ed4
fix gpio chown script
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 30 Nov 2015 21:40:45 +0800 |
parents | 26f20cee71be |
children | 97d99eb42d27 |
rev | line source |
---|---|
439 | 1 #!/usr/bin/env python2.7 |
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 | 5 import asyncio |
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 | 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 | 10 |
535 | 11 class SensorDS18B20(object): |
443 | 12 |
13 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) | |
439 | 14 |
443 | 15 def __init__(self, server): |
16 self.server = server | |
527 | 17 self.readthread = concurrent.futures.ThreadPoolExecutor(max_workers=1) |
447 | 18 self.master_dir = config.SENSOR_BASE_DIR |
443 | 19 |
527 | 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 | 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 | 34 @asyncio.coroutine |
35 def run(self): | |
443 | 36 while True: |
527 | 37 yield from self.do() |
556 | 38 yield from asyncio.sleep(config.SENSOR_SLEEP) |
443 | 39 |
535 | 40 |
527 | 41 @asyncio.coroutine |
447 | 42 def read_wait(self, f): |
527 | 43 # handles a blocking file read with a threadpool. A |
44 # real python thread performs the read while other | |
45 # asyncio tasks keep running. | |
447 | 46 # the ds18b20 takes ~750ms to read, which is noticable |
47 # interactively. | |
527 | 48 loop = asyncio.get_event_loop() |
535 | 49 return loop.run_in_executor(None, f.read) |
439 | 50 |
527 | 51 @asyncio.coroutine |
447 | 52 def do_sensor(self, s, contents = None): |
53 """ contents can be set by the caller for testing """ | |
443 | 54 try: |
55 if contents is None: | |
447 | 56 fn = os.path.join(self.master_dir, s, 'w1_slave') |
535 | 57 with open(fn, 'r') as f: |
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 | 60 match = self.THERM_RE.match(contents) |
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 | 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 | 65 if temp > 80: |
66 E("Problem reading sensor '%s': %f" % (s, temp)) | |
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 | 69 except Exception as e: |
443 | 70 EX("Problem reading sensor '%s': %s" % (s, str(e))) |
71 return None | |
439 | 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 | 75 with open(config.INTERNAL_TEMPERATURE, 'r') as f: |
76 return int(f.read()) / 1000.0 | |
529 | 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 | 81 |
82 def sensor_names(self): | |
83 """ Returns a sequence of sensorname """ | |
447 | 84 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") |
535 | 85 with open(slaves_path, 'r') as f: |
86 contents = f.read() | |
477 | 87 if 'not found' in contents: |
88 E("No W1 sensors found") | |
89 return [] | |
90 names = contents.split() | |
565
26f20cee71be
emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents:
556
diff
changeset
|
91 # only ds18b20 |
26f20cee71be
emergency change sensor config
Matt Johnston <matt@ucc.asn.au>
parents:
556
diff
changeset
|
92 names = [n for n in names if n.startswith('28-')] |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
93 return names |
439 | 94 |
95 def wort_name(self): | |
443 | 96 return config.WORT_NAME |
439 | 97 |
98 def fridge_name(self): | |
443 | 99 return config.FRIDGE_NAME |