annotate py/sensor_test.py @ 579:4a6e75ed2ed4

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