A UI on the vending machine.
authorBernard Blackham <dagobah@ucc.asn.au>
Fri, 25 Jun 2004 17:51:24 +0000 (17:51 +0000)
committerBernard Blackham <dagobah@ucc.asn.au>
Fri, 25 Jun 2004 17:51:24 +0000 (17:51 +0000)
sql-edition/servers/VendServer.py

index c3f04bc..25a24b6 100755 (executable)
@@ -1,10 +1,13 @@
 #!/usr/bin/python
 
-import sys, os, string, socket, time, re
+import sys, os, string, time, re, pwd
 import pg
 from LATClient import LATClient
 from VendingMachine import VendingMachine
 
+GREETING = 'UCC SNACKS'
+PIN_LENGTH = 4
+
 class DispenseDatabase:
        def __init__(self, vending_machine):
                self.vending_machine = vending_machine
@@ -35,6 +38,54 @@ class DispenseDatabase:
                        self.process_requests()
                        notify = self.db.getnotify()
 
+def get_pin(uid):
+       try:
+               info = pwd.getpwuid(uid)
+       except KeyError:
+               return None
+       if info.pw_dir == None: return False
+       pinfile = os.path.join(info.pw_dir, '.pin')
+       try:
+               s = os.stat(pinfile)
+       except OSError:
+               return False
+       if s.st_mode & 077:
+               return None
+       try:
+               f = file(pinfile)
+       except IOError:
+               return None
+       pinstr = f.readline()
+       f.close()
+       if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
+               return None
+       return int(pinstr)
+
+def has_good_pin(uid):
+       return get_pin != None
+
+def verify_user_pin(uid, pin):
+       if get_pin(uid) == pin:
+               info = pwd.getpwuid(uid)
+               return info.pw_name
+       else:
+               return None
+
+def door_open_mode(vending_machine):
+       print "Entering open door mode"
+       v.display("DOOR  OPEN")
+       while True:
+               v.wait_for_events(1)
+               while True:
+                       e = v.next_event()
+                       if e == None: break
+                       (event, params) = e
+                       if event == DOOR:
+                               if params == 1: # door closed
+                                       v.display("BYE BYE!")
+                                       time.sleep(1)
+                                       return
+
 if __name__ == '__main__':
        # Open vending machine via LAT
        latclient = LATClient(service = 'VEND', password = 'dmscptd')
@@ -49,6 +100,106 @@ if __name__ == '__main__':
 
        db = DispenseDatabase(v)
        db.process_requests()
+       cur_user = ''
+       cur_pin = ''
+       cur_selection = ''
+
+       scrolling_message = [GREETING]
+       scrolling_wraps = False
+       need_repaint = True
+       timeout = None
+       last_tick = time.time()
+
        while True:
+               if timeout != None and timeout > 0 and time.time() > last_tick+1:
+                       timeout -= 1
+                       if len(scrolling_message) > 0:
+                               need_repaint = True
+               if need_repaint and len(scrolling_message) > 0:
+                       v.display(scrolling_message[0])
+                       if scrolling_wraps:
+                               scrolling_message.append(scrolling_message[0])
+                       del scrolling_message[0]
+                       need_repaint = False
+
                v.wait_for_events(1)
+               while True:
+                       e = v.next_event()
+                       if e == None: break
+                       (event, params) = e
+                       if event == DOOR:
+                               if params == 0:
+                                       door_open_mode(v);
+                                       cur_user = ''
+                                       cur_pin = ''
+                                       scrolling_message = [GREETING]
+                                       scrolling_wraps = False
+                                       need_repaint = True
+                       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 = ''
+                                               scrolling_message = [GREETING]
+                                               scrolling_wraps = False
+                                               need_repaint = True
+                                               continue
+                                       cur_user += chr(key + ord('0'))
+                                       v.display('UID: '+cur_user)
+                                       if len(cur_user) == 5:
+                                               uid = int(cur_user)
+                                               if not has_good_pin(uid):
+                                                       v.display('PIN NO GOOD')
+                                                       time.sleep(1)
+                                                       continue
+                                               v.display('PIN: ')
+                                               continue
+                               elif len(cur_pin) < PIN_LENGTH:
+                                       if key == 11:
+                                               if cur_pin == '':
+                                                       cur_user = ''
+                                                       scrolling_message = [GREETING]
+                                                       scrolling_wraps = False
+                                                       need_repaint = True
+                                                       continue
+                                               cur_pin = ''
+                                               v.display('PIN: ')
+                                               continue
+                                       cur_pin += chr(key + ord('0'))
+                                       v.display('PIN: '+'X'*len(cur_pin))
+                                       if len(cur_pin) == PIN_LENGTH:
+                                               name = verify_user_pin(int(cur_user), int(cur_pin))
+                                               if name:
+                                                       v.beep(0, False)
+                                                       cur_selection = ''
+
+                                                       scrolling_message = [' WELCOME  ', name]
+                                                       scrolling_message.append('OR A SNACK')
+                                                       scrolling_wraps = True
+                                                       need_repaint = True
+                                               else:
+                                                       v.beep(255, False)
+                                                       scrolling_message = [' BAD PIN  ', '  SORRY   ', GREETING]
+                                                       scrolling_wraps = False
+                                                       need_repaint = True
+
+                                                       cur_user = ''
+                                                       cur_pin = ''
+                                                       continue
+                               elif len(cur_selection) < 2:
+                                       if key == 11:
+                                               if cur_selection == '':
+                                                       cur_pin = ''
+                                                       cur_user = ''
+                                                       v.display(GREETING)
+                                                       continue
+                                               cur_selection += chr(key + ord('0'))
+                                               if len(cur_selection) == 2:
+                                                       make_selection(cur_selection)
+
+
                db.handle_events()

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