Mercurial > templog
comparison server/ts.py @ 438:2607e4f322cf
use tcp instead
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 28 Oct 2012 07:52:35 +0800 |
parents | 649648020123 |
children |
comparison
equal
deleted
inserted
replaced
437:7d7e7f961eae | 438:2607e4f322cf |
---|---|
18 import hmac | 18 import hmac |
19 import zlib | 19 import zlib |
20 import urllib | 20 import urllib |
21 import urllib2 | 21 import urllib2 |
22 import logging | 22 import logging |
23 import socket | |
23 | 24 |
24 L = logging.info | 25 L = logging.info |
25 W = logging.warning | 26 W = logging.warning |
26 E = logging.error | 27 E = logging.error |
27 | 28 |
178 | 179 |
179 clear_meas(sock) | 180 clear_meas(sock) |
180 | 181 |
181 next_wake = 600 | 182 next_wake = 600 |
182 #next_wake = turn_off(sock) | 183 #next_wake = turn_off(sock) |
183 sock.close() | 184 #sock.close() |
184 return next_wake | 185 return next_wake |
185 | 186 |
186 testcount = 0 | 187 testcount = 0 |
187 | 188 |
188 def sleep_for(secs): | 189 def sleep_for(secs): |
196 def setup_logging(): | 197 def setup_logging(): |
197 logging.basicConfig(format='%(asctime)s %(message)s', | 198 logging.basicConfig(format='%(asctime)s %(message)s', |
198 datefmt='%m/%d/%Y %I:%M:%S %p', | 199 datefmt='%m/%d/%Y %I:%M:%S %p', |
199 level=logging.INFO) | 200 level=logging.INFO) |
200 | 201 |
202 def get_net_socket(host, port): | |
203 s = socket.create_connection((host, port)) | |
204 s.setblocking(False) | |
205 s.settimeout(1) | |
206 return s | |
207 | |
201 def main(): | 208 def main(): |
202 setup_logging() | 209 setup_logging() |
203 | 210 |
204 L("Running templog rfcomm server") | 211 L("Running templog rfcomm server") |
205 | 212 |
206 if '--daemon' in sys.argv: | 213 if '--daemon' in sys.argv: |
207 utils.cheap_daemon() | 214 utils.cheap_daemon() |
208 | 215 |
209 next_wake_time = 0 | 216 next_wake_time = 0 |
217 | |
210 while True: | 218 while True: |
211 sock = None | 219 sock = None |
212 try: | 220 try: |
213 sock = get_socket(config.BTADDR) | 221 sock = get_net_socket(config.SERIAL_HOST, config.SERIAL_PORT) |
214 except Exception, e: | 222 except Exception, e: |
215 #logging.exception("Error connecting") | 223 #logging.exception("Error connecting") |
216 pass | 224 pass |
217 if sock: | 225 |
226 if not sock: | |
227 sleep_for(config.SLEEP_TIME) | |
228 continue | |
229 | |
230 while True: | |
218 try: | 231 try: |
219 avr_wake = do_comms(sock) | 232 do_comms(sock) |
220 next_wake_time = time.time() + avr_wake | 233 sleep_for(config.SLEEP_TIME) |
221 except Exception, e: | 234 except Exception, e: |
222 logging.exception("Error in do_comms") | 235 logging.exception("Error in do_comms") |
223 | 236 break |
224 next_wake_interval = next_wake_time - time.time() + EXTRA_WAKEUP | |
225 sleep_time = config.SLEEP_TIME | |
226 if next_wake_interval > 0: | |
227 sleep_time = min(next_wake_interval, sleep_time) | |
228 if next_wake_interval < 0 and next_wake_interval > -30: | |
229 L("not sleeping, next_wake_interval overdue %f" % next_wake_interval) | |
230 continue | |
231 L("Sleeping for %d, next wake interval %f" % (sleep_time, next_wake_interval)) | |
232 sleep_for(sleep_time) | |
233 | 237 |
234 if __name__ == '__main__': | 238 if __name__ == '__main__': |
235 main() | 239 main() |