Use config parser
authorBernard Blackham <[email protected]>
Sun, 27 Jun 2004 10:15:08 +0000 (10:15 +0000)
committerBernard Blackham <[email protected]>
Sun, 27 Jun 2004 10:15:08 +0000 (10:15 +0000)
sql-edition/servers/VendServer.py

index 447dcf7..35d5d33 100755 (executable)
@@ -5,6 +5,7 @@ import sys, os, string, time, re, pwd
 import pg
 from LATClient import LATClient
 from VendingMachine import VendingMachine
+from ConfigParser import ConfigParser
 
 GREETING = 'UCC SNACKS'
 PIN_LENGTH = 4
@@ -14,9 +15,9 @@ SWITCH = 2
 KEY = 3
 
 class DispenseDatabase:
-       def __init__(self, vending_machine):
+       def __init__(self, vending_machine, host, name, user, password):
                self.vending_machine = vending_machine
-               self.db = pg.DB(dbname = 'dispense', host = 'dispense.ucc.gu.uwa.edu.au', user = 'vendserver', passwd = 'revresdnev')
+               self.db = pg.DB(dbname = name, host = host, user = user, passwd = password)
                self.db.query('LISTEN vend_requests')
 
        def process_requests(self):
@@ -89,9 +90,22 @@ def door_open_mode(vending_machine):
                                time.sleep(1)
                                return
 
+def center(str):
+       LEN = 10
+       return ' '*((LEN-len(str))/2)+str
+
 if __name__ == '__main__':
+       cp = ConfigParser()
+       cp.read('/etc/dispense/servers.conf')
+       DBServer = cp.get('Database', 'Server')
+       DBName = cp.get('Database', 'Name')
+       DBUser = cp.get('VendingMachine', 'DBUser')
+       DBPassword = cp.get('VendingMachine', 'DBPassword')
+
+       ServiceName = cp.get('VendingMachine', 'ServiceName')
+       ServicePassword = cp.get('VendingMachine', 'Password')
        # Open vending machine via LAT
-       latclient = LATClient(service = 'VEND', password = 'dmscptd')
+       latclient = LATClient(service = ServiceName, password = ServicePassword)
        (rfh, wfh) = latclient.get_fh()
        v = VendingMachine(rfh, wfh)
        print 'PING is', v.ping()
@@ -101,7 +115,7 @@ if __name__ == '__main__':
        #print 'DISPLAY is', v.display('WELCOME')
        print 'S is', v.get_switches()
 
-       db = DispenseDatabase(v)
+       db = DispenseDatabase(v, DBServer, DBName, DBUser, DBPassword)
        db.process_requests()
        cur_user = ''
        cur_pin = ''
@@ -118,6 +132,7 @@ if __name__ == '__main__':
                        if timeout != None and timeout > 0: timeout -= 1
                        if len(scrolling_message) > 0:
                                need_repaint = True
+                       last_tick = time.time()
                if need_repaint and len(scrolling_message) > 0:
                        v.display(scrolling_message[0])
                        if scrolling_wraps:
@@ -135,8 +150,15 @@ if __name__ == '__main__':
                        continue
 
                while True:
-                       e = v.next_event(1)
-                       if e == None: break
+                       #print 'waiting for event'
+                       e = v.next_event(0)
+                       if e == None:
+                               #print 'waiting harder for event'
+                               e = v.next_event(0.1)
+                               if e == None:
+                                       #print 'no event. passing'
+                                       break
+                       #print 'got event'
                        (event, params) = e
                        print e
                        if event == DOOR:
@@ -166,7 +188,7 @@ if __name__ == '__main__':
                                        if len(cur_user) == 5:
                                                uid = int(cur_user)
                                                if not has_good_pin(uid):
-                                                       scrolling_message = [' INVALID  ', '   PIN', '  `SETUP', GREETING]
+                                                       scrolling_message = map(center, ['INVALID','PIN','SETUP',GREETING])
                                                        scrolling_wraps = False
                                                        need_repaint = True
                                                        cur_user = ''

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