-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
297 lines (212 loc) · 10.8 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
# Run the application (as long as is named app.py) with:
# $ flask run
# $ python app.py
import requests
from flask import Flask, render_template, send_file
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
from datetime import datetime
import pytz
import os
import numpy as np
import pandas as pd
######################## ML MODEL
from joblib import load
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge
app = Flask(__name__)
hora_list = ["Iniciando..."]
CO2_list = [-1]
PM10_list = [-1]
PM25_list = [-1]
CO2_msg = ""
PM10_msg = ""
PM25_msg = ""
#################################### READ HOPU DATA
def get_datetime():
# datetime object containing current date and time
# dd/mm/YY H:M:S
global date,time,hora
dt = datetime.now(pytz.timezone("Europe/Madrid"))
date = dt.strftime("%d/%m/%Y")
time = dt.strftime("%H:%M:%S")
hora = dt.strftime("%H:%M")
# 1. Iniciar sesion en el APIRest de Hopu
# Obtener access token y refress token
def API_get_token():
url = "https://fiware.hopu.eu/keycloak/auth/realms/fiware-server/protocol/openid-connect/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = "username=julgonzalez&password=vZnAWE7FexwgEqwT&grant_type=password&client_id=fiware-login"
response = requests.post(url, data = data, headers = headers).json()
global access_token, refresh_token
access_token = response["access_token"]
refresh_token = response["refresh_token"]
def API_get_device_status(access_token):
url = "https://fiware.hopu.eu/orion/v2/entities?limit=1000&attrs=*,dateModified&options=count,keyValues"
headers = {"fiware-service": "Device", "fiware-servicepath": "/ctcon", "Authorization": "Bearer "+access_token}
response = requests.get(url, headers = headers).json()[0]
global operationalStatus
operationalStatus = response["operationalStatus"]
def API_get_calidad_aire(access_token):
url = "https://fiware.hopu.eu/orion/v2/entities?limit=1000&attrs=*,dateModified&options=keyValues"
headers = {"fiware-service": "AirQuality", "fiware-servicepath": "/ctcon", "Authorization": "Bearer "+access_token}
response = requests.get(url, headers = headers).json()[0]
global CO2,PM10,PM25,Temperatura,Humedad
CO2 = response["CO2"]
PM10 = response["PM10"]
PM25 = response["PM25"]
Temperatura = response["temperature"]
Humedad = response["humidity"]
def API_get_presencia(access_token):
url = "https://fiware.hopu.eu/orion/v2/entities?limit=1000&attrs=*,dateModified&options=count,keyValues"
headers = {"fiware-service": "PeopleCounting", "fiware-servicepath": "/ctcon", "Authorization": "Bearer "+access_token}
response = requests.get(url, headers = headers).json()[0]
global PersonasIn,PersonasOut,Personas
PersonasIn = response["numberOfIncoming"]
PersonasOut = response["numberOfOutgoing"]
Personas = PersonasIn - PersonasOut
####################################
def init_ml_model():
global ml_model
ml_model = make_pipeline(StandardScaler(), Ridge())
ml_model = load('models/ridge.joblib')
def init_empty_data():
f = open("data.csv", "w")
f.write("Fecha,Hora,PersonasIn,PersonasOut,Personas,Temperatura,Humedad,CO2,PM10,PM25\n")
f.close()
def save_data():
f = open('data.csv', 'a')
f.write(date+","+hora+","+
str(PersonasIn)+","+
str(PersonasOut)+","+
str(Personas)+","+
str(Temperatura)+","+
str(Humedad)+","+
str(CO2)+","+
str(PM10)+","+
str(PM25)+"\n")
f.close()
def print_data():
print(" PersonasIn = ", PersonasIn)
print(" PersonasOut = ", PersonasOut)
print(" Personas = ", Personas)
print(" Temperatura = ", Temperatura)
print(" Humedad = ", Humedad)
print(" CO2 = ", CO2)
print(" PM10 = ", PM10)
print(" PM25 = ", PM25)
####################################
def get_CO2_msg(pred_CO2_20mins):
start_msg = "PREDICCIÓN DE CO2 EN NIVEL "
advice_1 = " (IDA 1). NINGUNA ACCIÓN REQUERIDA."
advice_2 = " (IDA 2). SE RECOMIENDA VENTILAR LA OFICINA EN LOS PRÓXIMOS 15 MINUTOS"
advice_3 = " (IDA 3). SE DEBE VENTILAR LA OFICINA EN ESTE MOMENTO"
if pred_CO2_20mins < 500: return start_msg + "OPTIMO" + advice_1
elif pred_CO2_20mins >= 500 and pred_CO2_20mins < 900: return start_msg + "BUENO" + advice_1
elif pred_CO2_20mins >= 900 and pred_CO2_20mins < 1200: return start_msg + "ACEPTABLE" + advice_2
elif pred_CO2_20mins >= 1200: return start_msg + "DESACONSEJADO" + advice_3
def get_PM10_msg(pred_PM10_20mins):
start_msg = "PREDICCIÓN DE PARTÍCULAS EN SUSPENSIÓN INFERIORES A 10 MICRAS EN NIVEL "
advice_1 = ". NINGUNA ACCIÓN REQUERIDA."
advice_2 = ". CESEN CUALQUIER POSIBLE ACTIVIDAD GENERADORA DE POLVO EN LOS PRÓXIMOS 15 MINUTOS. REVISEN EL SISTEMA DE CLIMATIZACIÓN Y VENTILACIÓN EN LAS PRÓXIMAS 48 HORAS"
advice_3 = ". CESEN CUALQUIER POSIBLE ACTIVIDAD GENERADORA DE POLVO EN ESTE MOMENTO. REVISEN EL SISTEMA DE CLIMATIZACIÓN Y VENTILACIÓN EN LAS PRÓXIMAS 24 HORAS"
if pred_PM10_20mins < 20: return start_msg + "OPTIMO" + advice_1
elif pred_PM10_20mins >= 20 and pred_PM10_20mins < 40: return start_msg + "BUENO" + advice_1
elif pred_PM10_20mins >= 40 and pred_PM10_20mins < 60: return start_msg + "ACEPTABLE" + advice_2
elif pred_PM10_20mins >= 60: return start_msg + "DESACONSEJADO" + advice_3
def get_PM25_msg(pred_PM25_20mins):
start_msg = "PREDICCIÓN DE PARTÍCULAS EN SUSPENSIÓN INFERIORES A 2,5 MICRAS EN NIVEL "
advice_1 = ". NINGUNA ACCIÓN REQUERIDA."
advice_2 = ". CESEN CUALQUIER POSIBLE ACTIVIDAD GENERADORA DE POLVO EN LOS PRÓXIMOS 15 MINUTOS. REVISEN EL SISTEMA DE CLIMATIZACIÓN Y VENTILACIÓN EN LAS PRÓXIMAS 48 HORAS"
advice_3 = ". CESEN CUALQUIER POSIBLE ACTIVIDAD GENERADORA DE POLVO EN ESTE MOMENTO. REVISEN EL SISTEMA DE CLIMATIZACIÓN Y VENTILACIÓN EN LAS PRÓXIMAS 24 HORAS"
if pred_PM25_20mins < 20: return start_msg + "OPTIMO" + advice_1
elif pred_PM25_20mins >= 20 and pred_PM25_20mins < 40: return start_msg + "BUENO" + advice_1
elif pred_PM25_20mins >= 40 and pred_PM25_20mins < 60: return start_msg + "ACEPTABLE" + advice_2
elif pred_PM25_20mins >= 60: return start_msg + "DESACONSEJADO" + advice_3
def get_ml_predictions():
global hora_list, CO2_list, PM10_list, PM25_list, CO2_msg, PM10_msg, PM25_msg
global ml_model
# get tail(4) that means lag15, lag10, lag5, actual
in_dat = pd.read_csv("data.csv").tail(4)
hora_hist = in_dat["Hora"].values
temp_hist = in_dat["Temperatura"].values # np array [temp_lag15, temp_lag10, temp_lag5, temp_actual]
hume_hist = in_dat["Humedad"].values
pm25_hist = in_dat["PM25"].values
pm10_hist = in_dat["PM10"].values
CO2_hist = in_dat["CO2"].values
pers_hist = in_dat["Personas"].values
if len(in_dat) == 4:
#### ENOUGH DATA -> DO ML PREDICTION
# Prepare flat numpy matrix for the sklearn prediction
test_x = np.concatenate((temp_hist,
hume_hist,
pm25_hist,
pm10_hist,
CO2_hist,
pers_hist)).reshape(1,-1)
# DO sklearn prediction
#pred_np = np.array([[ 1.5137, 1.8456, 1.887 , 2.5185, 2.5037, 2.8493,
# 2.9192, 3.412 , 760.1504, 766.1264, 779.5188, 755.173 ]])[0]
pred_np = ml_model.predict(test_x)
assert pred_np.shape==(1,12)
hora_list = list(hora_hist) + ["+5 mins", "+10 mins", "+15 mins", "+20 mins"]
PM25_list = list(pm25_hist) + list(pred_np[0][0:4])
PM10_list = list(pm10_hist) + list(pred_np[0][4:8])
CO2_list = list(CO2_hist) + list(pred_np[0][8:12])
CO2_msg = get_CO2_msg(CO2_list[-1])
PM10_msg = get_PM10_msg(PM10_list[-1])
PM25_msg = get_PM25_msg(PM25_list[-1])
print("ML prediction done")
else:
#### NO ENOUGH DATA -> ERROR MSG
print("NO ENOUGH DATA FOR DOING ML PREDICTIONS")
hora_list = list(hora_hist)
PM25_list = list(pm25_hist)
PM10_list = list(pm10_hist)
CO2_list = list(CO2_hist)
PM25_msg = "No ha transcurrido el suficienciente tiempo (<15 mins) para predecir las partículas inferiores a 2,5 micra."
PM10_msg = "No ha transcurrido el suficienciente tiempo (<15 mins) para predecir las partículas inferiores a 10 micras."
CO2_msg = "No ha transcurrido el suficienciente tiempo (<15 mins) para predecir el CO2."
####################################
def fill_data_from_HOPU_and_do_ML():
global date, time
get_datetime()
print("pipeline at " + date + " " + time)
####### fill data from HOPU and save it into data.csv as a new row
API_get_token()
API_get_device_status(access_token)
API_get_calidad_aire(access_token)
API_get_presencia(access_token)
print_data()
save_data()
####### Get last 4 rows from data.csv and do ML predictions
get_ml_predictions()
@app.route('/')
def web_endpoint():
global hora_list, CO2_list, PM10_list, PM25_list, CO2_msg, PM10_msg, PM25_msg
data={
"x_labels": hora_list,
"CO2": CO2_list, #[120, 153, 213, 230, 240, 220, 180, 120],
"CO2_msg": CO2_msg,
"PM10": PM10_list, #[8, 10, 20, 26, 27, 22, 13, 11],
"PM10_msg": PM10_msg,
"PM25": PM25_list, #[6, 8, 18, 23, 24, 18, 10, 8],
"PM25_msg": PM25_msg
}
return render_template('frontend.html', **data)
@app.route('/data')
def downloadData ():
return send_file("data.csv", as_attachment=True)
if __name__ == '__main__':
init_empty_data()
init_ml_model()
scheduler = BackgroundScheduler(timezone='Europe/Madrid') # Default timezone is "utc"
#scheduler.add_job(fill_data_from_HOPU_and_do_ML, 'interval', seconds=5)
#scheduler.add_job(fill_data_from_HOPU_and_do_ML, 'cron', day_of_week='*', hour='*', minute='*')
scheduler.add_job(fill_data_from_HOPU_and_do_ML, 'cron', day_of_week='mon-fri', hour='7-20', minute='*/5')
scheduler.start()
port = os.getenv('PORT') # Port is given by Heroku as environmental variable
print("Port:", port)
#app.run(host="0.0.0.0", port=port, debug=True, use_reloader=False)
app.run(host="0.0.0.0")