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):
23 self.secret = 'AAAAAAAAAAAAAAAA'
27 # Initialise ourselves into a known state
30 self.wfh.write('echo off\n')
32 self.wfh.write('PING\n')
35 code = self.get_response()[0]
38 def await_prompt(self):
47 raise VendingException('failed to read input from vending machine')
48 if s == '': raise VendingException('nothing read!')
49 if (s != '#' and s != '%') and state == 1: prefix += s
50 if s == '\n' or s == '\r':
53 if (s == '#' or s == '%') and state == 1: state = 2
54 if s == ' ' and state == 2:
58 if re.search('^[0-9a-fA-F]{4}$', prefix):
59 self.challenge = int(prefix, 16)
62 def get_response(self, async = False):
67 s = self.rfh.readline()
69 raise VendingException('Input socket has closed!')
73 if code in asynchronous_responses:
74 self.handle_event(code, text)
80 def get_switches(self):
82 (code, text) = self.get_response()
84 return (False, code, text)
85 self.interpret_switches(text)
86 return (True, code, text)
88 def interpret_switches(self, text):
89 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
91 def handle_event(self, code, text):
93 self.events.append((DOOR, 1))
95 self.events.append((DOOR, 0))
97 self.events.append((SWITCH, None))
98 self.interpret_switches(text)
100 self.events.append((KEY, int(code[1:3])))
102 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
104 def authed_message(self, message):
105 if self.challenge == None:
107 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
108 crc = do_crc(self.secret, crc)
109 crc = do_crc(message, crc)
110 return message+'|'+('%04x'%crc)
113 self.wfh.write('PING\n')
114 (code, string) = self.get_response()
115 return (code == '000', code, string)
117 def vend(self, item):
118 if not re.search('^[0-9][0-9]$', item):
119 return (False, 'Invalid item requested (%s)'%item)
120 self.wfh.write(self.authed_message(('V%s'%item))+'\n')
121 (code, string) = self.get_response()
122 return (code == '100', code, string)
124 def beep(self, duration = None, synchronous = True):
126 if synchronous: msg += 'S'
128 if duration > 255: duration = 255
129 if duration < 1: duration = 1
130 msg += '%02x'%duration
131 self.wfh.write(msg+'\n')
132 (code, string) = self.get_response()
133 return (code == '500', code, string)
135 def silence(self, duration = None, synchronous = True):
137 if synchronous: msg += 'S'
139 if duration > 255: duration = 255
140 if duration < 1: duration = 1
141 msg += '%02x'%duration
142 self.wfh.write(msg+'\n')
143 (code, string) = self.get_response()
144 return (code == '501', code, string)
146 def display(self, string):
148 string = string[0:10]
149 string = re.sub('(.)\.', lambda match: '.'+match.group(1), string)
150 self.wfh.write('D'+string+'\n')
151 (code, string) = self.get_response()
152 return (code == '300', code, string)
154 def next_event(self, timeout = None):
155 # we don't want to buffer in the serial port, so we get all the events
157 if len(self.events) > 0: timeout = 0
159 (r, _, _) = select([self.rfh], [], [], timeout)
161 self.get_response(async = True)
165 if len(self.events) == 0: return (TICK, time())