This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
595 lines (515 loc) · 20 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/usr/bin/env python3
"""Application backend logic."""
import os
import shutil
import subprocess
import logging
from glob import glob
from logging.handlers import RotatingFileHandler
from functools import wraps, partial # noqa maybe can be used on save files
from threading import Thread, Event
from datetime import datetime
try:
import RPi.GPIO as GPIO
# ROOT = os.path.join(os.getcwd(), "resPI")
except (RuntimeError, ModuleNotFoundError):
GPIO = None # None means that is not running on raspberry pi
# ROOT = os.getcwd()
from flask_caching import Cache
from flask import (
Flask,
render_template,
redirect,
url_for,
request,
jsonify,
session,
flash,
send_from_directory,
)
from werkzeug.security import generate_password_hash, check_password_hash # noqa
from flask_socketio import SocketIO
from scripts.converter import ExperimentCycle, ControlFile
from scripts.stats import ResumeDataFrame, Control
from scripts.error_handler import checker
from scripts.utils import (
to_mbyte,
delete_zip_file,
to_js_time,
check_extensions,
SUPPORTED_FILES,
greeting,
config_from_file,
save_config_to_file,
)
ROOT = os.path.dirname(os.path.abspath(__file__)) # app root dir
# App basic configuration
config = {
"SECRET_KEY": "NONE",
"CACHE_TYPE": "simple",
"CACHE_DEFAULT_TIMEOUT": 0,
# "CACHE_ARGS": ["test", "Anna", "DIR"],
"UPLOAD_FOLDER": f"{ROOT}/static/uploads",
"LOGS_FOLDER": f"{ROOT}/logs",
"LOGS_MB_SIZE": 24578,
"LOGS_BACKUP": 10,
"ZIP_FOLDER": f"{ROOT}/static/uploads/zip_files",
} # UNIT: minutes
# DEFINE RASPBERRY PI PINS NUMBERS AND API FUNCTIONS
if GPIO is not None:
GPIO.setmode(GPIO.BCM) # Use GPIO Numbers
PUMP_GPIO = 26 # Digital input to the relay
GPIO.setup(PUMP_GPIO, GPIO.OUT) # GPIO Assign mode
# DEFINE APP
app = Flask(__name__)
app.config.from_mapping(config)
_active_threads = {}
exit_thread = Event()
# Setup cache
cache = Cache(app)
cache.set_many((("run_manual", False), ("run_auto", False), ("running", False)))
# SocketIO
socketio = SocketIO(app, async_mode=None)
# thread = None
# thread_lock = Lock()
# Setup logging
logger = app.logger
handler = RotatingFileHandler(
f"{app.config['LOGS_FOLDER']}/resPI.log",
maxBytes=app.config["LOGS_MB_SIZE"],
backupCount=app.config["LOGS_BACKUP"],
)
# handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(message)s"))
handler.setFormatter(logging.Formatter("%(message)s"))
handler.setLevel(logging.WARNING)
app.logger.addHandler(handler)
UNIT = 60 # 1 for seconds, 60 for minutes
def login_required(fn):
"""Decorate to protected against not authenticated users."""
# Functions warp
@wraps(fn)
def wrap(*args, **kwargs):
if not session.get("auth", False):
return redirect(url_for("login")) # Not authenticated
return fn(*args, **kwargs)
return wrap
def check_password(password):
"""Check if password is corrected."""
# Get hash password from os environment to check if matches NOTE: Must be set on pi env
hash = os.getenv(
"hash2",
"pbkdf2:sha256:150000$pMreM10r$6dc02f2deb0725f1f3c70766f44e2aa45d8614556b48e9165740ac6384b4de79", # noqa
)
check = check_password_hash(hash, password)
if check:
session["auth"] = True
return True
else:
session["auth"] = False
return False
####################
# PUMP SETUP AND CONFIGURATION
####################
def switch_on():
"""Turn pump ON."""
if GPIO:
GPIO.output(PUMP_GPIO, GPIO.HIGH) # on
cache.set("running", True)
run_mode = "automatic" if cache.get("run_auto") else "manual" # only for logging
logger.warning(f"Pump is running | Mode: {run_mode}")
def switch_off():
"""Turn pump OFF."""
if GPIO:
GPIO.output(PUMP_GPIO, GPIO.LOW) # off
cache.set_many((("cycle_ends_in", None), ("next_cycle_at", None), ("running", False)))
run_mode = "automatic" if cache.get("run_auto") else "manual" # only for logging
logger.warning(f"Pump is off | Mode: {run_mode}")
# PUMP CYCLE
def pump_cycle(cycle, period):
"""Define how long pump is ON in order to full the fish tank."""
# Turn on the pump
started = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cache.set("total_loops", cache.get("total_loops") + 1)
switch_on()
cache.set("cycle_ends_in", to_js_time(cycle, "auto"))
socketio.emit(
"automatic_program",
{
"data": "Server generated event",
"running": cache.get("running"),
"run_auto": cache.get("running"),
"cycle_ends_in": cache.get("cycle_ends_in"),
"total_loops": cache.get("total_loops"),
"auto_run_since": cache.get("auto_run_since"),
},
namespace="/resPi",
)
# Wait until tank is full
if not exit_thread.wait(timeout=cycle): # MINUTES
if cache.get("run_auto"): # If still in current automatic program
# Turn off the pump
switch_off()
cache.set("next_cycle_at", to_js_time(period, "auto"))
socketio.emit(
"automatic_program",
{
"data": "Server generated event",
"running": cache.get("running"),
"run_auto": True,
"next_cycle_at": cache.get("next_cycle_at"),
},
namespace="/resPi",
)
ended = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# print(f"Current automatic program: Started {str(started)} | Ended: {str(ended)}")
# Write information to logging file
print(
f"""Current program [{cache.get("total_loops")}]: Started {started} | Ended: {ended}"""
)
logger.warning(
f"""Current program [{cache.get("total_loops")}]: Started {started} | Ended: {ended}"""
)
else: # Ignore previous. Pump is already off
logger.warning(f"Automatic program: Started {started} was closed forced by user")
####################
# BACKGROUND TASKS
####################
# USER DEFINED PROGRAM
def start_program(app=None):
"""Start a new background thread to run the user program."""
# program()
"""User defined task.
Creates a periodic task using user form input.
"""
# Save starting time programming
cache.set("auto_run_since", (datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
user_program = cache.get("user_program")
# Turn the pump on every x seconds
period = (user_program.get("close") + user_program.get("wait")) * UNIT
cycle = user_program.get("flush") * UNIT # Run the pump for the time of x seconds
while cache.get("run_auto"):
pump_cycle(cycle, period)
if not exit_thread.wait(timeout=period):
continue
else:
return False
def process_excel_files(flush, wait, close, uploaded_excel_files, plot):
"""Start a new thread to process excel file uploaded by the user."""
# Loop throw all uploaded files and clean the data set
save_converted = False # if to save .txt file converted into .xlsx file
# CALCULATE BLANKS
control_file_1 = os.path.join(os.path.dirname(uploaded_excel_files[0]), "C1.txt")
control_file_2 = os.path.join(os.path.dirname(uploaded_excel_files[0]), "C2.txt")
for c in [control_file_1, control_file_2]:
C = ControlFile(flush, wait, close, c)
C_Total = Control(C)
C_Total.get_bank()
control = C_Total.calculate_blank()
######################
#
######################
total_files = len(uploaded_excel_files)
logger.warning(f"A total of {total_files} files received")
for i, file_path in enumerate(uploaded_excel_files):
# generate_data(flush, wait, close, file_path, new_column_name, plot, plot_title)
experiment = ExperimentCycle(flush, wait, close, file_path)
if save_converted:
experiment.original_file.save()
resume = ResumeDataFrame(experiment)
resume.generate_resume(control)
if plot:
experiment.create_plot()
resume.save()
# TODO: add flag to save or not all converted file to excel
resume = ResumeDataFrame(experiment)
logger.warning(f"Task concluded {i+1}/{total_files}")
socketio.emit(
"processing_files",
{"generating_files": True, "msg": f"fitxers processats {i+1}/{total_files}"},
namespace="/resPi",
)
cache.set("generating_files", False)
socketio.emit(
"processing_files", {"generating_files": False, "msg": ""}, namespace="/resPi"
)
####################
# APP ROUTES
####################
@app.route("/", methods=["GET"])
def landing():
"""Endpoint dispatcher to redirect user to the proper route."""
# Check if user is authenticated
if not (session.get("auth", False)):
flash(
f"{greeting()}, primer cal iniciar la sessió abans d’utilitzar aquesta aplicació",
"info",
)
return redirect(url_for("respi"))
else:
flash(f"Hey {greeting()}, benvingut {session['username']}", "info")
return redirect(url_for("login"))
@app.route("/respi", methods=["GET", "POST"])
@login_required
def respi():
"""Application GUI.
This route contains all user interface possibilities with the hardware.
"""
if request.method == "POST":
# Get information from user form data and run automatic program
if request.form.get("action", False) == "start":
# Avoids create a new thread if user reloads browser
if cache.get("running") or cache.get("run_auto"):
pass
else:
cache.set("run_auto", True)
flush = int(request.form["flush"])
wait = int(request.form["wait"])
close = int(request.form["close"])
# set program configuration on memory layer
cache.set("user_program", dict(close=close, flush=flush, wait=wait))
cache.set("total_loops", 0)
session["user_program"] = [flush, wait, close]
# Create a register of the started thread
global _active_threads
t = Thread(target=start_program)
t_name = t.getName()
_active_threads[t_name] = t # noqa
exit_thread.clear() # set all thread flags to false
t.start() # start a fresh new thread with the current program
elif request.form.get("action", False) == "stop":
switch_off() # TODO: Must be checked first
# Remove counters/timers and stop background thread
cache.set_many(
(
("running", False),
("cycle_ends", None),
("next_cycle_at", None),
("run_auto", False),
)
)
exit_thread.set()
###########################
# MANUAL MODE
###########################
if request.form.get("manual", False):
if request.form["manual"] == "start_manual":
cache.set_many(
(("started_at", to_js_time(run_type="manual")), ("run_manual", True))
)
switch_on()
else:
switch_off()
cache.set("run_manual", False)
# Populate form inputs with last inserted program or from config file values
if not cache.get("user_program"):
config = config_from_file()["pump_control_config"]
flush = int(config["flush"])
wait = int(config["wait"])
close = int(config["close"])
else:
flush = cache.get("user_program")["flush"]
wait = cache.get("user_program")["wait"]
close = cache.get("user_program")["close"]
return render_template("app.html", flush=flush, wait=wait, close=close)
@app.route("/excel_files", methods=["POST", "GET"])
def excel_files():
"""User GUI for upload and deal with excel files."""
session["excel_config"] = config_from_file()["file_cycle_config"]
if request.method == "POST":
# Get basic information about the data set
flush = int(request.form.get("flush"))
wait = int(request.form.get("wait"))
close = int(request.form.get("close"))
plot = True if request.form.get("plot") else False
cache.set("generating_files", True)
# Save file to the system
# NOTE: Must check for extensions
data_file = request.files.get("data_file")
control_file_1 = request.files.get("control_file_1")
control_file_2 = request.files.get("control_file_2")
# Contains a list of all uploaded file in a single uploaded request
uploaded_excel_files = []
# for file_ in files:
# Generate the folder name
time_stamp = datetime.now().strftime(f"%d_%m_%Y_%H_%M_%S")
filename, ext = data_file.filename.split(".")
if not check_extensions(ext):
flash(
f"El tipus de fitxer {ext} no és compatible. Seleccioneu un tipus de fitxer {SUPPORTED_FILES}", # noqa
"danger",
)
return redirect("excel_files")
folder_name = f"{filename}_{time_stamp}"
project_folder = os.path.join(app.config["UPLOAD_FOLDER"], folder_name)
try:
os.mkdir(project_folder)
except FileExistsError:
project_folder = os.path.join(app.config["UPLOAD_FOLDER"], f"{folder_name}_1")
os.mkdir(project_folder)
# Here filename complete with extension
control_file_1.filename = "C1.txt"
control_file_2.filename = "C2.txt"
# Save all files into project folder
files_list = [data_file, control_file_1, control_file_2]
for file_ in files_list:
file_path = os.path.join(project_folder, file_.filename)
file_.save(file_path)
# CHECK HEADERS
check = checker(file_path).match()
if check is not True:
for msg in check:
msg += " "
flash(check, "danger")
# Removes folder and file that doesn't match headers
shutil.rmtree(os.path.dirname(file_path))
return redirect("excel_files")
# save the full path of the saved file
uploaded_excel_files.append(os.path.join(project_folder, data_file.filename))
t = Thread(
target=process_excel_files, args=(flush, wait, close, uploaded_excel_files, plot),
)
t.start()
# Fixed
session["excel_config"] = {"flush": flush, "wait": wait, "close": close}
flash(
f"""El fitxer s'ha carregat correctament. Quan totes les dades s’hagin processat,
estaran disponibles a la secció de descàrregues..""",
"info",
)
return redirect("excel_files")
return render_template("excel_files.html", config=session.get("excel_config"))
####################
# DOWNLOAD ROUTES
####################
@app.route("/downloads", methods=["GET"])
def downloads():
"""Route to see all zip files available to download."""
zip_folder = glob(f"{app.config['ZIP_FOLDER']}/*.zip")
# get only the file name and the size of it excluding the path.
# Create a list of tuples sorted by file name
zip_folder = sorted(zip_folder, key=lambda x: os.path.getmtime(x))[::-1]
# TODO: Convert to namedtuple or class
zip_folder = [
(
os.path.basename(f),
os.path.getsize(f),
datetime.utcfromtimestamp(os.path.getmtime(f)),
)
for f in zip_folder
]
zip_files = [
{
"id_": i,
"name": file_[0],
"created": file_[2].strftime("%Y/%m/%d %H:%M"),
"size": to_mbyte(file_[1]),
}
for i, file_ in enumerate(zip_folder)
]
return render_template("download.html", zip_files=zip_files)
@app.route("/get_file/<file_>", methods=["GET"])
def get_file(file_):
"""Download a zip file based on file name."""
return send_from_directory(app.config["ZIP_FOLDER"], file_)
@app.route("/remove_file/<file_>", methods=["GET"])
def remove_file(file_):
"""Delete a zip file based on file name."""
location = os.path.join(app.config["ZIP_FOLDER"], file_)
delete_zip_file(location)
return redirect(url_for("downloads"))
@app.route("/settings", methods=["POST", "GET"])
def settings():
config = config_from_file()
if request.method == "POST":
config = save_config_to_file(request.form.to_dict())
flash("Configuration updated", "info")
return redirect("settings")
return render_template("settings.html", config=config)
####################
# LOGS ROUTES
####################
@app.route("/logs", methods=["GET"])
def logs():
"""Route to see all zip files available to download."""
logs_folder = glob(f"{app.config['LOGS_FOLDER']}/*.log*")
# get only the file name and the size of it excluding the path.
# Create a list of tuples sorted by file name
logs_folder = sorted([os.path.basename(f) for f in logs_folder])
logs = [{"id_": i, "name": file_} for i, file_ in enumerate(logs_folder)]
return render_template("logs.html", logs=logs)
@app.route("/read_log/<log>")
def read_log(log):
"""Open a log file and return it to a html page."""
file_ = os.path.join(app.config["LOGS_FOLDER"], log)
with open(file_, "r") as f:
log_text = [f"<p>{line}</p>" for line in f.readlines()]
log_ = "\n".join(log_text)
return log_
@app.route("/download_log/<log>")
def download_log(log):
"""Download a log file."""
return send_from_directory(app.config["LOGS_FOLDER"], log)
####################
# AUTHENTICATION AND SYSTEM STUFF
####################
@app.route("/login", methods=["GET", "POST"])
def login():
"""User login page."""
if request.method == "POST":
password = request.form.get("password", None)
session["username"] = request.form.get("username", None)
if check_password(password):
logger.warning(f"{request.form.get('username')} connectado")
flash(f"Hey {greeting()}! Benvingut {session['username']}", "info")
return redirect(url_for("respi"))
flash("Contrasenya incorrecta", "danger")
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log out user."""
session["auth"] = False
logger.warning(f"{session['username']} left.")
flash(f"Adeu!! {session['username']}", "info")
return redirect(url_for("landing"))
@app.route("/turn_off")
def turn_off():
"""Turn off PI."""
cmd = "sudo shutdown now"
subprocess.Popen(cmd, shell=True)
flash(f"Apagar el sistema... Això pot trigar un parell, espereu si us plau", "info")
return redirect(url_for("landing"))
@app.route("/restart")
def restart():
"""Restart PI."""
cmd = "sudo reboot"
subprocess.Popen(cmd, shell=True)
flash(
f"""Reinicieu el sistema. Això pot trigar un parell de segons, espereu si us plau.
Continuar prement F5 fins que torni a actualitzar la pàgina.
""",
"info",
)
return redirect(url_for("landing"))
@app.route("/status", methods=["GET"])
def get_status():
"""Return information about the different components of the system."""
return jsonify(
{
"running": cache.get("running"),
"run_auto": cache.get("run_auto"),
"run_manual": cache.get("run_manual"),
"started_at": cache.get("started_at"),
"cycle_ends_in": cache.get("cycle_ends_in"),
"next_cycle_at": cache.get("next_cycle_at"),
"generating_files": cache.get("generating_files"),
"total_loops": cache.get("total_loops"),
"auto_run_since": cache.get("auto_run_since"),
}
)
@app.route("/user_time/<local_time>", methods=["GET", "POST"])
def update_time(local_time):
"""Get user local time to update server time."""
print(local_time)
return redirect(url_for("login"))
if __name__ == "__main__":
socketio.run(app, debug=True, host="0.0.0.0")