From 9f0dc95f27bd5cedf601bae506d0c40343be40ee Mon Sep 17 00:00:00 2001 From: Bernard Blackham Date: Fri, 25 Jun 2004 14:29:09 +0000 Subject: [PATCH] Initial allowance of unexpected responses --- sql-edition/servers/VendServer.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index 57a2682..9221a4d 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -5,6 +5,10 @@ from popen2 import popen2 from pyPgSQL import PgSQL from LATClient import LATClient +asynchronous_responses = [ '400', '401', # door open/closed + '610', # switches changed + ] + class VendingMachine: def __init__(self, rfh, wfh): self.rfh = rfh @@ -17,24 +21,26 @@ class VendingMachine: 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 == '#' and state == 1: state = 2 - if s == ' ' and state == 2: 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 + 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') - return self.get_response() + (code, string) = self.get_response() + return code == '000' + if __name__ == '__main__': # Open vending machine via LAT -- 2.20.1