4 from select import select
6 from time import time, sleep
8 asynchronous_responses = [ '400', '401', # door open/closed
9 '610', # switches changed
10 '200', '201', '202', '203', '204', '205', '206',
11 '207', '208', '209', '211', # key presses
18 class VendingException(Exception): pass
21 def __init__(self, rfh, wfh):
24 self.secret = 'SN4CKZ0RZZZZZZZZ'
28 # Initialise ourselves into a known state
31 self.wfh.write('echo off\n')
33 self.wfh.write('PING\n')
36 code = self.get_response()[0]
39 def await_prompt(self):
45 # mtearle - vending machine was dying wait for a response from
46 # the hardware, suspect it was missing characters
48 # fixed by migration to pyserial - but future good place to start
53 raise VendingException('failed to read input from vending machine')
54 if s == '': raise VendingException('nothing read!')
55 if (s != '#' and s != '%') and state == 1: prefix += s
56 if s == '\n' or s == '\r':
59 if (s == '#' or s == '%') and state == 1: state = 2
60 if s == ' ' and state == 2:
64 if re.search('^[0-9a-fA-F]{4}$', prefix):
65 self.challenge = int(prefix, 16)
68 def get_response(self, async = False):
73 s = self.rfh.readline()
75 raise VendingException('Input socket has closed!')
79 if code in asynchronous_responses:
80 self.handle_event(code, text)
86 def get_switches(self):
88 (code, text) = self.get_response()
90 return (False, code, text)
91 self.interpret_switches(text)
92 return (True, code, text)
94 def interpret_switches(self, text):
95 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
97 def handle_event(self, code, text):
99 self.events.append((DOOR, 1))
101 self.events.append((DOOR, 0))
103 # NOP this. Nothing handles this yet.
104 #self.events.append((SWITCH, None))
105 self.interpret_switches(text)
107 self.events.append((KEY, int(code[1:3])))
109 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
111 def authed_message(self, message):
112 if self.challenge == None:
114 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
115 crc = do_crc(self.secret, crc)
116 crc = do_crc(message, crc)
117 return message+'|'+('%04x'%crc)
120 self.wfh.write('PING\n')
121 (code, string) = self.get_response()
122 return (code == '000', code, string)
124 def vend(self, item):
125 if not re.search('^[0-9][0-9]$', item):
126 return (False, 'Invalid item requested (%s)'%item)
127 self.wfh.write(self.authed_message(('V%s'%item))+'\n')
128 (code, string) = self.get_response()
129 return (code == '100', code, string)
131 def beep(self, duration = None, synchronous = True):
133 if synchronous: msg += 'S'
135 if duration > 255: duration = 255
136 if duration < 1: duration = 1
137 msg += '%02x'%duration
138 self.wfh.write(msg+'\n')
139 (code, string) = self.get_response()
140 return (code == '500', code, string)
142 def silence(self, duration = None, synchronous = True):
144 if synchronous: msg += 'S'
146 if duration > 255: duration = 255
147 if duration < 1: duration = 1
148 msg += '%02x'%duration
149 self.wfh.write(msg+'\n')
150 (code, string) = self.get_response()
151 return (code == '501', code, string)
153 def display(self, string):
155 string = string[0:10]
156 string = re.sub('(.)\.', lambda match: '.'+match.group(1), string)
157 self.wfh.write('D'+string+'\n')
158 (code, string) = self.get_response()
159 return (code == '300', code, string)
161 def next_event(self, timeout = None):
162 # we don't want to buffer in the serial port, so we get all the events
164 if timeout < 0: timeout = 0
165 if len(self.events) > 0: timeout = 0
167 (r, _, _) = select([self.rfh], [], [], timeout)
169 self.get_response(async = True)
173 if len(self.events) == 0: return (TICK, time())