Mercurial > templog
comparison py/tempserver.py @ 141:4755e6f9a5b8
python raspberry pi rewrite
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 19 Nov 2012 22:46:34 +0800 |
parents | |
children | 0895f5ad7731 |
comparison
equal
deleted
inserted
replaced
139:94330d90f11f | 141:4755e6f9a5b8 |
---|---|
1 #!/usr/bin/env python2.7 | |
2 | |
3 import sys | |
4 import os | |
5 import gevent | |
6 | |
7 import utils | |
8 | |
9 class Tempserver(object): | |
10 def __init__(self): | |
11 self.readings = [] | |
12 self.current = (None, None, None) | |
13 | |
14 self.start_time = utils.monotonic_time() | |
15 | |
16 self.fridge = fridge.Fridge() | |
17 self.fridge.run(self) | |
18 | |
19 | |
20 def take_readings(self): | |
21 ret = self.readings | |
22 self.readings = [] | |
23 return ret | |
24 | |
25 def pushfront(self, readings): | |
26 """ used if a caller of take_readings() fails """ | |
27 self.readings = pushback + self.readings | |
28 | |
29 def add_reading(self, reading): | |
30 """ adds a reading at the current time """ | |
31 self.readings.append( (reading, utils.monotonic_time())) | |
32 | |
33 def current_temps(self): | |
34 """ returns (wort_temp, fridge_temp, ambient_temp) tuple """ | |
35 return current | |
36 | |
37 def set_current(self, wort, fridge, ambient): | |
38 current = (wort, fridge, ambient) | |
39 | |
40 def uptime(self): | |
41 return utils.monotonic_time() - self.start_time() | |
42 | |
43 def now(self): | |
44 return utils.monotonic_time() | |
45 | |
46 def spawn_timer(seconds, fn, *args, **kwargs): | |
47 def loop(): | |
48 while True: | |
49 fn(*args, **kwargs) | |
50 gevent.sleep(seconds) | |
51 return gevent.spawn(loop) | |
52 | |
53 def setup(): | |
54 pass | |
55 | |
56 def main(): | |
57 setup() | |
58 | |
59 def p(x): | |
60 print "hello %s" % x | |
61 | |
62 spawn_timer(2, p, 'one') | |
63 | |
64 gevent.sleep(20) | |
65 | |
66 | |
67 if __name__ == '__main__': | |
68 main() |