annotate py/sensor_test.py @ 229:99255c501e02

test sensor
author Matt Johnston <matt@ucc.asn.au>
date Fri, 10 Apr 2015 23:55:07 +0800
parents
children 185621f47040
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:
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 return open(f, 'r').read()
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 except Exception, e:
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
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 while True:
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 yield from asyncio.sleep(1)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 vals = self.make_vals()
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 self.server.add_reading(vals)
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def wort_name(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return '28-wortname'
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def fridge_name(self):
99255c501e02 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 return '28-fridgename'