Commit c46bfcfa authored by Mark Tearle's avatar Mark Tearle
Browse files

Remove redundant code associated with Bernard Postgres Dispense prototype

parent 8be035ce
#!/usr/bin/python #!/usr/bin/python
# vim:ts=4 # vim:ts=4
USE_DB = 0
USE_MIFARE = 1 USE_MIFARE = 1
import ConfigParser import ConfigParser
import sys, os, string, re, pwd, signal, math, syslog import sys, os, string, re, pwd, signal, math, syslog
import logging, logging.handlers import logging, logging.handlers
from traceback import format_tb from traceback import format_tb
if USE_DB: import pg
from time import time, sleep, mktime, localtime from time import time, sleep, mktime, localtime
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from LATClient import LATClient, LATClientException from LATClient import LATClient, LATClientException
...@@ -95,36 +93,6 @@ class VendConfigFile: ...@@ -95,36 +93,6 @@ class VendConfigFile:
except ConfigParser.Error, e: except ConfigParser.Error, e:
raise SystemExit("Error reading config file "+config_file+": " + str(e)) raise SystemExit("Error reading config file "+config_file+": " + str(e))
class DispenseDatabaseException(Exception): pass
class DispenseDatabase:
def __init__(self, vending_machine, host, name, user, password):
self.vending_machine = vending_machine
self.db = pg.DB(dbname = name, host = host, user = user, passwd = password)
self.db.query('LISTEN vend_requests')
def process_requests(self):
logging.debug('database 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:
raise DispenseDatabaseException('Failed to query database: %s\n'%(db_err.strip()))
for (id, slot) in outstanding:
(worked, code, string) = self.vending_machine.vend(slot)
logging.debug (str((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 handle_events(self):
notifier = self.db.getnotify()
while notifier is not None:
self.process_requests()
notify = self.db.getnotify()
""" """
This class manages the current state of the vending machine. This class manages the current state of the vending machine.
""" """
...@@ -927,18 +895,10 @@ class VendServer(): ...@@ -927,18 +895,10 @@ class VendServer():
logging.debug('PING is ' + str(self.v.ping())) logging.debug('PING is ' + str(self.v.ping()))
if USE_DB: db = DispenseDatabase(v, cf.DBServer, cf.DBName, cf.DBUser, cf.DBPassword)
self.setup_idlers() self.setup_idlers()
self.reset_idler() self.reset_idler()
while True: while True:
if USE_DB:
try:
db.handle_events()
except DispenseDatabaseException, e:
logging.error('Database error: '+str(e))
timeout = self.time_to_next_update() timeout = self.time_to_next_update()
(event, params) = self.v.next_event(timeout) (event, params) = self.v.next_event(timeout)
self.run_handler(event, params) self.run_handler(event, params)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment