2019-11-03 16:38:01 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import dash
|
2019-11-14 00:50:53 +01:00
|
|
|
import dash_bootstrap_components as dbc
|
2019-11-03 16:38:01 +01:00
|
|
|
import dash_core_components as dcc
|
|
|
|
import dash_html_components as html
|
|
|
|
from dash.dependencies import Input, Output
|
|
|
|
import time
|
|
|
|
import requests
|
2019-11-14 00:50:53 +01:00
|
|
|
from math import inf
|
2019-11-03 16:38:01 +01:00
|
|
|
|
2019-11-10 20:59:08 +01:00
|
|
|
URL_BASE="http://innocence:5000"
|
|
|
|
|
2019-11-14 00:50:53 +01:00
|
|
|
def get_sensors():
|
|
|
|
ret = {}
|
|
|
|
try:
|
|
|
|
url = "%s/sensor/get" % URL_BASE
|
|
|
|
res = requests.get(url)
|
|
|
|
ret = res.json()
|
|
|
|
except Exception as ex:
|
|
|
|
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def get_values(sensorId, min_ts, max_ts, limit):
|
|
|
|
ret = {}
|
|
|
|
if sensorId:
|
|
|
|
try:
|
|
|
|
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE,
|
|
|
|
sensorId, min_ts, max_ts, limit)
|
|
|
|
res = requests.get(url)
|
|
|
|
ret = res.json()[sensorId]
|
|
|
|
except Exception as ex:
|
|
|
|
print('Exception Type:%s, args:\n%s' % (type(ex).__name__, ex.args))
|
|
|
|
return ret
|
|
|
|
|
2019-11-03 16:38:01 +01:00
|
|
|
|
2019-11-14 00:50:53 +01:00
|
|
|
sensors = get_sensors()
|
|
|
|
sensor = next(iter(sensors), None)
|
|
|
|
|
|
|
|
# external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
|
|
|
|
external_stylesheets = [dbc.themes.BOOTSTRAP]
|
2019-11-11 10:25:32 +01:00
|
|
|
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}]
|
2019-11-03 16:38:01 +01:00
|
|
|
|
2019-11-11 10:25:32 +01:00
|
|
|
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, meta_tags=meta_tags)
|
2019-11-14 00:50:53 +01:00
|
|
|
app.title = "dashboard.ykonni.de"
|
2019-11-03 16:38:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
app.layout = html.Div(children=[
|
2019-11-11 10:25:32 +01:00
|
|
|
html.H1(children='dashboard.ykonni.de'),
|
2019-11-03 16:38:01 +01:00
|
|
|
|
|
|
|
html.Div(children='''
|
2019-11-11 10:25:32 +01:00
|
|
|
visualize sensor data
|
2019-11-03 16:38:01 +01:00
|
|
|
'''),
|
2019-11-14 00:50:53 +01:00
|
|
|
dbc.Row([
|
|
|
|
dbc.Col(
|
|
|
|
dcc.Graph(
|
|
|
|
id='graph-sensor-values',
|
|
|
|
figure={
|
|
|
|
'data': [
|
|
|
|
{'x': [0], 'y': [0], 'mode': 'markers', 'name': 'None'}
|
|
|
|
],
|
|
|
|
'layout': {
|
|
|
|
'title': 'initial values'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
dbc.Row([
|
|
|
|
dbc.Col([
|
|
|
|
html.Div(id='select-sensor-txt'),
|
|
|
|
dcc.Dropdown(
|
|
|
|
id='select-sensorid',
|
|
|
|
options=[{'label': sensors[i]["sensorType"], 'value': i} for i in sensors],
|
|
|
|
value=sensor),
|
|
|
|
]),
|
|
|
|
dbc.Col([
|
|
|
|
html.Div(id='slider-min-txt'),
|
|
|
|
dcc.Slider(
|
|
|
|
id='slider-min',
|
|
|
|
min=0,
|
|
|
|
max=round(time.time()),
|
|
|
|
step=600,
|
|
|
|
value=0
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dbc.Col([
|
|
|
|
html.Div(id='slider-max-txt'),
|
|
|
|
dcc.Slider(
|
|
|
|
id='slider-max',
|
|
|
|
min=0,
|
|
|
|
max=round(time.time()),
|
|
|
|
step=600,
|
|
|
|
value=round(time.time())
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dbc.Col([
|
|
|
|
html.Div(id='slider-limit-txt'),
|
|
|
|
dcc.Slider(
|
|
|
|
id='slider-limit',
|
|
|
|
min=0,
|
|
|
|
max=1000,
|
|
|
|
step=10,
|
|
|
|
value=0,
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
]),
|
2019-11-03 16:38:01 +01:00
|
|
|
])
|
|
|
|
|
2019-11-14 00:50:53 +01:00
|
|
|
@app.callback(
|
|
|
|
[Output('slider-min', 'min'), Output('slider-min', 'value'),
|
|
|
|
Output('slider-max', 'min'), Output('select-sensor-txt', 'children')],
|
|
|
|
[Input('select-sensorid', 'value')])
|
|
|
|
def update_slider_min(sensorId):
|
|
|
|
res = get_values(sensorId, 0, int(time.time()), 1)
|
|
|
|
min_ts = 0
|
|
|
|
sensorType = None
|
|
|
|
if "values" in res:
|
|
|
|
min_ts = int(res["values"][0]["ts"])
|
|
|
|
sensorType = res["sensorType"]
|
|
|
|
|
|
|
|
return min_ts, min_ts, min_ts, "Sensor: %s" % sensorType
|
2019-11-03 16:38:01 +01:00
|
|
|
|
|
|
|
@app.callback(
|
2019-11-14 00:50:53 +01:00
|
|
|
Output('slider-limit-txt', 'children'),
|
|
|
|
[Input('slider-limit', 'value')])
|
|
|
|
def update_slider_limit(value):
|
|
|
|
return "Limit: %s" % (value if value > 0 else None)
|
2019-11-03 16:38:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.callback(
|
2019-11-14 00:50:53 +01:00
|
|
|
Output('slider-min-txt', 'children'),
|
|
|
|
[Input('slider-min', 'value')])
|
|
|
|
def update_slider_min(value):
|
|
|
|
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if
|
|
|
|
value > 0 else 0)
|
|
|
|
|
|
|
|
|
|
|
|
@app.callback(
|
|
|
|
Output('slider-max-txt', 'children'),
|
|
|
|
[Input('slider-max', 'value')])
|
|
|
|
def update_slider_max(value):
|
|
|
|
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))))
|
|
|
|
|
|
|
|
|
|
|
|
@app.callback(
|
|
|
|
Output('graph-sensor-values', 'figure'),
|
|
|
|
[Input('select-sensorid', 'value'), Input('slider-min', 'value'),
|
|
|
|
Input('slider-max', 'value'), Input('slider-limit', 'value')])
|
|
|
|
def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
|
|
|
|
res = get_values(sensorId, min_ts, max_ts, limit)
|
|
|
|
if "values" in res:
|
|
|
|
v = res["values"]
|
|
|
|
s = res["sensorType"]
|
|
|
|
l = len(v)
|
2019-11-11 10:25:32 +01:00
|
|
|
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
|
2019-11-10 20:59:08 +01:00
|
|
|
y = [v[i]["value"] for i in range(len(v))]
|
|
|
|
else:
|
2019-11-14 00:50:53 +01:00
|
|
|
s = sensorId
|
|
|
|
x = [0]
|
|
|
|
y = [0]
|
2019-11-03 16:38:01 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
'data': [
|
2019-11-14 00:50:53 +01:00
|
|
|
{'x': x, 'y': y, 'mode': 'markers', 'name': "%s" % (s)}
|
2019-11-03 16:38:01 +01:00
|
|
|
],
|
|
|
|
'layout': {
|
2019-11-14 00:50:53 +01:00
|
|
|
'title': 'Data for sensor: %s (%d elements)' % (s, l)
|
2019-11-03 16:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2019-11-11 10:25:32 +01:00
|
|
|
app.run_server(port=8081,debug=True)
|