439
|
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 now(self): |
|
41 return utils.monotonic_time() |
|
42 |
|
43 def spawn_timer(seconds, fn, *args, **kwargs): |
|
44 def loop(): |
|
45 while True: |
|
46 fn(*args, **kwargs) |
|
47 gevent.sleep(seconds) |
|
48 return gevent.spawn(loop) |
|
49 |
|
50 def setup(): |
|
51 pass |
|
52 |
|
53 def main(): |
|
54 setup() |
|
55 |
|
56 def p(x): |
|
57 print "hello %s" % x |
|
58 |
|
59 spawn_timer(2, p, 'one') |
|
60 |
|
61 gevent.sleep(20) |
|
62 |
|
63 |
|
64 if __name__ == '__main__': |
|
65 main() |