447
|
1 #!/home/matt/templog/venv/bin/python |
439
|
2 |
|
3 import sys |
|
4 import os |
|
5 import gevent |
444
|
6 import gevent.monkey |
439
|
7 |
|
8 import utils |
444
|
9 import fridge |
|
10 import config |
|
11 import sensor_ds18b20 |
|
12 import params |
|
13 |
439
|
14 |
|
15 class Tempserver(object): |
|
16 def __init__(self): |
|
17 self.readings = [] |
444
|
18 self.current = (None, None) |
|
19 |
|
20 # don't patch os, fork() is used by daemonize |
|
21 gevent.monkey.patch_all(os=False, thread=False) |
439
|
22 |
444
|
23 self.start_time = self.now() |
|
24 |
|
25 self.params = params.Params() |
|
26 self.params.load() |
439
|
27 |
443
|
28 self.fridge = fridge.Fridge(self) |
|
29 self.fridge.start() |
|
30 |
|
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
439
|
32 |
444
|
33 def run(self): |
|
34 # won't return. |
|
35 while True: |
|
36 gevent.sleep(60) |
|
37 |
|
38 def now(self): |
|
39 return utils.monotonic_time() |
|
40 |
443
|
41 def set_sensors(self, sensors): |
447
|
42 if hasattr(self, 'sensors'): |
443
|
43 self.sensors.kill() |
|
44 self.sensors = sensors |
|
45 self.wort_name = sensors.wort_name() |
|
46 self.fridge_name = sensors.fridge_name() |
439
|
47 |
|
48 def take_readings(self): |
|
49 ret = self.readings |
|
50 self.readings = [] |
|
51 return ret |
|
52 |
|
53 def pushfront(self, readings): |
|
54 """ used if a caller of take_readings() fails """ |
|
55 self.readings = pushback + self.readings |
|
56 |
443
|
57 # a reading is a map of {sensorname: value}. temperatures |
|
58 # are float degrees |
439
|
59 def add_reading(self, reading): |
|
60 """ adds a reading at the current time """ |
444
|
61 self.readings.append( (reading, self.now())) |
443
|
62 self.current = (reading.get(self.wort_name, None), |
|
63 reading.get(self.fridge_name, None)) |
439
|
64 |
|
65 def current_temps(self): |
443
|
66 """ returns (wort_temp, fridge_temp) tuple """ |
447
|
67 return self.current |
444
|
68 |
|
69 def setup_logging(): |
|
70 logging.basicConfig(format='%(asctime)s %(message)s', |
|
71 datefmt='%m/%d/%Y %I:%M:%S %p', |
|
72 level=logging.INFO) |
|
73 |
|
74 def main(): |
|
75 server = Tempserver() |
|
76 |
|
77 if '--daemon' in sys.argv: |
|
78 utils.cheap_daemon() |
|
79 |
|
80 server.run() |
|
81 |
|
82 if __name__ == '__main__': |
|
83 main() |