comparison server/ts.py @ 328:46070aaf29ea

A bit of work on the server python
author Matt Johnston <matt@ucc.asn.au>
date Sat, 26 May 2012 10:17:27 +0800
parents 5639c74f2cbb
children b5925cb4f264
comparison
equal deleted inserted replaced
327:5639c74f2cbb 328:46070aaf29ea
1 #!/usr/bin/env python2.7 1 #!/usr/bin/env python2.7
2
3 BTADDR = "00:12:03:27:70:88"
4 SLEEP_TIME = 180
2 5
3 import sys 6 import sys
4 import httplib 7 import httplib
8 import time
9 import traceback
10
11 from utils import monotonic_time, retry
5 12
6 lightblue = None 13 lightblue = None
7 try: 14 try:
8 import lightblue 15 import lightblue
9 except ImportError: 16 except ImportError:
10 import bluetooth 17 import bluetooth
11
12 BTADDR = "00:12:03:27:70:88"
13 18
14 def get_socket(addr): 19 def get_socket(addr):
15 if lightblue: 20 if lightblue:
16 s = lightblue.socket() 21 s = lightblue.socket()
17 s.connect(addr, 1) 22 s.connect(addr, 1)
18 else: 23 else:
19 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 24 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
20 s.connect((addr, 1)) 25 s.connect((addr, 1))
21 26
27 s.setnonblocking(True)
28
22 return s 29 return s
23 30
24 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html 31 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
25 def crc16(buff, crc = 0, poly = 0x8408): 32 def crc16(buff, crc = 0, poly = 0x8408):
26     l = len(buff) 33 l = len(buff)
27     i = 0 34 i = 0
28     while i < l: 35 while i < l:
29         ch = ord(buff[i]) 36 ch = ord(buff[i])
30         uc = 0 37 uc = 0
31         while uc < 8: 38 while uc < 8:
32             if (crc & 1) ^ (ch & 1): 39 if (crc & 1) ^ (ch & 1):
33                 crc = (crc >> 1) ^ poly 40 crc = (crc >> 1) ^ poly
34             else: 41 else:
35                 crc >>= 1 42 crc >>= 1
36             ch >>= 1 43 ch >>= 1
37             uc += 1 44 uc += 1
38         i += 1 45 i += 1
39     return crc 46 return crc
40 47
41 48
49 @retry()
42 def fetch(sock): 50 def fetch(sock):
43 sock.send("fetch\n") 51 sock.send("fetch\n")
44
45 def readline(self):
46 buf = ''
47 while true:
48 c = self.recv(1)
49 if c == '\r':
50 continue
51
52 buf.append(c)
53 if c == '\n':
54 return buf
55 52
56 crc = 0 53 crc = 0
57 54
58 lines = [] 55 lines = []
59 l = readline(sock) 56 l = readline(sock)
91 if recv_crc != crc: 88 if recv_crc != crc:
92 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc) 89 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)
93 return None 90 return None
94 91
95 return ''.join(lines) 92 return ''.join(lines)
93
94 @retry()
95 def turn_off(sock):
96 sock.send("btoff\n");
97 # read newline
98 l = readline(sock)
99 if not l:
100 print>>sys.stderr, "Bad response to btoff\n"
101 return None
102
103 off, next_wake = l.rstrip().split(':')
104 if off != 'Off':
105 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
106
107 return int(next_wake)
108
109
110 def do_comms(sock):
111 d = None
112 # serial could be unreliable, try a few times
113 for i in range(FETCH_TRIES):
114 d = fetch(sock)
115 if d:
116 break
117 time.sleep(1)
118 if not d:
119 return
120
121 res = send_results()
122 if not res:
123 return
124
125 clear_meas(sock)
126
127 next_wake = turn_off(sock)
128 sock.close()
129 return next_wake
130
131 testcount = 0
132
133 def sleep_for(secs):
134 until = monotonic_time + secs
135 while True:
136 length = until < monotonic_time()
137 if length <= 0:
138 return
139 time.sleep(length)
140
141 def main():
142
143 while True:
144 sock = get_socket()
145 sleep_time = SLEEP_TIME
146 if sock:
147 next_wake = None
148 try:
149 next_wake = do_comms(sock)
150 except Exception, e:
151 print>>sys.stderr, "Error in do_comms:"
152 traceback.print_last(file=sys.stderr)
153 if next_wake:
154 sleep_time = min(next_wake, sleep_time)
155
156 sleep_for(sleep_time)
157
158 if __name__ == '__main__':
159 main()