3 from binascii import unhexlify
5 asynchronous_responses = [ '400', '401', # door open/closed
6 '610', # switches changed
10 def __init__(self, rfh, wfh):
11 self.secret = 'AAAAAAAAAAAAAAAA'
15 # Initialise ourselves into a known state
18 self.wfh.write('echo off\n')
20 self.wfh.write('PING\n')
23 (code, _) = self.get_response()
25 def await_prompt(self):
32 if s == '': raise Exception
33 if s == '\n' or s == '\r':
36 if (s == '#' or s == '%') and state == 1: state = 2
37 if s == ' ' and state == 2:
41 if re.search('^[0-9a-fA-F]{4}$', prefix):
42 self.challenge = unhexlify(prefix)
45 def get_response(self):
50 s = self.rfh.readline()
51 if s == '': return None
55 if code in asynchronous_responses:
56 self.handle_event(code, text)
61 def handle_event(self, code, text):
64 def authed_message(self, message):
65 if self.challenge == None:
67 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
68 crc = do_crc(self.secret, crc)
69 crc = do_crc(message, crc)
70 return message+'|'+('%04x'%crc)
73 self.wfh.write('PING\n')
74 (code, string) = self.get_response()
75 return (code == '000', code, string)
78 if not re.search('^[0-9][0-9]$', item):
79 return (False, 'Invalid item requested (%s)'%item)
80 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
81 (code, string) = self.get_response()
84 def beep(self, duration = None, synchronous = True):
86 if synchronous: msg += 'S'
88 if duration > 255: duration = 255
89 if duration < 1: duration = 1
90 msg += '%02x'%duration
91 self.wfh.write(msg+'\n')
92 (code, string) = self.get_response()
93 return (code == '500', code, string)
95 def silence(self, duration = None, synchronous = True):
97 if synchronous: msg += 'S'
99 if duration > 255: duration = 255
100 if duration < 1: duration = 1
101 msg += '%02x'%duration
102 self.wfh.write(msg+'\n')
103 (code, string) = self.get_response()
104 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
105 return (code == '500' or code == '501', code, string)
107 def display(self, string):
109 string = string[0:10]
110 self.wfh.write('D'+string+'\n')
111 (code, string) = self.get_response()
112 return (code == '300', code, string)