comparison server/dump.py @ 53:56b1f02f470c

add dump program
author Matt Johnston <matt@ucc.asn.au>
date Sun, 24 Jun 2012 17:22:26 +0800
parents
children 6c18e0d12276
comparison
equal deleted inserted replaced
52:c3f5e02c1c42 53:56b1f02f470c
1 #!/usr/bin/env python2.7
2
3 BTADDR = "00:12:03:27:70:88"
4 SLEEP_TIME = 5
5 # time that the bluetooth takes to get going?
6 EXTRA_WAKEUP = 0
7
8 FETCH_TRIES = 3
9
10 # avoid turning off the bluetooth etc.
11 TESTING = False
12
13 import sys
14 # for wrt
15 sys.path.append('/root/python')
16 import httplib
17 import time
18 import traceback
19 import binascii
20 import hmac
21 import zlib
22 import urllib
23 import urllib2
24
25 import config
26
27 from utils import monotonic_time, retry, readline, crc16
28
29 lightblue = None
30 try:
31 import lightblue
32 except ImportError:
33 import bluetooth
34
35 def get_socket(addr):
36 if lightblue:
37 s = lightblue.socket()
38 s.connect((addr, 1))
39 s.settimeout(3)
40 else:
41 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
42 s.connect((addr, 1))
43
44 s.setblocking(False)
45
46 return s
47
48
49 @retry()
50 def fetch(sock):
51 print "fetch"
52 sock.send("fetch\n")
53
54 crc = 0
55
56 lines = []
57 l = readline(sock)
58 if l != 'START\n':
59 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
60 return None
61 crc = crc16(l, crc)
62
63 while True:
64 l = readline(sock)
65
66 crc = crc16(l, crc)
67
68 if l == 'END\n':
69 break
70
71 lines.append(l.rstrip('\n'))
72
73 print lines
74
75 l = readline(sock)
76 recv_crc = None
77 try:
78 k, v = l.rstrip('\n').split('=')
79 print k,v
80 if k == 'CRC':
81 recv_crc = int(v)
82 if recv_crc < 0 or recv_crc > 0xffff:
83 recv_crc = None
84 except ValueError:
85 pass
86
87 if recv_crc is None:
88 print>>sys.stderr, "Bad expected CRC line '%s'\n" % l.rstrip('\n')
89 return None
90
91 if recv_crc != crc:
92 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)
93 return None
94
95 return lines
96
97 @retry()
98 def turn_off(sock):
99 if TESTING:
100 return 99
101 print>>sys.stderr, "sending btoff"
102 sock.send("btoff\n");
103 # read newline
104 l = readline(sock)
105 if not l:
106 print>>sys.stderr, "Bad response to btoff\n"
107 return None
108
109 if not l.startswith('off:'):
110 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
111 return None
112 off, next_wake = l.rstrip().split(':')
113 print>>sys.stderr, "Next wake %s" % next_wake
114
115 return int(next_wake)
116
117 @retry()
118 def clear_meas(sock):
119 sock.send("clear\n");
120 l = readline(sock)
121 if l and l.rstrip() == 'cleared':
122 return True
123
124 print>>sys.stderr, "Bad response to clear %s\n" % str(l)
125 return False
126
127 def send_results(lines):
128 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines)))
129 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
130
131 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} )
132 con = urllib2.urlopen(config.UPDATE_URL, url_data)
133 result = con.read(100)
134 if result == 'OK':
135 return True
136 else:
137 print>>sys.stderr, "Bad result '%s'" % result
138 return False
139
140 def do_comms(sock):
141 args = sys.argv[1:]
142 print "do_comms"
143 for a in args:
144 sock.send('%s\n', a)
145
146 while True:
147 l = readline(sock)
148 print l
149
150 testcount = 0
151
152 def sleep_for(secs):
153 until = monotonic_time() + secs
154 while True:
155 length = until - monotonic_time()
156 if length <= 0:
157 return
158 time.sleep(length)
159
160 def main():
161 next_wake_time = 0
162
163 while True:
164 sock = None
165 try:
166 sock = get_socket(BTADDR)
167 except Exception, e:
168 print>>sys.stderr, "Error connecting:"
169 traceback.print_exc(file=sys.stderr)
170 sleep_time = SLEEP_TIME
171 if sock:
172 next_wake = None
173 try:
174 next_wake_interval = do_comms(sock)
175 next_wake_time = time.time() + next_wake_interval
176 except Exception, e:
177 print>>sys.stderr, "Error in do_comms:"
178 traceback.print_exc(file=sys.stderr)
179 if next_wake_time > time.time():
180 sleep_time = min(next_wake_time - time.time() - EXTRA_WAKEUP, sleep_time)
181
182 print "Sleeping for %d" % sleep_time
183 sleep_for(sleep_time)
184
185 if __name__ == '__main__':
186 main()