homecontrol-dash: default to last 7 days of values, some cleanup
This commit is contained in:
parent
5587ac45f6
commit
bb21cd3b80
1 changed files with 42 additions and 17 deletions
|
@ -6,6 +6,7 @@ import dash_html_components as html
|
||||||
from dash.dependencies import Input, Output
|
from dash.dependencies import Input, Output
|
||||||
import time
|
import time
|
||||||
import requests
|
import requests
|
||||||
|
from math import log, log10
|
||||||
|
|
||||||
URL_BASE="http://innocence:5000"
|
URL_BASE="http://innocence:5000"
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ def get_values(sensorId, min_ts, max_ts, limit):
|
||||||
try:
|
try:
|
||||||
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE,
|
url = "%s/sensor/get_values/%s?min_ts=%s&max_ts=%s&limit=%s" % (URL_BASE,
|
||||||
sensorId, min_ts, max_ts, limit)
|
sensorId, min_ts, max_ts, limit)
|
||||||
|
# print(url)
|
||||||
res = requests.get(url)
|
res = requests.get(url)
|
||||||
ret = res.json()[sensorId]
|
ret = res.json()[sensorId]
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
@ -91,55 +93,78 @@ app.layout = html.Div(children=[
|
||||||
]),
|
]),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
@app.callback(
|
@app.callback(
|
||||||
[Output('slider-min', 'min'), Output('slider-min', 'value'),
|
[Output('slider-min', 'min'), Output('slider-min', 'value'),
|
||||||
Output('slider-max', 'min')],
|
Output('slider-min', 'max')],
|
||||||
[Input('tabs-select-sensor', 'value')])
|
[Input('tabs-select-sensor', 'value')])
|
||||||
def update_slider_min(sensorId):
|
def update_slider_min(sensorId):
|
||||||
res = get_values(sensorId, 0, int(time.time()), 1)
|
res = get_values(sensorId, 0, int(time.time()), 1)
|
||||||
min_ts = 0
|
min_ts = 0
|
||||||
|
# default last 7 days
|
||||||
|
cur_ts = round(time.time() - (60*60*24*7))
|
||||||
|
max_ts = int(time.time())
|
||||||
sensorType = None
|
sensorType = None
|
||||||
if "values" in res:
|
if "values" in res:
|
||||||
min_ts = int(res["values"][0]["ts"])
|
min_ts = int(res["values"][0]["ts"])
|
||||||
sensorType = res["sensorType"]
|
sensorType = res["sensorType"]
|
||||||
|
|
||||||
return min_ts, min_ts, min_ts
|
# print("min: [%f [%f] %f]" % (min_ts, min_ts, max_ts))
|
||||||
|
return min_ts, cur_ts, max_ts
|
||||||
|
|
||||||
@app.callback(
|
|
||||||
Output('slider-limit-txt', 'children'),
|
|
||||||
[Input('slider-limit', 'value')])
|
|
||||||
def update_slider_limit(value):
|
|
||||||
return "Limit: %s" % (value if value > 0 else None)
|
|
||||||
|
|
||||||
|
|
||||||
@app.callback(
|
@app.callback(
|
||||||
Output('slider-min-txt', 'children'),
|
Output('slider-min-txt', 'children'),
|
||||||
[Input('slider-min', 'value')])
|
[Input('slider-min', 'value')])
|
||||||
def update_slider_min(value):
|
def update_slider_min_txt(value):
|
||||||
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if
|
return "From: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))) if
|
||||||
value > 0 else 0)
|
value > 0 else 0)
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
[Output('slider-max', 'min'), Output('slider-max', 'value'),
|
||||||
|
Output('slider-max', 'max')],
|
||||||
|
[Input('tabs-select-sensor', 'value')])
|
||||||
|
def update_slider_max(sensorId):
|
||||||
|
res = get_values(sensorId, 0, int(time.time()), 1)
|
||||||
|
min_ts = 0
|
||||||
|
max_ts = int(time.time())
|
||||||
|
sensorType = None
|
||||||
|
if "values" in res:
|
||||||
|
min_ts = int(res["values"][0]["ts"])
|
||||||
|
sensorType = res["sensorType"]
|
||||||
|
|
||||||
|
# print("max: [%f [%f] %f]" % (min_ts, max_ts, max_ts))
|
||||||
|
return min_ts, max_ts, max_ts
|
||||||
|
|
||||||
|
|
||||||
@app.callback(
|
@app.callback(
|
||||||
Output('slider-max-txt', 'children'),
|
Output('slider-max-txt', 'children'),
|
||||||
[Input('slider-max', 'value')])
|
[Input('slider-max', 'value')])
|
||||||
def update_slider_max(value):
|
def update_slider_max_txt(value):
|
||||||
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))))
|
return "To: %s" % (time.strftime("%Y/%m/%d %H:%M", time.localtime(float(value))))
|
||||||
|
|
||||||
|
|
||||||
@app.callback(
|
@app.callback(
|
||||||
[Output('graph-sensor-values', 'figure'), Output('slider-min', 'max'),
|
Output('slider-limit-txt', 'children'),
|
||||||
Output('slider-max', 'max')],
|
[Input('slider-limit', 'value')])
|
||||||
|
def update_slider_limit_txt(value):
|
||||||
|
return "Limit: %s" % (value if value > 0 else None)
|
||||||
|
|
||||||
|
|
||||||
|
@app.callback(
|
||||||
|
Output('graph-sensor-values', 'figure'),
|
||||||
[Input('tabs-select-sensor', 'value'), Input('slider-min', 'value'),
|
[Input('tabs-select-sensor', 'value'), Input('slider-min', 'value'),
|
||||||
Input('slider-max', 'value'), Input('slider-limit', 'value')])
|
Input('slider-max', 'value'), Input('slider-limit', 'value')])
|
||||||
def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
|
def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
|
||||||
|
res = {}
|
||||||
|
if min_ts > 0:
|
||||||
res = get_values(sensorId, min_ts, max_ts, limit)
|
res = get_values(sensorId, min_ts, max_ts, limit)
|
||||||
if "values" in res:
|
if "values" in res:
|
||||||
v = res["values"]
|
v = res["values"]
|
||||||
s = res["sensorType"]
|
s = res["sensorType"]
|
||||||
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
|
x = [time.strftime("%m%d-%H%M", time.localtime(float(v[i]["ts"]))) for i in range(len(v))]
|
||||||
|
if s == "luminance":
|
||||||
|
y = [log10(v[i]["value"]/5+1) for i in range(len(v))]
|
||||||
|
else:
|
||||||
y = [v[i]["value"] for i in range(len(v))]
|
y = [v[i]["value"] for i in range(len(v))]
|
||||||
else:
|
else:
|
||||||
s = sensorId
|
s = sensorId
|
||||||
|
@ -148,7 +173,7 @@ def update_graph_sensor_values(sensorId, min_ts, max_ts, limit):
|
||||||
return {
|
return {
|
||||||
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': "%s" % (s)} ],
|
'data': [ {'x': x, 'y': y, 'mode': 'line', 'name': "%s" % (s)} ],
|
||||||
'layout': { 'title': 'Data for sensor: %s (%d elements)' % (s, len(x)) }
|
'layout': { 'title': 'Data for sensor: %s (%d elements)' % (s, len(x)) }
|
||||||
}, time.time(), time.time()
|
}
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue