/[anoncvs]/projects/dispense2/sql-edition/servers/VendingMachine.py


UCC Code Repository

Contents of /projects/dispense2/sql-edition/servers/VendingMachine.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations) (download) (as text)
Sun Aug 1 08:21:01 2004 UTC (16 years, 10 months ago) by cameron
Branch: MAIN
Changes since 1.11: +2 -3 lines
File MIME type: text/x-python
death to stderr and print statements!

1 dagobah 1.4 # vim:ts=4
2 dagobah 1.7 import re
3 dagobah 1.1 from CRC import do_crc
4 dagobah 1.2 from select import select
5 cameron 1.12 import socket, logging
6 dagobah 1.1
7     asynchronous_responses = [ '400', '401', # door open/closed
8     '610', # switches changed
9 dagobah 1.2 '200', '201', '202', '203', '204', '205', '206',
10     '207', '208', '209', '211', # key presses
11 dagobah 1.1 ]
12 dagobah 1.2 DOOR = 1
13     SWITCH = 2
14     KEY = 3
15 dagobah 1.1
16 cameron 1.8 class VendingException(Exception): pass
17    
18 dagobah 1.1 class VendingMachine:
19     def __init__(self, rfh, wfh):
20 dagobah 1.2 self.events = []
21 dagobah 1.1 self.secret = 'AAAAAAAAAAAAAAAA'
22     self.rfh = rfh
23     self.wfh = wfh
24     self.challenge = None
25     # Initialise ourselves into a known state
26     self.wfh.write('\n')
27     self.await_prompt()
28     self.wfh.write('echo off\n')
29     self.await_prompt()
30     self.wfh.write('PING\n')
31     code = ''
32     while code != '000':
33 cameron 1.8 code = self.get_response()[0]
34 dagobah 1.2 self.get_switches()
35 dagobah 1.1
36     def await_prompt(self):
37     self.wfh.flush()
38     state = 1
39     prefix = ''
40     s = ''
41     while True:
42 cameron 1.8 try:
43     s = self.rfh.read(1)
44     except socket.error:
45     raise VendingException('failed to read input from vending machine')
46     if s == '': raise VendingException('nothing read!')
47 dagobah 1.1 if s == '\n' or s == '\r':
48     state = 1
49     prefix = ''
50     if (s == '#' or s == '%') and state == 1: state = 2
51     if s == ' ' and state == 2:
52     if prefix == '':
53     self.challenge = None
54     return
55     if re.search('^[0-9a-fA-F]{4}$', prefix):
56 dagobah 1.2 self.challenge = int(prefix, 16)
57 dagobah 1.1 return
58    
59 dagobah 1.3 def get_response(self, async = False):
60 dagobah 1.1 self.wfh.flush()
61     while True:
62     s = ''
63     while s == '':
64     s = self.rfh.readline()
65 dagobah 1.9 if s == '':
66 cameron 1.10 raise VendingException('Input socket has closed!')
67 dagobah 1.1 s = s.strip('\r\n')
68     code = s[0:3]
69     text = s[4:]
70     if code in asynchronous_responses:
71     self.handle_event(code, text)
72 dagobah 1.3 if async: return None
73 dagobah 1.1 else:
74     self.await_prompt()
75     return (code, text)
76    
77 dagobah 1.2 def get_switches(self):
78     self.wfh.write('S\n')
79     (code, text) = self.get_response()
80     if code != '600':
81     return (False, code, text)
82     self.interpret_switches(text)
83     return (True, code, text)
84    
85     def interpret_switches(self, text):
86     self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
87    
88 dagobah 1.1 def handle_event(self, code, text):
89 dagobah 1.2 if code == '400':
90     self.events.append((DOOR, 0))
91     elif code == '401':
92     self.events.append((DOOR, 1))
93     elif code == '610':
94 dagobah 1.4 self.events.append((SWITCH, None))
95 dagobah 1.2 self.interpret_switches(text)
96     elif code[0] == '2':
97     self.events.append((KEY, int(code[1:3])))
98     else:
99 cameron 1.12 logging.warning('Unhandled event! (%s %s)\n'%(code,text))
100 dagobah 1.1
101     def authed_message(self, message):
102     if self.challenge == None:
103     return message
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)
108    
109     def ping(self):
110     self.wfh.write('PING\n')
111     (code, string) = self.get_response()
112     return (code == '000', code, string)
113    
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 dagobah 1.2 return (code == '100', code, string)
120 dagobah 1.1
121     def beep(self, duration = None, synchronous = True):
122     msg = 'B'
123     if synchronous: msg += 'S'
124     if duration != None:
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)
131    
132     def silence(self, duration = None, synchronous = True):
133     msg = 'C'
134     if synchronous: msg += 'S'
135     if duration != None:
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 dagobah 1.6 return (code == '501', code, string)
142 dagobah 1.1
143     def display(self, string):
144     if len(string) > 10:
145     string = string[0:10]
146     self.wfh.write('D'+string+'\n')
147     (code, string) = self.get_response()
148     return (code == '300', code, string)
149    
150 dagobah 1.4 def next_event(self, timeout = None):
151     # we don't want to buffer in the serial port, so we get all the events
152     # we can ASAP.
153     if len(self.events) > 0: timeout = 0
154 dagobah 1.2 while True:
155 dagobah 1.4 (r, _, _) = select([self.rfh], [], [], timeout)
156     if r:
157     self.get_response(async = True)
158 dagobah 1.5 timeout = 0
159 dagobah 1.4 else:
160     break
161     if len(self.events) == 0: return None
162     ret = self.events[0]
163     del self.events[0]
164     return ret

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26