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
SWITCH = 2
KEY = 3
TICK = 4
+MIFARE = 5
class VendingException(Exception): pass
while code != '000':
code = self.get_response()[0]
self.get_switches()
+ self.mifare = MIFAREClient()
def await_prompt(self):
self.wfh.flush()
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]