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")
87 if params == 1: # door closed
92 if __name__ == '__main__':
93 # Open vending machine via LAT
94 latclient = LATClient(service = 'VEND', password = 'dmscptd')
95 (rfh, wfh) = latclient.get_fh()
96 v = VendingMachine(rfh, wfh)
97 print 'PING is', v.ping()
98 #print 'BEEP is', v.beep()
99 #print 'VEND 11 is', v.vend('11')
100 #print 'SILENCE is', v.silence()
101 #print 'DISPLAY is', v.display('WELCOME')
102 print 'S is', v.get_switches()
104 db = DispenseDatabase(v)
105 db.process_requests()
110 scrolling_message = [GREETING]
111 scrolling_wraps = False
114 last_tick = time.time()
117 if time.time() > last_tick+1:
118 if timeout != None and timeout > 0: timeout -= 1
119 if len(scrolling_message) > 0:
121 if need_repaint and len(scrolling_message) > 0:
122 v.display(scrolling_message[0])
124 scrolling_message.append(scrolling_message[0])
125 del scrolling_message[0]
132 scrolling_message = [GREETING]
133 scrolling_wraps = False
147 scrolling_message = [GREETING]
148 scrolling_wraps = False
150 elif event == SWITCH:
151 # don't care right now.
155 # complicated key handling here:
156 if len(cur_user) < 5:
159 scrolling_message = [GREETING]
160 scrolling_wraps = False
163 cur_user += chr(key + ord('0'))
164 scrolling_message = []
165 v.display('UID: '+cur_user)
166 if len(cur_user) == 5:
168 if not has_good_pin(uid):
169 scrolling_message = [' INVALID ', ' PIN', ' `SETUP', GREETING]
170 scrolling_wraps = False
177 scrolling_message = []
179 elif len(cur_pin) < PIN_LENGTH:
183 scrolling_message = [GREETING]
184 scrolling_wraps = False
189 scrolling_message = []
191 cur_pin += chr(key + ord('0'))
192 v.display('PIN: '+'X'*len(cur_pin))
193 scrolling_message = []
194 if len(cur_pin) == PIN_LENGTH:
195 username = verify_user_pin(int(cur_user), int(cur_pin))
200 scrolling_message = [' WELCOME ', username]
201 scrolling_message.append('OR A SNACK')
202 scrolling_wraps = True
207 scrolling_message = [' BAD PIN ', ' SORRY ', GREETING]
208 scrolling_wraps = False
214 elif len(cur_selection) == 0:
221 scrolling_message = [GREETING]
222 scrolling_wraps = False
225 cur_selection += chr(key + ord('0'))
226 scrolling_message = []
227 v.display('SELECT: '+cur_selection)
228 elif len(cur_selection) == 1:
231 scrolling_message = []
232 v.display('SELECT: ')
235 cur_selection += chr(key + ord('0'))
236 #make_selection(cur_selection)
237 # XXX this should move somewhere else:
238 if cur_selection == '55':
239 v.display('GOT DOOR?')
240 os.system('su - "%s" -c "dispense door"'%username)
241 elif cur_selection[1] == '8':
242 v.display('GOT COKE?')
243 os.system('su - "%s" -c "dispense %s"'%(username, cur_selection[0]))
245 v.display('HERES A '+cur_selection)
246 v.vend(cur_selection)
248 v.display('THANK YOU')
251 scrolling_message = [
259 scrolling_wraps = True