forked from 21isenough/LightningATM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·227 lines (181 loc) · 6.93 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
#!/usr/bin/python3
## Import python libraries
import RPi.GPIO as GPIO
import os, sys, time, logging
from PIL import Image, ImageFont, ImageDraw
## Import own modules
import lightning, display, qr
## Import utils.py and config.py
from utils import *
from config import *
## Check EPD_SIZE is defined
EPD_SIZE=0.0
if os.path.exists('/etc/default/epd-fuse'):
exec(open('/etc/default/epd-fuse').read())
if EPD_SIZE == 0.0:
print("Please select your screen size by running 'papirus-config'.")
sys.exit()
## Set sat, fiat
FIAT = 0
SATS = 0
INVOICE = ''
## Set btc and sat price
BTCPRICE = getbtcprice(CURRENCY)
SATPRICE = round((1 / (BTCPRICE * 100)) * 100000000, 2)
## Button / Acceptor Pulses
LASTIMPULSE = 0
PULSES = 0
LASTPUSHES = 0
PUSHES = 0
def main():
global FIAT
global SATS
global PULSES
global PUSHES
global INVOICE
## Initiating logging instance
logging.basicConfig(filename='/home/pi/LightningATM/resources/debug.log',
format='%(asctime)s %(name)s %(levelname)s %(message)s',
datefmt='%Y/%m/%d %I:%M:%S %p',
level=logging.INFO)
logging.info('Application started')
## Display startup startup_screen
display.update_startup_screen()
## Defining GPIO BCM Mode
GPIO.setmode(GPIO.BCM)
## Setup GPIO Pins for coin acceptor and button
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=buttonevent, bouncetime=200)
GPIO.add_event_detect(6,GPIO.FALLING,callback=coinevent)
while True:
time.sleep(0.5)
## Detect when coins are being inserted
if((time.time() - LASTIMPULSE > 0.5) and (PULSES > 0)):
if (PULSES == 2):
FIAT += 0.02
SATS = FIAT * 100 * SATPRICE
logging.info('2 cents added')
update_amount_screen(PAPIRUS)
if (PULSES == 3):
FIAT += 0.05
SATS = FIAT * 100 * SATPRICE
logging.info('5 cents added')
update_amount_screen(PAPIRUS)
if (PULSES == 4):
FIAT += 0.1
SATS = FIAT * 100 * SATPRICE
logging.info('10 cents added')
update_amount_screen(PAPIRUS)
if (PULSES == 5):
FIAT += 0.2
SATS = FIAT * 100 * SATPRICE
logging.info('20 cents added')
update_amount_screen(PAPIRUS)
if (PULSES == 6):
FIAT += 0.5
SATS = FIAT * 100 * SATPRICE
logging.info('50 cents added')
update_amount_screen(PAPIRUS)
if (PULSES == 7):
FIAT += 1
SATS = FIAT * 100 * SATPRICE
logging.info('100 cents added')
update_amount_screen(PAPIRUS)
PULSES = 0
## Detect if the buttons has been pushed
if((time.time() - LASTPUSHES > 0.5) and (PUSHES > 0)):
if (PUSHES == 1):
if FIAT == 0:
display.update_nocoin_screen()
time.sleep(3)
display.update_startup_screen()
else:
display.update_qr_request()
INVOICE = qr.scan()
while INVOICE == False:
display.update_qr_failed()
time.sleep(1)
display.update_qr_request()
INVOICE = qr.scan()
update_payout_screen(PAPIRUS)
if (PUSHES == 2):
logging.info('Button pushed twice (add coin)')
print('Button pushed twice (add coin)')
PULSES = 2
if (PUSHES == 3):
logging.warning('Button pushed three times (restart)')
print('Button pushed three times (restart)')
os.execv('/home/pi/LightningATM/app.py', [''])
GPIO.cleanup()
if (PUSHES == 4):
display.update_shutdown_screen()
GPIO.cleanup()
logging.info('ATM shutdown (4 times button)')
os.system('sudo shutdown -h now')
PUSHES = 0
def buttonevent(channel):
global LASTPUSHES
global PUSHES
LASTPUSHES = time.time()
PUSHES = PUSHES + 1
def coinevent(channel):
global LASTIMPULSE
global PULSES
LASTIMPULSE = time.time()
PULSES = PULSES + 1
print(PULSES)
def update_amount_screen(papirus):
## initially set all white background
image = Image.new('1', PAPIRUS.size, WHITE)
## Set width and heigt of screen
width, height = image.size
## prepare for drawing
draw = ImageDraw.Draw(image)
draw.rectangle((2, 2, width - 2, height - 2), fill=WHITE, outline=BLACK)
draw.text((13, 10), str(round(SATS)) + ' sats', fill=BLACK, font=createfont('freemono',28))
draw.text((11, 37), '(' + '%.2f' % round(FIAT,2) + ' ' + CURRENCY + ')', fill=BLACK, font=createfont('freemono',20))
draw.text((11, 70), '(1 cent = ' + str(SATPRICE) + ' sats)', fill=BLACK, font=createfont('freemono',14))
PAPIRUS.display(image)
PAPIRUS.partial_update()
def update_payout_screen(papirus):
# global INVOICE
## initially set all white background
image = Image.new('1', PAPIRUS.size, WHITE)
## Set width and heigt of screen
width, height = image.size
## prepare for drawing
draw = ImageDraw.Draw(image)
draw.rectangle((2, 2, width - 2, height - 2), fill=WHITE, outline=BLACK)
draw.text((15, 30), str(round(SATS)) + ' sats' , fill=BLACK, font=createfont('freemono',20))
draw.text((15, 50), 'on the way!' , fill=BLACK, font=createfont('freemono',15))
PAPIRUS.display(image)
PAPIRUS.update()
# INVOICE = qr.scan()
decodreq = lightning.decoderequest(INVOICE)
print(decodreq,round(SATS))
if (decodreq == str(round(SATS))) or (decodreq == True):
lightning.payout(SATS, INVOICE)
result = lightning.lastpayment(INVOICE)
draw.text((15, 70), str(result), fill=BLACK, font=createfont('freemono',15))
PAPIRUS.display(image)
PAPIRUS.partial_update()
time.sleep(1)
if result == 'Success':
display.update_thankyou_screen()
else:
display.update_payment_failed()
time.sleep(120)
#os.execl(os.path.expanduser('~/LightningATM/app.py'), *sys.argv)
os.execv('/home/pi/LightningATM/app.py', [''])
else:
print('Please show correct invoice')
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
display.update_shutdown_screen()
GPIO.cleanup()
logging.info('Application finished (Keyboard Interrupt)')
sys.exit('Manually Interrupted')