/[anoncvs]/projects/dispense2/sql-edition/servers/VendingMachine.py


UCC Code Repository

Diff of /projects/dispense2/sql-edition/servers/VendingMachine.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.20 by root, Sun Jan 6 06:11:33 2008 UTC revision 1.21 by dagobah, Fri Feb 29 13:25:50 2008 UTC
# Line 4  from CRC import do_crc Line 4  from CRC import do_crc
4  from select import select  from select import select
5  import socket, logging  import socket, logging
6  from time import time, sleep  from time import time, sleep
7    from MIFAREClient import MIFAREClient
8    
9  asynchronous_responses = [      '400', '401', # door open/closed  asynchronous_responses = [      '400', '401', # door open/closed
10                                  '610',        # switches changed                                  '610',        # switches changed
# Line 14  DOOR = 1 Line 15  DOOR = 1
15  SWITCH = 2  SWITCH = 2
16  KEY = 3  KEY = 3
17  TICK = 4  TICK = 4
18    MIFARE = 5
19    
20  class VendingException(Exception): pass  class VendingException(Exception): pass
21    
# Line 35  class VendingMachine: Line 37  class VendingMachine:
37                  while code != '000':                  while code != '000':
38                          code = self.get_response()[0]                          code = self.get_response()[0]
39                  self.get_switches()                  self.get_switches()
40                    self.mifare = MIFAREClient()
41    
42          def await_prompt(self):          def await_prompt(self):
43                  self.wfh.flush()                  self.wfh.flush()
# Line 161  class VendingMachine: Line 164  class VendingMachine:
164          def next_event(self, timeout = None):          def next_event(self, timeout = None):
165                  # we don't want to buffer in the serial port, so we get all the events                  # we don't want to buffer in the serial port, so we get all the events
166                  # we can ASAP.                  # we can ASAP.
167                  if timeout < 0: timeout = 0  
168                  if len(self.events) > 0: timeout = 0                  # Never have no timeout...
169                  while True:                  if timeout == None: timeout = 60*60*24*365
170                          (r, _, _) = select([self.rfh], [], [], timeout)  
171                    # Make sure we go through the loop at least once.
172                    if timeout <= 0: timeout = 0.01
173    
174                    while timeout > 0:
175                            this_timeout = min(timeout, 0.2)
176                            timeout -= this_timeout
177    
178                            (r, _, _) = select([self.rfh], [], [], this_timeout)
179                          if r:                          if r:
180                                  self.get_response(async = True)                                  self.get_response(async = True)
181                                  timeout = 0                                  timeout = 0
182                          else:  
183                                  break                          try:
184                                    mifare_uid = self.mifare.get_card_uid()
185                            except ValueError:
186                                    mifare_uid = None
187                            if mifare_uid != None:
188                                    logging.info('Got MIFARE uid %s'%(str(mifare_uid)))
189                                    self.events.append((MIFARE, mifare_uid))
190                                    timeout = 0
191    
192                  if len(self.events) == 0: return (TICK, time())                  if len(self.events) == 0: return (TICK, time())
193                  ret = self.events[0]                  ret = self.events[0]
194                  del self.events[0]                  del self.events[0]

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.21

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26