3 import sys, os, string, time, re, pwd
5 from LATClient import LATClient
6 from VendingMachine import VendingMachine
8 GREETING = 'UCC SNACKS'
11 class DispenseDatabase:
12 def __init__(self, vending_machine):
13 self.vending_machine = vending_machine
14 self.db = pg.DB(dbname = 'dispense', host = 'dispense.ucc.gu.uwa.edu.au', user = 'vendserver', passwd = 'revresdnev')
15 self.db.query('LISTEN vend_requests')
17 def process_requests(self):
19 query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false'
21 outstanding = self.db.query(query).getresult()
22 except (pg.error,), db_err:
23 sys.stderr.write('Failed to query database: %s\n'%(db_err.strip()))
25 for (id, slot) in outstanding:
26 (worked, code, string) = self.vending_machine.vend(slot)
27 print (worked, code, string)
29 query = 'SELECT vend_success(%s)'%id
30 self.db.query(query).getresult()
32 query = 'SELECT vend_failed(%s)'%id
33 self.db.query(query).getresult()
35 def handle_events(self):
36 notifier = self.db.getnotify()
37 while notifier is not None:
38 self.process_requests()
39 notify = self.db.getnotify()
43 info = pwd.getpwuid(uid)
46 if info.pw_dir == None: return False
47 pinfile = os.path.join(info.pw_dir, '.pin')
60 if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
64 def has_good_pin(uid):
65 return get_pin != None
67 def verify_user_pin(uid, pin):
68 if get_pin(uid) == pin:
69 info = pwd.getpwuid(uid)
74 def door_open_mode(vending_machine):
75 print "Entering open door mode"
76 v.display("DOOR OPEN")
84 if params == 1: # door closed
89 if __name__ == '__main__':
90 # Open vending machine via LAT
91 latclient = LATClient(service = 'VEND', password = 'dmscptd')
92 (rfh, wfh) = latclient.get_fh()
93 v = VendingMachine(rfh, wfh)
94 print 'PING is', v.ping()
95 #print 'BEEP is', v.beep()
96 #print 'VEND 11 is', v.vend('11')
97 #print 'SILENCE is', v.silence()
98 #print 'DISPLAY is', v.display('WELCOME')
99 print 'S is', v.get_switches()
101 db = DispenseDatabase(v)
102 db.process_requests()
107 scrolling_message = [GREETING]
108 scrolling_wraps = False
111 last_tick = time.time()
114 if timeout != None and timeout > 0 and time.time() > last_tick+1:
116 if len(scrolling_message) > 0:
118 if need_repaint and len(scrolling_message) > 0:
119 v.display(scrolling_message[0])
121 scrolling_message.append(scrolling_message[0])
122 del scrolling_message[0]
135 scrolling_message = [GREETING]
136 scrolling_wraps = False
138 elif event == SWITCH:
139 # don't care right now.
143 # complicated key handling here:
144 if len(cur_user) < 5:
147 scrolling_message = [GREETING]
148 scrolling_wraps = False
151 cur_user += chr(key + ord('0'))
152 v.display('UID: '+cur_user)
153 if len(cur_user) == 5:
155 if not has_good_pin(uid):
156 v.display('PIN NO GOOD')
161 elif len(cur_pin) < PIN_LENGTH:
165 scrolling_message = [GREETING]
166 scrolling_wraps = False
172 cur_pin += chr(key + ord('0'))
173 v.display('PIN: '+'X'*len(cur_pin))
174 if len(cur_pin) == PIN_LENGTH:
175 name = verify_user_pin(int(cur_user), int(cur_pin))
180 scrolling_message = [' WELCOME ', name]
181 scrolling_message.append('OR A SNACK')
182 scrolling_wraps = True
186 scrolling_message = [' BAD PIN ', ' SORRY ', GREETING]
187 scrolling_wraps = False
193 elif len(cur_selection) < 2:
195 if cur_selection == '':
200 cur_selection += chr(key + ord('0'))
201 if len(cur_selection) == 2:
202 make_selection(cur_selection)