annotate py/tempserver.py @ 442:02318c9660cd

copied fridge logic from main.c
author Matt Johnston <matt@ucc.asn.au>
date Tue, 20 Nov 2012 22:03:10 +0800
parents 31ac84425a2d
children 482d7852b511
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
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 self.fridge = fridge.Fridge()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.fridge.run(self)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def take_readings(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 ret = self.readings
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.readings = []
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 return ret
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def pushfront(self, readings):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 """ used if a caller of take_readings() fails """
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 self.readings = pushback + self.readings
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 add_reading(self, reading):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 """ adds a reading at the current time """
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 self.readings.append( (reading, utils.monotonic_time()))
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 def current_temps(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 """ returns (wort_temp, fridge_temp, ambient_temp) tuple """
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 return current
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def set_current(self, wort, fridge, ambient):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 current = (wort, fridge, ambient)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 def now(self):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 return utils.monotonic_time()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def spawn_timer(seconds, fn, *args, **kwargs):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 def loop():
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 while True:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 fn(*args, **kwargs)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 gevent.sleep(seconds)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 return gevent.spawn(loop)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 def setup():
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 pass
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 main():
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 setup()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 def p(x):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 print "hello %s" % x
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 spawn_timer(2, p, 'one')
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 gevent.sleep(20)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if __name__ == '__main__':
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 main()