Mercurial > templog
annotate py/tempserver.py @ 456:3db3665af2e2
reload config on SIGHUP
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jan 2013 20:12:07 +0800 |
parents | dce9f7841696 |
children | 256505f98c4d |
rev | line source |
---|---|
447 | 1 #!/home/matt/templog/venv/bin/python |
439 | 2 |
3 import sys | |
4 import os | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
5 import logging |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
6 |
439 | 7 import gevent |
444 | 8 import gevent.monkey |
439 | 9 |
10 import utils | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
11 from utils import L,D,EX,W |
444 | 12 import fridge |
13 import config | |
14 import sensor_ds18b20 | |
15 import params | |
16 | |
439 | 17 |
18 class Tempserver(object): | |
19 def __init__(self): | |
20 self.readings = [] | |
444 | 21 self.current = (None, None) |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
22 self.fridge = None |
444 | 23 |
24 # don't patch os, fork() is used by daemonize | |
25 gevent.monkey.patch_all(os=False, thread=False) | |
439 | 26 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
27 def __enter__(self): |
444 | 28 self.params = params.Params() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
29 self.fridge = fridge.Fridge(self) |
444 | 30 self.params.load() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
32 return self |
439 | 33 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
34 def __exit__(self, exc_type, exc_value, traceback): |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
35 L("Exiting, cleanup handler"); |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
36 self.fridge.off() |
439 | 37 |
444 | 38 def run(self): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
39 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
40 if self.fridge is None: |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
41 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'") |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
42 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
43 # XXX do these go here or in __enter_() ? |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
44 self.start_time = self.now() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
45 self.fridge.start() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 self.sensors.start() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
47 |
444 | 48 # won't return. |
49 while True: | |
456 | 50 try: |
51 gevent.sleep(60) | |
52 except KeyboardInterrupt: | |
53 break | |
444 | 54 |
55 def now(self): | |
56 return utils.monotonic_time() | |
57 | |
443 | 58 def set_sensors(self, sensors): |
447 | 59 if hasattr(self, 'sensors'): |
443 | 60 self.sensors.kill() |
61 self.sensors = sensors | |
62 self.wort_name = sensors.wort_name() | |
63 self.fridge_name = sensors.fridge_name() | |
439 | 64 |
65 def take_readings(self): | |
66 ret = self.readings | |
67 self.readings = [] | |
68 return ret | |
69 | |
70 def pushfront(self, readings): | |
71 """ used if a caller of take_readings() fails """ | |
72 self.readings = pushback + self.readings | |
73 | |
443 | 74 # a reading is a map of {sensorname: value}. temperatures |
75 # are float degrees | |
439 | 76 def add_reading(self, reading): |
77 """ adds a reading at the current time """ | |
454 | 78 D("add_reading(%s)" % str(reading)) |
444 | 79 self.readings.append( (reading, self.now())) |
443 | 80 self.current = (reading.get(self.wort_name, None), |
81 reading.get(self.fridge_name, None)) | |
439 | 82 |
83 def current_temps(self): | |
443 | 84 """ returns (wort_temp, fridge_temp) tuple """ |
447 | 85 return self.current |
444 | 86 |
87 def setup_logging(): | |
88 logging.basicConfig(format='%(asctime)s %(message)s', | |
89 datefmt='%m/%d/%Y %I:%M:%S %p', | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
90 level=logging.DEBUG) |
444 | 91 |
92 def main(): | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
93 setup_logging() |
444 | 94 |
95 if '--daemon' in sys.argv: | |
96 utils.cheap_daemon() | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
97 with Tempserver() as server: |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
98 server.run() |
444 | 99 |
100 if __name__ == '__main__': | |
101 main() |