-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmachine_app.py
72 lines (55 loc) · 1.77 KB
/
machine_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import yaml
import json
import requests
from two1.lib.wallet import Wallet
from two1.lib.bitserv.flask import Payment
# import flask web microframework
from flask import Flask
import atexit
import threading
from datetime import datetime, timedelta
from payout import execute_payout, execute_mock
PaymentLock = threading.Lock()
PaymentThread = threading.Thread()
# remove for dev env
wallet = Wallet()
# wallet = None
PAYMENT_REQ = 10000
CURR_PRICE = 'https://api.coindesk.com/v1/bpi/currentprice.json'
# fetch current bitcoin price
def get_quote():
q = requests.get(CURR_PRICE)
if q.content:
quote = json.loads(q.content.decode('utf-8'))
usd = quote.get('bpi').get('USD')
return usd.get('rate_float')
def vending_machine():
app = Flask(__name__)
def interrupt():
global PaymentThread
#PaymentThread.cancel()
def doPayment():
global wallet
global PAYMENT_REQ
global get_quote
global PaymentThread
usd_rate = get_quote()
with PaymentLock:
PaymentLock.acquire()
#next_date = execute_payout(conn, wallet, usd_rate, PAYMENT_REQ)
#next_date = execute_mock(conn, wallet, usd_rate, PAYMENT_REQ)
Paymentlock.release()
# Set the next event to happen
wait = datetime.now() + timedelta(hours=24) - next_date
PaymentThread = threading.Timer(wait.total_seconds(), doPayment)
PaymentThread.start()
def doPaymentStart():
global PaymentThread
wait = timedelta(hours=24)
PaymentThread = threading.Timer(wait.total_seconds(), doPayment)
PaymentThread.start()
# Initiate
# doPaymentStart()
# clear the trigger for the next thread
atexit.register(interrupt)
return app