Start of asynchronous support - reading database & vending machine simultaneously
[zanchey/dispense2.git] / sql-edition / servers / VendServer.py
index 57a2682..c3f04bc 100755 (executable)
@@ -1,44 +1,54 @@
 #!/usr/bin/python
 
-import sys, os, string, socket, time
-from popen2 import popen2
-from pyPgSQL import PgSQL
+import sys, os, string, socket, time, re
+import pg
 from LATClient import LATClient
+from VendingMachine import VendingMachine
 
-class VendingMachine:
-       def __init__(self, rfh, wfh):
-               self.rfh = rfh
-               self.wfh = wfh
-               self.wfh.write('\n')
-               self.await_prompt()
+class DispenseDatabase:
+       def __init__(self, vending_machine):
+               self.vending_machine = vending_machine
+               self.db = pg.DB(dbname = 'dispense', host = 'dispense.ucc.gu.uwa.edu.au', user = 'vendserver', passwd = 'revresdnev')
+               self.db.query('LISTEN vend_requests')
 
-       def await_prompt(self):
-               self.wfh.flush()
-               state = 0
-               s = ''
-               while state != 3:
-                   s = self.rfh.read(1)
-                   if s == '': raise Exception
-                   if s == '\n' and state == 0: state = 1
-                   if s == '#' and state == 1: state = 2
-                   if s == ' ' and state == 2: state = 3
+       def process_requests(self):
+               print 'processing'
+               query = 'SELECT request_id, request_slot FROM vend_requests WHERE request_handled = false'
+               try:
+                       outstanding = self.db.query(query).getresult()
+               except (pg.error,), db_err:
+                       sys.stderr.write('Failed to query database: %s\n'%(db_err.strip()))
+                       return
+               for (id, slot) in outstanding:
+                       (worked, code, string) = self.vending_machine.vend(slot)
+                       print (worked, code, string)
+                       if worked:
+                               query = 'SELECT vend_success(%s)'%id
+                               self.db.query(query).getresult()
+                       else:
+                               query = 'SELECT vend_failed(%s)'%id
+                               self.db.query(query).getresult()
 
-       def get_response(self):
-               self.wfh.flush()
-               s = ''
-               while s == '':
-                   s = self.rfh.readline()
-                   if s == '': return None
-                   s = s.strip('\r\n')
-               return s
-
-       def ping(self):
-               self.wfh.write('PING\n')
-               return self.get_response()
+       def handle_events(self):
+               notifier = self.db.getnotify()
+               while notifier is not None:
+                       self.process_requests()
+                       notify = self.db.getnotify()
 
 if __name__ == '__main__':
        # Open vending machine via LAT
        latclient = LATClient(service = 'VEND', password = 'dmscptd')
        (rfh, wfh) = latclient.get_fh()
        v = VendingMachine(rfh, wfh)
-       print v.ping()
+       print 'PING is', v.ping()
+       #print 'BEEP is', v.beep()
+       #print 'VEND 11 is', v.vend('11')
+       #print 'SILENCE is', v.silence()
+       #print 'DISPLAY is', v.display('WELCOME')
+       print 'S is', v.get_switches()
+
+       db = DispenseDatabase(v)
+       db.process_requests()
+       while True:
+               v.wait_for_events(1)
+               db.handle_events()

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