-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_gpio.py
More file actions
265 lines (223 loc) · 7.55 KB
/
flask_gpio.py
File metadata and controls
265 lines (223 loc) · 7.55 KB
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
#!/usr/bin/env python3
import flask
import datetime
import sys
import os
import json
import argparse
import multiprocessing
import RPi.GPIO as GPIO
from RebootServer import Setup, Cleanup
from flask import Flask, render_template, Markup, request
from werkzeug.serving import WSGIRequestHandler
from multiprocessing import Pool, TimeoutError, cpu_count, Process, Queue
app = Flask(__name__)
# Try loading the config file and die if not found
#TODO: Add CLI arg for different config file?
try:
os.stat("config.json")
except Exception:
print ("Config file missing!!!")
sys.exit(1)
else:
with open('config.json', 'r') as infile:
config = json.load(infile)
# End with
# End try/except block
# Setup our list of miner objects
miners = Setup(config)
# Setup our results queue
r_queue = Queue()
def makeJoiner(q):
a = Joiner(q)
a.run()
# End def
@app.route("/")
def hello():
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %H:%M")
templateData = {
'title' : 'HELLO!',
'time': timeString
}
return render_template('main.html', **templateData)
@app.route("/reboot/<name>") # Need to pass a parameter called `pass` as well
def reboot(name):
#try:
if name in miners.keys():
miner = miners[name]
else:
return flask.abort(400)
#raise Exception("Name not found")
# End if/else block
password = request.args.get('pass')
print (password)
if not password == config['password']:
return flask.abort(403)
#raise Exception("Invalid password!")
# End if
p = Process(target=miner.reboot, args=())
p.start()
print (multiprocessing.active_children())
#miner.reboot()
response = "Successfully rebooted miner " + name +"!"
#except Exception as e:
# response = "There was an error rebooting miner " + name + "!\n" + str(e)
# End try/except block
templateData = {
'title' : 'Miner "%s" Reboot Status',
'response' : response
}
return render_template('reboot.html', **templateData)
# End def
@app.route("/change/<name>") # Need to pass a parameter called `pass` as well
def change(name):
#try:
if name in miners.keys():
miner = miners[name]
else:
return flask.abort(400)
#raise Exception("Name not found")
# End if/else block
password = request.args.get('pass')
print (password)
if not password == config['password']:
return flask.abort(403)
#raise Exception("Invalid password!")
# End if
p = Process(target=miner.change, args=())
p.start()
print (multiprocessing.active_children())
#miner.change()
response = "Successfully changed the state of miner " + name +"!"
#except Exception as e:
# response = "There was an error changing the state of miner " + name + "!\n" + str(e)
# End try/except block
templateData = {
'title' : 'Miner "%s" State Change Status',
'response' : response
}
return render_template('change.html', **templateData)
# End def
@app.route("/status/<name>") # Need to pass a parameter called `pass` as well
def status(name):
#try:
if name in miners.keys():
miner = miners[name]
else:
return flask.abort(400)
#raise Exception("Name not found")
# End if/else block
password = request.args.get('pass')
print (password)
if not password == config['password']:
return flask.abort(403)
#raise Exception("Invalid password!")
# End if
status_code = miner.status()
# Returns a number which tells us the current state of this miner:
# 0: Power LED is off, indicating the miner is powered off.
# 1: Power LED on and mining software reachable.
# 2: Power LED on, but the mining software is unreachable.
if status_code == 0:
response = """<img style='align:center; display:block; width:100px; height:100px;' id='red_light' src='/static/red_light.png' />
</br>
</br>
<p>Miner %s is currently completely powered off.</p>
""" % miner.name
elif status_code == 2:
response = """<img style='align:center; display:block; width:100px; height:100px;' id='yellow_light' src='/static/yellow_light.png' />
</br>
</br>
<p>Miner %s is currently powered on, but the mining software is unreachable.</p>
""" % miner.name
elif status_code == 1:
response = """<img style='align:center; display:block; width:100px; height:100px;' id='green_light' src='/static/green_light.png' />
</br>
</br>
<p>Miner %s is currently powered on and the mining software is reachable!</p>
""" % miner.name
# End if/else block
templateData = {
'title' : 'Miner %s status' % miner.name,
'response' : Markup(response)
}
return render_template('status.html', **templateData)
# End def
@app.route("/statusAll") # Need to pass a parameter called `pass` as well
def statusAll():
password = request.args.get('pass')
print (password)
if not password == config['password']:
return flask.abort(403)
#raise Exception("Invalid password!")
# End if
processes = []
for name in miners:
miner = miners[name]
p = Process(target=miner.status, args=(r_queue,))
p.start()
processes.append(p)
# End for
for _p in processes:
_p.join()
# End for
results = {}
while not r_queue.empty():
res = r_queue.get()
results[res[0]] = res[1]
# End while
# Create a sorted list of the keys in our results dict
s_keys = sorted(results.keys())
symbols = ""
names = ""
for item in s_keys:
if results[item] == 0:
symbols += "<td>" + "<img style='align:center; display:block; width:100px; height:100px;' id='red_light' src='/static/red_light.png' />" + "</td>"
elif results[item] == 1:
symbols += "<td>" + "<img style='align:center; display:block; width:100px; height:100px;' id='green_light' src='/static/green_light.png' />" + "</td>"
elif results[item] == 2:
symbols += "<td>" + "<img style='align:center; display:block; width:100px; height:100px;' id='yellow_light' src='/static/yellow_light.png' />" + "</td>"
else:
print (results[item])
# End if/else block
# End for
for item in s_keys:
names += "<td>" + item + "</td>"
# End for
response = "<tr>" + symbols + "</tr><tr>" + names + "</tr>"
explaination = "\
</br>\
</br>\
<p>\
Red: Power LED is off, indicating the miner is powered off.</br>\
Green: Power LED on and mining software reachable.</br>\
Yellow: Power LED on, but the mining software is unreachable.</p>"
templateData = {
'title' : 'Farm status',
'response' : Markup(response),
'explaination' : Markup(explaination)
}
return render_template('fstatus.html', **templateData)
# End def
@app.errorhandler(403)
def page_not_found(e):
return "You are not authorized to view this page!", 403
# End def
@app.errorhandler(400)
def page_not_found(e):
return "Your request was malformed. Double check the miner name you provided, maybe?", 400
# End def
if __name__ == "__main__":
try:
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host='0.0.0.0', port=config['server_port'], debug=True)
except (KeyboardInterrupt, SystemExit):
Cleanup()
finally:
sys.exit(0)
for item in multiprocessing.active_children():
item.join()
# End for
# End try/except block
# End if