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, use_mifare):
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]
41 self.mifare = MIFAREClient()
42 self.mifare_timeout = 0
46 def await_prompt(self):
52 # mtearle - vending machine was dying wait for a response from
53 # the hardware, suspect it was missing characters
55 # fixed by migration to pyserial - but future good place to start
60 raise VendingException('failed to read input from vending machine')
61 if s == '': raise VendingException('nothing read!')
62 if (s != '#' and s != '%') and state == 1: prefix += s
63 if s == '\n' or s == '\r':
66 if (s == '#' or s == '%') and state == 1: state = 2
67 if s == ' ' and state == 2:
71 if re.search('^[0-9a-fA-F]{4}$', prefix):
72 self.challenge = int(prefix, 16)
75 def get_response(self, async = False):
80 s = self.rfh.readline()
82 raise VendingException('Input socket has closed!')
86 if code in asynchronous_responses:
87 self.handle_event(code, text)
93 def get_switches(self):
95 (code, text) = self.get_response()
97 return (False, code, text)
98 self.interpret_switches(text)
99 return (True, code, text)
101 def interpret_switches(self, text):
102 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
104 def handle_event(self, code, text):
106 self.events.append((DOOR, 1))
108 self.events.append((DOOR, 0))
110 # NOP this. Nothing handles this yet.
111 #self.events.append((SWITCH, None))
112 self.interpret_switches(text)
114 self.events.append((KEY, int(code[1:3])))
116 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
118 def authed_message(self, message):
119 print 'self.challenge = %04x' % self.challenge
120 if self.challenge == None:
122 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
123 crc = do_crc(self.secret, crc)
124 crc = do_crc(message, crc)
125 print 'output = "%s|%04x"' % (message, crc)
126 return message+'|'+('%04x'%crc)
129 self.wfh.write('PING\n')
130 (code, string) = self.get_response()
131 return (code == '000', code, string)
133 def vend(self, item):
134 if not re.search('^[0-9][0-9]$', item):
135 return (False, 'Invalid item requested (%s)'%item)
136 self.wfh.write(self.authed_message(('V%s'%item))+'\n')
137 (code, string) = self.get_response()
138 return (code == '100', code, string)
140 def beep(self, duration = None, synchronous = True):
142 if synchronous: msg += 'S'
144 if duration > 255: duration = 255
145 if duration < 1: duration = 1
146 msg += '%02x'%duration
147 self.wfh.write(msg+'\n')
148 (code, string) = self.get_response()
149 return (code == '500', code, string)
151 def silence(self, duration = None, synchronous = True):
153 if synchronous: msg += 'S'
155 if duration > 255: duration = 255
156 if duration < 1: duration = 1
157 msg += '%02x'%duration
158 self.wfh.write(msg+'\n')
159 (code, string) = self.get_response()
160 return (code == '501', code, string)
162 def display(self, string):
163 # display first ten characters of string, left aligned
164 self.wfh.write('D%-10.10s\n' % string)
166 (code, string) = self.get_response()
167 return (code == '300', code, string)
169 def next_event(self, timeout = None):
170 # we don't want to buffer in the serial port, so we get all the events
173 # Never have no timeout...
174 if timeout == None: timeout = 60*60*24*365
176 # Make sure we go through the loop at least once.
177 if timeout < 0: timeout = 0
180 this_timeout = min(timeout, 0.2)
181 timeout -= this_timeout
183 (r, _, _) = select([self.rfh], [], [], this_timeout)
185 self.get_response(async = True)
190 if now > self.mifare_timeout:
191 self.mifare_timeout = now + 0.5
192 mifare_uid = self.mifare.get_card_id()
193 if mifare_uid != None:
194 logging.info('Got MIFARE card id %s'%(repr(mifare_uid)))
195 self.events.append((MIFARE, mifare_uid))
200 if len(self.events) == 0: return (TICK, time())