+ print 'PING is', v.ping()
+
+ if USE_DB: db = DispenseDatabase(v, DBServer, DBName, DBUser, DBPassword)
+ cur_user = ''
+ cur_pin = ''
+ cur_selection = ''
+
+ mk = MessageKeeper(v)
+ mk.set_message(GREETING)
+ logout_timeout = None
+ last_timeout_refresh = None
+
+ while True:
+ if USE_DB: db.handle_events()
+
+ if logout_timeout != None:
+ time_left = logout_timeout - time()
+ if time_left < 5 and (last_timeout_refresh is None or last_timeout_refresh > time_left):
+ mk.set_message('LOGOUT: '+str(int(time_left)))
+ last_timeout_refresh = int(time_left)
+ cur_selection = ''
+
+ if logout_timeout != None and logout_timeout - time() <= 0:
+ logout_timeout = None
+ cur_user = ''
+ cur_pin = ''
+ cur_selection = ''
+ mk.set_message(GREETING)
+
+ if logout_timeout and not mk.done(): logout_timeout = None
+ if len(cur_pin) == PIN_LENGTH and mk.done() and logout_timeout == None:
+ # start autologout
+ logout_timeout = time() + 15
+
+ mk.update_display()
+
+ e = v.next_event(0)
+ if e == None:
+ e = v.next_event(0.1)
+ if e == None:
+ continue
+ (event, params) = e
+ print e
+ if event == DOOR:
+ if params == 0:
+ door_open_mode(v);
+ cur_user = ''
+ cur_pin = ''
+ mk.set_message(GREETING)
+ elif event == SWITCH:
+ # don't care right now.
+ pass
+ elif event == KEY:
+ key = params
+ # complicated key handling here:
+ if len(cur_user) < 5:
+ if key == 11:
+ cur_user = ''
+ mk.set_message(GREETING)
+ continue
+ cur_user += chr(key + ord('0'))
+ mk.set_message('UID: '+cur_user)
+ if len(cur_user) == 5:
+ uid = int(cur_user)
+ if not has_good_pin(uid):
+ mk.set_messages(
+ [(center('INVALID'), False, 0.7),
+ (center('PIN'), False, 0.7),
+ (center('SETUP'), False, 1.0),
+ (GREETING, False, None)])
+ cur_user = ''
+ cur_pin = ''
+ continue
+ cur_pin = ''
+ mk.set_message('PIN: ')
+ continue
+ elif len(cur_pin) < PIN_LENGTH:
+ if key == 11:
+ if cur_pin == '':
+ cur_user = ''
+ mk.set_message(GREETING)
+ continue
+ cur_pin = ''
+ mk.set_message('PIN: ')
+ continue
+ cur_pin += chr(key + ord('0'))
+ mk.set_message('PIN: '+'X'*len(cur_pin))
+ if len(cur_pin) == PIN_LENGTH:
+ username = verify_user_pin(int(cur_user), int(cur_pin))
+ if username:
+ v.beep(0, False)
+ cur_selection = ''
+ scroll_options(username, mk, True)
+ continue
+ else:
+ v.beep(40, False)
+ mk.set_messages(
+ [(center('BAD PIN'), False, 1.0),
+ (center('SORRY'), False, 0.5),
+ (GREETING, False, None)])
+ cur_user = ''
+ cur_pin = ''
+ continue
+ elif len(cur_selection) == 0:
+ if key == 11:
+ cur_pin = ''
+ cur_user = ''
+ cur_selection = ''
+ v.display('BYE!')
+ sleep(0.5)
+ mk.set_message(GREETING)
+ continue
+ cur_selection += chr(key + ord('0'))
+ mk.set_message('SELECT: '+cur_selection)
+ elif len(cur_selection) == 1:
+ if key == 11:
+ cur_selection = ''
+ logout_timeout = None
+ scroll_options(username, mk)
+ continue
+ else:
+ cur_selection += chr(key + ord('0'))
+ #make_selection(cur_selection)
+ # XXX this should move somewhere else:
+ if cur_selection == '55':
+ v.display('GOT DOOR?')
+ os.system('su - "%s" -c "dispense door"'%username)
+ elif cur_selection == '99':
+ scroll_options(username, mk)
+ cur_selection = ''
+ continue
+ elif cur_selection[1] == '8':
+ v.display('GOT COKE?')
+ os.system('su - "%s" -c "dispense %s"'%(username, cur_selection[0]))
+ else:
+ v.display('HERES A '+cur_selection)
+ v.vend(cur_selection)
+ sleep(0.5)
+ v.display('THANK YOU')
+ sleep(0.5)
+ cur_selection = ''
+ logout_timeout = time() + 10