comparison py/sensor_test.py @ 531:fd0fd9f947a1

improve exception handling
author Matt Johnston <matt@ucc.asn.au>
date Sat, 11 Apr 2015 21:09:13 +0800
parents 5201f441bf4a
children
comparison
equal deleted inserted replaced
530:73257bae83ea 531:fd0fd9f947a1
9 L("Killed SensorTest") 9 L("Killed SensorTest")
10 10
11 def make_vals(self): 11 def make_vals(self):
12 def try_read(f, fallback): 12 def try_read(f, fallback):
13 try: 13 try:
14 return open(f, 'r').read() 14 return float(open(f, 'r').read())
15 except Exception as e: 15 except Exception as e:
16 return fallback 16 return fallback
17 17
18 vals = {} 18 vals = {}
19 vals[self.wort_name()] = try_read('test_wort.txt', 18) 19 vals[self.wort_name()] = try_read('test_wort.txt', 18)
20 vals[self.fridge_name()] = try_read('test_fridge.txt', 20) 20 vals[self.fridge_name()] = try_read('test_fridge.txt', 20)
21 vals['ambient'] = 31.2 21 vals['ambient'] = 31.2
22 return vals 22 return vals
23 23
24 def run(self): 24 def run(self):
25
26 while True: 25 while True:
27 yield from asyncio.sleep(1) 26 yield from asyncio.sleep(1)
28 vals = self.make_vals() 27 vals = self.make_vals()
29 self.server.add_reading(vals) 28 self.server.add_reading(vals)
30 29