+
+def handle_getting_uid_key(state, event, params, v, vstatus):
+ #print "handle_getting_uid_key (s,e,p)", state, " ", event, " ", params
+ key = params
+ # complicated key handling here:
+ if len(vstatus.cur_user) < 5:
+ if key == 11:
+ vstatus.cur_user = ''
+ vstatus.mk.set_message(GREETING)
+
+ idle_in(vstatus,5)
+ vstatus.state = STATE_IDLE
+ return
+
+ vstatus.cur_user += chr(key + ord('0'))
+ vstatus.mk.set_message('UID: '+vstatus.cur_user)
+
+ if len(vstatus.cur_user) == 5:
+ uid = int(vstatus.cur_user)
+ if not has_good_pin(uid):
+ logging.info('user '+vstatus.cur_user+' has a bad PIN')
+ vstatus.mk.set_messages(
+ [(' '*10+'INVALID PIN SETUP'+' '*10, False, 3),
+ (GREETING, False, None)])
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+
+ idle_in(vstatus,5)
+ vstatus.state = STATE_IDLE
+
+ return
+
+
+ vstatus.cur_pin = ''
+ vstatus.mk.set_message('PIN: ')
+ logging.info('need pin for user %s'%vstatus.cur_user)
+ vstatus.state = STATE_GETTING_PIN
+ return
+
+
+def handle_idle_key(state, event, params, v, vstatus):
+ #print "handle_idle_key (s,e,p)", state, " ", event, " ", params
+
+ key = params
+
+ if key == 11:
+ vstatus.cur_user = ''
+ vstatus.mk.set_message(GREETING)
+ idle_in(vstatus,5)
+ choose_idler()
+ return
+
+ vstatus.state = STATE_GETTING_UID
+ run_handler(event, key, v, vstatus)
+
+
+def handle_idle_tick(state, event, params, v, vstatus):
+ ### State logged out ... initiate idler in 5 (first start?)
+ if vstatus.time_to_idle == None and vstatus.cur_user == '':
+ vstatus.time_to_idle = time() + 5
+ choose_idler()
+
+ ### State idling
+
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle:
+ idle_step()
+
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle + 30:
+ vstatus.time_to_idle = time()
+ choose_idler()
+
+ ###
+
+ vstatus.mk.update_display()
+
+
+def handle_door_idle(state, event, params, v, vstatus):
+ # don't care right now.
+ pass
+
+def handle_door_event(state, event, params, v, vstatus):
+ vstatus.time_to_idle = None
+
+ if params == 1: #door open
+ vstatus.state = STATE_DOOR_OPENING
+ logging.warning("Entering open door mode")
+ v.display("-FEED ME-")
+ #door_open_mode(v);
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+ elif params == 0: #door closed
+ vstatus.state = STATE_DOOR_CLOSING
+ idle_in(vstatus, 5)
+
+ logging.warning('Leaving open door mode')
+ v.display("-YUM YUM!-")
+
+def idle_in(vstatus,seconds):
+ vstatus.time_to_idle = time() + seconds
+
+def return_to_idle(state,event,params,v,vstatus):
+ if vstatus.time_to_idle is not None and time() > vstatus.time_to_idle:
+ vstatus.mk.set_message(GREETING)
+ vstatus.state = STATE_IDLE
+ return
+ if not vstatus.time_to_idle:
+ vstatus.mk.set_message(GREETING)
+ vstatus.state = STATE_IDLE
+ return
+
+def create_state_table(vstatus):
+ vstatus.state_table[(STATE_IDLE,TICK,1)] = handle_idle_tick
+ vstatus.state_table[(STATE_IDLE,KEY,1)] = handle_idle_key
+ vstatus.state_table[(STATE_IDLE,DOOR,1)] = handle_door_event
+
+ vstatus.state_table[(STATE_DOOR_OPENING,TICK,1)] = handle_door_idle
+ vstatus.state_table[(STATE_DOOR_OPENING,DOOR,1)] = handle_door_event
+ vstatus.state_table[(STATE_DOOR_OPENING,KEY,1)] = do_nothing
+
+ vstatus.state_table[(STATE_DOOR_CLOSING,TICK,1)] = return_to_idle
+ vstatus.state_table[(STATE_DOOR_CLOSING,DOOR,1)] = handle_door_event
+ vstatus.state_table[(STATE_DOOR_CLOSING,KEY,1)] = do_nothing
+
+ vstatus.state_table[(STATE_GETTING_UID,TICK,1)] = handle_getting_uid_idle
+ vstatus.state_table[(STATE_GETTING_UID,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GETTING_UID,KEY,1)] = handle_getting_uid_key
+
+ vstatus.state_table[(STATE_GETTING_PIN,TICK,1)] = handle_getting_pin_idle
+ vstatus.state_table[(STATE_GETTING_PIN,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GETTING_PIN,KEY,1)] = handle_getting_pin_key
+
+ vstatus.state_table[(STATE_GET_SELECTION,TICK,1)] = handle_get_selection_idle
+ vstatus.state_table[(STATE_GET_SELECTION,DOOR,1)] = do_nothing
+ vstatus.state_table[(STATE_GET_SELECTION,KEY,1)] = handle_get_selection_key
+
+def get_state_table_handler(vstatus, state, event, counter):
+ return vstatus.state_table[(state,event,counter)]
+
+def run_forever(rfh, wfh, options, cf):
+ v = VendingMachine(rfh, wfh)
+ vstatus = VendState(v)
+ create_state_table(vstatus)
+
+ logging.debug('PING is ' + str(v.ping()))
+
+ if USE_DB: db = DispenseDatabase(v, cf.DBServer, cf.DBName, cf.DBUser, cf.DBPassword)
+
+ vstatus.mk.set_message(GREETING)
+ setup_idlers(v)
+ choose_idler()
+ vstatus.mk.set_message("Booted")
+
+
+ # This main loop was hideous and the work of the devil.
+ # This has now been fixed (mostly) - mtearle
+ #
+ #
+ # notes for later surgery
+ # (event, counter, ' ')
+ # V
+ # d[ ] = (method)
+ #
+ # ( return state - not currently implemented )
+
+ while True:
+ if USE_DB:
+ try:
+ db.handle_events()
+ except DispenseDatabaseException, e:
+ logging.error('Database error: '+str(e))
+
+
+ e = v.next_event(0)
+ (event, params) = e
+
+ vstatus.counter=1
+ run_handler(event, params, v, vstatus)
+
+# logging.debug('Got event: ' + repr(e))
+
+
+def run_handler(event, params, v, vstatus):
+ handler = get_state_table_handler(vstatus,vstatus.state,event,vstatus.counter)
+ if handler:
+ handler(vstatus.state, event, params, v, vstatus)
+
+def connect_to_vend(options, cf):
+
+ if options.use_lat:
+ logging.info('Connecting to vending machine using LAT')
+ latclient = LATClient(service = cf.ServiceName, password = cf.ServicePassword, server_name = cf.ServerName, connect_password = cf.ConnectPassword, priv_password = cf.PrivPassword)
+ rfh, wfh = latclient.get_fh()
+ elif options.use_serial:
+ # Open vending machine via serial.
+ logging.info('Connecting to vending machine using serial')
+ serialclient = SerialClient(port = '/dev/ttyS1', baud = 9600)
+ rfh,wfh = serialclient.get_fh()