annotate py/tempserver.py @ 144:482d7852b511

a bit more, and some tests
author Matt Johnston <matt@ucc.asn.au>
date Mon, 26 Nov 2012 23:21:03 +0800
parents 0895f5ad7731
children 6517ddee3187
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import os
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import gevent
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import utils
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 class Tempserver(object):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 def __init__(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 self.current = (None, None, None)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 self.start_time = utils.monotonic_time()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
16 self.fridge = fridge.Fridge(self)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
17 self.fridge.start()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
18
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
19 self.set_sensors(sensor_ds18b20.DS18B20s(self))
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
21 def set_sensors(self, sensors):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
22 if self.hasattr(self, 'sensors'):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
23 self.sensors.kill()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
24 self.sensors = sensors
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
25 self.wort_name = sensors.wort_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
26 self.fridge_name = sensors.fridge_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
27 self.sensor_names = sensors.sensor_names()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 def take_readings(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 ret = self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return ret
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def pushfront(self, readings):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 """ used if a caller of take_readings() fails """
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 self.readings = pushback + self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
38 # a reading is a map of {sensorname: value}. temperatures
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
39 # are float degrees
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 def add_reading(self, reading):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 """ adds a reading at the current time """
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 self.readings.append( (reading, utils.monotonic_time()))
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
43 self.current = (reading.get(self.wort_name, None),
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
44 reading.get(self.fridge_name, None))
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 def current_temps(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
47 """ returns (wort_temp, fridge_temp) tuple """
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 return current