annotate server/ts.py @ 40:9b5b202129c3

main.c: - get rid of some debugging - separate uart_enabled flag ts.py: - remember next wake time, not the interval log.py: - comments for sqlite templog.py - use cgi
author Matt Johnston <matt@ucc.asn.au>
date Sat, 23 Jun 2012 22:10:23 +0800
parents 4bfa9b7b2e4b
children 26c8ab2ba3f1
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
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
3 BTADDR = "00:12:03:27:70:88"
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
4 SLEEP_TIME = 180
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
5 # 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
6 EXTRA_WAKEUP = 0
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
7
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
8 FETCH_TRIES = 3
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
9
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
10 # avoid turning off the bluetooth etc.
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
11 TESTING = True
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
12
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 import sys
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
14 # for wrt
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
15 sys.path.append('/root/python')
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
16 import httplib
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
17 import time
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
18 import traceback
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
19 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
20 import hmac
34
79124d7d3f79 - import zlib
Matt Johnston <matt@ucc.asn.au>
parents: 32
diff changeset
21 import zlib
35
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
22 import urllib
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
23 import urllib2
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
24
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
25 import config
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
26
26
d3e5934fe55c - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 25
diff changeset
27 from utils import monotonic_time, retry, readline, crc16
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 lightblue = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 import lightblue
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 except ImportError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 import bluetooth
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):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 if lightblue:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 s = lightblue.socket()
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
38 s.connect((addr, 1))
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
39 s.settimeout(3)
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 else:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 s.connect((addr, 1))
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
44 s.setblocking(False)
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
45
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return s
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
49 @retry()
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 def fetch(sock):
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
51 print "fetch"
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 sock.send("fetch\n")
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 crc = 0
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 lines = []
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 if l != 'START\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
63 while True:
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if l == 'END\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 break
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
71 lines.append(l.rstrip('\n'))
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
73 print lines
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
74
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 l = readline(sock)
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 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 k, v = l.rstrip('\n').split('=')
25
2943f62c8e62 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
79 print k,v
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if k == 'CRC':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 recv_crc = int(v)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if recv_crc < 0 or recv_crc > 0xffff:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 except ValueError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 pass
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 if recv_crc is None:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 print>>sys.stderr, "Bad expected CRC line '%s'\n" % l.rstrip('\n')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 if recv_crc != crc:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
95 return lines
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
96
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
97 @retry()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
98 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
99 if TESTING:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
100 return None
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
101 sock.send("btoff\n");
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
102 # read newline
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
103 l = readline(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
104 if not l:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
105 print>>sys.stderr, "Bad response to btoff\n"
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
106 return None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
107
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
108 off, next_wake = l.rstrip().split(':')
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
109 if off != 'Off':
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
110 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
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 return int(next_wake)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
113
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
114 @retry()
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
115 def clear_meas(sock):
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
116 sock.send("clear\n");
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
117 l = readline(sock)
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
118 if l and l.rstrip() == 'cleared':
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
119 return True
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
120
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
121 print>>sys.stderr, "Bad response to clear %s\n" % str(l)
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
122 return False
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
123
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
124 def send_results(lines):
29
048143905092 work on web interface
Matt Johnston <matt@ucc.asn.au>
parents: 28
diff changeset
125 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
126 mac = hmac.new(config.HMAC_KEY, enc_lines).hexdigest()
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
127
35
4bfa9b7b2e4b - fix mac/urllib to work
Matt Johnston <matt@ucc.asn.au>
parents: 34
diff changeset
128 url_data = urllib.urlencode( {'lines': enc_lines, 'hmac': mac} )
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
129 con = urllib2.urlopen(config.UPDATE_URL, url_data)
32
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
130 result = con.read(100)
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
131 if result == 'OK':
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
132 return True
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
133 else:
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
134 print>>sys.stderr, "Bad result '%s'" % result
e18d7e89c17d More minor work
Matt Johnston <matt@ucc.asn.au>
parents: 29
diff changeset
135 return False
28
Matt Johnston <matt@ucc.asn.au>
parents: 27
diff changeset
136
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
137 def do_comms(sock):
25
2943f62c8e62 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
138 print "do_comms"
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
139 d = None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
140 # serial could be unreliable, try a few times
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
141 for i in range(FETCH_TRIES):
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
142 d = fetch(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
143 if d:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
144 break
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
145 time.sleep(1)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
146 if not d:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
147 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
148
27
dbbd503119ba Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents: 26
diff changeset
149 res = send_results(d)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
150 if not res:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
151 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
152
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
153 clear_meas(sock)
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 next_wake = turn_off(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
156 sock.close()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
157 return next_wake
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
158
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
159 testcount = 0
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 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
162 until = monotonic_time() + secs
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
163 while True:
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
164 length = until - monotonic_time()
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
165 if length <= 0:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
166 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
167 time.sleep(length)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
168
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
169 def main():
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 35
diff changeset
170 next_wake_time = 0
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
171
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
172 while True:
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
173 sock = None
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
174 try:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
175 sock = get_socket(BTADDR)
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
176 except Exception, e:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
177 print>>sys.stderr, "Error connecting:"
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
178 traceback.print_exc(file=sys.stderr)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
179 sleep_time = SLEEP_TIME
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
180 if sock:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
181 next_wake = None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
182 try:
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 35
diff changeset
183 next_wake_interval = do_comms(sock)
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 35
diff changeset
184 next_wake_time = time.now() + next_wake_interval
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
185 except Exception, e:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
186 print>>sys.stderr, "Error in do_comms:"
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
187 traceback.print_exc(file=sys.stderr)
40
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 35
diff changeset
188 if next_wake_time > time.now():
9b5b202129c3 main.c:
Matt Johnston <matt@ucc.asn.au>
parents: 35
diff changeset
189 sleep_time = min(next_wake_time - time.now() - EXTRA_WAKEUP, sleep_time)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
190
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
191 if TESTING:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
192 print "Sleeping for %d" % sleep_time
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
193 sleep_for(sleep_time)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
194
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
195 if __name__ == '__main__':
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
196 main()