annotate py/tempserver.py @ 444:c5629d79b4ac

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