3046360fe9deb58424853d769cc581483d7a95de
[uccvend-vendserver.git] / sql-edition / servers / VendingMachine.py
1 import re
2 from CRC import do_crc
3 from binascii import unhexlify
4
5 asynchronous_responses = [      '400', '401', # door open/closed
6                                 '610',        # switches changed
7                          ]
8
9 class VendingMachine:
10         def __init__(self, rfh, wfh):
11                 self.secret = 'AAAAAAAAAAAAAAAA'
12                 self.rfh = rfh
13                 self.wfh = wfh
14                 self.challenge = None
15                 # Initialise ourselves into a known state
16                 self.wfh.write('\n')
17                 self.await_prompt()
18                 self.wfh.write('echo off\n')
19                 self.await_prompt()
20                 self.wfh.write('PING\n')
21                 code = ''
22                 while code != '000':
23                         (code, _) = self.get_response()
24
25         def await_prompt(self):
26                 self.wfh.flush()
27                 state = 1
28                 prefix = ''
29                 s = ''
30                 while True:
31                         s = self.rfh.read(1)
32                         if s == '': raise Exception
33                         if s == '\n' or s == '\r':
34                                 state = 1
35                                 prefix = ''
36                         if (s == '#' or s == '%') and state == 1: state = 2
37                         if s == ' ' and state == 2:
38                                 if prefix == '':
39                                         self.challenge = None
40                                         return
41                                 if re.search('^[0-9a-fA-F]{4}$', prefix):
42                                         self.challenge = unhexlify(prefix)
43                                         return
44
45         def get_response(self):
46                 self.wfh.flush()
47                 while True:
48                         s = ''
49                         while s == '':
50                                 s = self.rfh.readline()
51                                 if s == '': return None
52                                 s = s.strip('\r\n')
53                         code = s[0:3]
54                         text = s[4:]
55                         if code in asynchronous_responses:
56                                 self.handle_event(code, text)
57                         else:
58                                 self.await_prompt()
59                                 return (code, text)
60
61         def handle_event(self, code, text):
62                 pass
63
64         def authed_message(self, message):
65                 if self.challenge == None:
66                         return message
67                 crc = do_crc('%c%c'%(self.challenge >> 8, self.challenge & 0xff))
68                 crc = do_crc(self.secret, crc)
69                 crc = do_crc(message, crc)
70                 return message+'|'+('%04x'%crc)
71
72         def ping(self):
73                 self.wfh.write('PING\n')
74                 (code, string) = self.get_response()
75                 return (code == '000', code, string)
76
77         def vend(self, item):
78                 if not re.search('^[0-9][0-9]$', item):
79                         return (False, 'Invalid item requested (%s)'%item)
80                 self.wfh.write(self.authed_message(('V%s\n'%item)+'\n'))
81                 (code, string) = self.get_response()
82                 return (code, string)
83
84         def beep(self, duration = None, synchronous = True):
85                 msg = 'B'
86                 if synchronous: msg += 'S'
87                 if duration != None:
88                         if duration > 255: duration = 255
89                         if duration < 1: duration = 1
90                         msg += '%02x'%duration
91                 self.wfh.write(msg+'\n')
92                 (code, string) = self.get_response()
93                 return (code == '500', code, string)
94
95         def silence(self, duration = None, synchronous = True):
96                 msg = 'C'
97                 if synchronous: msg += 'S'
98                 if duration != None:
99                         if duration > 255: duration = 255
100                         if duration < 1: duration = 1
101                         msg += '%02x'%duration
102                 self.wfh.write(msg+'\n')
103                 (code, string) = self.get_response()
104                 # FIXME: workaround a bug in rom W. should be just: return (code == '500', code, string)
105                 return (code == '500' or code == '501', code, string)
106
107         def display(self, string):
108                 if len(string) > 10:
109                         string = string[0:10]
110                 self.wfh.write('D'+string+'\n')
111                 (code, string) = self.get_response()
112                 return (code == '300', code, string)
113

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