-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi-sth.py
132 lines (113 loc) · 4.34 KB
/
api-sth.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
import dash
from dash import dcc, html
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go
import requests
from datetime import datetime
import pytz
# Constants for IP and port
IP_ADDRESS = "46.17.108.113"
PORT_STH = 8666
DASH_HOST = "0.0.0.0" # Set this to "0.0.0.0" to allow access from any IP
# Function to get luminosity data from the API
def get_luminosity_data(lastN):
url = f"http://{IP_ADDRESS}:{PORT_STH}/STH/v1/contextEntities/type/Lamp/id/urn:ngsi-ld:Lamp:001/attributes/luminosity?lastN={lastN}"
headers = {
'fiware-service': 'smart',
'fiware-servicepath': '/'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
try:
values = data['contextResponses'][0]['contextElement']['attributes'][0]['values']
return values
except KeyError as e:
print(f"Key error: {e}")
return []
else:
print(f"Error accessing {url}: {response.status_code}")
return []
# Function to convert UTC timestamps to Lisbon time
def convert_to_lisbon_time(timestamps):
utc = pytz.utc
lisbon = pytz.timezone('Europe/Lisbon')
converted_timestamps = []
for timestamp in timestamps:
try:
timestamp = timestamp.replace('T', ' ').replace('Z', '')
converted_time = utc.localize(datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')).astimezone(lisbon)
except ValueError:
# Handle case where milliseconds are not present
converted_time = utc.localize(datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')).astimezone(lisbon)
converted_timestamps.append(converted_time)
return converted_timestamps
# Set lastN value
lastN = 10 # Get 10 most recent points at each interval
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Luminosity Data Viewer'),
dcc.Graph(id='luminosity-graph'),
# Store to hold historical data
dcc.Store(id='luminosity-data-store', data={'timestamps': [], 'luminosity_values': []}),
dcc.Interval(
id='interval-component',
interval=10*1000, # in milliseconds (10 seconds)
n_intervals=0
)
])
@app.callback(
Output('luminosity-data-store', 'data'),
Input('interval-component', 'n_intervals'),
State('luminosity-data-store', 'data')
)
def update_data_store(n, stored_data):
# Get luminosity data
data_luminosity = get_luminosity_data(lastN)
if data_luminosity:
# Extract values and timestamps
luminosity_values = [float(entry['attrValue']) for entry in data_luminosity] # Ensure values are floats
timestamps = [entry['recvTime'] for entry in data_luminosity]
# Convert timestamps to Lisbon time
timestamps = convert_to_lisbon_time(timestamps)
# Append new data to stored data
stored_data['timestamps'].extend(timestamps)
stored_data['luminosity_values'].extend(luminosity_values)
return stored_data
return stored_data
@app.callback(
Output('luminosity-graph', 'figure'),
Input('luminosity-data-store', 'data')
)
def update_graph(stored_data):
if stored_data['timestamps'] and stored_data['luminosity_values']:
# Calculate mean luminosity
mean_luminosity = sum(stored_data['luminosity_values']) / len(stored_data['luminosity_values'])
# Create traces for the plot
trace_luminosity = go.Scatter(
x=stored_data['timestamps'],
y=stored_data['luminosity_values'],
mode='lines+markers',
name='Luminosity',
line=dict(color='orange')
)
trace_mean = go.Scatter(
x=[stored_data['timestamps'][0], stored_data['timestamps'][-1]],
y=[mean_luminosity, mean_luminosity],
mode='lines',
name='Mean Luminosity',
line=dict(color='blue', dash='dash')
)
# Create figure
fig_luminosity = go.Figure(data=[trace_luminosity, trace_mean])
# Update layout
fig_luminosity.update_layout(
title='Luminosity Over Time',
xaxis_title='Timestamp',
yaxis_title='Luminosity',
hovermode='closest'
)
return fig_luminosity
return {}
if __name__ == '__main__':
app.run_server(debug=True, host=DASH_HOST, port=8050)