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

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