-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
144 lines (126 loc) · 5.96 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
import threading
import time
from datetime import datetime
from pathlib import Path
import json
from flask import Flask, url_for, render_template, redirect
from turbo_flask import Turbo
import webbrowser
import data_and_settings as settings
from my_functions import general_utils
from my_routes.routes_visualize import vis_bp
from my_routes.routes_esp32 import esp32_bp
from my_routes.routes_settings import set_bp
import os
# get the path of the current file
settings.FILEPATH = os.path.expanduser("~")
# define the path of the settings file
print(settings.FILEPATH)
settings.SETTINGSPATH = Path(settings.FILEPATH) / "SQM_settings.json"
print(settings.SETTINGSPATH)
# check if "SQM_settings.json" exists
if not Path(settings.SETTINGSPATH).is_file():
with open(settings.SETTINGSPATH, 'w') as f:
settings.SETTINGS["PATH"] = str(settings.FILEPATH)
json.dump(settings.SETTINGS, f)
# if file exists, load settings
else:
with open(settings.SETTINGSPATH, 'r') as file:
settings.SETTINGS = json.load(file)
# set specific directory to the loaded path
settings.SPECIFIC_DIRECTORY = Path(settings.SETTINGS["PATH"])
print(settings.SETTINGS)
# Create a Flask application instance
app = Flask(__name__)
app.secret_key = b'key_blabla'
# register blueprints (routes)
app.register_blueprint(vis_bp)
app.register_blueprint(esp32_bp)
app.register_blueprint(set_bp)
# Initialize Turbo-Flask with the Flask app instance
turbo = Turbo(app)
# Homepage with status and current settings
@app.route('/', methods=["GET"])
def statuspage():
return render_template('index.html', stats=url_for('static', filename='statistics.svg'))
#error screen with info
@app.route('/error/<er>')
def error(er):
return render_template('error.html', error_message=er)
#redirect to error screen
@app.errorhandler(404)
def page_not_found(error):
return redirect("/error/url not found")
# turbo-flask, update website every 5 sec
def update_load():
with app.app_context():
while True:
# if a measurement exists, read time and date and calculate the time difference to now, show if (not) running
if (settings.SPECIFIC_DIRECTORY / "last_measurement.txt").is_file():
with open(settings.SPECIFIC_DIRECTORY / "last_measurement.txt", 'r') as f4:
# read last measurement time
loaded_time = datetime.strptime(f4.read(), "%d-%b-%Y (%H:%M:%S.%f)")
general_utils.calculate_time_dif(loaded_time)
turbo.push(turbo.replace(render_template('replace_content.html', stats="static/statistics.svg"), 'load'))
time.sleep(5)
# inject settings/sensor values into the website
@app.context_processor
def inject_load():
return {'last_transm': settings.LAST_TRANSMISSION,
'min_ago': settings.MINUTES_AGO,
"isRunning": settings.IS_RUNNING,
"isSeeing": int(settings.SENSOR_VALUES["isSeeing"]),
"raining": int(settings.SENSOR_VALUES["raining"]),
"ambient": float(settings.SENSOR_VALUES["ambient"]),
"object": float(settings.SENSOR_VALUES["object"]),
"lux": float(settings.SENSOR_VALUES["lux"]),
"luminosity": float(settings.SENSOR_VALUES["luminosity"]),
"seeing": float(settings.SENSOR_VALUES["seeing"]),
"lightning_distanceToStorm": int(settings.SENSOR_VALUES["lightning_distanceToStorm"]),
"nelm": float(settings.SENSOR_VALUES["nelm"]),
"concentration": int(settings.SENSOR_VALUES["concentration"]),
"errors": settings.SENSOR_VALUES["errors"].strip()[:-1],
"SLEEPTIME_s": settings.SETTINGS["SLEEPTIME_s"],
"DISPLAY_TIMEOUT_s": settings.SETTINGS["DISPLAY_TIMEOUT_s"],
"DISPLAY_ON": settings.SETTINGS["DISPLAY_ON"],
"PATH": settings.SETTINGS["PATH"],
"skystate": settings.SKYSTATE,
"localIP": settings.LOCAL_IP,
"calculated_mag_limit": settings.SETTINGS["calculated_mag_limit"],
"set_sqm_limit": settings.SETTINGS["set_sqm_limit"],
"seeing_thr": settings.SETTINGS["seeing_thr"],
"max_lux": settings.SETTINGS["max_lux"],
"setpoint1": settings.SETTINGS["setpoint1"],
"setpoint2": settings.SETTINGS["setpoint2"],
"abr_seeing": settings.SETTINGS["seeing"],
"abr_raining": settings.SETTINGS["raining"],
"abr_luminosity": settings.SETTINGS["luminosity"],
"abr_nelm": settings.SETTINGS["nelm"],
"abr_concentration": settings.SETTINGS["concentration"],
"abr_object": settings.SETTINGS["object"],
"abr_ambient": settings.SETTINGS["ambient"],
"abr_lux": settings.SETTINGS["lux"],
"abr_lightning_distanceToStorm": settings.SETTINGS["lightning_distanceToStorm"],
"en_seeing": settings.SETTINGS["en_seeing"],
"en_raining": settings.SETTINGS["en_raining"],
"en_luminosity": settings.SETTINGS["en_luminosity"],
"en_nelm": settings.SETTINGS["en_nelm"],
"en_concentration": settings.SETTINGS["en_concentration"],
"en_object": settings.SETTINGS["en_object"],
"en_ambient": settings.SETTINGS["en_ambient"],
"en_lux": settings.SETTINGS["en_lux"],
"en_lightning_distanceToStorm": settings.SETTINGS["en_lightning_distanceToStorm"],
"plot": settings.BAR,
}
if __name__ == '__main__':
# start thread to update website with current values, runs parallel to Flask app
threading.Thread(target=update_load).start()
# get ip of this server/PC
settings.LOCAL_IP = general_utils.get_ip()
# run Flask app in a thread so it doesn't block the main thread
server_thread = threading.Thread(target=app.run, kwargs={'host': '0.0.0.0', 'port': 5000, 'threaded': True})
server_thread.start()
# wait some time for the server to start
time.sleep(3)
# open IP in browser
webbrowser.open("http://" + str(settings.LOCAL_IP) + ":5000")