54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import time
|
|
|
|
from bitvend.models import db, Transaction
|
|
from bitvend.stats import cashless_purchase_counter, coin_counter, purchase_counter
|
|
|
|
from mdb.device import CashlessMDBDevice
|
|
from mdb.constants import *
|
|
|
|
|
|
class BitvendCashlessMDBDevice(CashlessMDBDevice):
|
|
current_tx_id = None
|
|
app = None
|
|
|
|
def init_app(self, app):
|
|
self.app = app
|
|
|
|
def begin_session(self, amount, tx_id=None):
|
|
self.current_tx_id = tx_id
|
|
|
|
super(BitvendCashlessMDBDevice, self).begin_session(amount)
|
|
|
|
def vend_request(self, product, value):
|
|
# FIXME we report success here, because database write takes too much
|
|
# time to respond in 5ms.
|
|
self.send([0x05, 0x00, 0xFF])
|
|
self.poll_queue.put([0x05])
|
|
self.current_request.processed = True
|
|
|
|
self.logger.info("got vend request: %r", self.current_tx_id)
|
|
|
|
if self.current_tx_id:
|
|
with self.app.app_context():
|
|
tx = Transaction.query.get(self.current_tx_id)
|
|
tx.product_id = product + 9
|
|
tx.product_value = value
|
|
|
|
if tx.amount is None:
|
|
tx.amount = -value
|
|
|
|
db.session.commit()
|
|
|
|
return True
|
|
|
|
def vend_failure(self):
|
|
with self.app.app_context():
|
|
self.logger.info("Reverting transaction {}...".format(self.current_tx_id))
|
|
Transaction.query.filter(Transaction.id == self.current_tx_id).delete()
|
|
db.session.commit()
|
|
|
|
def vend_complete(self):
|
|
self.logger.info("Vend complete!")
|
|
self.current_tx_id = None
|
|
cashless_purchase_counter.inc()
|