comparison server/ts.py @ 96:b2d0887fb306

be quicker responding
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Jul 2012 22:56:41 +0800
parents 229b740a607f
children 9485da05bc11
comparison
equal deleted inserted replaced
94:229b740a607f 96:b2d0887fb306
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2 2
3 # time that the bluetooth takes to get going? 3 # time that the bluetooth takes to get going?
4 EXTRA_WAKEUP = 0 4 EXTRA_WAKEUP = -3
5 5
6 FETCH_TRIES = 3 6 FETCH_TRIES = 3
7 7
8 # avoid turning off the bluetooth etc. 8 # avoid turning off the bluetooth etc.
9 TESTING = False 9 TESTING = False
32 32
33 import bluetooth 33 import bluetooth
34 34
35 def get_socket(addr): 35 def get_socket(addr):
36 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 36 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
37 L("connecting")
37 s.connect((addr, 1)) 38 s.connect((addr, 1))
38 s.setblocking(False) 39 s.setblocking(False)
40 s.settimeout(1)
39 41
40 return s 42 return s
41 43
42 44
43 @retry() 45 @retry()
47 crc = 0 49 crc = 0
48 50
49 lines = [] 51 lines = []
50 l = readline(sock) 52 l = readline(sock)
51 if l != 'START\n': 53 if l != 'START\n':
52 W("Bad expected START line '%s'\n" % l.rstrip('\n')) 54 W("Bad expected START line '%s'\n" % str(l).rstrip('\n'))
53 return None 55 return None
54 crc = crc16(l, crc) 56 crc = crc16(l, crc)
55 57
56 while True: 58 while True:
57 l = readline(sock) 59 l = readline(sock)
106 108
107 toks = dict(v.split('=') for v in l.split(',')) 109 toks = dict(v.split('=') for v in l.split(','))
108 110
109 rem = int(toks['rem']) 111 rem = int(toks['rem'])
110 tick_secs = int(toks['tick_secs']) 112 tick_secs = int(toks['tick_secs'])
111 tick_secs = int(toks['tick_wake']) 113 tick_wake = int(toks['tick_wake'])
112 next_wake = int(toks['next_wake']) 114 next_wake = int(toks['next_wake'])
113 115
114 rem_secs = float(rem) / tick_wake * tick_secs 116 rem_secs = float(rem) / tick_wake * tick_secs
115 117
116 next_wake_secs = next_wake - rem_secs 118 next_wake_secs = next_wake - rem_secs
179 L("Running templog rfcomm server") 181 L("Running templog rfcomm server")
180 182
181 if '--daemon' in sys.argv: 183 if '--daemon' in sys.argv:
182 utils.cheap_daemon() 184 utils.cheap_daemon()
183 185
186 next_wake_time = 0
184 while True: 187 while True:
185 sock = None 188 sock = None
186 try: 189 try:
187 sock = get_socket(config.BTADDR) 190 sock = get_socket(config.BTADDR)
188 except Exception, e: 191 except Exception, e:
189 #logging.exception("Error connecting") 192 #logging.exception("Error connecting")
190 pass 193 pass
191 next_wake_time = 0
192 if sock: 194 if sock:
193 try: 195 try:
194 avr_wake = do_comms(sock) 196 avr_wake = do_comms(sock)
195 next_wake_time = time.time() + avr_wake 197 next_wake_time = time.time() + avr_wake
196 except Exception, e: 198 except Exception, e:
197 logging.exception("Error in do_comms") 199 logging.exception("Error in do_comms")
198 200
199 next_wake_interval = next_wake_time - time.time() - EXTRA_WAKEUP 201 next_wake_interval = next_wake_time - time.time() + EXTRA_WAKEUP
200 sleep_time = config.SLEEP_TIME 202 sleep_time = config.SLEEP_TIME
201 if next_wake_interval > 0: 203 if next_wake_interval > 0:
202 sleep_time = min(next_wake_interval, sleep_time) 204 sleep_time = min(next_wake_interval, sleep_time)
203 L("Sleeping for %d, next wake time %f" % (sleep_time, next_wake_time)) 205 if next_wake_interval < 0 and next_wake_interval > -30:
206 L("not sleeping, next_wake_interval overdue %f" % next_wake_interval)
207 continue
208 L("Sleeping for %d, next wake interval %f" % (sleep_time, next_wake_interval))
204 sleep_for(sleep_time) 209 sleep_for(sleep_time)
205 210
206 if __name__ == '__main__': 211 if __name__ == '__main__':
207 main() 212 main()