annotate py/tempserver.py @ 443:bca470d153fd

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