annotate py/sensor_test.py @ 530:73257bae83ea

fix a few python3 issues
author Matt Johnston <matt@ucc.asn.au>
date Sat, 11 Apr 2015 00:16:05 +0800
parents 5201f441bf4a
children a01b7bccccd3
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:
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 return 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
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 while True:
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 yield from asyncio.sleep(1)
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 vals = self.make_vals()
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 self.server.add_reading(vals)
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def wort_name(self):
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return '28-wortname'
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def fridge_name(self):
61e61de49a50 test sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 return '28-fridgename'