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('nothing read!')
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, async = False):
57 s = self.rfh.readline()
58 if s == '': return None
62 if code in asynchronous_responses:
63 self.handle_event(code, text)
69 def get_switches(self):
71 (code, text) = self.get_response()
73 return (False, code, text)
74 self.interpret_switches(text)
75 return (True, code, text)
77 def interpret_switches(self, text):
78 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
80 def handle_event(self, code, text):
82 self.events.append((DOOR, 0))
84 self.events.append((DOOR, 1))
86 self.events_append((SWITCH, None))
87 self.interpret_switches(text)
89 self.events.append((KEY, int(code[1:3])))
91 sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
93 def authed_message(self, message):
94 if self.challenge == None:
96 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
97 crc = do_crc(self.secret, crc)
98 crc = do_crc(message, crc)
99 return message+'|'+('%04x'%crc)
102 self.wfh.write('PING\n')
103 (code, string) = self.get_response()
104 return (code == '000', code, string)
106 def vend(self, item):
107 if not re.search('^[0-9][0-9]$', item):
108 return (False, 'Invalid item requested (%s)'%item)
109 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
110 (code, string) = self.get_response()
111 return (code == '100', code, string)
113 def beep(self, duration = None, synchronous = True):
115 if synchronous: msg += 'S'
117 if duration > 255: duration = 255
118 if duration < 1: duration = 1
119 msg += '%02x'%duration
120 self.wfh.write(msg+'\n')
121 (code, string) = self.get_response()
122 return (code == '500', code, string)
124 def silence(self, duration = None, synchronous = True):
126 if synchronous: msg += 'S'
128 if duration > 255: duration = 255
129 if duration < 1: duration = 1
130 msg += '%02x'%duration
131 self.wfh.write(msg+'\n')
132 (code, string) = self.get_response()
133 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
134 return (code == '500' or code == '501', code, string)
136 def display(self, string):
138 string = string[0:10]
139 self.wfh.write('D'+string+'\n')
140 (code, string) = self.get_response()
141 return (code == '300', code, string)
143 def next_event(self):
144 if len(self.events) > 0:
150 def wait_for_events(self, timeout = None):
151 if self.events: return True
152 (r, _, _) = select([self.rfh], [], [], timeout)
153 if not r: return False
156 self.get_response(async = True)
157 (r, _, _) = select([self.rfh], [], [], 0)
158 if not r: return event_added