annotate old/server/ts.py @ 510:fa282fb36f04

add link to live site
author Matt Johnston <matt@ucc.asn.au>
date Thu, 26 Jun 2014 21:44:16 +0800
parents 95043860e437
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
45d96250387c be quicker responding
Matt Johnston <matt@ucc.asn.au>
parents: 400
diff changeset
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
536128b90573 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 346
diff changeset
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
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
17 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
18 import hmac
340
3baca8d980f4 - import zlib
Matt Johnston <matt@ucc.asn.au>
parents: 338
diff changeset
19 import zlib
342
ba9bfcc9c526 - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
20 import urllib
ba9bfcc9c526 - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
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
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
23 import socket
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
24
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
25 L = logging.info
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
26 W = logging.warning
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
27 E = logging.error
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
28
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
29 import config
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
30
332
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
31 from utils import monotonic_time, retry, readline, crc16
372
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
32 import utils
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
34 import bluetooth
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 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
37 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
403
45d96250387c be quicker responding
Matt Johnston <matt@ucc.asn.au>
parents: 400
diff changeset
38 L("connecting")
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
39 s.connect((addr, 1))
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
40 s.setblocking(False)
403
45d96250387c be quicker responding
Matt Johnston <matt@ucc.asn.au>
parents: 400
diff changeset
41 s.settimeout(1)
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
42
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 return s
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
418
1603d0310dd0 try to flush
Matt Johnston <matt@ucc.asn.au>
parents: 416
diff changeset
46 def flush(sock):
429
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
47 ret = []
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
48 while True:
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
49 l = readline(sock)
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
50 if l:
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
51 ret.append(l)
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
52 else:
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
53 break
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
54 return ret
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
55
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
56 def encode_extra(extra_lines):
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
57 return ['extra%d=%s' % (n, l.strip()) for (n,l) in enumerate(extra_lines)]
418
1603d0310dd0 try to flush
Matt Johnston <matt@ucc.asn.au>
parents: 416
diff changeset
58
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
59 @retry()
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 def fetch(sock):
429
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
61 extra_lines = flush(sock)
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 sock.send("fetch\n")
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 crc = 0
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 lines = []
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 l = readline(sock)
429
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
68 if not l:
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
69 return None
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
70
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 if l != 'START\n':
429
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
72 W("Bad expected START line '%s'\n" % l.rstrip('\n'))
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
73 extra_lines.append(l)
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
74 return encode_extra(extra_lines)
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
77 while True:
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 crc = crc16(l, crc)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if l == 'END\n':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 break
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
85 lines.append(l.rstrip('\n'))
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
429
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
87 lines += encode_extra(extra_lines)
649648020123 log errors too
Matt Johnston <matt@ucc.asn.au>
parents: 418
diff changeset
88
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
89 for d in lines:
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
90 L("Received: %s" % d)
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
91
327
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 l = readline(sock)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 try:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 k, v = l.rstrip('\n').split('=')
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 if k == 'CRC':
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 recv_crc = int(v)
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 if recv_crc < 0 or recv_crc > 0xffff:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 recv_crc = None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 except ValueError:
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 pass
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 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
104 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
105 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 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
108 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
109 return None
5639c74f2cbb untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
111 return lines
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
112
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
113 @retry()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
114 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
115 if TESTING:
348
536128b90573 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 346
diff changeset
116 return 99
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
117 L("Sending btoff")
418
1603d0310dd0 try to flush
Matt Johnston <matt@ucc.asn.au>
parents: 416
diff changeset
118 flush(sock)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
119 sock.send("btoff\n");
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
120 # read newline
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
121 l = readline(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
122 if not l:
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
123 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
124 return None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
125
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
126 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
127 W("Bad response to btoff '%s'" % l)
348
536128b90573 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 346
diff changeset
128 return None
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
129 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
130
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
131 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
132
398
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
133 rem = int(toks['rem'])
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
134 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
135 tick_wake = int(toks['tick_wake']) + 1
398
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
136 next_wake = int(toks['next_wake'])
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
137
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
138 rem_secs = float(rem) / tick_wake * tick_secs
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
139
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
140 next_wake_secs = next_wake - rem_secs
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
141 L("next_wake_secs %f\n", next_wake_secs)
c738a52e31e4 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 397
diff changeset
142 return next_wake_secs
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
143
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
144 @retry()
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
145 def clear_meas(sock):
418
1603d0310dd0 try to flush
Matt Johnston <matt@ucc.asn.au>
parents: 416
diff changeset
146 flush(sock)
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
147 sock.send("clear\n");
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
148 l = readline(sock)
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
149 if l and l.rstrip() == 'cleared':
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
150 return True
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
151
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
152 E("Bad response to clear '%s'" % str(l))
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
153 return False
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
154
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
155 def send_results(lines):
335
1e22eaf93620 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 334
diff changeset
156 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines)))
342
ba9bfcc9c526 - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
157 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
158
342
ba9bfcc9c526 - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 340
diff changeset
159 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} )
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
160 con = urllib2.urlopen(config.UPDATE_URL, url_data)
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
161 result = con.read(100)
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
162 if result == 'OK':
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
163 return True
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
164 else:
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
165 W("Bad result '%s'" % result)
338
12123e390169 More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 335
diff changeset
166 return False
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
167
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
168 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
169 L("do_comms")
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
170 d = None
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
171 # 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
172 d = fetch(sock)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
173 if not d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
174 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
175
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
176 res = send_results(d)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
177 if not res:
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
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
180 clear_meas(sock)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
181
416
99ef51dd1f61 stay on
Matt Johnston <matt@ucc.asn.au>
parents: 406
diff changeset
182 next_wake = 600
99ef51dd1f61 stay on
Matt Johnston <matt@ucc.asn.au>
parents: 406
diff changeset
183 #next_wake = turn_off(sock)
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
184 #sock.close()
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
185 return next_wake
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
186
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
187 testcount = 0
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
188
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
189 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
190 until = monotonic_time() + secs
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
191 while True:
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
192 length = until - monotonic_time()
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
193 if length <= 0:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
194 return
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
195 time.sleep(length)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
196
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
197 def setup_logging():
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
198 logging.basicConfig(format='%(asctime)s %(message)s',
397
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
199 datefmt='%m/%d/%Y %I:%M:%S %p',
f1428cddb882 handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 395
diff changeset
200 level=logging.INFO)
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
201
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
202 def get_net_socket(host, port):
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
203 s = socket.create_connection((host, port))
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
204 s.setblocking(False)
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
205 s.settimeout(1)
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
206 return s
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
207
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
208 def main():
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
209 setup_logging()
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
210
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
211 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
212
371
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
213 if '--daemon' in sys.argv:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
214 utils.cheap_daemon()
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
215
403
45d96250387c be quicker responding
Matt Johnston <matt@ucc.asn.au>
parents: 400
diff changeset
216 next_wake_time = 0
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
217
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
218 while True:
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
219 sock = None
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
220 try:
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
221 sock = get_net_socket(config.SERIAL_HOST, config.SERIAL_PORT)
329
740438e21ea0 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 328
diff changeset
222 except Exception, e:
400
1137f315209b use the remainder of times
Matt Johnston <matt@ucc.asn.au>
parents: 398
diff changeset
223 #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
224 pass
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
225
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
226 if not sock:
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
227 sleep_for(config.SLEEP_TIME)
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
228 continue
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
229
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
230 while True:
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
231 try:
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
232 do_comms(sock)
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
233 sleep_for(config.SLEEP_TIME)
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
234 except Exception, e:
395
f0ddb75bcf04 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 388
diff changeset
235 logging.exception("Error in do_comms")
438
2607e4f322cf use tcp instead
Matt Johnston <matt@ucc.asn.au>
parents: 429
diff changeset
236 break
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
237
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
238 if __name__ == '__main__':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 327
diff changeset
239 main()