+ vstatus.cur_selection += chr(key + ord('0'))
+ vstatus.mk.set_message('SELECT: '+vstatus.cur_selection)
+ vstatus.time_to_autologout = None
+ elif len(vstatus.cur_selection) == 1:
+ if key == 11:
+ vstatus.cur_selection = ''
+ vstatus.time_to_autologout = None
+ scroll_options(vstatus.username, vstatus.mk)
+ return
+ else:
+ vstatus.cur_selection += chr(key + ord('0'))
+ if vstatus.cur_user:
+ make_selection(v,vstatus)
+ vstatus.cur_selection = ''
+ vstatus.time_to_autologout = time() + 8
+ vstatus.last_timeout_refresh = None
+ else:
+ # Price check mode.
+ price_check(v,vstatus)
+ vstatus.cur_selection = ''
+ vstatus.time_to_autologout = None
+ vstatus.last_timeout_refresh = None
+
+def make_selection(v, vstatus):
+ # should use sudo here
+ if vstatus.cur_selection == '55':
+ vstatus.mk.set_message('OPENSESAME')
+ logging.info('dispensing a door for %s'%vstatus.username)
+ if geteuid() == 0:
+ #ret = os.system('su - "%s" -c "dispense door"'%vstatus.username)
+ ret = os.system('dispense -u "%s" door'%vstatus.username)
+ else:
+ ret = os.system('dispense door')
+ if ret == 0:
+ logging.info('door opened')
+ vstatus.mk.set_message(center('DOOR OPEN'))
+ else:
+ logging.warning('user %s tried to dispense a bad door'%vstatus.username)
+ vstatus.mk.set_message(center('BAD DOOR'))
+ sleep(1)
+ elif vstatus.cur_selection == '81':
+ cookie(v)
+ elif vstatus.cur_selection == '99':
+ scroll_options(vstatus.username, vstatus.mk)
+ vstatus.cur_selection = ''
+ return
+ elif vstatus.cur_selection[1] == '8':
+ v.display('GOT DRINK?')
+ if ((os.system('dispense -u "%s" coke:%s'%(vstatus.username, vstatus.cur_selection[0])) >> 8) != 0):
+ v.display('SEEMS NOT')
+ else:
+ v.display('GOT DRINK!')
+ else:
+ # first see if it's a named slot
+ try:
+ price, shortname, name = get_snack( vstatus.cur_selection )
+ except:
+ price, shortname, name = get_snack( '--' )
+ dollarprice = "$%.2f" % ( price / 100.0 )
+ v.display(vstatus.cur_selection+' - %s'%dollarprice)
+ exitcode = os.system('dispense -u "%s" give \>snacksales %d "%s"'%(vstatus.username, price, name)) >> 8
+# exitcode = os.system('dispense -u "%s" give \>sales\:snack %d "%s"'%(vstatus.username, price, name)) >> 8
+# exitcode = os.system('dispense -u "%s" snack:%s'%(vstatus.username, vstatus.cur_selection)) >> 8
+ if (exitcode == 0):
+ # magic dispense syslog service
+ syslog.syslog(syslog.LOG_INFO | syslog.LOG_LOCAL4, "vended %s (slot %s) for %s" % (name, vstatus.cur_selection, vstatus.username))
+ v.vend(vstatus.cur_selection)
+ v.display('THANK YOU')
+ elif (exitcode == 5): # RV_BALANCE
+ v.display('NO MONEY?')
+ elif (exitcode == 4): # RV_ARGUMENTS (zero give causes arguments)
+ v.display('EMPTY SLOT')
+ elif (exitcode == 1): # RV_BADITEM (Dead slot)
+ v.display('EMPTY SLOT')
+ else:
+ syslog.syslog(syslog.LOG_INFO | syslog.LOG_LOCAL4, "failed vending %s (slot %s) for %s (code %d)" % (name, vstatus.cur_selection, vstatus.username, exitcode))
+ v.display('UNK ERROR')
+ sleep(1)
+
+
+def price_check(v, vstatus):
+ if vstatus.cur_selection[1] == '8':
+ args = ('dispense', 'iteminfo', 'coke:' + vstatus.cur_selection[0])
+ info, unused = Popen(args, close_fds=True, stdout=PIPE).communicate()
+ dollarprice = re.match("\s*[a-z]+:\d+\s+(\d+\.\d\d)\s+([^\n]+)", info).group(1)
+ else:
+ # first see if it's a named slot
+ try:
+ price, shortname, name = get_snack( vstatus.cur_selection )
+ except:
+ price, shortname, name = get_snack( '--' )
+ dollarprice = "$%.2f" % ( price / 100.0 )
+ v.display(vstatus.cur_selection+' - %s'%dollarprice)
+
+
+def handle_getting_pin_key(state, event, params, v, vstatus):
+ #print "handle_getting_pin_key (s,e,p)", state, " ", event, " ", params
+ key = params
+ if len(vstatus.cur_pin) < PIN_LENGTH:
+ if key == 11:
+ if vstatus.cur_pin == '':
+ vstatus.cur_user = ''
+ reset_idler(v, vstatus)
+
+ return
+ vstatus.cur_pin = ''
+ vstatus.mk.set_message('PIN: ')
+ return
+ vstatus.cur_pin += chr(key + ord('0'))
+ vstatus.mk.set_message('PIN: '+'X'*len(vstatus.cur_pin))
+ if len(vstatus.cur_pin) == PIN_LENGTH:
+ vstatus.username = verify_user_pin(int(vstatus.cur_user), int(vstatus.cur_pin))
+ if vstatus.username:
+ v.beep(0, False)
+ vstatus.cur_selection = ''
+ vstatus.change_state(STATE_GET_SELECTION)
+ scroll_options(vstatus.username, vstatus.mk, True)
+ return
+ else:
+ v.beep(40, False)
+ vstatus.mk.set_messages(
+ [(center('BAD PIN'), False, 1.0),
+ (center('SORRY'), False, 0.5)])
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+
+ reset_idler(v, vstatus, 2)
+
+ return
+
+
+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) == 0 and key == 9:
+ vstatus.cur_selection = ''
+ vstatus.time_to_autologout = None
+ vstatus.mk.set_message('PRICECHECK')
+ sleep(0.5)
+ scroll_options('', vstatus.mk)
+ vstatus.change_state(STATE_GET_SELECTION)
+ return
+
+ if len(vstatus.cur_user) <8:
+ if key == 11:
+ vstatus.cur_user = ''
+
+ reset_idler(v, vstatus)
+ return
+ vstatus.cur_user += chr(key + ord('0'))
+ #logging.info('dob: '+vstatus.cur_user)
+ if len(vstatus.cur_user) > 5:
+ vstatus.mk.set_message('>'+vstatus.cur_user)
+ else:
+ vstatus.mk.set_message('UID: '+vstatus.cur_user)
+
+ if len(vstatus.cur_user) == 5:
+ uid = int(vstatus.cur_user)
+
+ if uid == 0:
+ logging.info('user '+vstatus.cur_user+' has a bad PIN')
+ pfalken="""
+CARRIER DETECTED
+
+CONNECT 128000
+
+Welcome to Picklevision Sytems, Sunnyvale, CA
+
+Greetings Professor Falken.
+
+
+
+
+Shall we play a game?
+
+
+Please choose from the following menu:
+
+1. Tic-Tac-Toe
+2. Chess
+3. Checkers
+4. Backgammon
+5. Poker
+6. Toxic and Biochemical Warfare
+7. Global Thermonuclear War
+
+7 [ENTER]
+
+Wouldn't you prefer a nice game of chess?
+
+""".replace('\n',' ')
+ vstatus.mk.set_messages([(pfalken, False, 10)])
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+
+ reset_idler(v, vstatus, 10)
+
+ return
+
+ if not has_good_pin(uid):
+ logging.info('user '+vstatus.cur_user+' has a bad PIN')
+ vstatus.mk.set_messages(
+ [(' '*10+'INVALID PIN SETUP'+' '*11, False, 3)])
+ vstatus.cur_user = ''
+ vstatus.cur_pin = ''
+
+ reset_idler(v, vstatus, 3)
+
+ return
+
+
+ vstatus.cur_pin = ''
+ vstatus.mk.set_message('PIN: ')
+ logging.info('need pin for user %s'%vstatus.cur_user)
+ vstatus.change_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 = ''
+ reset_idler(v, vstatus)
+ return
+
+ vstatus.change_state(STATE_GETTING_UID)
+ run_handler(event, key, v, vstatus)
+
+
+def handle_idle_tick(state, event, params, v, vstatus):
+ ### State idling
+ if vstatus.mk.done():
+ idle_step(vstatus)
+
+ if vstatus.time_of_next_idler and time() > vstatus.time_of_next_idler:
+ vstatus.time_of_next_idler = time() + 30
+ choose_idler()
+
+ ###
+
+ vstatus.mk.update_display()
+
+ vstatus.change_state(STATE_GRANDFATHER_CLOCK)
+ run_handler(event, params, v, vstatus)
+ sleep(0.05)
+
+def beep_on(when, before=0):
+ start = int(when - before)
+ end = int(when)
+ now = int(time())
+
+ if now >= start and now <= end:
+ return 1
+ return 0
+
+def handle_idle_grandfather_tick(state, event, params, v, vstatus):
+ ### check for interesting times
+ now = localtime()
+
+ quarterhour = mktime([now[0],now[1],now[2],now[3],15,0,now[6],now[7],now[8]])
+ halfhour = mktime([now[0],now[1],now[2],now[3],30,0,now[6],now[7],now[8]])
+ threequarterhour = mktime([now[0],now[1],now[2],now[3],45,0,now[6],now[7],now[8]])
+ fivetothehour = mktime([now[0],now[1],now[2],now[3],55,0,now[6],now[7],now[8]])
+
+ hourfromnow = localtime(time() + 3600)
+
+ #onthehour = mktime([now[0],now[1],now[2],now[3],03,0,now[6],now[7],now[8]])
+ onthehour = mktime([hourfromnow[0],hourfromnow[1],hourfromnow[2],hourfromnow[3], \
+ 0,0,hourfromnow[6],hourfromnow[7],hourfromnow[8]])
+
+ ## check for X seconds to the hour
+ ## if case, update counter to 2
+ if beep_on(onthehour,15) \
+ or beep_on(halfhour,0) \
+ or beep_on(quarterhour,0) \
+ or beep_on(threequarterhour,0) \
+ or beep_on(fivetothehour,0):
+ vstatus.change_state(STATE_GRANDFATHER_CLOCK,2)
+ run_handler(event, params, v, vstatus)
+ else:
+ vstatus.change_state(STATE_IDLE)
+
+def handle_grandfather_tick(state, event, params, v, vstatus):
+ go_idle = 1
+
+ msg = []
+ ### we live in interesting times
+ now = localtime()
+
+ quarterhour = mktime([now[0],now[1],now[2],now[3],15,0,now[6],now[7],now[8]])
+ halfhour = mktime([now[0],now[1],now[2],now[3],30,0,now[6],now[7],now[8]])
+ threequarterhour = mktime([now[0],now[1],now[2],now[3],45,0,now[6],now[7],now[8]])
+ fivetothehour = mktime([now[0],now[1],now[2],now[3],55,0,now[6],now[7],now[8]])
+
+ hourfromnow = localtime(time() + 3600)
+
+# onthehour = mktime([now[0],now[1],now[2],now[3],03,0,now[6],now[7],now[8]])
+ onthehour = mktime([hourfromnow[0],hourfromnow[1],hourfromnow[2],hourfromnow[3], \
+ 0,0,hourfromnow[6],hourfromnow[7],hourfromnow[8]])
+
+
+ #print "when it fashionable to wear a onion on your hip"
+
+ if beep_on(onthehour,15):
+ go_idle = 0
+ next_hour=((hourfromnow[3] + 11) % 12) + 1
+ if onthehour - time() < next_hour and onthehour - time() > 0:
+ v.beep(0, False)
+
+ t = int(time())
+ if (t % 2) == 0:
+ msg.append(("DING!", False, None))