-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
177 lines (128 loc) · 5.51 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
from flask import *
from config import *
from main import HeatmapMain
from file_update_tools import update_map
from data_tools import init_data_tools, fill_all_rooms, update_air_data
import os
import time
import sched
import shutil
import datetime
import calendar
import threading
import timeinterval
import datetime as dt
app
# Set up the color values (temperature, co2)
# BLUE_VALUE is the temperatures/co2 levels that will be marked cold/low (with a light blue color)
# GREEN_VALUE is the temperatures/co2 levels that will be marked good (with a green color)
# RED_VALUE is the temperatures/co2 levels that will be marked hot/high (with a light red color)
BLUE_VALUE = (60, 100)
GREEN_VALUE = (70, 900)
RED_VALUE = (80, 2000)
# Empty out the temporary folder by deleting it and making a new one
if os.path.exists(TEMP_UPDATE_PATH):
shutil.rmtree(TEMP_UPDATE_PATH)
os.mkdir(TEMP_UPDATE_PATH)
init_data_tools(ROOM_SENSOR_CSV)
for level in BUILDING_LEVELS:
svg_file_name = SVG_FILE_PREFIX + str(level) + '.svg'
svg_path = os.path.join(SVG_AND_CONVERSIONS_PATH, svg_file_name)
heatmap = HeatmapMain(svg_path, RED_VALUE, GREEN_VALUE, BLUE_VALUE)
fill_all_rooms(heatmap, True) # First start with temperature
fill_all_rooms(heatmap, False)
scheduler = sched.scheduler(time.time, time.sleep)
def datetime_to_utc(dt):
"""Converts a datetime object to UTC timestamp
naive datetime will be considered UTC."""
return calendar.timegm(dt.utctimetuple())
def update_svg():
update_air_data()
for floor_level in BUILDING_LEVELS:
file_name = SVG_FILE_PREFIX + str(floor_level) + '.svg'
file_path = os.path.join(SVG_AND_CONVERSIONS_PATH, file_name)
update_map(file_name, file_path, RED_VALUE, GREEN_VALUE, BLUE_VALUE)
def start_app():
global app
app = Flask(__name__)
# @app.context_processor
# def inject_floors_to_all_templates():
# return dict(floors=levels)
@app.context_processor
def inject_year_to_all_templates():
return dict(year=get_year())
@app.route("{0}/".format(HOST_PREFIX))
def home():
return render_template("index.html", title="AHS Heatmaps")
@app.route("{0}/about".format(HOST_PREFIX))
def about():
return render_template("about.html", title="About | AHS Heatmaps")
@app.route("{0}/ahs/<floor>".format(HOST_PREFIX))
def load_svg(floor):
try:
floor_num = float(floor)
except ValueError:
abort(404)
return None # Stops the following code from executing
if floor_num in BUILDING_LEVELS:
return render_template('svg_output_page.html', title='Andover HS Level {0}'.format(floor),
file_filled_prefix="Andover-HS-level-{0}_filled_rooms_".format(floor), floor=floor)
else:
abort(404)
@app.errorhandler(404)
def error_404(e):
return load_error_page(404, 'Page Not Found', 'The page you are looking for might have been removed, had its ' +
'name changed, or be temporarily unavailable.')
@app.errorhandler(403)
def error_403(e):
return load_error_page(403, 'Forbidden', 'You don\'t have permission to access this page on this server')
@app.errorhandler(500)
def error_500(e):
return load_error_page(500, 'Internal Server Error', Markup('The server encountered an internal error or ' +
'misconfiguration and was unable to complete your request. <br><br>' +
'Please contact Daniel Ivanovich (<a href="mailto:[email protected]">[email protected]</a>) ' +
'to make this issue known.'))
def load_error_page(code, tagline, details):
return render_template('error.html', code=str(code), tagline=tagline, details=details), code
app.register_error_handler(404, error_404)
app.jinja_env.globals['host_prefix'] = HOST_PREFIX
app.jinja_env.globals['floors'] = BUILDING_LEVELS
app.jinja_env.globals['date'] = datetime.datetime.now().strftime("%B %d, %Y")
app.jinja_env.globals['blue_values'] = BLUE_VALUE
app.jinja_env.globals['green_values'] = GREEN_VALUE
app.jinja_env.globals['red_values'] = RED_VALUE
app.static_url_path = '{0}/static'.format(HOST_PREFIX)
# remove old static map
url_map = app.url_map
try:
for rule in url_map.iter_rules('static'):
url_map._rules.remove(rule)
except ValueError:
# no static view was created yet
pass
# register new; the same view function is used
app.add_url_rule(
app.static_url_path + '/<path:filename>',
endpoint='static', view_func=app.send_static_file)
def get_time():
return datetime_to_utc(datetime.datetime.now())
def get_year():
return datetime.datetime.now().year
def start_svg_auto_updater():
second = dt.datetime.now().second + 1
while second != 60:
time.sleep(1)
second = dt.datetime.now().second + 1
print("Seconds synced")
minute = dt.datetime.now().minute % 10 + 1
while minute != 4 and minute != 9:
time.sleep(60)
minute = dt.datetime.now().minute % 10 + 1
print("Minutes synced\nStarting update thread...")
timeinterval.start(5 * 60 * 1000, update_svg)
update_svg()
update_thread = threading.Thread(target=start_svg_auto_updater)
update_thread.start()
start_app()
if __name__ == '__main__':
app.run()