Mercurial > templog
annotate server/ts.py @ 423:d828f409c4ed
update sensors config
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 10 Oct 2012 22:50:02 +0800 |
parents | 1603d0310dd0 |
children | de950be796dd |
rev | line source |
---|---|
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 #!/usr/bin/env python2.7 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
3 # time that the bluetooth takes to get going? |
403 | 4 EXTRA_WAKEUP = -3 |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
5 |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
329
diff
changeset
|
6 FETCH_TRIES = 3 |
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
329
diff
changeset
|
7 |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
8 # avoid turning off the bluetooth etc. |
348 | 9 TESTING = False |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
10 |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 import sys |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
12 # for wrt |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
13 sys.path.append('/root/python') |
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
14 import httplib |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
15 import time |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
16 import traceback |
334 | 17 import binascii |
18 import hmac | |
340 | 19 import zlib |
342 | 20 import urllib |
21 import urllib2 | |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
22 import logging |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
23 |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
24 L = logging.info |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
25 W = logging.warning |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
26 E = logging.error |
334 | 27 |
28 import config | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
29 |
332
05c1249da994
- Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents:
331
diff
changeset
|
30 from utils import monotonic_time, retry, readline, crc16 |
372 | 31 import utils |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
33 import bluetooth |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 def get_socket(addr): |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
36 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) |
403 | 37 L("connecting") |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
38 s.connect((addr, 1)) |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
39 s.setblocking(False) |
403 | 40 s.settimeout(1) |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
41 |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 return s |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 |
418 | 45 def flush(sock): |
46 while readline(sock): | |
47 pass | |
48 | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
49 @retry() |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 def fetch(sock): |
418 | 51 flush(sock) |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 sock.send("fetch\n") |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 crc = 0 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
55 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 lines = [] |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 l = readline(sock) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 if l != 'START\n': |
403 | 59 W("Bad expected START line '%s'\n" % str(l).rstrip('\n')) |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 return None |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 crc = crc16(l, crc) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
329
diff
changeset
|
63 while True: |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 l = readline(sock) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
66 crc = crc16(l, crc) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
67 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 if l == 'END\n': |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 break |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
71 lines.append(l.rstrip('\n')) |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
72 |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
73 for d in lines: |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
74 L("Received: %s" % d) |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
75 |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 l = readline(sock) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 recv_crc = None |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 try: |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
79 k, v = l.rstrip('\n').split('=') |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 if k == 'CRC': |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 recv_crc = int(v) |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 if recv_crc < 0 or recv_crc > 0xffff: |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 recv_crc = None |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 except ValueError: |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 pass |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 if recv_crc is None: |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
88 W("Bad expected CRC line '%s'\n" % l.rstrip('\n')) |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 return None |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
90 |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 if recv_crc != crc: |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
92 W("Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)) |
327
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
93 return None |
5639c74f2cbb
untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
95 return lines |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
96 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
97 @retry() |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
98 def turn_off(sock): |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
99 if TESTING: |
348 | 100 return 99 |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
101 L("Sending btoff") |
418 | 102 flush(sock) |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
103 sock.send("btoff\n"); |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
104 # read newline |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
105 l = readline(sock) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
106 if not l: |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
107 W("Bad response to btoff") |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
108 return None |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
109 |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
110 if not l.startswith('next_wake'): |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
111 W("Bad response to btoff '%s'" % l) |
348 | 112 return None |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
113 L("Next wake line %s" % l) |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
114 |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
115 toks = dict(v.split('=') for v in l.split(',')) |
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
116 |
398 | 117 rem = int(toks['rem']) |
118 tick_secs = int(toks['tick_secs']) | |
405
d9b78a1bdd1d
fix off-by-one in remainder code
Matt Johnston <matt@ucc.asn.au>
parents:
400
diff
changeset
|
119 tick_wake = int(toks['tick_wake']) + 1 |
398 | 120 next_wake = int(toks['next_wake']) |
121 | |
122 rem_secs = float(rem) / tick_wake * tick_secs | |
123 | |
124 next_wake_secs = next_wake - rem_secs | |
125 L("next_wake_secs %f\n", next_wake_secs) | |
126 return next_wake_secs | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
127 |
338 | 128 @retry() |
129 def clear_meas(sock): | |
418 | 130 flush(sock) |
338 | 131 sock.send("clear\n"); |
132 l = readline(sock) | |
133 if l and l.rstrip() == 'cleared': | |
134 return True | |
135 | |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
136 E("Bad response to clear '%s'" % str(l)) |
338 | 137 return False |
138 | |
334 | 139 def send_results(lines): |
335 | 140 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines))) |
342 | 141 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest() |
334 | 142 |
342 | 143 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} ) |
334 | 144 con = urllib2.urlopen(config.UPDATE_URL, url_data) |
338 | 145 result = con.read(100) |
146 if result == 'OK': | |
147 return True | |
148 else: | |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
149 W("Bad result '%s'" % result) |
338 | 150 return False |
334 | 151 |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
152 def do_comms(sock): |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
153 L("do_comms") |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
154 d = None |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
155 # serial could be unreliable, try a few times |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
156 d = fetch(sock) |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
157 if not d: |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
158 return |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
159 |
333
298e502fdcd4
Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
332
diff
changeset
|
160 res = send_results(d) |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
161 if not res: |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
162 return |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
163 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
164 clear_meas(sock) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
165 |
416 | 166 next_wake = 600 |
167 #next_wake = turn_off(sock) | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
168 sock.close() |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
169 return next_wake |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
170 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
171 testcount = 0 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
172 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
173 def sleep_for(secs): |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
174 until = monotonic_time() + secs |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
175 while True: |
330
7ac6b8846eea
- some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents:
329
diff
changeset
|
176 length = until - monotonic_time() |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
177 if length <= 0: |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
178 return |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
179 time.sleep(length) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
180 |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
181 def setup_logging(): |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
182 logging.basicConfig(format='%(asctime)s %(message)s', |
397
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
183 datefmt='%m/%d/%Y %I:%M:%S %p', |
f1428cddb882
handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents:
395
diff
changeset
|
184 level=logging.INFO) |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
185 |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
186 def main(): |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
187 setup_logging() |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
188 |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
189 L("Running templog rfcomm server") |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
190 |
371 | 191 if '--daemon' in sys.argv: |
192 utils.cheap_daemon() | |
193 | |
403 | 194 next_wake_time = 0 |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
195 while True: |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
196 sock = None |
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
197 try: |
388
b33045e7e08e
move server config into config.py
Matt Johnston <matt@ucc.asn.au>
parents:
372
diff
changeset
|
198 sock = get_socket(config.BTADDR) |
329
740438e21ea0
Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents:
328
diff
changeset
|
199 except Exception, e: |
400 | 200 #logging.exception("Error connecting") |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
201 pass |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
202 if sock: |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
203 try: |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
204 avr_wake = do_comms(sock) |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
205 next_wake_time = time.time() + avr_wake |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
206 except Exception, e: |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
207 logging.exception("Error in do_comms") |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
208 |
403 | 209 next_wake_interval = next_wake_time - time.time() + EXTRA_WAKEUP |
395
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
210 sleep_time = config.SLEEP_TIME |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
211 if next_wake_interval > 0: |
f0ddb75bcf04
main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents:
388
diff
changeset
|
212 sleep_time = min(next_wake_interval, sleep_time) |
403 | 213 if next_wake_interval < 0 and next_wake_interval > -30: |
214 L("not sleeping, next_wake_interval overdue %f" % next_wake_interval) | |
215 continue | |
216 L("Sleeping for %d, next wake interval %f" % (sleep_time, next_wake_interval)) | |
328
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
217 sleep_for(sleep_time) |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
218 |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
219 if __name__ == '__main__': |
46070aaf29ea
A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
327
diff
changeset
|
220 main() |