SWITCH = 2
KEY = 3
+class DispenseDatabaseException(Exception): pass
+
class DispenseDatabase:
def __init__(self, vending_machine, host, name, user, password):
self.vending_machine = vending_machine
self.db.query('LISTEN vend_requests')
def process_requests(self):
- print 'processing'
+ 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:
- sys.stderr.write('Failed to query database: %s\n'%(db_err.strip()))
- return
+ raise DispenseDatabaseException('Failed to query database: %s\n'%(db_err.strip()))
for (id, slot) in outstanding:
(worked, code, string) = self.vending_machine.vend(slot)
- print (worked, code, string)
+ logging.debug (str((worked, code, string)))
if worked:
query = 'SELECT vend_success(%s)'%id
self.db.query(query).getresult()
return None
def door_open_mode(v):
- print "Entering open door mode"
+ logging.warning("Entering open door mode")
v.display("-FEED ME-")
while True:
e = v.next_event()
(event, params) = e
if event == DOOR:
if params == 1: # door closed
+ logging.warning('Leaving open door mode')
v.display("-YUM YUM!-")
sleep(1)
return
def run_forever(rfh, wfh, options, cf):
v = VendingMachine(rfh, wfh)
- print 'PING is', v.ping()
+ logging.debug('PING is', v.ping())
if USE_DB: db = DispenseDatabase(v, cf.DBServer, cf.DBName, cf.DBUser, cf.DBPassword)
cur_user = ''
last_timeout_refresh = None
while True:
- if USE_DB: db.handle_events()
+ if USE_DB:
+ try:
+ db.handle_events()
+ except DispenseDatabaseException, e:
+ logging.error('Database error: '+str(e))
if time_to_autologout != None:
time_left = time_to_autologout - time()
continue
time_to_idle = None
(event, params) = e
- print e
+ logging.debug('Got event: ' + repr(e))
if event == DOOR:
if params == 0:
door_open_mode(v);
op.add_option('-s', '--syslog', dest='syslog', action='store_true', default=False, help='log output to syslog')
op.add_option('-d', '--daemon', dest='daemon', action='store_true', default=False, help='run as a daemon')
op.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='spit out lots of debug output')
+ op.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False, help='only report errors')
op.add_option('--pid-file', dest='pid_file', metavar='FILE', default='', help='store daemon\'s pid in the given file')
options, args = op.parse_args()
self.__dict__[option] = value
except ConfigParser.Error, e:
- print "Error reading config file "+config_file+": " + str(e)
+ logging.critical("Error reading config file "+config_file+": " + str(e))
+ logging.critical("Bailing out")
sys.exit(1)
def create_pid_file(name):
signal.signal(signal.SIGINT, stop_server)
options = parse_args()
+ set_up_logging(options)
config_opts = VendConfigFile(options.config_file, config_options)
if options.daemon: become_daemon()
- set_up_logging(options)
if options.pid_file != '': create_pid_file(options.pid_file)
return options, config_opts
sys_logger = logging.handlers.SysLogHandler('/dev/log', 'daemon')
sys_logger.setFormatter(logging.Formatter('vendserver[%d]'%(os.getpid()) + ' %(levelname)s: %(message)s'))
logger.addHandler(sys_logger)
-
- if options.verbose:
+
+ if options.quiet:
+ logger.setLevel(logging.WARNING)
+ elif options.verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)