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