annotate py/sensor_test.py @ 278:fd9c6517225e

merge
author Matt Johnston <matt@ucc.asn.au>
date Tue, 17 Nov 2015 22:26:54 +0800
parents a01b7bccccd3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import asyncio
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 class SensorTest(object):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 def __init__(self, server):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 self.server = server
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 def kill(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 L("Killed SensorTest")
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 def make_vals(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 def try_read(f, fallback):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 try:
232
a01b7bccccd3 improve exception handling
Matt Johnston <matt@ucc.asn.au>
parents: 230
diff changeset
14 return float(open(f, 'r').read())
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 229
diff changeset
15 except Exception as e:
229
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 return fallback
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 vals = {}
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 vals[self.wort_name()] = try_read('test_wort.txt', 18)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 vals[self.fridge_name()] = try_read('test_fridge.txt', 20)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 vals['ambient'] = 31.2
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 return vals
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 def run(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 while True:
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 yield from asyncio.sleep(1)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 vals = self.make_vals()
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 self.server.add_reading(vals)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 def wort_name(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 return '28-wortname'
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 def fridge_name(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 return '28-fridgename'