annotate server/ts.py @ 97:bf707a3775d2

change default comms_wake
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Jul 2012 22:58:25 +0800
parents 229b740a607f
children b2d0887fb306 1a88bb989afb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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?
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
4 EXTRA_WAKEUP = 0
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
26c8ab2ba3f1 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 40
diff changeset
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
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
17 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
18 import hmac
34
79124d7d3f79 - import zlib
Matt Johnston <matt@ucc.asn.au>
parents: 32
diff changeset
19 import zlib
35
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
20 import urllib
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
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
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
27
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
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
c5ad12670cae dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 65
diff changeset
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 )
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
37 s.connect((addr, 1))
91
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
38 s.setblocking(False)
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
39
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 return s
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
43 @retry()
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 def fetch(sock):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 sock.send("fetch\n")
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 crc = 0
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 lines = []
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 if l != 'START\n':
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
52 W("Bad expected START line '%s'\n" % l.rstrip('\n'))
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
56 while True:
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if l == 'END\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 break
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
64 lines.append(l.rstrip('\n'))
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
66 for d in lines:
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
67 L("Received: %s" % d)
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
68
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 k, v = l.rstrip('\n').split('=')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 if k == 'CRC':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 recv_crc = int(v)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 if recv_crc < 0 or recv_crc > 0xffff:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 except ValueError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 pass
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 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
81 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
82 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 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
85 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
86 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
88 return lines
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
89
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
90 @retry()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
91 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
92 if TESTING:
42
26c8ab2ba3f1 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 40
diff changeset
93 return 99
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
94 L("Sending btoff")
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
95 sock.send("btoff\n");
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
96 # read newline
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
97 l = readline(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
98 if not l:
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
99 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
100 return None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
101
91
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
102 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
103 W("Bad response to btoff '%s'" % l)
42
26c8ab2ba3f1 mostly works
Matt Johnston <matt@ucc.asn.au>
parents: 40
diff changeset
104 return None
91
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
105 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
106
91
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
107 toks = dict(v.split('=') for v in l.split(','))
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
108
92
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
109 rem = int(toks['rem'])
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
110 tick_secs = int(toks['tick_secs'])
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
111 tick_secs = int(toks['tick_wake'])
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
112 next_wake = int(toks['next_wake'])
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
113
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
114 rem_secs = float(rem) / tick_wake * tick_secs
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
115
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
116 next_wake_secs = next_wake - rem_secs
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
117 L("next_wake_secs %f\n", next_wake_secs)
c3eb67cd8b61 handle rem from btoff
Matt Johnston <matt@ucc.asn.au>
parents: 91
diff changeset
118 return next_wake_secs
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
119
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
120 @retry()
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
121 def clear_meas(sock):
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
122 sock.send("clear\n");
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
123 l = readline(sock)
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
124 if l and l.rstrip() == 'cleared':
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
125 return True
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
126
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
127 E("Bad response to clear '%s'" % str(l))
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
128 return False
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
129
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
130 def send_results(lines):
29
048143905092 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 28
diff changeset
131 enc_lines = binascii.b2a_base64(zlib.compress('\n'.join(lines)))
35
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
132 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
133
35
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
134 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} )
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
135 con = urllib2.urlopen(config.UPDATE_URL, url_data)
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
136 result = con.read(100)
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
137 if result == 'OK':
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
138 return True
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
139 else:
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
140 W("Bad result '%s'" % result)
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
141 return False
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
142
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
143 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
144 L("do_comms")
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
145 d = None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
146 # 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
147 d = fetch(sock)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
148 if not d:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
149 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
150
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
151 res = send_results(d)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
152 if not res:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
153 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
154
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
155 clear_meas(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
156
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
157 next_wake = turn_off(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
158 sock.close()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
159 return next_wake
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
160
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
161 testcount = 0
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
162
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
163 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
164 until = monotonic_time() + secs
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
165 while True:
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
166 length = until - monotonic_time()
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
167 if length <= 0:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
168 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
169 time.sleep(length)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
170
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
171 def setup_logging():
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
172 logging.basicConfig(format='%(asctime)s %(message)s',
91
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
173 datefmt='%m/%d/%Y %I:%M:%S %p',
231662c6690c handle new next_wake format
Matt Johnston <matt@ucc.asn.au>
parents: 89
diff changeset
174 level=logging.INFO)
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
175
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
176 def main():
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
177 setup_logging()
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
178
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
179 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
180
65
b09c164debf6 cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 47
diff changeset
181 if '--daemon' in sys.argv:
b09c164debf6 cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 47
diff changeset
182 utils.cheap_daemon()
b09c164debf6 cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 47
diff changeset
183
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
184 while True:
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
185 sock = None
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
186 try:
82
0f7c005b3f87 move server config into config.py
Matt Johnston <matt@ucc.asn.au>
parents: 66
diff changeset
187 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
188 except Exception, e:
94
229b740a607f use the remainder of times
Matt Johnston <matt@ucc.asn.au>
parents: 92
diff changeset
189 #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
190 pass
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
191 next_wake_time = 0
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
192 if sock:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
193 try:
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
194 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
195 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
196 except Exception, e:
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
197 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
198
89
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
199 next_wake_interval = next_wake_time - time.time() - EXTRA_WAKEUP
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
200 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
201 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
202 sleep_time = min(next_wake_interval, sleep_time)
51d889ad39a3 main.c : add a delay before turning on uart
Matt Johnston <matt@ucc.asn.au>
parents: 82
diff changeset
203 L("Sleeping for %d, next wake time %f" % (sleep_time, next_wake_time))
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
204 sleep_for(sleep_time)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
205
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
206 if __name__ == '__main__':
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
207 main()