comparison py/tempserver.py @ 219:16a83e2c97a0

sleep on a semaphore so it can start/stop immediately when there's a SIGHUP from the web UI. makes it seem more magical.
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Dec 2014 21:52:50 +0800
parents 5eb7e2400c18
children d9e81a563923
comparison
equal deleted inserted replaced
218:11631a7f4cf2 219:16a83e2c97a0
23 class Tempserver(object): 23 class Tempserver(object):
24 def __init__(self): 24 def __init__(self):
25 self.readings = [] 25 self.readings = []
26 self.current = (None, None) 26 self.current = (None, None)
27 self.fridge = None 27 self.fridge = None
28 self._wakeup = gevent.event.Event()
28 29
29 # don't patch os, fork() is used by daemonize 30 # don't patch os, fork() is used by daemonize
30 gevent.monkey.patch_all(os=False, thread=False) 31 gevent.monkey.patch_all(os=False, thread=False)
31 32
32 def __enter__(self): 33 def __enter__(self):
33 self.params = params.Params() 34 self.params = params.Params()
34 self.fridge = fridge.Fridge(self) 35 self.fridge = fridge.Fridge(self)
35 self.uploader = uploader.Uploader(self) 36 self.uploader = uploader.Uploader(self)
36 self.params.load() 37 self.params.load()
37 self.set_sensors(sensor_ds18b20.DS18B20s(self)) 38 self.set_sensors(sensor_ds18b20.DS18B20s(self))
39 gevent.signal(signal.SIGHUP, self._reload_signal)
38 return self 40 return self
39 41
40 def __exit__(self, exc_type, exc_value, traceback): 42 def __exit__(self, exc_type, exc_value, traceback):
41 L("Exiting, cleanup handler"); 43 L("Exiting, cleanup handler");
42 self.fridge.off() 44 self.fridge.off()
90 self.readings = self.readings[-config.MAX_READINGS:] 92 self.readings = self.readings[-config.MAX_READINGS:]
91 93
92 def current_temps(self): 94 def current_temps(self):
93 """ returns (wort_temp, fridge_temp) tuple """ 95 """ returns (wort_temp, fridge_temp) tuple """
94 return self.current 96 return self.current
97
98 def sleep(self, timeout):
99 """ sleeps for timeout seconds, though wakes if the server's config is updated """
100 self._wakeup.wait(timeout)
101
102 def _reload_signal(self):
103 try:
104 self.params.load()
105 L("Reloaded.")
106 self._wakeup.set()
107 self._wakeup.clear()
108 except self.Error, e:
109 W("Problem reloading: %s" % str(e))
95 110
96 def setup_logging(): 111 def setup_logging():
97 logging.basicConfig(format='%(asctime)s %(message)s', 112 logging.basicConfig(format='%(asctime)s %(message)s',
98 datefmt='%m/%d/%Y %I:%M:%S %p', 113 datefmt='%m/%d/%Y %I:%M:%S %p',
99 level=logging.INFO) 114 level=logging.INFO)