homecontrol-dash/homecontrol-dash.py

89 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env python3
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import time
import requests
URL_BASE="http://innocence:5000"
x = [1]
y = [2]
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, meta_tags=meta_tags)
available_sensors = ["undefined"]
app.layout = html.Div(children=[
html.H1(children='dashboard.ykonni.de'),
html.Div(children='''
visualize sensor data
'''),
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': x, 'y': y, 'mode': 'markers', 'name': 'SF'}
],
'layout': {
'title': 'initial values'
}
}
),
dcc.Dropdown(
id='select-sensor',
options=[{'label': i, 'value': i} for i in available_sensors],
value="undefined"
),
html.Button(id='refresh-button', n_clicks=0, children='refresh')
])
@app.callback(
Output('select-sensor', 'options'),
[Input('refresh-button', 'n_clicks')])
def update_list(n_clicks):
url = "%s/sensor/get" % URL_BASE
print(url)
res = requests.get(url)
content = res.json()
options = [{'label': content[i]["sensorType"], 'value': i} for i in content]
return options
@app.callback(
Output('example-graph', 'figure'),
[Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')])
def update_graph(n_clicks, sensorId):
if not sensorId == "undefined":
# url = "%s/sensor/get_values/%s?limit=10" % (URL_BASE, sensorId)
url = "%s/sensor/get_values/%s" % (URL_BASE, sensorId)
res = requests.get(url)
content = res.json()
v = content[sensorId]["values"]
sensorType = content[sensorId]["sensorType"]
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
y = [v[i]["value"] for i in range(len(v))]
else:
sensorType = sensorId
x = [1]
y = [1]
return {
'data': [
{'x': x, 'y': y, 'mode': 'markers', 'name': "%s" % (sensorType)}
],
'layout': {
'title': 'Data for sensor: %s' % (sensorType)
}
}
if __name__ == '__main__':
app.run_server(port=8081,debug=True)