4 from select import select
6 asynchronous_responses = [ '400', '401', # door open/closed
7 '610', # switches changed
8 '200', '201', '202', '203', '204', '205', '206',
9 '207', '208', '209', '211', # key presses
16 def __init__(self, rfh, wfh):
18 self.secret = 'AAAAAAAAAAAAAAAA'
22 # Initialise ourselves into a known state
25 self.wfh.write('echo off\n')
27 self.wfh.write('PING\n')
30 (code, _) = self.get_response()
33 def await_prompt(self):
40 if s == '': raise Exception('nothing read!')
41 if s == '\n' or s == '\r':
44 if (s == '#' or s == '%') and state == 1: state = 2
45 if s == ' ' and state == 2:
49 if re.search('^[0-9a-fA-F]{4}$', prefix):
50 self.challenge = int(prefix, 16)
53 def get_response(self, async = False):
58 s = self.rfh.readline()
59 if s == '': return None
63 if code in asynchronous_responses:
64 self.handle_event(code, text)
70 def get_switches(self):
72 (code, text) = self.get_response()
74 return (False, code, text)
75 self.interpret_switches(text)
76 return (True, code, text)
78 def interpret_switches(self, text):
79 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
81 def handle_event(self, code, text):
83 self.events.append((DOOR, 0))
85 self.events.append((DOOR, 1))
87 self.events.append((SWITCH, None))
88 self.interpret_switches(text)
90 self.events.append((KEY, int(code[1:3])))
92 sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
94 def authed_message(self, message):
95 if self.challenge == None:
97 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
98 crc = do_crc(self.secret, crc)
99 crc = do_crc(message, crc)
100 return message+'|'+('%04x'%crc)
103 self.wfh.write('PING\n')
104 (code, string) = self.get_response()
105 return (code == '000', code, string)
107 def vend(self, item):
108 if not re.search('^[0-9][0-9]$', item):
109 return (False, 'Invalid item requested (%s)'%item)
110 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
111 (code, string) = self.get_response()
112 return (code == '100', code, string)
114 def beep(self, duration = None, synchronous = True):
116 if synchronous: msg += 'S'
118 if duration > 255: duration = 255
119 if duration < 1: duration = 1
120 msg += '%02x'%duration
121 self.wfh.write(msg+'\n')
122 (code, string) = self.get_response()
123 return (code == '500', code, string)
125 def silence(self, duration = None, synchronous = True):
127 if synchronous: msg += 'S'
129 if duration > 255: duration = 255
130 if duration < 1: duration = 1
131 msg += '%02x'%duration
132 self.wfh.write(msg+'\n')
133 (code, string) = self.get_response()
134 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
135 return (code == '500' or code == '501', code, string)
137 def display(self, string):
139 string = string[0:10]
140 self.wfh.write('D'+string+'\n')
141 (code, string) = self.get_response()
142 return (code == '300', code, string)
144 def next_event(self, timeout = None):
145 # we don't want to buffer in the serial port, so we get all the events
147 if len(self.events) > 0: timeout = 0
149 (r, _, _) = select([self.rfh], [], [], timeout)
151 self.get_response(async = True)
154 if len(self.events) == 0: return None