annotate py/tempserver.py @ 146:3b4277aaed3c

update web to handle new style params
author Matt Johnston <matt@ucc.asn.au>
date Wed, 05 Dec 2012 22:44:13 +0800
parents 6517ddee3187
children b32e5a11a4cb
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
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
6 import gevent.monkey
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 import utils
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
9 import fridge
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
10 import config
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
11 import sensor_ds18b20
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
12 import params
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
13
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 class Tempserver(object):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 def __init__(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.readings = []
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
18 self.current = (None, None)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
19
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
20 # don't patch os, fork() is used by daemonize
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
21 gevent.monkey.patch_all(os=False, thread=False)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
23 self.start_time = self.now()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
24
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
25 self.params = params.Params()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
26 self.params.load()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
28 self.fridge = fridge.Fridge(self)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
29 self.fridge.start()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
30
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
31 self.set_sensors(sensor_ds18b20.DS18B20s(self))
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
33 def run(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
34 # won't return.
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
35 while True:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
36 gevent.sleep(60)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
37
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
38 def now(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
39 return utils.monotonic_time()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
40
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
41 def set_sensors(self, sensors):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
42 if self.hasattr(self, 'sensors'):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
43 self.sensors.kill()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
44 self.sensors = sensors
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
45 self.wort_name = sensors.wort_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
46 self.fridge_name = sensors.fridge_name()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 def take_readings(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 ret = self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 return ret
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 def pushfront(self, readings):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 """ used if a caller of take_readings() fails """
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 self.readings = pushback + self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
57 # 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
58 # are float degrees
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 def add_reading(self, reading):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 """ adds a reading at the current time """
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
61 self.readings.append( (reading, self.now()))
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
62 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
63 reading.get(self.fridge_name, None))
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 def current_temps(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
66 """ returns (wort_temp, fridge_temp) tuple """
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 return current
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
68
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
69 def setup_logging():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
70 logging.basicConfig(format='%(asctime)s %(message)s',
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
71 datefmt='%m/%d/%Y %I:%M:%S %p',
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
72 level=logging.INFO)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
73
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
74 def main():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
75 server = Tempserver()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
76
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
77 if '--daemon' in sys.argv:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
78 utils.cheap_daemon()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
79
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
80 server.run()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
81
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
82 if __name__ == '__main__':
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
83 main()