-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
124 lines (111 loc) · 5.04 KB
/
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
import datetime
import random
import requests
import json
import threading
from dash import Dash, html, dcc, Input, Output, no_update
import dash_daq as daq
class Dashboard:
def __init__(self, Subscriber):
self.app = None
self.setup_layout()
self.subscriber = Subscriber
self.time = datetime.datetime.now()
def create_thermometer(self, id_suffix):
return html.Div([
daq.Thermometer(
id=f'my-thermometer-{id_suffix}',
value=0,
min=-40,
max=60,
label='Temperature',
showCurrentValue=True,
units='C',
style={
'display': 'inline-block',
'marginBottom': '5%' if id_suffix != '1' else '0'
}
),
html.Div([
html.Div(id=f'my-thermometer-{id_suffix}-datetime'),
html.Div(id=f'my-thermometer-{id_suffix}-conditions'),
html.Div(id=f'my-thermometer-{id_suffix}-intensities')
], style={'display': 'flex', 'flexDirection': 'column'}),
], style={'display': 'flex', 'flexDirection': 'column', 'marginRight': '5%'})
def setup_layout(self):
self.app = Dash(__name__)
self.app.layout = html.Div([
*[
self.create_thermometer(str(i))
for i in range(1, 2)
],
html.Div(id='motion-detection'),
dcc.Interval(
id='interval-component',
interval=5 * 1000, # in milliseconds
n_intervals=0
),
], style={
'display': 'flex',
'flexDirection': 'row',
})
@self.app.callback(
[
Output(f'my-thermometer-{i}', 'value') for i in range(1, 2)
] + [
Output(f'my-thermometer-{i}-datetime', 'children') for i in range(1, 2)
] + [
Output(f'my-thermometer-{i}-conditions', 'children') for i in range(1, 2)
] + [
Output(f'my-thermometer-{i}-intensities', 'children') for i in range(1, 2)
] + [
Output('motion-detection', 'children')
],
Input('interval-component', 'n_intervals')
)
def update_thermometer(n):
# check if 5 seconds have passed
if (datetime.datetime.now() - self.time).total_seconds() < 5:
return no_update
self.time = datetime.datetime.now() # Update the time
try:
# Retrieve data from the Subscriber
response_json = self.subscriber.get_weather_forecast_message()
#print(response_json)
response_motion_json = self.subscriber.get_motion_detection_message()
#print(response_motion_json)
except Exception as e:
raise Exception(f'Could not connect to the server: {str(e)}')
try:
# Parse the 'message' into a dictionary
response_json = json.loads(response_json['message'])
response_motion_json = json.loads(response_motion_json['message'])
except json.JSONDecodeError:
raise Exception('Invalid JSON format')
values = []
dates = []
conditions = []
intensities = []
motiondetection = []
try:
values.append(response_json['TemperatureC']) # Use 'TemperatureC' here
dates.append(response_json['Datetime']) # Use 'Datetime' here
conditions.append(response_json['Conditions']) # Use 'Conditions' here
intensities.append(response_json['Intensity']) # Use 'Intensity' here
# Extract motion detection details
motion_detection_data = response_motion_json.get('detection', {})
motion_detection_type = motion_detection_data.get('type', '')
motion_detection_value = motion_detection_data.get('value', False)
motion_detection_datetime = response_motion_json.get('datetime', '')
motion_detection_postal_code = response_motion_json.get('postal_code', '')
# Format motion detection details
motion_detection_str = f"Datetime: {str(motion_detection_datetime)}, Type: {str(motion_detection_type)}, Value: {str(motion_detection_value)}, Postal Code: {str(motion_detection_postal_code)}"
motiondetection.append(html.P(motion_detection_str))
except KeyError as e:
# Handle the case where the key is not present in the response_json
print(f"KeyError: {str(e)}")
return no_update
return [value for value in values + dates + conditions + intensities + motiondetection]
def run_dashboard(self):
self.app.run_server(debug=False, host='localhost', threaded=False)
print("Dashboard running on http://localhost:8050/")