comparison py/sensor_ds18b20.py @ 239:659953f2ee03

ds18b20 works
author Matt Johnston <matt@ucc.asn.au>
date Tue, 19 May 2015 15:07:57 +0000
parents 185621f47040
children 26eee8591f61
comparison
equal deleted inserted replaced
235:4cbcbba567ab 239:659953f2ee03
6 import concurrent.futures 6 import concurrent.futures
7 7
8 import config 8 import config
9 from utils import D,L,W,E,EX 9 from utils import D,L,W,E,EX
10 10
11 class DS18B20s(object): 11 class SensorDS18B20(object):
12 12
13 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE) 13 THERM_RE = re.compile('.* YES\n.*t=(.*)\n', re.MULTILINE)
14 14
15 def __init__(self, server): 15 def __init__(self, server):
16 self.server = server 16 self.server = server
35 def run(self): 35 def run(self):
36 while True: 36 while True:
37 yield from self.do() 37 yield from self.do()
38 yield from self.server.sleep(config.SENSOR_SLEEP) 38 yield from self.server.sleep(config.SENSOR_SLEEP)
39 39
40
40 @asyncio.coroutine 41 @asyncio.coroutine
41 def read_wait(self, f): 42 def read_wait(self, f):
42 # handles a blocking file read with a threadpool. A 43 # handles a blocking file read with a threadpool. A
43 # real python thread performs the read while other 44 # real python thread performs the read while other
44 # asyncio tasks keep running. 45 # asyncio tasks keep running.
45 # the ds18b20 takes ~750ms to read, which is noticable 46 # the ds18b20 takes ~750ms to read, which is noticable
46 # interactively. 47 # interactively.
47 loop = asyncio.get_event_loop() 48 loop = asyncio.get_event_loop()
48 yield from loop.run_in_executor(self.readthread, f.read) 49 return loop.run_in_executor(None, f.read)
49 50
50 @asyncio.coroutine 51 @asyncio.coroutine
51 def do_sensor(self, s, contents = None): 52 def do_sensor(self, s, contents = None):
52 """ contents can be set by the caller for testing """ 53 """ contents can be set by the caller for testing """
53 try: 54 try:
54 if contents is None: 55 if contents is None:
55 fn = os.path.join(self.master_dir, s, 'w1_slave') 56 fn = os.path.join(self.master_dir, s, 'w1_slave')
56 f = open(fn, 'r') 57 with open(fn, 'r') as f:
57 contents = yield from self.read_wait(f) 58 contents = yield from self.read_wait(f)
58 59
59 match = self.THERM_RE.match(contents) 60 match = self.THERM_RE.match(contents)
60 if match is None: 61 if match is None:
61 D("no match") 62 D("no match")
62 return None 63 return None
69 EX("Problem reading sensor '%s': %s" % (s, str(e))) 70 EX("Problem reading sensor '%s': %s" % (s, str(e)))
70 return None 71 return None
71 72
72 def do_internal(self): 73 def do_internal(self):
73 try: 74 try:
74 return int(open(config.INTERNAL_TEMPERATURE, 'r').read()) / 1000.0 75 with open(config.INTERNAL_TEMPERATURE, 'r') as f:
76 return int(f.read()) / 1000.0
75 except Exception as e: 77 except Exception as e:
76 EX("Problem reading internal sensor: %s" % str(e)) 78 EX("Problem reading internal sensor: %s" % str(e))
77 return None 79 return None
78 80
79 81
80 def sensor_names(self): 82 def sensor_names(self):
81 """ Returns a sequence of sensorname """ 83 """ Returns a sequence of sensorname """
82 slaves_path = os.path.join(self.master_dir, "w1_master_slaves") 84 slaves_path = os.path.join(self.master_dir, "w1_master_slaves")
83 contents = open(slaves_path, 'r').read() 85 with open(slaves_path, 'r') as f:
86 contents = f.read()
84 if 'not found' in contents: 87 if 'not found' in contents:
85 E("No W1 sensors found") 88 E("No W1 sensors found")
86 return [] 89 return []
87 names = contents.split() 90 names = contents.split()
88 return names 91 return names