4 from select import select
6 from time import time, sleep
7 from MIFAREClient import MIFAREClient
9 asynchronous_responses = [ '400', '401', # door open/closed
10 '610', # switches changed
11 '200', '201', '202', '203', '204', '205', '206',
12 '207', '208', '209', '211', # key presses
20 class VendingException(Exception): pass
23 def __init__(self, rfh, wfh):
26 self.secret = 'SN4CKZ0RZZZZZZZZ'
30 # Initialise ourselves into a known state
33 self.wfh.write('echo off\n')
35 self.wfh.write('PING\n')
38 code = self.get_response()[0]
40 self.mifare = MIFAREClient()
42 def await_prompt(self):
48 # mtearle - vending machine was dying wait for a response from
49 # the hardware, suspect it was missing characters
51 # fixed by migration to pyserial - but future good place to start
56 raise VendingException('failed to read input from vending machine')
57 if s == '': raise VendingException('nothing read!')
58 if (s != '#' and s != '%') and state == 1: prefix += s
59 if s == '\n' or s == '\r':
62 if (s == '#' or s == '%') and state == 1: state = 2
63 if s == ' ' and state == 2:
67 if re.search('^[0-9a-fA-F]{4}$', prefix):
68 self.challenge = int(prefix, 16)
71 def get_response(self, async = False):
76 s = self.rfh.readline()
78 raise VendingException('Input socket has closed!')
82 if code in asynchronous_responses:
83 self.handle_event(code, text)
89 def get_switches(self):
91 (code, text) = self.get_response()
93 return (False, code, text)
94 self.interpret_switches(text)
95 return (True, code, text)
97 def interpret_switches(self, text):
98 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
100 def handle_event(self, code, text):
102 self.events.append((DOOR, 1))
104 self.events.append((DOOR, 0))
106 # NOP this. Nothing handles this yet.
107 #self.events.append((SWITCH, None))
108 self.interpret_switches(text)
110 self.events.append((KEY, int(code[1:3])))
112 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
114 def authed_message(self, message):
115 if self.challenge == None:
117 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
118 crc = do_crc(self.secret, crc)
119 crc = do_crc(message, crc)
120 return message+'|'+('%04x'%crc)
123 self.wfh.write('PING\n')
124 (code, string) = self.get_response()
125 return (code == '000', code, string)
127 def vend(self, item):
128 if not re.search('^[0-9][0-9]$', item):
129 return (False, 'Invalid item requested (%s)'%item)
130 self.wfh.write(self.authed_message(('V%s'%item))+'\n')
131 (code, string) = self.get_response()
132 return (code == '100', code, string)
134 def beep(self, duration = None, synchronous = True):
136 if synchronous: msg += 'S'
138 if duration > 255: duration = 255
139 if duration < 1: duration = 1
140 msg += '%02x'%duration
141 self.wfh.write(msg+'\n')
142 (code, string) = self.get_response()
143 return (code == '500', code, string)
145 def silence(self, duration = None, synchronous = True):
147 if synchronous: msg += 'S'
149 if duration > 255: duration = 255
150 if duration < 1: duration = 1
151 msg += '%02x'%duration
152 self.wfh.write(msg+'\n')
153 (code, string) = self.get_response()
154 return (code == '501', code, string)
156 def display(self, string):
158 string = string[0:10]
159 string = re.sub('(.)\.', lambda match: '.'+match.group(1), string)
160 self.wfh.write('D'+string+'\n')
161 (code, string) = self.get_response()
162 return (code == '300', code, string)
164 def next_event(self, timeout = None):
165 # we don't want to buffer in the serial port, so we get all the events
168 # Never have no timeout...
169 if timeout == None: timeout = 60*60*24*365
171 # Make sure we go through the loop at least once.
172 if timeout <= 0: timeout = 0.01
175 this_timeout = min(timeout, 0.2)
176 timeout -= this_timeout
178 (r, _, _) = select([self.rfh], [], [], this_timeout)
180 self.get_response(async = True)
184 mifare_uid = self.mifare.get_card_uid()
187 if mifare_uid != None:
188 logging.info('Got MIFARE uid %s'%(str(mifare_uid)))
189 self.events.append((MIFARE, mifare_uid))
192 if len(self.events) == 0: return (TICK, time())