4 import sys, os, string, time, re, pwd
6 from LATClient import LATClient
7 from VendingMachine import VendingMachine
9 GREETING = 'UCC SNACKS'
16 class DispenseDatabase:
17 def __init__(self, vending_machine):
18 self.vending_machine = vending_machine
19 self.db = pg.DB(dbname = 'dispense', host = 'dispense.ucc.gu.uwa.edu.au', user = 'vendserver', passwd = 'revresdnev')
20 self.db.query('LISTEN vend_requests')
22 def process_requests(self):
24 query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false'
26 outstanding = self.db.query(query).getresult()
27 except (pg.error,), db_err:
28 sys.stderr.write('Failed to query database: %s\n'%(db_err.strip()))
30 for (id, slot) in outstanding:
31 (worked, code, string) = self.vending_machine.vend(slot)
32 print (worked, code, string)
34 query = 'SELECT vend_success(%s)'%id
35 self.db.query(query).getresult()
37 query = 'SELECT vend_failed(%s)'%id
38 self.db.query(query).getresult()
40 def handle_events(self):
41 notifier = self.db.getnotify()
42 while notifier is not None:
43 self.process_requests()
44 notify = self.db.getnotify()
48 info = pwd.getpwuid(uid)
51 if info.pw_dir == None: return False
52 pinfile = os.path.join(info.pw_dir, '.pin')
65 if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
69 def has_good_pin(uid):
70 return get_pin(uid) != None
72 def verify_user_pin(uid, pin):
73 if get_pin(uid) == pin:
74 info = pwd.getpwuid(uid)
79 def door_open_mode(vending_machine):
80 print "Entering open door mode"
81 v.display("DOOR OPEN")
89 if params == 1: # door closed
94 if __name__ == '__main__':
95 # Open vending machine via LAT
96 latclient = LATClient(service = 'VEND', password = 'dmscptd')
97 (rfh, wfh) = latclient.get_fh()
98 v = VendingMachine(rfh, wfh)
99 print 'PING is', v.ping()
100 #print 'BEEP is', v.beep()
101 #print 'VEND 11 is', v.vend('11')
102 #print 'SILENCE is', v.silence()
103 #print 'DISPLAY is', v.display('WELCOME')
104 print 'S is', v.get_switches()
106 db = DispenseDatabase(v)
107 db.process_requests()
112 scrolling_message = [GREETING]
113 scrolling_wraps = False
116 last_tick = time.time()
119 if time.time() > last_tick+1:
120 if timeout != None and timeout > 0: timeout -= 1
121 if len(scrolling_message) > 0:
123 if need_repaint and len(scrolling_message) > 0:
124 v.display(scrolling_message[0])
126 scrolling_message.append(scrolling_message[0])
127 del scrolling_message[0]
134 scrolling_message = [GREETING]
135 scrolling_wraps = False
151 scrolling_message = [GREETING]
152 scrolling_wraps = False
154 elif event == SWITCH:
155 # don't care right now.
159 # complicated key handling here:
160 if len(cur_user) < 5:
163 scrolling_message = [GREETING]
164 scrolling_wraps = False
167 cur_user += chr(key + ord('0'))
168 scrolling_message = []
169 v.display('UID: '+cur_user)
170 if len(cur_user) == 5:
172 if not has_good_pin(uid):
173 scrolling_message = [' INVALID ', ' PIN', ' `SETUP', GREETING]
174 scrolling_wraps = False
181 scrolling_message = []
183 elif len(cur_pin) < PIN_LENGTH:
187 scrolling_message = [GREETING]
188 scrolling_wraps = False
193 scrolling_message = []
195 cur_pin += chr(key + ord('0'))
196 v.display('PIN: '+'X'*len(cur_pin))
197 scrolling_message = []
198 if len(cur_pin) == PIN_LENGTH:
199 username = verify_user_pin(int(cur_user), int(cur_pin))
204 scrolling_message = [' WELCOME ', username]
205 scrolling_message.append('OR A SNACK')
206 scrolling_wraps = True
211 scrolling_message = [' BAD PIN ', ' SORRY ', GREETING]
212 scrolling_wraps = False
218 elif len(cur_selection) == 0:
225 scrolling_message = [GREETING]
226 scrolling_wraps = False
229 cur_selection += chr(key + ord('0'))
230 scrolling_message = []
231 v.display('SELECT: '+cur_selection)
232 elif len(cur_selection) == 1:
235 scrolling_message = []
236 v.display('SELECT: ')
239 cur_selection += chr(key + ord('0'))
240 #make_selection(cur_selection)
241 # XXX this should move somewhere else:
242 if cur_selection == '55':
243 v.display('GOT DOOR?')
244 os.system('su - "%s" -c "dispense door"'%username)
245 elif cur_selection[1] == '8':
246 v.display('GOT COKE?')
247 os.system('su - "%s" -c "dispense %s"'%(username, cur_selection[0]))
249 v.display('HERES A '+cur_selection)
250 v.vend(cur_selection)
252 v.display('THANK YOU')
255 scrolling_message = [
263 scrolling_wraps = True