Mercurial > templog
comparison py/tempserver.py @ 149:d686b111dab4
working better. logging works properly, cleanup fridge.off() happens.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 16 Dec 2012 22:14:46 +0800 |
parents | b32e5a11a4cb |
children | d03157c7ad60 |
comparison
equal
deleted
inserted
replaced
148:b32e5a11a4cb | 149:d686b111dab4 |
---|---|
1 #!/home/matt/templog/venv/bin/python | 1 #!/home/matt/templog/venv/bin/python |
2 | 2 |
3 import sys | 3 import sys |
4 import os | 4 import os |
5 import logging | |
6 | |
5 import gevent | 7 import gevent |
6 import gevent.monkey | 8 import gevent.monkey |
7 | 9 |
8 import utils | 10 import utils |
11 from utils import L,D,EX,W | |
9 import fridge | 12 import fridge |
10 import config | 13 import config |
11 import sensor_ds18b20 | 14 import sensor_ds18b20 |
12 import params | 15 import params |
13 | 16 |
14 | 17 |
15 class Tempserver(object): | 18 class Tempserver(object): |
16 def __init__(self): | 19 def __init__(self): |
17 self.readings = [] | 20 self.readings = [] |
18 self.current = (None, None) | 21 self.current = (None, None) |
22 self.fridge = None | |
19 | 23 |
20 # don't patch os, fork() is used by daemonize | 24 # don't patch os, fork() is used by daemonize |
21 gevent.monkey.patch_all(os=False, thread=False) | 25 gevent.monkey.patch_all(os=False, thread=False) |
22 | 26 |
23 self.start_time = self.now() | 27 def __enter__(self): |
28 self.params = params.Params() | |
29 self.fridge = fridge.Fridge(self) | |
30 self.params.load() | |
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) | |
32 return self | |
24 | 33 |
25 self.params = params.Params() | 34 def __exit__(self, exc_type, exc_value, traceback): |
26 self.params.load() | 35 L("Exiting, cleanup handler"); |
27 | 36 self.fridge.off() |
28 self.fridge = fridge.Fridge(self) | |
29 self.fridge.start() | |
30 | |
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) | |
32 | 37 |
33 def run(self): | 38 def run(self): |
39 | |
40 if self.fridge is None: | |
41 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'") | |
42 | |
43 # XXX do these go here or in __enter_() ? | |
44 self.start_time = self.now() | |
45 self.fridge.start() | |
46 self.sensors.start() | |
47 | |
34 # won't return. | 48 # won't return. |
35 while True: | 49 while True: |
36 gevent.sleep(60) | 50 gevent.sleep(60) |
37 | 51 |
38 def now(self): | 52 def now(self): |
67 return self.current | 81 return self.current |
68 | 82 |
69 def setup_logging(): | 83 def setup_logging(): |
70 logging.basicConfig(format='%(asctime)s %(message)s', | 84 logging.basicConfig(format='%(asctime)s %(message)s', |
71 datefmt='%m/%d/%Y %I:%M:%S %p', | 85 datefmt='%m/%d/%Y %I:%M:%S %p', |
72 level=logging.INFO) | 86 level=logging.DEBUG) |
73 | 87 |
74 def main(): | 88 def main(): |
75 server = Tempserver() | 89 setup_logging() |
76 | 90 |
77 if '--daemon' in sys.argv: | 91 if '--daemon' in sys.argv: |
78 utils.cheap_daemon() | 92 utils.cheap_daemon() |
79 | 93 with Tempserver() as server: |
80 server.run() | 94 server.run() |
81 | 95 |
82 if __name__ == '__main__': | 96 if __name__ == '__main__': |
83 main() | 97 main() |