9d8e0ae9c453b3b2179e4a831e8b6fa140cbb864
[uccvend-vendserver.git] / sql-edition / servers / VendServer.py
1 #!/usr/bin/python
2 # vim:ts=4
3
4 import sys, os, string, time, re, pwd
5 import pg
6 from LATClient import LATClient
7 from VendingMachine import VendingMachine
8
9 GREETING = 'UCC SNACKS'
10 PIN_LENGTH = 4
11
12 DOOR = 1
13 SWITCH = 2
14 KEY = 3
15
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')
21
22         def process_requests(self):
23                 print 'processing'
24                 query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false'
25                 try:
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()))
29                         return
30                 for (id, slot) in outstanding:
31                         (worked, code, string) = self.vending_machine.vend(slot)
32                         print (worked, code, string)
33                         if worked:
34                                 query = 'SELECT vend_success(%s)'%id
35                                 self.db.query(query).getresult()
36                         else:
37                                 query = 'SELECT vend_failed(%s)'%id
38                                 self.db.query(query).getresult()
39
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()
45
46 def get_pin(uid):
47         try:
48                 info = pwd.getpwuid(uid)
49         except KeyError:
50                 return None
51         if info.pw_dir == None: return False
52         pinfile = os.path.join(info.pw_dir, '.pin')
53         try:
54                 s = os.stat(pinfile)
55         except OSError:
56                 return None
57         if s.st_mode & 077:
58                 return None
59         try:
60                 f = file(pinfile)
61         except IOError:
62                 return None
63         pinstr = f.readline()
64         f.close()
65         if not re.search('^'+'[0-9]'*PIN_LENGTH+'$', pinstr):
66                 return None
67         return int(pinstr)
68
69 def has_good_pin(uid):
70         return get_pin(uid) != None
71
72 def verify_user_pin(uid, pin):
73         if get_pin(uid) == pin:
74                 info = pwd.getpwuid(uid)
75                 return info.pw_name
76         else:
77                 return None
78
79 def door_open_mode(vending_machine):
80         print "Entering open door mode"
81         v.display("DOOR  OPEN")
82         while True:
83                 v.wait_for_events(1)
84                 while True:
85                         e = v.next_event()
86                         if e == None: break
87                         (event, params) = e
88                         if event == DOOR:
89                                 if params == 1: # door closed
90                                         v.display("BYE BYE!")
91                                         time.sleep(1)
92                                         return
93
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()
105
106         db = DispenseDatabase(v)
107         db.process_requests()
108         cur_user = ''
109         cur_pin = ''
110         cur_selection = ''
111
112         scrolling_message = [GREETING]
113         scrolling_wraps = False
114         need_repaint = True
115         timeout = None
116         last_tick = time.time()
117
118         while True:
119                 if time.time() > last_tick+1:
120                         if timeout != None and timeout > 0: timeout -= 1
121                         if len(scrolling_message) > 0:
122                                 need_repaint = True
123                 if need_repaint and len(scrolling_message) > 0:
124                         v.display(scrolling_message[0])
125                         if scrolling_wraps:
126                                 scrolling_message.append(scrolling_message[0])
127                         del scrolling_message[0]
128                         need_repaint = False
129                 if timeout == 0:
130                         timeout = None
131                         cur_user = ''
132                         cur_pin = ''
133                         cur_selection = ''
134                         scrolling_message = [GREETING]
135                         scrolling_wraps = False
136                         need_repaint = True
137                         continue
138
139                 v.wait_for_events(1)
140                 while True:
141                         e = v.next_event()
142                         if e == None: break
143                         (event, params) = e
144                         print e
145                         if event == DOOR:
146                                 if params == 0:
147                                         door_open_mode(v);
148                                         cur_user = ''
149                                         cur_pin = ''
150                                         scrolling_message = [GREETING]
151                                         scrolling_wraps = False
152                                         need_repaint = True
153                         elif event == SWITCH:
154                                 # don't care right now.
155                                 pass
156                         elif event == KEY:
157                                 key = params
158                                 # complicated key handling here:
159                                 if len(cur_user) < 5:
160                                         if key == 11:
161                                                 cur_user = ''
162                                                 scrolling_message = [GREETING]
163                                                 scrolling_wraps = False
164                                                 need_repaint = True
165                                                 continue
166                                         cur_user += chr(key + ord('0'))
167                                         scrolling_message = []
168                                         v.display('UID: '+cur_user)
169                                         if len(cur_user) == 5:
170                                                 uid = int(cur_user)
171                                                 if not has_good_pin(uid):
172                                                         scrolling_message = [' INVALID  ', '   PIN', '  `SETUP', GREETING]
173                                                         scrolling_wraps = False
174                                                         need_repaint = True
175                                                         cur_user = ''
176                                                         cur_pin = ''
177                                                         continue
178                                                 cur_pin = ''
179                                                 v.display('PIN: ')
180                                                 scrolling_message = []
181                                                 continue
182                                 elif len(cur_pin) < PIN_LENGTH:
183                                         if key == 11:
184                                                 if cur_pin == '':
185                                                         cur_user = ''
186                                                         scrolling_message = [GREETING]
187                                                         scrolling_wraps = False
188                                                         need_repaint = True
189                                                         continue
190                                                 cur_pin = ''
191                                                 v.display('PIN: ')
192                                                 scrolling_message = []
193                                                 continue
194                                         cur_pin += chr(key + ord('0'))
195                                         v.display('PIN: '+'X'*len(cur_pin))
196                                         scrolling_message = []
197                                         if len(cur_pin) == PIN_LENGTH:
198                                                 username = verify_user_pin(int(cur_user), int(cur_pin))
199                                                 if username:
200                                                         v.beep(0, False)
201                                                         cur_selection = ''
202
203                                                         scrolling_message = [' WELCOME  ', username]
204                                                         scrolling_message.append('OR A SNACK')
205                                                         scrolling_wraps = True
206                                                         need_repaint = True
207                                                         continue
208                                                 else:
209                                                         v.beep(40, False)
210                                                         scrolling_message = [' BAD PIN  ', '  SORRY   ', GREETING]
211                                                         scrolling_wraps = False
212                                                         need_repaint = True
213
214                                                         cur_user = ''
215                                                         cur_pin = ''
216                                                         continue
217                                 elif len(cur_selection) == 0:
218                                         if key == 11:
219                                                 cur_pin = ''
220                                                 cur_user = ''
221                                                 cur_selection = ''
222                                                 v.display('BYE!')
223                                                 time.sleep(0.5)
224                                                 scrolling_message = [GREETING]
225                                                 scrolling_wraps = False
226                                                 need_repaint = True
227                                                 continue
228                                         cur_selection += chr(key + ord('0'))
229                                         scrolling_message = []
230                                         v.display('SELECT: '+cur_selection)
231                                 elif len(cur_selection) == 1:
232                                         if key == 11:
233                                                 cur_selection = ''
234                                                 scrolling_message = []
235                                                 v.display('SELECT: ')
236                                                 continue
237                                         else:
238                                                 cur_selection += chr(key + ord('0'))
239                                                 #make_selection(cur_selection)
240                                                 # XXX this should move somewhere else:
241                                                 if cur_selection == '55':
242                                                         v.display('GOT DOOR?')
243                                                         os.system('su - "%s" -c "dispense door"'%username)
244                                                 elif cur_selection[1] == '8':
245                                                         v.display('GOT COKE?')
246                                                         os.system('su - "%s" -c "dispense %s"'%(username, cur_selection[0]))
247                                                 else:
248                                                         v.display('HERES A '+cur_selection)
249                                                         v.vend(cur_selection)
250                                                 time.sleep(0.5)
251                                                 v.display('THANK YOU')
252                                                 time.sleep(0.5)
253                                                 cur_selection = ''
254                                                 scrolling_message = [
255                                                         'LOGOUT: 5',
256                                                         'LOGOUT: 4',
257                                                         'LOGOUT: 3',
258                                                         'LOGOUT: 2',
259                                                         'LOGOUT: 1',
260                                                         'BYE BYE!']
261                                                 timeout = 7
262                                                 scrolling_wraps = True
263                                                 need_repaint = True
264
265                         v.wait_for_events(0)
266
267                 db.handle_events()

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