homecontrol-dash: small steps forward

This commit is contained in:
Konstantin Koslowski 2019-11-10 20:59:08 +01:00
parent 9c29ab825b
commit 91b4debe09

View file

@ -6,6 +6,8 @@ from dash.dependencies import Input, Output
import time import time
import requests import requests
URL_BASE="http://innocence:5000"
x = [1] x = [1]
y = [2] y = [2]
@ -13,7 +15,7 @@ external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets) app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
available_sensors = ["refresh first"] available_sensors = ["undefined"]
app.layout = html.Div(children=[ app.layout = html.Div(children=[
html.H1(children='homecontrol-dash'), html.H1(children='homecontrol-dash'),
@ -35,7 +37,7 @@ app.layout = html.Div(children=[
dcc.Dropdown( dcc.Dropdown(
id='select-sensor', id='select-sensor',
options=[{'label': i, 'value': i} for i in available_sensors], options=[{'label': i, 'value': i} for i in available_sensors],
value='Available Sensors' value="undefined"
), ),
html.Button(id='refresh-button', n_clicks=0, children='refresh') html.Button(id='refresh-button', n_clicks=0, children='refresh')
]) ])
@ -45,28 +47,39 @@ app.layout = html.Div(children=[
Output('select-sensor', 'options'), Output('select-sensor', 'options'),
[Input('refresh-button', 'n_clicks')]) [Input('refresh-button', 'n_clicks')])
def update_list(n_clicks): def update_list(n_clicks):
url = "http://localhost:5000/sensors/get" url = "%s/sensor/get" % URL_BASE
print(url)
res = requests.get(url) res = requests.get(url)
content = res.json() content = res.json()
return [{'label': content[i]["type"], 'value': i} for i in content] options = [{'label': content[i]["sensorType"], 'value': i} for i in content]
return options
@app.callback( @app.callback(
Output('example-graph', 'figure'), Output('example-graph', 'figure'),
[Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')]) [Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')])
def update_graph(n_clicks, sensor): def update_graph(n_clicks, sensorId):
url = "http://localhost:5000/sensors/get_values/%s" % sensor if not sensorId == "undefined":
res = requests.get(url) # url = "%s/sensor/get_values/%s?limit=10" % (URL_BASE, sensorId)
content = res.json() url = "%s/sensor/get_values/%s" % (URL_BASE, sensorId)
x = [time.strftime("%Y%m%d-%H%M%S", time.localtime(float(i))) for i in content] res = requests.get(url)
y = [content[i] for i in content] content = res.json()
v = content[sensorId]["values"]
sensorType = content[sensorId]["sensorType"]
x = [time.strftime("%m%d-%H%M%S", 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 { return {
'data': [ 'data': [
{'x': x, 'y': y, 'mode': 'markers', 'name': sensor} {'x': x, 'y': y, 'mode': 'markers', 'name': "%s (%s)" % (sensorType,
sensorId)}
], ],
'layout': { 'layout': {
'title': 'Data for sensor: %s' % sensor 'title': 'Data for sensor: %s (%s)' % (sensorType, sensorId)
} }
} }