148
|
1 #!/home/matt/templog/venv/bin/python |
141
|
2 |
|
3 import sys |
|
4 import os |
|
5 import gevent |
145
|
6 import gevent.monkey |
141
|
7 |
|
8 import utils |
145
|
9 import fridge |
|
10 import config |
|
11 import sensor_ds18b20 |
|
12 import params |
|
13 |
141
|
14 |
|
15 class Tempserver(object): |
|
16 def __init__(self): |
|
17 self.readings = [] |
145
|
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) |
141
|
22 |
145
|
23 self.start_time = self.now() |
|
24 |
|
25 self.params = params.Params() |
|
26 self.params.load() |
141
|
27 |
144
|
28 self.fridge = fridge.Fridge(self) |
|
29 self.fridge.start() |
|
30 |
|
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
141
|
32 |
145
|
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 |
144
|
41 def set_sensors(self, sensors): |
148
|
42 if hasattr(self, 'sensors'): |
144
|
43 self.sensors.kill() |
|
44 self.sensors = sensors |
|
45 self.wort_name = sensors.wort_name() |
|
46 self.fridge_name = sensors.fridge_name() |
141
|
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 |
144
|
57 # a reading is a map of {sensorname: value}. temperatures |
|
58 # are float degrees |
141
|
59 def add_reading(self, reading): |
|
60 """ adds a reading at the current time """ |
145
|
61 self.readings.append( (reading, self.now())) |
144
|
62 self.current = (reading.get(self.wort_name, None), |
|
63 reading.get(self.fridge_name, None)) |
141
|
64 |
|
65 def current_temps(self): |
144
|
66 """ returns (wort_temp, fridge_temp) tuple """ |
148
|
67 return self.current |
145
|
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() |