-
-asynchronous_responses = [ '400', '401', # door open/closed
- '610', # switches changed
- ]
-
-class VendingMachine:
- def __init__(self, rfh, wfh):
- self.rfh = rfh
- self.wfh = wfh
- self.wfh.write('\n')
- self.await_prompt()
-
- def await_prompt(self):
- self.wfh.flush()
- state = 0
- s = ''
- while state != 3:
- s = self.rfh.read(1)
- if s == '': raise Exception
- if s == '\n' and state == 0: state = 1
- if (s == '#' or s == '%') and state == 1: state = 2
- if s == ' ' and state == 2: state = 3
-
- def get_response(self):
- self.wfh.flush()
- s = ''
- while s == '':
- s = self.rfh.readline()
- if s == '': return None
- s = s.strip('\r\n')
- return (s[0:3], s[4:])
-
- def ping(self):
- self.wfh.write('PING\n')
- (code, string) = self.get_response()
- return code == '000'
-