summaryrefslogtreecommitdiffstats
path: root/bitvend/processor.py
diff options
context:
space:
mode:
authorPiotr Dobrowolski <admin@tastycode.pl>2017-01-14 02:04:41 +0100
committerPiotr Dobrowolski <admin@tastycode.pl>2017-01-14 02:05:25 +0100
commit3f85eab05dafe6be5142c9a3f7c02368e67df0a5 (patch)
treee1267a104416dc7ed133d834c08befb482d489a7 /bitvend/processor.py
parent4215faffd1225f8891cb378f0db295f07c3972f6 (diff)
downloadbitvend-3f85eab05dafe6be5142c9a3f7c02368e67df0a5.tar.gz
bitvend-3f85eab05dafe6be5142c9a3f7c02368e67df0a5.tar.bz2
bitvend-3f85eab05dafe6be5142c9a3f7c02368e67df0a5.tar.xz
bitvend-3f85eab05dafe6be5142c9a3f7c02368e67df0a5.zip
Add storage/models, bring back stats
Diffstat (limited to 'bitvend/processor.py')
-rw-r--r--bitvend/processor.py35
1 files changed, 29 insertions, 6 deletions
diff --git a/bitvend/processor.py b/bitvend/processor.py
index 1291940..8b7c9f1 100644
--- a/bitvend/processor.py
+++ b/bitvend/processor.py
@@ -1,24 +1,35 @@
import threading
import json
import websocket
-import pprint
import time
import logging
from bitvend.utils import to_local_currency
+from bitvend.models import db, Transaction
class PaymentProcessor(threading.Thread):
daemon = True
input_address = None
device = None
+ last_pong = None
+ app = None
- def __init__(self, device, input_address='12fkW5EBb3uBy1zD8pan4TcbabP5Fjato7'):
+ def __init__(self, device, input_address=None, app=None):
super(PaymentProcessor, self).__init__()
self.device = device
self.input_address = input_address
self.logger = logging.getLogger(type(self).__name__)
+ if app:
+ self.init_app(app)
+
+ def init_app(self, app):
+ self.app = app
+
+ if not self.input_address:
+ self.input_address = self.app.config['INPUT_ADDRESS']
+
def run(self):
self.logger.info('Starting...')
ws = websocket.WebSocketApp(
@@ -46,6 +57,12 @@ class PaymentProcessor(threading.Thread):
self.logger.info('%r %r %r %r %r', tx_size, tx_hash, tx_value, fee, fee_byte)
self.logger.info('In local currency: %r', to_local_currency(tx_value))
+ with self.app.app_context():
+ tx = Transaction(tx_hash=tx_hash)
+ tx.value = to_local_currency(tx_value)
+ db.session.add(tx)
+ db.session.commit()
+
if to_local_currency(tx_value) < 100:
self.logger.warning('Whyyyy so low...')
return
@@ -57,15 +74,18 @@ class PaymentProcessor(threading.Thread):
self.logger.info('Transaction ok, going to device...')
# FIXME we need better handling of ACK on POLL responses...
- self.device.begin_session(to_local_currency(tx_value))
- self.device.begin_session(to_local_currency(tx_value))
- self.device.begin_session(to_local_currency(tx_value))
+ self.device.begin_session(to_local_currency(tx_value), tx_hash)
+ self.device.begin_session(to_local_currency(tx_value), tx_hash)
+ self.device.begin_session(to_local_currency(tx_value), tx_hash)
def on_message(self, ws, message):
#print message
data = json.loads(message)
+
if data['op'] == 'utx':
self.process_transaction(data)
+ elif data['op'] == 'pong':
+ self.last_pong = time.time()
def on_error(self, ws, error):
self.logger.error(error)
@@ -85,10 +105,13 @@ class PaymentProcessor(threading.Thread):
def keepalive(self, ws):
# Keepalive thread target, just send ping once in a while
+ self.last_pong = time.time()
while True:
# FIXME check last ping time
- self.logger.info('Pinging...')
ws.send(json.dumps({
"op": "ping"
}))
time.sleep(20)
+
+ if time.time() - self.last_pong > 60:
+ ws.close()