A UI on the vending machine.
[uccvend-vendserver.git] / sql-edition / servers / VendServer.py
1 #!/usr/bin/python
2
3 import sys, os, string, time, re, pwd
4 import pg
5 from LATClient import LATClient
6 from VendingMachine import VendingMachine
7
8 GREETING = 'UCC SNACKS'
9 PIN_LENGTH = 4
10
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')
16
17         def process_requests(self):
18                 print 'processing'
19                 query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false'
20                 try:
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()))
24                         return
25                 for (id, slot) in outstanding:
26                         (worked, code, string) = self.vending_machine.vend(slot)
27                         print (worked, code, string)
28                         if worked:
29                                 query = 'SELECT vend_success(%s)'%id
30                                 self.db.query(query).getresult()
31                         else:
32                                 query = 'SELECT vend_failed(%s)'%id
33                                 self.db.query(query).getresult()
34
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()
40
41 def get_pin(uid):
42         try:
43                 info = pwd.getpwuid(uid)
44         except KeyError:
45                 return None
46         if info.pw_dir == None: return False
47         pinfile = os.path.join(info.pw_dir, '.pin')
48         try:
49                 s = os.stat(pinfile)
50         except OSError:
51                 return False
52         if s.st_mode & 077:
53                 return None
54         try:
55                 f = file(pinfile)
56         except IOError:
57                 return None
58         pinstr = f.readline()
59         f.close()
60         if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
61                 return None
62         return int(pinstr)
63
64 def has_good_pin(uid):
65         return get_pin != None
66
67 def verify_user_pin(uid, pin):
68         if get_pin(uid) == pin:
69                 info = pwd.getpwuid(uid)
70                 return info.pw_name
71         else:
72                 return None
73
74 def door_open_mode(vending_machine):
75         print "Entering open door mode"
76         v.display("DOOR  OPEN")
77         while True:
78                 v.wait_for_events(1)
79                 while True:
80                         e = v.next_event()
81                         if e == None: break
82                         (event, params) = e
83                         if event == DOOR:
84                                 if params == 1: # door closed
85                                         v.display("BYE BYE!")
86                                         time.sleep(1)
87                                         return
88
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()
100
101         db = DispenseDatabase(v)
102         db.process_requests()
103         cur_user = ''
104         cur_pin = ''
105         cur_selection = ''
106
107         scrolling_message = [GREETING]
108         scrolling_wraps = False
109         need_repaint = True
110         timeout = None
111         last_tick = time.time()
112
113         while True:
114                 if timeout != None and timeout > 0 and time.time() > last_tick+1:
115                         timeout -= 1
116                         if len(scrolling_message) > 0:
117                                 need_repaint = True
118                 if need_repaint and len(scrolling_message) > 0:
119                         v.display(scrolling_message[0])
120                         if scrolling_wraps:
121                                 scrolling_message.append(scrolling_message[0])
122                         del scrolling_message[0]
123                         need_repaint = False
124
125                 v.wait_for_events(1)
126                 while True:
127                         e = v.next_event()
128                         if e == None: break
129                         (event, params) = e
130                         if event == DOOR:
131                                 if params == 0:
132                                         door_open_mode(v);
133                                         cur_user = ''
134                                         cur_pin = ''
135                                         scrolling_message = [GREETING]
136                                         scrolling_wraps = False
137                                         need_repaint = True
138                         elif event == SWITCH:
139                                 # don't care right now.
140                                 pass
141                         elif event == KEY:
142                                 key = params
143                                 # complicated key handling here:
144                                 if len(cur_user) < 5:
145                                         if key == 11:
146                                                 cur_user = ''
147                                                 scrolling_message = [GREETING]
148                                                 scrolling_wraps = False
149                                                 need_repaint = True
150                                                 continue
151                                         cur_user += chr(key + ord('0'))
152                                         v.display('UID: '+cur_user)
153                                         if len(cur_user) == 5:
154                                                 uid = int(cur_user)
155                                                 if not has_good_pin(uid):
156                                                         v.display('PIN NO GOOD')
157                                                         time.sleep(1)
158                                                         continue
159                                                 v.display('PIN: ')
160                                                 continue
161                                 elif len(cur_pin) < PIN_LENGTH:
162                                         if key == 11:
163                                                 if cur_pin == '':
164                                                         cur_user = ''
165                                                         scrolling_message = [GREETING]
166                                                         scrolling_wraps = False
167                                                         need_repaint = True
168                                                         continue
169                                                 cur_pin = ''
170                                                 v.display('PIN: ')
171                                                 continue
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))
176                                                 if name:
177                                                         v.beep(0, False)
178                                                         cur_selection = ''
179
180                                                         scrolling_message = [' WELCOME  ', name]
181                                                         scrolling_message.append('OR A SNACK')
182                                                         scrolling_wraps = True
183                                                         need_repaint = True
184                                                 else:
185                                                         v.beep(255, False)
186                                                         scrolling_message = [' BAD PIN  ', '  SORRY   ', GREETING]
187                                                         scrolling_wraps = False
188                                                         need_repaint = True
189
190                                                         cur_user = ''
191                                                         cur_pin = ''
192                                                         continue
193                                 elif len(cur_selection) < 2:
194                                         if key == 11:
195                                                 if cur_selection == '':
196                                                         cur_pin = ''
197                                                         cur_user = ''
198                                                         v.display(GREETING)
199                                                         continue
200                                                 cur_selection += chr(key + ord('0'))
201                                                 if len(cur_selection) == 2:
202                                                         make_selection(cur_selection)
203
204
205                 db.handle_events()

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