forked from 21isenough/LightningATM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·326 lines (277 loc) · 9.78 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/python3
import logging
import os
import sys
import time
import math
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw
import display
import lndrest
import lntxbot
import qr
import config
import utils
import importlib
import graphicpage
import graphicapp
from kivy.app import App
import threading
led = "off"
logger = logging.getLogger("MAIN")
graphic_app = graphicapp.GraficApp()
def softreset():
"""Displays startup screen and deletes fiat amount
"""
global led
config.SATS = 0
config.FIAT = 0
# Turn off button LED
GPIO.output(13, GPIO.LOW)
led = "off"
display.update_startup_screen()
logger.info("Softreset executed")
def button_event(channel):
"""Registers a button push event
"""
config.LASTPUSHES = time.time()
config.PUSHES = config.PUSHES + 1
def coin_event(channel):
"""Registers a coin insertion event
"""
# print(float(round(time.time() - config.LASTIMPULSE, 3)))
# if time.time() - config.LASTIMPULSE > 0.2:
# config.COINLIST.append("0")
config.LASTIMPULSE = time.time()
config.PULSES = config.PULSES + 1
# config.COINLIST.append("1")
# print(config.COINLIST)
def button_pushed():
"""Actions button pushes by number
"""
if config.PUSHES == 1:
"""If no coins inserted, update the screen.
If coins inserted, scan a qr code for the exchange amount
"""
if config.FIAT == 0:
display.update_nocoin_screen()
time.sleep(3)
display.update_startup_screen()
else:
display.update_qr_request()
qrcode = qr.scan()
config.INVOICE = lndrest.evaluate_scan(qrcode)
while config.INVOICE is False:
display.update_qr_failed()
time.sleep(1)
display.update_qr_request()
qrcode = qr.scan()
config.INVOICE = lndrest.evaluate_scan(qrcode)
display.update_payout_screen()
lndrest.handle_invoice()
# lntxbot.payout(config.SATS, config.INVOICE)
softreset()
if config.PUSHES == 2:
"""If no coins inserted, update the screen.
If coins are inserted, return a lnurl for the exchange amount
"""
if config.FIAT == 0:
display.update_nocoin_screen()
time.sleep(3)
display.update_startup_screen()
else:
lntxbot.process_using_lnurl(config.SATS)
# Softreset and startup screen
softreset()
if config.PUSHES == 3:
"""Store new lntxbot credential via a QR code scan
"""
display.update_lntxbot_scan()
# scan the credentials
lntxcreds = lntxbot.scan_creds()
# save them to the current config and reload config file
config.update_config("lntxbot", "creds", lntxcreds)
if config.check_dangermode():
importlib.reload(config)
# return the current balance to the user on the screen
balance = lntxbot.get_lnurl_balance()
display.update_lntxbot_balance(balance)
softreset()
if config.PUSHES == 4:
"""Simulates adding a coin
"""
logger.info("Button pushed four times (add coin)")
print("Button pushed four times (add coin)")
config.PULSES = 2
if config.PUSHES == 5:
"""Restarts the application
"""
logger.warning("Button pushed five times (restart)")
print("Button pushed five times (restart)")
softreset()
if config.PUSHES == 6:
"""Shutdown the host machine
"""
display.update_shutdown_screen()
GPIO.cleanup()
logger.warning("ATM shutdown (6 times button)")
os.system("sudo shutdown -h now")
config.PUSHES = 0
def coins_inserted():
"""Actions coins inserted
"""
global led
if config.FIAT == 0:
config.BTCPRICE = utils.get_btc_price(config.conf["atm"]["cur"])
config.SATPRICE = math.floor((1 / (config.BTCPRICE * 100)) * 100000000)
logger.info("Satoshi price updated")
if config.PULSES == 2:
config.FIAT += 0.02
config.SATS = utils.get_sats()
config.SATSFEE = utils.get_sats_with_fee()
config.SATS -= config.SATSFEE
logger.info("2 cents added")
display.update_amount_screen()
if config.PULSES == 3:
config.FIAT += 0.05
config.SATS = utils.get_sats()
config.SATSFEE = utils.get_sats_with_fee()
config.SATS -= config.SATSFEE
logger.info("5 cents added")
display.update_amount_screen()
if config.PULSES == 4:
config.FIAT += 0.1
config.SATS = utils.get_sats()
config.SATSFEE = utils.get_sats_with_fee()
config.SATS -= config.SATSFEE
logger.info("10 cents added")
display.update_amount_screen()
if config.PULSES == 5:
config.FIAT += 0.2
config.SATS = utils.get_sats()
config.SATSFEE = utils.get_sats_with_fee()
config.SATS -= config.SATSFEE
logger.info("20 cents added")
display.update_amount_screen()
if config.PULSES == 6:
config.FIAT += 0.5
config.SATS = utils.get_sats()
config.SATSFEE = utils.get_sats_with_fee()
config.SATS -= config.SATSFEE
logger.info("50 cents added")
display.update_amount_screen()
if config.PULSES == 7:
config.FIAT += 1
config.SATS = utils.get_sats()
logger.info("100 cents added")
display.update_amount_screen()
config.PULSES = 0
if config.FIAT > 0 and led == "off":
# Turn on the LED after first coin
GPIO.output(13, GPIO.HIGH)
led = "on"
logger.info("Button-LED turned on (if connected)")
def monitor_coins_and_button():
"""Monitors coins inserted and buttons pushed
"""
print('start monitor_coins_and_buttons')
time.sleep(0.2)
# if config.COINLIST:
# time.sleep(1)
# if config.COINLIST.count("0") > 1:
# print(config.COINLIST[1 : config.COINLIST.index("0", 1)])
# print(len(config.COINLIST[1 : config.COINLIST.index("0", 1)]))
# else:
# print(config.COINLIST[1:])
# print(len(config.COINLIST[1:]))
# if len(config.COINLIST[1:]) > 0:
# config.PULSLIST.append(len(config.COINLIST[1:]))
# del config.COINLIST[: len(config.COINLIST[1:])]
#
# print(config.PULSLIST)
# Detect when coins are being inserted
if (time.time() - config.LASTIMPULSE > 0.5) and (config.PULSES > 0):
coins_inserted()
# Detect if the button has been pushed
if (time.time() - config.LASTPUSHES > 0.5) and (config.PUSHES > 0):
button_pushed()
def setup_coin_acceptor():
"""Initialises the coin acceptor parameters and sets up a callback for button pushes
and coin inserts.
"""
print('begin setup_coin_acceptor')
# Defining GPIO BCM Mode
GPIO.setmode(GPIO.BCM)
# Setup GPIO Pins for coin acceptor, button and button-led
GPIO.setwarnings(False)
GPIO.setup(13, GPIO.OUT)
GPIO.output(13, GPIO.LOW)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Setup coin interrupt channel (bouncetime for switch bounce)
GPIO.add_event_detect(5, GPIO.RISING, callback=button_event, bouncetime=350)
GPIO.add_event_detect(6, GPIO.FALLING, callback=coin_event)
print('end setup_coin_acceptor')
def run_graphic_app(graphic):
graphic.run()
# def check_dangermode():
# """Check for DANGERMODE and wipe any saved credentials if off"
# """
# # if dangermode is NOT on
# if config.conf["atm"]["dangermode"].lower() != "on":
# logger.warning("DANGERMODE off")
#
# # wipe any saved values from the config by saving an empty value to it
# config.update_config("lntxbot", "creds", "")
# config.update_config("lnd", "macaroon", "")
# config.update_config("atm", "activewallet", "")
#
# # get the static dict from within the conf and overwrite it to config.conf
# config.conf = config.conf._sections
#
# # get new lntxbot creds from qr code scan
# print("Scan lntxbot creds now\n")
# print(" +---+")
# print("+-----------+---+")
# print("| .-. |")
# print("| ( ) |")
# print("| `-' |")
# print("+---------------+\n")
# time.sleep(2)
# try:
# config.conf["lntxbot"]["creds"] = lntxbot.scan_creds()
# logger.info("Credentials saved in volatile memory (deleted after reboot)")
# except utils.ScanError:
# logger.error("Error scanning lntxbot creds with dangermode off")
# return
# else:
# logger.info("DANGERMODE on. Loading values from config.ini...")
# # config.check_config()
def main():
#utils.check_epd_size()
logger.info("Application started")
# Checks dangermode and start scanning for credentials
# Only activate once software ready for it
# check_dangermode()
t = threading.Thread(target=run_graphic_app, args=(graphic_app,))
t.start()
print('before startup_screen')
time.sleep(1)
# Display startup startup_screen
display.update_startup_screen()
setup_coin_acceptor()
while True:
monitor_coins_and_button()
if __name__ == "__main__":
while True:
try:
main()
except KeyboardInterrupt:
display.update_shutdown_screen()
GPIO.cleanup()
logger.info("Application finished (Keyboard Interrupt)")
sys.exit("Manually Interrupted")
except Exception:
logger.exception("Oh no, something bad happened! Restarting...")
GPIO.cleanup()
os.execv("/home/pi/LightningATM/app.py", [""])