2574c6d36a23ffc9c82adb62200df5d30ebbae3c
[uccvend-vendserver.git] / sql-edition / servers / VendingMachine.py
1 import re, pg
2 from CRC import do_crc
3 from select import select
4
5 asynchronous_responses = [      '400', '401', # door open/closed
6                                 '610',        # switches changed
7                                 '200', '201', '202', '203', '204', '205', '206',
8                                 '207', '208', '209', '211', # key presses
9                          ]
10 DOOR = 1
11 SWITCH = 2
12 KEY = 3
13
14 class VendingMachine:
15         def __init__(self, rfh, wfh):
16                 self.events = []
17                 self.secret = 'AAAAAAAAAAAAAAAA'
18                 self.rfh = rfh
19                 self.wfh = wfh
20                 self.challenge = None
21                 # Initialise ourselves into a known state
22                 self.wfh.write('\n')
23                 self.await_prompt()
24                 self.wfh.write('echo off\n')
25                 self.await_prompt()
26                 self.wfh.write('PING\n')
27                 code = ''
28                 while code != '000':
29                         (code, _) = self.get_response()
30                 self.get_switches()
31
32         def await_prompt(self):
33                 self.wfh.flush()
34                 state = 1
35                 prefix = ''
36                 s = ''
37                 while True:
38                         s = self.rfh.read(1)
39                         if s == '': raise Exception
40                         if s == '\n' or s == '\r':
41                                 state = 1
42                                 prefix = ''
43                         if (s == '#' or s == '%') and state == 1: state = 2
44                         if s == ' ' and state == 2:
45                                 if prefix == '':
46                                         self.challenge = None
47                                         return
48                                 if re.search('^[0-9a-fA-F]{4}$', prefix):
49                                         self.challenge = int(prefix, 16)
50                                         return
51
52         def get_response(self):
53                 self.wfh.flush()
54                 while True:
55                         s = ''
56                         while s == '':
57                                 s = self.rfh.readline()
58                                 if s == '': return None
59                                 s = s.strip('\r\n')
60                         code = s[0:3]
61                         text = s[4:]
62                         if code in asynchronous_responses:
63                                 self.handle_event(code, text)
64                         else:
65                                 self.await_prompt()
66                                 return (code, text)
67
68         def get_switches(self):
69                 self.wfh.write('S\n')
70                 (code, text) = self.get_response()
71                 if code != '600':
72                         return (False, code, text)
73                 self.interpret_switches(text)
74                 return (True, code, text)
75
76         def interpret_switches(self, text):
77                 self.switches = (int(text[0:2], 16) << 8) | int(text[3:5], 16)
78
79         def handle_event(self, code, text):
80                 if code == '400':
81                         self.events.append((DOOR, 0))
82                 elif code == '401':
83                         self.events.append((DOOR, 1))
84                 elif code == '610':
85                         self.events_append((SWITCH, None))
86                         self.interpret_switches(text)
87                 elif code[0] == '2':
88                         self.events.append((KEY, int(code[1:3])))
89                 else:
90                         sys.stderr.write('WARNING: Unhandled event! (%s %s)\n'%(code,text))
91
92         def authed_message(self, message):
93                 if self.challenge == None:
94                         return message
95                 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
96                 crc = do_crc(self.secret, crc)
97                 crc = do_crc(message, crc)
98                 return message+'|'+('%04x'%crc)
99
100         def ping(self):
101                 self.wfh.write('PING\n')
102                 (code, string) = self.get_response()
103                 return (code == '000', code, string)
104
105         def vend(self, item):
106                 if not re.search('^[0-9][0-9]$', item):
107                         return (False, 'Invalid item requested (%s)'%item)
108                 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
109                 (code, string) = self.get_response()
110                 return (code == '100', code, string)
111
112         def beep(self, duration = None, synchronous = True):
113                 msg = 'B'
114                 if synchronous: msg += 'S'
115                 if duration != None:
116                         if duration > 255: duration = 255
117                         if duration < 1: duration = 1
118                         msg += '%02x'%duration
119                 self.wfh.write(msg+'\n')
120                 (code, string) = self.get_response()
121                 return (code == '500', code, string)
122
123         def silence(self, duration = None, synchronous = True):
124                 msg = 'C'
125                 if synchronous: msg += 'S'
126                 if duration != None:
127                         if duration > 255: duration = 255
128                         if duration < 1: duration = 1
129                         msg += '%02x'%duration
130                 self.wfh.write(msg+'\n')
131                 (code, string) = self.get_response()
132                 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
133                 return (code == '500' or code == '501', code, string)
134
135         def display(self, string):
136                 if len(string) > 10:
137                         string = string[0:10]
138                 self.wfh.write('D'+string+'\n')
139                 (code, string) = self.get_response()
140                 return (code == '300', code, string)
141
142         def next_event(self):
143                 if len(self.events) > 0:
144                         ret = self.events[0]
145                         del self.events[0]
146                         return ret
147                 return None
148
149         def wait_for_events(self, timeout = None):
150                 (r, _, _) = select([self.rfh], [], [], timeout)
151                 if not r: return False
152                 event_added = False
153                 while True:
154                     r.get_response()
155                     (r, _, _) = select([self.rfh], [], [], 0)
156                     if not r: return event_added

UCC git Repository :: git.ucc.asn.au