comparison server/ts.py @ 329:740438e21ea0

Fix bugs in server code (try actually running it)
author Matt Johnston <matt@ucc.asn.au>
date Mon, 04 Jun 2012 23:50:42 +0800
parents 46070aaf29ea
children 44c5ab5ea879
comparison
equal deleted inserted replaced
328:46070aaf29ea 329:740438e21ea0
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2 2
3 BTADDR = "00:12:03:27:70:88" 3 BTADDR = "00:12:03:27:70:88"
4 SLEEP_TIME = 180 4 SLEEP_TIME = 180
5 # time that the bluetooth takes to get going?
6 EXTRA_WAKEUP = 0
7
8 # avoid turning off the bluetooth etc.
9 TESTING = True
5 10
6 import sys 11 import sys
7 import httplib 12 import httplib
8 import time 13 import time
9 import traceback 14 import traceback
10 15
11 from utils import monotonic_time, retry 16 from utils import monotonic_time, retry, readline
12 17
13 lightblue = None 18 lightblue = None
14 try: 19 try:
15 import lightblue 20 import lightblue
16 except ImportError: 21 except ImportError:
17 import bluetooth 22 import bluetooth
18 23
19 def get_socket(addr): 24 def get_socket(addr):
20 if lightblue: 25 if lightblue:
21 s = lightblue.socket() 26 s = lightblue.socket()
22 s.connect(addr, 1) 27 s.connect((addr, 1))
23 else: 28 else:
24 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 29 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
25 s.connect((addr, 1)) 30 s.connect((addr, 1))
26 31
27 s.setnonblocking(True) 32 s.setnonblocking(True)
91 96
92 return ''.join(lines) 97 return ''.join(lines)
93 98
94 @retry() 99 @retry()
95 def turn_off(sock): 100 def turn_off(sock):
101 if TESTING:
102 return None
96 sock.send("btoff\n"); 103 sock.send("btoff\n");
97 # read newline 104 # read newline
98 l = readline(sock) 105 l = readline(sock)
99 if not l: 106 if not l:
100 print>>sys.stderr, "Bad response to btoff\n" 107 print>>sys.stderr, "Bad response to btoff\n"
103 off, next_wake = l.rstrip().split(':') 110 off, next_wake = l.rstrip().split(':')
104 if off != 'Off': 111 if off != 'Off':
105 print>>sys.stderr, "Bad response to btoff '%s'\n" % l 112 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
106 113
107 return int(next_wake) 114 return int(next_wake)
108
109 115
110 def do_comms(sock): 116 def do_comms(sock):
111 d = None 117 d = None
112 # serial could be unreliable, try a few times 118 # serial could be unreliable, try a few times
113 for i in range(FETCH_TRIES): 119 for i in range(FETCH_TRIES):
129 return next_wake 135 return next_wake
130 136
131 testcount = 0 137 testcount = 0
132 138
133 def sleep_for(secs): 139 def sleep_for(secs):
134 until = monotonic_time + secs 140 until = monotonic_time() + secs
135 while True: 141 while True:
136 length = until < monotonic_time() 142 length = until < monotonic_time()
137 if length <= 0: 143 if length <= 0:
138 return 144 return
139 time.sleep(length) 145 time.sleep(length)
140 146
141 def main(): 147 def main():
142 148
143 while True: 149 while True:
144 sock = get_socket() 150 sock = None
151 try:
152 sock = get_socket(BTADDR)
153 except Exception, e:
154 print>>sys.stderr, "Error connecting:"
155 traceback.print_exc(file=sys.stderr)
145 sleep_time = SLEEP_TIME 156 sleep_time = SLEEP_TIME
146 if sock: 157 if sock:
147 next_wake = None 158 next_wake = None
148 try: 159 try:
149 next_wake = do_comms(sock) 160 next_wake = do_comms(sock)
150 except Exception, e: 161 except Exception, e:
151 print>>sys.stderr, "Error in do_comms:" 162 print>>sys.stderr, "Error in do_comms:"
152 traceback.print_last(file=sys.stderr) 163 traceback.print_exc(file=sys.stderr)
153 if next_wake: 164 if next_wake:
154 sleep_time = min(next_wake, sleep_time) 165 sleep_time = min(next_wake+EXTRA_WAKEUP, sleep_time)
155 166
167 if TESTING:
168 print "Sleeping for %d" % sleep_time
156 sleep_for(sleep_time) 169 sleep_for(sleep_time)
157 170
158 if __name__ == '__main__': 171 if __name__ == '__main__':
159 main() 172 main()