Work from tonight:
[uccvend-vendserver.git] / sql-edition / servers / VendingMachine.py
index d73eb4b..aca860e 100644 (file)
@@ -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]

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