comparison py/tempserver.py @ 253:0a1b642e3086

long polling config updates
author Matt Johnston <matt@ucc.asn.au>
date Mon, 08 Jun 2015 22:29:46 +0800
parents fd29ae905d1b
children 26eee8591f61
comparison
equal deleted inserted replaced
251:b6079cb0c665 253:0a1b642e3086
30 30
31 def __enter__(self): 31 def __enter__(self):
32 self.params = params.Params() 32 self.params = params.Params()
33 self.fridge = fridge.Fridge(self) 33 self.fridge = fridge.Fridge(self)
34 self.uploader = uploader.Uploader(self) 34 self.uploader = uploader.Uploader(self)
35 self.configwaiter = configwaiter.ConfigWaiter(self)
35 self.params.load() 36 self.params.load()
36 self.set_sensors(sensor.make_sensor(self)) 37 self.set_sensors(sensor.make_sensor(self))
37 asyncio.get_event_loop().add_signal_handler(signal.SIGHUP, self._reload_signal) 38 asyncio.get_event_loop().add_signal_handler(signal.SIGHUP, self.reload_signal)
38 return self 39 return self
39 40
40 def __exit__(self, exc_type, exc_value, traceback): 41 def __exit__(self, exc_type, exc_value, traceback):
41 L("Exiting, cleanup handler"); 42 L("Exiting, cleanup handler");
42 self.fridge.off() 43 self.fridge.off()
50 self.start_time = self.now() 51 self.start_time = self.now()
51 runloops = [ 52 runloops = [
52 self.fridge.run(), 53 self.fridge.run(),
53 self.sensors.run(), 54 self.sensors.run(),
54 self.uploader.run(), 55 self.uploader.run(),
56 self.configwaiter.run(),
55 ] 57 ]
56 58
57 loop = asyncio.get_event_loop() 59 loop = asyncio.get_event_loop()
58 try: 60 try:
59 loop.run_until_complete(asyncio.gather(*runloops)) 61 loop.run_until_complete(asyncio.gather(*runloops))
107 try: 109 try:
108 yield from asyncio.wait_for(self._wakeup.wait(), timeout=timeout) 110 yield from asyncio.wait_for(self._wakeup.wait(), timeout=timeout)
109 except asyncio.TimeoutError: 111 except asyncio.TimeoutError:
110 pass 112 pass
111 113
112 def _reload_signal(self): 114 def reload_signal(self, no_file = False):
113 try: 115 try:
114 self.params.load() 116 if not no_file:
115 L("Reloaded.") 117 self.params.load()
118 L("Reloaded.")
116 self._wakeup.set() 119 self._wakeup.set()
117 self._wakeup.clear() 120 self._wakeup.clear()
118 except Error as e: 121 except Error as e:
119 W("Problem reloading: %s" % str(e)) 122 W("Problem reloading: %s" % str(e))
120 123