3 from select import select
5 asynchronous_responses = [ '400', '401', # door open/closed
6 '610', # switches changed
7 '200', '201', '202', '203', '204', '205', '206',
8 '207', '208', '209', '211', # key presses
15 def __init__(self, rfh, wfh):
17 self.secret = 'AAAAAAAAAAAAAAAA'
21 # Initialise ourselves into a known state
24 self.wfh.write('echo off\n')
26 self.wfh.write('PING\n')
29 (code, _) = self.get_response()
32 def await_prompt(self):
39 if s == '': raise Exception
40 if s == '\n' or s == '\r':
43 if (s == '#' or s == '%') and state == 1: state = 2
44 if s == ' ' and state == 2:
48 if re.search('^[0-9a-fA-F]{4}$', prefix):
49 self.challenge = int(prefix, 16)
52 def get_response(self):
57 s = self.rfh.readline()
58 if s == '': return None
62 if code in asynchronous_responses:
63 self.handle_event(code, text)
68 def get_switches(self):
70 (code, text) = self.get_response()
72 return (False, code, text)
73 self.interpret_switches(text)
74 return (True, code, text)
76 def interpret_switches(self, text):
77 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
79 def handle_event(self, code, text):
81 self.events.append((DOOR, 0))
83 self.events.append((DOOR, 1))
85 self.events_append((SWITCH, None))
86 self.interpret_switches(text)
88 self.events.append((KEY, int(code[1:3])))
90 sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
92 def authed_message(self, message):
93 if self.challenge == None:
95 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
96 crc = do_crc(self.secret, crc)
97 crc = do_crc(message, crc)
98 return message+'|'+('%04x'%crc)
101 self.wfh.write('PING\n')
102 (code, string) = self.get_response()
103 return (code == '000', code, string)
105 def vend(self, item):
106 if not re.search('^[0-9][0-9]$', item):
107 return (False, 'Invalid item requested (%s)'%item)
108 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
109 (code, string) = self.get_response()
110 return (code == '100', code, string)
112 def beep(self, duration = None, synchronous = True):
114 if synchronous: msg += 'S'
116 if duration > 255: duration = 255
117 if duration < 1: duration = 1
118 msg += '%02x'%duration
119 self.wfh.write(msg+'\n')
120 (code, string) = self.get_response()
121 return (code == '500', code, string)
123 def silence(self, duration = None, synchronous = True):
125 if synchronous: msg += 'S'
127 if duration > 255: duration = 255
128 if duration < 1: duration = 1
129 msg += '%02x'%duration
130 self.wfh.write(msg+'\n')
131 (code, string) = self.get_response()
132 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
133 return (code == '500' or code == '501', code, string)
135 def display(self, string):
137 string = string[0:10]
138 self.wfh.write('D'+string+'\n')
139 (code, string) = self.get_response()
140 return (code == '300', code, string)
142 def next_event(self):
143 if len(self.events) > 0:
149 def wait_for_events(self, timeout = None):
150 (r, _, _) = select([self.rfh], [], [], timeout)
151 if not r: return False
155 (r, _, _) = select([self.rfh], [], [], 0)
156 if not r: return event_added