comparison server/ts.py @ 42:26c8ab2ba3f1

mostly works
author Matt Johnston <matt@ucc.asn.au>
date Sat, 23 Jun 2012 23:36:00 +0800
parents 9b5b202129c3
children 96c336896201
comparison
equal deleted inserted replaced
41:1701457e6007 42:26c8ab2ba3f1
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 = 2
5 # time that the bluetooth takes to get going? 5 # time that the bluetooth takes to get going?
6 EXTRA_WAKEUP = 0 6 EXTRA_WAKEUP = 0
7 7
8 FETCH_TRIES = 3 8 FETCH_TRIES = 3
9 9
10 # avoid turning off the bluetooth etc. 10 # avoid turning off the bluetooth etc.
11 TESTING = True 11 TESTING = False
12 12
13 import sys 13 import sys
14 # for wrt 14 # for wrt
15 sys.path.append('/root/python') 15 sys.path.append('/root/python')
16 import httplib 16 import httplib
95 return lines 95 return lines
96 96
97 @retry() 97 @retry()
98 def turn_off(sock): 98 def turn_off(sock):
99 if TESTING: 99 if TESTING:
100 return None 100 return 99
101 sock.send("btoff\n"); 101 sock.send("btoff\n");
102 # read newline 102 # read newline
103 l = readline(sock) 103 l = readline(sock)
104 if not l: 104 if not l:
105 print>>sys.stderr, "Bad response to btoff\n" 105 print>>sys.stderr, "Bad response to btoff\n"
106 return None 106 return None
107 107
108 if not l.startswith('off:'):
109 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
110 return None
108 off, next_wake = l.rstrip().split(':') 111 off, next_wake = l.rstrip().split(':')
109 if off != 'Off': 112 print>>sys.stderr, "Next wake %s" % next_wake
110 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
111 113
112 return int(next_wake) 114 return int(next_wake)
113 115
114 @retry() 116 @retry()
115 def clear_meas(sock): 117 def clear_meas(sock):
165 if length <= 0: 167 if length <= 0:
166 return 168 return
167 time.sleep(length) 169 time.sleep(length)
168 170
169 def main(): 171 def main():
170 next_wake_time = 0 172 next_wake_time = 0
171 173
172 while True: 174 while True:
173 sock = None 175 sock = None
174 try: 176 try:
175 sock = get_socket(BTADDR) 177 sock = get_socket(BTADDR)
179 sleep_time = SLEEP_TIME 181 sleep_time = SLEEP_TIME
180 if sock: 182 if sock:
181 next_wake = None 183 next_wake = None
182 try: 184 try:
183 next_wake_interval = do_comms(sock) 185 next_wake_interval = do_comms(sock)
184 next_wake_time = time.now() + next_wake_interval 186 next_wake_time = time.time() + next_wake_interval
185 except Exception, e: 187 except Exception, e:
186 print>>sys.stderr, "Error in do_comms:" 188 print>>sys.stderr, "Error in do_comms:"
187 traceback.print_exc(file=sys.stderr) 189 traceback.print_exc(file=sys.stderr)
188 if next_wake_time > time.now(): 190 if next_wake_time > time.time():
189 sleep_time = min(next_wake_time - time.now() - EXTRA_WAKEUP, sleep_time) 191 sleep_time = min(next_wake_time - time.time() - EXTRA_WAKEUP, sleep_time)
190 192
191 if TESTING: 193 print "Sleeping for %d" % sleep_time
192 print "Sleeping for %d" % sleep_time
193 sleep_for(sleep_time) 194 sleep_for(sleep_time)
194 195
195 if __name__ == '__main__': 196 if __name__ == '__main__':
196 main() 197 main()