/[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.8 - (hide annotations) (download) (as text)
Sat Jul 31 09:39:34 2004 UTC (16 years, 10 months ago) by cameron
Branch: MAIN
Changes since 1.7: +9 -3 lines
File MIME type: text/x-python
clean up code a little, in preparation for automatically rebooting DEC server when it dies

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 dagobah 1.1
6     asynchronous_responses = [ '400', '401', # door open/closed
7     '610', # switches changed
8 dagobah 1.2 '200', '201', '202', '203', '204', '205', '206',
9     '207', '208', '209', '211', # key presses
10 dagobah 1.1 ]
11 dagobah 1.2 DOOR = 1
12     SWITCH = 2
13     KEY = 3
14 dagobah 1.1
15 cameron 1.8 class VendingException(Exception): pass
16    
17 dagobah 1.1 class VendingMachine:
18     def __init__(self, rfh, wfh):
19 dagobah 1.2 self.events = []
20 dagobah 1.1 self.secret = 'AAAAAAAAAAAAAAAA'
21     self.rfh = rfh
22     self.wfh = wfh
23     self.challenge = None
24     # Initialise ourselves into a known state
25     self.wfh.write('\n')
26     self.await_prompt()
27     self.wfh.write('echo off\n')
28     self.await_prompt()
29     self.wfh.write('PING\n')
30     code = ''
31     while code != '000':
32 cameron 1.8 code = self.get_response()[0]
33 dagobah 1.2 self.get_switches()
34 dagobah 1.1
35     def await_prompt(self):
36     self.wfh.flush()
37     state = 1
38     prefix = ''
39     s = ''
40     while True:
41 cameron 1.8 try:
42     s = self.rfh.read(1)
43     except socket.error:
44     print "Blah, seems DEC server has fallen over"
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     if s == '': return None
66     s = s.strip('\r\n')
67     code = s[0:3]
68     text = s[4:]
69     if code in asynchronous_responses:
70     self.handle_event(code, text)
71 dagobah 1.3 if async: return None
72 dagobah 1.1 else:
73     self.await_prompt()
74     return (code, text)
75    
76 dagobah 1.2 def get_switches(self):
77     self.wfh.write('S\n')
78     (code, text) = self.get_response()
79     if code != '600':
80     return (False, code, text)
81     self.interpret_switches(text)
82     return (True, code, text)
83    
84     def interpret_switches(self, text):
85     self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
86    
87 dagobah 1.1 def handle_event(self, code, text):
88 dagobah 1.2 if code == '400':
89     self.events.append((DOOR, 0))
90     elif code == '401':
91     self.events.append((DOOR, 1))
92     elif code == '610':
93 dagobah 1.4 self.events.append((SWITCH, None))
94 dagobah 1.2 self.interpret_switches(text)
95     elif code[0] == '2':
96     self.events.append((KEY, int(code[1:3])))
97     else:
98     sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
99 dagobah 1.1
100     def authed_message(self, message):
101     if self.challenge == None:
102     return message
103     crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
104     crc = do_crc(self.secret, crc)
105     crc = do_crc(message, crc)
106     return message+'|'+('%04x'%crc)
107    
108     def ping(self):
109     self.wfh.write('PING\n')
110     (code, string) = self.get_response()
111     return (code == '000', code, string)
112    
113     def vend(self, item):
114     if not re.search('^[0-9][0-9]$', item):
115     return (False, 'Invalid item requested (%s)'%item)
116     self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
117     (code, string) = self.get_response()
118 dagobah 1.2 return (code == '100', code, string)
119 dagobah 1.1
120     def beep(self, duration = None, synchronous = True):
121     msg = 'B'
122     if synchronous: msg += 'S'
123     if duration != None:
124     if duration > 255: duration = 255
125     if duration < 1: duration = 1
126     msg += '%02x'%duration
127     self.wfh.write(msg+'\n')
128     (code, string) = self.get_response()
129     return (code == '500', code, string)
130    
131     def silence(self, duration = None, synchronous = True):
132     msg = 'C'
133     if synchronous: msg += 'S'
134     if duration != None:
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 dagobah 1.6 return (code == '501', code, string)
141 dagobah 1.1
142     def display(self, string):
143     if len(string) > 10:
144     string = string[0:10]
145     self.wfh.write('D'+string+'\n')
146     (code, string) = self.get_response()
147     return (code == '300', code, string)
148    
149 dagobah 1.4 def next_event(self, timeout = None):
150     # we don't want to buffer in the serial port, so we get all the events
151     # we can ASAP.
152     if len(self.events) > 0: timeout = 0
153 dagobah 1.2 while True:
154 dagobah 1.4 (r, _, _) = select([self.rfh], [], [], timeout)
155     if r:
156     self.get_response(async = True)
157 dagobah 1.5 timeout = 0
158 dagobah 1.4 else:
159     break
160     if len(self.events) == 0: return None
161     ret = self.events[0]
162     del self.events[0]
163     return ret

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26