-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_dashboard.py
256 lines (226 loc) · 8.76 KB
/
main_dashboard.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
__author__ = "Nicolas Gutierrez"
# Standard libraries
import os
# Third party libraries
import pandas as pd
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import numpy as np
# Custom libraries
from utilities.utilities import load_yaml
from datahandling.postgresqlinterface import PostGreSqlInterface
from datahandling.dataextractor import DataExtractor
from ping.pingdevicetype import PingDeviceType
from ping.pingfunctions import calculate_stats, calculate_histogram, calculate_downtime
from power.powerdevicetype import PowerDeviceType
from layout.ping_tab import ping_tab
from layout.power_tab import power_tab
from layout.temperature_tab import temperature_tab
from temp.tempfunctions import calculate_temp_and_ref
from utilities.init_logger import init_logger
# User configuration
configuration_path = os.path.join("config", "config.yaml")
logger = init_logger("house_dashboard_logs.txt")
# Style
logger.info("Loading styles")
pd.options.plotting.backend = "plotly"
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# Code Configuration
logger.info("Loading configuration")
configuration = load_yaml(configuration_path)
# Querier
querier = PostGreSqlInterface(configuration["postgresql"])
logger.info("Initializing Data Extractors")
# Initialization ping
sensor_type = "ping"
ping_data_extractor = DataExtractor(sensor_type, configuration[sensor_type], querier)
logger.debug(f"ping devices: {','.join(list(configuration[sensor_type]['devices'].keys()))}")
# Initialization energy
sensor_type = "power"
power_data_extractor = DataExtractor(sensor_type, configuration[sensor_type], querier)
logger.debug(f"power devices: {','.join(list(configuration[sensor_type]['devices'].keys()))}")
# Initilization temperature
sensor_type = "temp"
temperature_data_extractor = DataExtractor(sensor_type, configuration[sensor_type], querier)
logger.debug(f"temp devices: {','.join(list(configuration[sensor_type]['devices'].keys()))}")
# Dash layout
logger.info("Initializing Dash App")
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(style={'backgroundColor': '#111111'},
children=[
html.H1(children="House Dashboard",
style={'textAlign': 'center',
'color': '#FFFFFF'}),
dcc.Tabs([
ping_tab,
power_tab,
temperature_tab
])
])
@app.callback(
Output(component_id='device_ping_distribution_graph', component_property='figure'),
Input(component_id='interval_refresh_ping_slow', component_property="n_intervals")
)
def stream_fig_internet(value):
# Retrieve data
sensor_name = "google"
dfs_dict = ping_data_extractor.retrieve_sensors_data([sensor_name], 24)
list_of_histogram_pds = calculate_histogram(dfs_dict)
fig = go.Figure()
# Plot
for (values, bins) in list_of_histogram_pds:
bins_average = np.average(np.diff(bins))
fig.add_trace(go.Bar(x=bins[:-1]-bins_average/2, y=values, name=sensor_name))
fig.update_layout(
xaxis_title="Ping [ms]",
yaxis_title="Number of values",
template="plotly_dark",
margin=dict(t=5, b=5),
)
return fig
@app.callback(
Output(component_id='downtime_led', component_property='value'),
Input(component_id='interval_refresh_ping_slow', component_property="n_intervals")
)
def update_led(value):
# Retrieve data
sensor_name = "google"
dfs_dict = ping_data_extractor.retrieve_sensors_data([sensor_name], 24)
list_of_downtime = calculate_downtime(dfs_dict)
return np.around(list_of_downtime[0], 1)
# Callbacks
@app.callback(
Output(component_id='infrastructure_graph', component_property='figure'),
Input(component_id='interval_refresh_ping', component_property="n_intervals")
)
def stream_fig_network(value):
# Retrieve data
dfs_dict = ping_data_extractor.retrieve_type_data(
[PingDeviceType.INFRASTRUCTURE],
configuration["hours_to_display"])
fig = go.Figure()
# Plot
for df_index, df_name in enumerate(dfs_dict):
fig.add_trace(go.Scatter(x=dfs_dict[df_name].index, y=dfs_dict[df_name]["value"],
name=df_name))
fig.update_layout(
xaxis_title="Date and Time",
yaxis_title="Ping [ms]",
template="plotly_dark",
margin=dict(t=5, b=5),
yaxis={"rangemode": "nonnegative"}
)
return fig
@app.callback(
Output(component_id='power_graph', component_property='figure'),
Input(component_id='interval_refresh_power', component_property="n_intervals")
)
def stream_fig_power(value):
# Retrieve data
dfs_dict = power_data_extractor.retrieve_type_data(
[PowerDeviceType.PLUG],
configuration["hours_to_display"])
fig = go.Figure()
# Plot
for df_index, df_name in enumerate(dfs_dict):
fig.add_trace(go.Scatter(x=dfs_dict[df_name].index, y=dfs_dict[df_name]["value"],
name=df_name))
fig.update_layout(
xaxis_title="Date and Time",
yaxis_title="Power [W]",
template="plotly_dark",
margin=dict(t=5, b=5),
yaxis={"rangemode": "nonnegative"}
)
return fig
@app.callback(
Output(component_id='temperature_graph', component_property='figure'),
Input(component_id='interval_refresh_temperature_slow', component_property="n_intervals")
)
def stream_fig_temperature(value):
# Retrieve data
dfs_dict = temperature_data_extractor.retrieve_type_data(
[1],
168,
25)
fig = go.Figure()
# Plot
for df_index, df_name in enumerate(dfs_dict):
fig.add_trace(go.Scatter(x=dfs_dict[df_name].index, y=dfs_dict[df_name]["value"],
name=df_name))
fig.update_layout(
xaxis_title="Date and Time",
yaxis_title="Temperature [C]",
template="plotly_dark",
margin=dict(t=5, b=5),
yaxis={"rangemode": "nonnegative"}
)
return fig
# @app.callback(
# Output(component_id='output-container-button', component_property='children'),
# [Input(component_id='button-example-1', component_property='n_clicks')],
# )
# def update_output(n_clicks):
# if n_clicks is not None:
# if n_clicks % 2 == 0:
# switch_off_function("192.168.0.49")
# else:
# switch_on_function("192.168.0.49")
#
# return 'The button has been clicked {} times'.format(
# n_clicks
# )
@app.callback(
Output(component_id='dining_room_indicator', component_property='figure'),
[Input(component_id='interval_refresh_temperature', component_property='n_intervals')],
)
def update_dining_room_temp(n_clicks):
hours_to_retrieve = 1
sensor_name = "dining_room"
dfs_dict = temperature_data_extractor.retrieve_sensors_data([sensor_name], hours_to_retrieve)
current_value, reference_value = calculate_temp_and_ref(dfs_dict[sensor_name])
fig = go.Figure(go.Indicator(
mode="number+delta",
value=current_value,
number={"prefix": "", "suffix": " C"},
delta={"reference": reference_value, "valueformat": ".1f", "prefix": "", "suffix": " C/h"},
title={"text": "Dining Room Temp", 'font': {'size': 24}},
domain={'y': [0, 1], 'x': [0.1, 0.9]}))
fig.update_layout(
paper_bgcolor='#111111',
font={'color': "white",
'family': "Calibri",
},
autosize=False,
height=250,
)
return fig
@app.callback(
Output(component_id='bed_room_indicator', component_property='figure'),
[Input(component_id='interval_refresh_temperature', component_property='n_intervals')],
)
def update_dining_room_temp(n_clicks):
hours_to_retrieve = 1
sensor_name = "bed_room"
dfs_dict = temperature_data_extractor.retrieve_sensors_data([sensor_name], hours_to_retrieve)
current_value, reference_value = calculate_temp_and_ref(dfs_dict[sensor_name])
fig = go.Figure(go.Indicator(
mode="number+delta",
value=current_value,
number={"prefix": "", "suffix": " C"},
delta={"reference": reference_value, "valueformat": ".1f", "prefix": "", "suffix": " C/h"},
title={"text": "Bed Room Temp", 'font': {'size': 24}},
domain={'y': [0, 1], 'x': [0.1, 0.9]}))
fig.update_layout(
paper_bgcolor='#111111',
font={'color': "white",
'family': "Calibri",
},
autosize=False,
height=250,
)
return fig
logger.info("Starting server")
app.run_server(host="0.0.0.0", port=8069, dev_tools_ui=True, # debug=True,
dev_tools_hot_reload=True, threaded=True)