Mercurial > templog
comparison py/tempserver.py @ 299:358c50004679
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 27 Feb 2017 23:20:07 +0800 |
parents | 08c3cf3d0ded |
children | 02aff9ff8d24 |
comparison
equal
deleted
inserted
replaced
286:61269311ed3d | 299:358c50004679 |
---|---|
20 import uploader | 20 import uploader |
21 import configwaiter | 21 import configwaiter |
22 | 22 |
23 | 23 |
24 class Tempserver(object): | 24 class Tempserver(object): |
25 def __init__(self, test_mode): | 25 def __init__(self, test_mode, nowait): |
26 self.readings = [] | 26 self.readings = [] |
27 self.current = (None, None) | 27 self.current = (None, None) |
28 self.fridge = None | 28 self.fridge = None |
29 self._wakeup = asyncio.Event() | 29 self._wakeup = asyncio.Event() |
30 self._test_mode = test_mode | 30 self._test_mode = test_mode |
31 self._nowait = nowait | |
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, self._nowait) |
35 self.uploader = uploader.Uploader(self) | 36 self.uploader = uploader.Uploader(self) |
36 self.configwaiter = configwaiter.ConfigWaiter(self) | 37 self.configwaiter = configwaiter.ConfigWaiter(self) |
37 self.params.load() | 38 self.params.load() |
38 self.set_sensors(sensor.make_sensor(self)) | 39 self.set_sensors(sensor.make_sensor(self)) |
39 asyncio.get_event_loop().add_signal_handler(signal.SIGHUP, self.reload_signal) | 40 asyncio.get_event_loop().add_signal_handler(signal.SIGHUP, self.reload_signal) |
128 def setup_logging(debug = False): | 129 def setup_logging(debug = False): |
129 level = logging.INFO | 130 level = logging.INFO |
130 if debug: | 131 if debug: |
131 level = logging.DEBUG | 132 level = logging.DEBUG |
132 logging.basicConfig(format='%(asctime)s %(message)s', | 133 logging.basicConfig(format='%(asctime)s %(message)s', |
133 datefmt='%m/%d/%Y %I:%M:%S %p', | 134 datefmt='%d/%m/%Y %I:%M:%S %p', |
134 level=level) | 135 level=level) |
135 #logging.getLogger("asyncio").setLevel(logging.DEBUG) | 136 #logging.getLogger("asyncio").setLevel(logging.DEBUG) |
136 | 137 |
137 def start(test_mode): | 138 def start(test_mode, nowait): |
138 with Tempserver(test_mode) as server: | 139 with Tempserver(test_mode, nowait) as server: |
139 server.run() | 140 server.run() |
140 | 141 |
141 def main(): | 142 def main(): |
142 parser = argparse.ArgumentParser() | 143 parser = argparse.ArgumentParser() |
143 parser.add_argument('--hup', action='store_true') | 144 parser.add_argument('--hup', action='store_true') |
144 parser.add_argument('--new', action='store_true') | 145 parser.add_argument('--new', action='store_true') |
145 parser.add_argument('-D', '--daemon', action='store_true') | 146 parser.add_argument('-D', '--daemon', action='store_true') |
146 parser.add_argument('-d', '--debug', action='store_true') | 147 parser.add_argument('-d', '--debug', action='store_true') |
147 parser.add_argument('-t', '--test', action='store_true') | 148 parser.add_argument('-t', '--test', action='store_true') |
149 parser.add_argument('--nowait', action='store_true') | |
148 args = parser.parse_args() | 150 args = parser.parse_args() |
149 | 151 |
150 setup_logging(args.debug) | 152 setup_logging(args.debug) |
151 | 153 |
152 heredir = os.path.abspath(os.path.dirname(__file__)) | 154 heredir = os.path.abspath(os.path.dirname(__file__)) |
203 | 205 |
204 if args.daemon: | 206 if args.daemon: |
205 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') | 207 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') |
206 logf = open(logpath, 'a+') | 208 logf = open(logpath, 'a+') |
207 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): | 209 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
208 start(args.test) | 210 start(args.test, args.nowait) |
209 else: | 211 else: |
210 with pidf: | 212 with pidf: |
211 start(args.test) | 213 start(args.test, args.nowait) |
212 | 214 |
213 if __name__ == '__main__': | 215 if __name__ == '__main__': |
214 main() | 216 main() |