Rearrange File Slightly
[uccvend-vendserver.git] / VendServer / VendServer.py
index 58622cf..4eb2986 100755 (executable)
@@ -22,6 +22,7 @@ from SnackConfig import get_snack#, get_snacks
 import socket
 from posix import geteuid
 from LDAPConnector import get_uid,get_uname, set_card_id
+from OpenDispense import OpenDispense as Dispense
 
 CREDITS="""
 This vending machine software brought to you by:
@@ -31,7 +32,11 @@ Nick Bannon
 Cameron Patrick
 and a collective of hungry alpacas.
 
+The MIFARE card reader bought to you by:
+David Adam
 
+Bug Hunting and hardware maintenance by:
+Mitchell Pomery
 
 For a good time call +61 8 6488 3901
 
@@ -61,6 +66,16 @@ STATE_GRANDFATHER_CLOCK,
 TEXT_SPEED = 0.8
 IDLE_SPEED = 0.05
 
+_pin_uid = 0
+_pin_uname = 'root'
+_pin_pin = '----'
+
+_last_card_id = -1
+
+idlers = []
+idler = None
+
+
 class DispenseDatabaseException(Exception): pass
 
 class DispenseDatabase:
@@ -92,6 +107,38 @@ class DispenseDatabase:
                        self.process_requests()
                        notify = self.db.getnotify()
 
+class VendState:
+       def __init__(self,v):
+               self.state_table = {}
+               self.state = STATE_IDLE
+               self.counter = 0
+
+               self.mk = MessageKeeper(v)
+               self.cur_user = ''
+               self.cur_pin = ''
+               self.username = ''
+               self.cur_selection = ''
+               self.time_to_autologout = None
+
+               self.last_timeout_refresh = None
+
+       def change_state(self,newstate,newcounter=None):
+               if self.state != newstate:
+                       #print "Changing state from: ", 
+                       #print self.state,
+                       #print " to ", 
+                       #print newstate 
+                       self.state = newstate
+
+               if newcounter is not None and self.counter != newcounter:
+                       #print "Changing counter from: ", 
+                       #print self.counter,
+                       #print " to ", 
+                       #print newcounter 
+                       self.counter = newcounter
+
+
+
 def scroll_options(username, mk, welcome = False):
        if welcome:
                # Balance checking
@@ -138,10 +185,6 @@ def scroll_options(username, mk, welcome = False):
        msg.append((choices, False, None))
        mk.set_messages(msg)
 
-_pin_uid = 0
-_pin_uname = 'root'
-_pin_pin = '----'
-
 def _check_pin(uid, pin):
        global _pin_uid
        global _pin_uname
@@ -244,11 +287,6 @@ def center(str):
        LEN = 10
        return ' '*((LEN-len(str))/2)+str
 
-
-
-idlers = []
-idler = None
-
 def setup_idlers(v):
        global idlers, idler
        idlers = [
@@ -332,36 +370,6 @@ def idle_step(vstatus):
                nextidle = IDLE_SPEED
        vstatus.time_of_next_idlestep = time()+nextidle
 
-class VendState:
-       def __init__(self,v):
-               self.state_table = {}
-               self.state = STATE_IDLE
-               self.counter = 0
-
-               self.mk = MessageKeeper(v)
-               self.cur_user = ''
-               self.cur_pin = ''
-               self.username = ''
-               self.cur_selection = ''
-               self.time_to_autologout = None
-
-               self.last_timeout_refresh = None
-
-       def change_state(self,newstate,newcounter=None):
-               if self.state != newstate:
-                       #print "Changing state from: ", 
-                       #print self.state,
-                       #print " to ", 
-                       #print newstate 
-                       self.state = newstate
-
-               if newcounter is not None and self.counter != newcounter:
-                       #print "Changing counter from: ", 
-                       #print self.counter,
-                       #print " to ", 
-                       #print newcounter 
-                       self.counter = newcounter
-
 
 
 def handle_tick_event(event, params, v, vstatus):
@@ -386,6 +394,7 @@ def handle_getting_pin_idle(state, event, params, v, vstatus):
        pass
 
 def handle_get_selection_idle(state, event, params, v, vstatus):
+       global _last_card_id
        # don't care right now.
        ###
        ### State logging out ..
@@ -401,7 +410,7 @@ def handle_get_selection_idle(state, event, params, v, vstatus):
                vstatus.cur_user = ''
                vstatus.cur_pin = ''
                vstatus.cur_selection = ''
-                       
+               _last_card_id = -1
                reset_idler(v, vstatus)
 
        ### State fully logged out ... reset variables
@@ -423,13 +432,14 @@ def handle_get_selection_idle(state, event, params, v, vstatus):
 
 
 def handle_get_selection_key(state, event, params, v, vstatus):
+       global _last_card_id
        key = params
        if len(vstatus.cur_selection) == 0:
                if key == 11:
                        vstatus.cur_pin = ''
                        vstatus.cur_user = ''
                        vstatus.cur_selection = ''
-                       
+                       _last_card_id = -1
                        vstatus.mk.set_messages([(center('BYE!'), False, 1.5)])
                        reset_idler(v, vstatus, 2)
                        return
@@ -838,12 +848,16 @@ def handle_door_event(state, event, params, v, vstatus):
                logging.warning('Leaving open door mode')
                v.display("-YUM YUM!-")
 
+
 def handle_mifare_event(state, event, params, v, vstatus):
+       global _last_card_id
        card_id = params
        # Translate card_id into uid.
-       if card_id == None:
+       if card_id == None or card_id == _last_card_id:
                return
 
+       _last_card_id = card_id
+       
        try:
                vstatus.cur_user = get_uid(card_id)
                logging.info('Mapped card id to uid %s'%vstatus.cur_user)
@@ -876,17 +890,21 @@ def handle_mifare_event(state, event, params, v, vstatus):
                         (center('SORRY'), False, 0.5)])
                vstatus.cur_user = ''
                vstatus.cur_pin = ''
+               _last_card_id = -1
        
                reset_idler(v, vstatus, 2)
                return
 
 def handle_mifare_add_user_event(state, event, params, v, vstatus):
-       card_id = params
+       global _last_card_id
+       card_id = params
 
        # Translate card_id into uid.
-       if card_id == None:
+       if card_id == None or card_id == _last_card_id:
                return
 
+       _last_card_id = card_id
+       
        try:
                if get_uid(card_id) != None:
                        vstatus.mk.set_messages(
@@ -926,24 +944,24 @@ def create_state_table(vstatus):
        vstatus.state_table[(STATE_DOOR_CLOSING,MIFARE,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,DOOR,1)] = handle_door_event
        vstatus.state_table[(STATE_GETTING_UID,KEY,1)] = handle_getting_uid_key
        vstatus.state_table[(STATE_GETTING_UID,MIFARE,1)] = handle_mifare_event
 
        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,DOOR,1)] = handle_door_event
        vstatus.state_table[(STATE_GETTING_PIN,KEY,1)] = handle_getting_pin_key
        vstatus.state_table[(STATE_GETTING_PIN,MIFARE,1)] = handle_mifare_event
 
        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,DOOR,1)] = handle_door_event
        vstatus.state_table[(STATE_GET_SELECTION,KEY,1)] = handle_get_selection_key
        vstatus.state_table[(STATE_GET_SELECTION,MIFARE,1)] = handle_mifare_add_user_event
 
        vstatus.state_table[(STATE_GRANDFATHER_CLOCK,TICK,1)] = handle_idle_grandfather_tick
        vstatus.state_table[(STATE_GRANDFATHER_CLOCK,TICK,2)] = handle_grandfather_tick
-       vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,1)] = do_nothing
-       vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,2)] = do_nothing
+       vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,1)] = handle_door_event
+       vstatus.state_table[(STATE_GRANDFATHER_CLOCK,DOOR,2)] = handle_door_event
        vstatus.state_table[(STATE_GRANDFATHER_CLOCK,KEY,1)] = do_nothing
        vstatus.state_table[(STATE_GRANDFATHER_CLOCK,KEY,2)] = do_nothing
        vstatus.state_table[(STATE_GRANDFATHER_CLOCK,MIFARE,1)] = handle_mifare_event
@@ -1171,7 +1189,8 @@ def do_vend_server(options, config_opts):
                        logging.info("Trying again in 5 seconds.")
                        sleep(5)
 
-if __name__ == '__main__':
+
+def main(argv=None):
        options, config_opts = set_stuff_up()
        while True:
                try:
@@ -1201,3 +1220,5 @@ if __name__ == '__main__':
                        sleep(10)
                        logging.warning("Trying again anyway (might not help, but hey...)")
 
+if __name__ == '__main__':
+       sys.exit(main())

UCC git Repository :: git.ucc.asn.au