4 from select import select
7 asynchronous_responses = [ '400', '401', # door open/closed
8 '610', # switches changed
9 '200', '201', '202', '203', '204', '205', '206',
10 '207', '208', '209', '211', # key presses
16 class VendingException(Exception): pass
19 def __init__(self, rfh, wfh):
21 self.secret = 'AAAAAAAAAAAAAAAA'
25 # Initialise ourselves into a known state
28 self.wfh.write('echo off\n')
30 self.wfh.write('PING\n')
33 code = self.get_response()[0]
36 def await_prompt(self):
45 raise VendingException('failed to read input from vending machine')
46 if s == '': raise VendingException('nothing read!')
47 if s == '\n' or s == '\r':
50 if (s == '#' or s == '%') and state == 1: state = 2
51 if s == ' ' and state == 2:
55 if re.search('^[0-9a-fA-F]{4}$', prefix):
56 self.challenge = int(prefix, 16)
59 def get_response(self, async = False):
64 s = self.rfh.readline()
66 raise VendingException('Input socket has closed!')
70 if code in asynchronous_responses:
71 self.handle_event(code, text)
77 def get_switches(self):
79 (code, text) = self.get_response()
81 return (False, code, text)
82 self.interpret_switches(text)
83 return (True, code, text)
85 def interpret_switches(self, text):
86 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
88 def handle_event(self, code, text):
90 self.events.append((DOOR, 0))
92 self.events.append((DOOR, 1))
94 self.events.append((SWITCH, None))
95 self.interpret_switches(text)
97 self.events.append((KEY, int(code[1:3])))
99 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
101 def authed_message(self, message):
102 if self.challenge == None:
104 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
105 crc = do_crc(self.secret, crc)
106 crc = do_crc(message, crc)
107 return message+'|'+('%04x'%crc)
110 self.wfh.write('PING\n')
111 (code, string) = self.get_response()
112 return (code == '000', code, string)
114 def vend(self, item):
115 if not re.search('^[0-9][0-9]$', item):
116 return (False, 'Invalid item requested (%s)'%item)
117 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
118 (code, string) = self.get_response()
119 return (code == '100', code, string)
121 def beep(self, duration = None, synchronous = True):
123 if synchronous: msg += 'S'
125 if duration > 255: duration = 255
126 if duration < 1: duration = 1
127 msg += '%02x'%duration
128 self.wfh.write(msg+'\n')
129 (code, string) = self.get_response()
130 return (code == '500', code, string)
132 def silence(self, duration = None, synchronous = True):
134 if synchronous: msg += 'S'
136 if duration > 255: duration = 255
137 if duration < 1: duration = 1
138 msg += '%02x'%duration
139 self.wfh.write(msg+'\n')
140 (code, string) = self.get_response()
141 return (code == '501', code, string)
143 def display(self, string):
145 string = string[0:10]
146 self.wfh.write('D'+string+'\n')
147 (code, string) = self.get_response()
148 return (code == '300', code, string)
150 def next_event(self, timeout = None):
151 # we don't want to buffer in the serial port, so we get all the events
153 if len(self.events) > 0: timeout = 0
155 (r, _, _) = select([self.rfh], [], [], timeout)
157 self.get_response(async = True)
161 if len(self.events) == 0: return None