/[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.13 by dagobah, Sun Oct 31 09:29:05 2004 UTC revision 1.18 by dagobah, Fri Mar 2 12:33:13 2007 UTC
# Line 3  import re Line 3  import re
3  from CRC import do_crc  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
7    
8  asynchronous_responses = [      '400', '401', # door open/closed  asynchronous_responses = [      '400', '401', # door open/closed
9                                  '610',        # switches changed                                  '610',        # switches changed
# Line 12  asynchronous_responses = [     '400', '401', Line 13  asynchronous_responses = [     '400', '401',
13  DOOR = 1  DOOR = 1
14  SWITCH = 2  SWITCH = 2
15  KEY = 3  KEY = 3
16    TICK = 4
17    
18  class VendingException(Exception): pass  class VendingException(Exception): pass
19    
# Line 88  class VendingMachine: Line 90  class VendingMachine:
90    
91          def handle_event(self, code, text):          def handle_event(self, code, text):
92                  if code == '400':                  if code == '400':
                         self.events.append((DOOR, 0))  
                 elif code == '401':  
93                          self.events.append((DOOR, 1))                          self.events.append((DOOR, 1))
94                    elif code == '401':
95                            self.events.append((DOOR, 0))
96                  elif code == '610':                  elif code == '610':
97                          self.events.append((SWITCH, None))                          # NOP this. Nothing handles this yet.
98                            #self.events.append((SWITCH, None))
99                          self.interpret_switches(text)                          self.interpret_switches(text)
100                  elif code[0] == '2':                  elif code[0] == '2':
101                          self.events.append((KEY, int(code[1:3])))                          self.events.append((KEY, int(code[1:3])))
# Line 144  class VendingMachine: Line 147  class VendingMachine:
147          def display(self, string):          def display(self, string):
148                  if len(string) > 10:                  if len(string) > 10:
149                          string = string[0:10]                          string = string[0:10]
150                    string = re.sub('(.)\.', lambda match: '.'+match.group(1), string)
151                  self.wfh.write('D'+string+'\n')                  self.wfh.write('D'+string+'\n')
152                  (code, string) = self.get_response()                  (code, string) = self.get_response()
153                  return (code == '300', code, string)                  return (code == '300', code, string)
# Line 151  class VendingMachine: Line 155  class VendingMachine:
155          def next_event(self, timeout = None):          def next_event(self, timeout = None):
156                  # 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
157                  # we can ASAP.                  # we can ASAP.
158                    if timeout < 0: timeout = 0
159                  if len(self.events) > 0: timeout = 0                  if len(self.events) > 0: timeout = 0
160                  while True:                  while True:
161                          (r, _, _) = select([self.rfh], [], [], timeout)                          (r, _, _) = select([self.rfh], [], [], timeout)
# Line 159  class VendingMachine: Line 164  class VendingMachine:
164                                  timeout = 0                                  timeout = 0
165                          else:                          else:
166                                  break                                  break
167                  if len(self.events) == 0: return None                  if len(self.events) == 0: return (TICK, time())
168                  ret = self.events[0]                  ret = self.events[0]
169                  del self.events[0]                  del self.events[0]
170                  return ret                  return ret

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.18

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26