diff --git a/homecontrol-dash.py b/homecontrol-dash.py index 2929941..72b47eb 100755 --- a/homecontrol-dash.py +++ b/homecontrol-dash.py @@ -6,6 +6,8 @@ from dash.dependencies import Input, Output import time import requests +URL_BASE="http://innocence:5000" + x = [1] y = [2] @@ -13,7 +15,7 @@ external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) -available_sensors = ["refresh first"] +available_sensors = ["undefined"] app.layout = html.Div(children=[ html.H1(children='homecontrol-dash'), @@ -35,7 +37,7 @@ app.layout = html.Div(children=[ dcc.Dropdown( id='select-sensor', 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') ]) @@ -45,32 +47,43 @@ app.layout = html.Div(children=[ Output('select-sensor', 'options'), [Input('refresh-button', '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) 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( Output('example-graph', 'figure'), [Input('refresh-button', 'n_clicks'), Input('select-sensor', 'value')]) -def update_graph(n_clicks, sensor): - url = "http://localhost:5000/sensors/get_values/%s" % sensor - res = requests.get(url) - content = res.json() - x = [time.strftime("%Y%m%d-%H%M%S", time.localtime(float(i))) for i in content] - y = [content[i] for i in content] +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%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 { 'data': [ - {'x': x, 'y': y, 'mode': 'markers', 'name': sensor} + {'x': x, 'y': y, 'mode': 'markers', 'name': "%s (%s)" % (sensorType, + sensorId)} ], 'layout': { - 'title': 'Data for sensor: %s' % sensor + 'title': 'Data for sensor: %s (%s)' % (sensorType, sensorId) } } if __name__ == '__main__': - app.run_server(debug=True) \ No newline at end of file + app.run_server(debug=True)