Mercurial > templog
comparison old/server/dump.py @ 511:cf52d5817709
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 26 Jun 2014 23:03:32 +0800 |
parents | 95043860e437 |
children |
comparison
equal
deleted
inserted
replaced
505:ad846b9bdd10 | 511:cf52d5817709 |
---|---|
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 if not l: | |
149 print '.', | |
150 sys.stdout.flush() | |
151 else: | |
152 print l | |
153 | |
154 testcount = 0 | |
155 | |
156 def sleep_for(secs): | |
157 until = monotonic_time() + secs | |
158 while True: | |
159 length = until - monotonic_time() | |
160 if length <= 0: | |
161 return | |
162 time.sleep(length) | |
163 | |
164 def main(): | |
165 next_wake_time = 0 | |
166 | |
167 while True: | |
168 sock = None | |
169 try: | |
170 sock = get_socket(BTADDR) | |
171 except Exception, e: | |
172 print>>sys.stderr, "Error connecting:" | |
173 traceback.print_exc(file=sys.stderr) | |
174 sleep_time = SLEEP_TIME | |
175 if sock: | |
176 next_wake = None | |
177 try: | |
178 next_wake_interval = do_comms(sock) | |
179 next_wake_time = time.time() + next_wake_interval | |
180 except Exception, e: | |
181 print>>sys.stderr, "Error in do_comms:" | |
182 traceback.print_exc(file=sys.stderr) | |
183 if next_wake_time > time.time(): | |
184 sleep_time = min(next_wake_time - time.time() - EXTRA_WAKEUP, sleep_time) | |
185 | |
186 print "Sleeping for %d" % sleep_time | |
187 sleep_for(sleep_time) | |
188 | |
189 if __name__ == '__main__': | |
190 main() |