X-Git-Url: https://git.ucc.asn.au/?p=uccvend-vendserver.git;a=blobdiff_plain;f=sql-edition%2Fservers%2FVendingMachine.py;h=aca860e11152fda781953aabacb253bf2b1d1397;hp=d73eb4beb5234ba96c27cee0fafd22cd96156a95;hb=4b4e4c8deae85f1bb0c49c51fea49a3e33127b44;hpb=912fe3f190d0fbffaa6bde6a499ab60aa0db89e4 diff --git a/sql-edition/servers/VendingMachine.py b/sql-edition/servers/VendingMachine.py index d73eb4b..aca860e 100644 --- a/sql-edition/servers/VendingMachine.py +++ b/sql-edition/servers/VendingMachine.py @@ -4,6 +4,7 @@ from CRC import do_crc from select import select import socket, logging from time import time, sleep +from MIFAREClient import MIFAREClient asynchronous_responses = [ '400', '401', # door open/closed '610', # switches changed @@ -14,6 +15,7 @@ DOOR = 1 SWITCH = 2 KEY = 3 TICK = 4 +MIFARE = 5 class VendingException(Exception): pass @@ -35,6 +37,7 @@ class VendingMachine: while code != '000': code = self.get_response()[0] self.get_switches() + self.mifare = MIFAREClient() def await_prompt(self): self.wfh.flush() @@ -161,15 +164,31 @@ class VendingMachine: def next_event(self, timeout = None): # we don't want to buffer in the serial port, so we get all the events # we can ASAP. - if timeout < 0: timeout = 0 - if len(self.events) > 0: timeout = 0 - while True: - (r, _, _) = select([self.rfh], [], [], timeout) + + # Never have no timeout... + if timeout == None: timeout = 60*60*24*365 + + # Make sure we go through the loop at least once. + if timeout <= 0: timeout = 0.01 + + while timeout > 0: + this_timeout = min(timeout, 0.2) + timeout -= this_timeout + + (r, _, _) = select([self.rfh], [], [], this_timeout) if r: self.get_response(async = True) timeout = 0 - else: - break + + try: + mifare_uid = self.mifare.get_card_uid() + except ValueError: + mifare_uid = None + if mifare_uid != None: + logging.info('Got MIFARE uid %s'%(str(mifare_uid))) + self.events.append((MIFARE, mifare_uid)) + timeout = 0 + if len(self.events) == 0: return (TICK, time()) ret = self.events[0] del self.events[0]