comparison server/ts.py @ 91:231662c6690c

handle new next_wake format
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Jul 2012 21:24:43 +0800
parents 51d889ad39a3
children c3eb67cd8b61
comparison
equal deleted inserted replaced
90:a1c1c2d475b0 91:231662c6690c
32 32
33 import bluetooth 33 import bluetooth
34 34
35 def get_socket(addr): 35 def get_socket(addr):
36 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 36 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
37 s.settimeout(1)
38 s.connect((addr, 1)) 37 s.connect((addr, 1))
39 38 s.setblocking(False)
39
40 return s 40 return s
41 41
42 42
43 @retry() 43 @retry()
44 def fetch(sock): 44 def fetch(sock):
97 l = readline(sock) 97 l = readline(sock)
98 if not l: 98 if not l:
99 W("Bad response to btoff") 99 W("Bad response to btoff")
100 return None 100 return None
101 101
102 if not l.startswith('off:'): 102 if not l.startswith('next_wake'):
103 W("Bad response to btoff '%s'" % l) 103 W("Bad response to btoff '%s'" % l)
104 return None 104 return None
105 off, next_wake = l.rstrip().split(':') 105 L("Next wake line %s" % l)
106 L("Next wake %s" % next_wake)
107 106
108 return int(next_wake) 107 toks = dict(v.split('=') for v in l.split(','))
108
109 return int(toks['next_wake'])
109 110
110 @retry() 111 @retry()
111 def clear_meas(sock): 112 def clear_meas(sock):
112 sock.send("clear\n"); 113 sock.send("clear\n");
113 l = readline(sock) 114 l = readline(sock)
158 return 159 return
159 time.sleep(length) 160 time.sleep(length)
160 161
161 def setup_logging(): 162 def setup_logging():
162 logging.basicConfig(format='%(asctime)s %(message)s', 163 logging.basicConfig(format='%(asctime)s %(message)s',
163 datefmt='%m/%d/%Y %I:%M:%S %p') 164 datefmt='%m/%d/%Y %I:%M:%S %p',
165 level=logging.INFO)
164 166
165 def main(): 167 def main():
166 setup_logging() 168 setup_logging()
167 169
168 L("Running templog rfcomm server") 170 L("Running templog rfcomm server")
173 while True: 175 while True:
174 sock = None 176 sock = None
175 try: 177 try:
176 sock = get_socket(config.BTADDR) 178 sock = get_socket(config.BTADDR)
177 except Exception, e: 179 except Exception, e:
180 logging.exception("Error connecting")
178 pass 181 pass
179 #print>>sys.stderr, "Error connecting:"
180 #traceback.print_exc(file=sys.stderr)
181 next_wake_time = 0 182 next_wake_time = 0
182 if sock: 183 if sock:
183 try: 184 try:
184 avr_wake = do_comms(sock) 185 avr_wake = do_comms(sock)
185 next_wake_time = time.time() + avr_wake 186 next_wake_time = time.time() + avr_wake