Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
UCC
UCC Snack Machine Server Code
Commits
c46bfcfa
Commit
c46bfcfa
authored
Apr 06, 2015
by
Mark Tearle
Browse files
Remove redundant code associated with Bernard Postgres Dispense prototype
parent
8be035ce
Changes
1
Hide whitespace changes
Inline
Side-by-side
VendServer/VendServer.py
View file @
c46bfcfa
#!/usr/bin/python
# vim:ts=4
USE_DB
=
0
USE_MIFARE
=
1
import
ConfigParser
import
sys
,
os
,
string
,
re
,
pwd
,
signal
,
math
,
syslog
import
logging
,
logging
.
handlers
from
traceback
import
format_tb
if
USE_DB
:
import
pg
from
time
import
time
,
sleep
,
mktime
,
localtime
from
subprocess
import
Popen
,
PIPE
from
LATClient
import
LATClient
,
LATClientException
...
...
@@ -95,36 +93,6 @@ class VendConfigFile:
except
ConfigParser
.
Error
,
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.
"""
...
...
@@ -927,18 +895,10 @@ class VendServer():
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
.
reset_idler
()
while
True
:
if
USE_DB
:
try
:
db
.
handle_events
()
except
DispenseDatabaseException
,
e
:
logging
.
error
(
'Database error: '
+
str
(
e
))
timeout
=
self
.
time_to_next_update
()
(
event
,
params
)
=
self
.
v
.
next_event
(
timeout
)
self
.
run_handler
(
event
,
params
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment