homecontrol-dash: adapt layout for sensors or actors

This commit is contained in:
Konstantin Koslowski 2020-03-01 22:30:35 +01:00
parent 7126ced865
commit f1b6b11db5
3 changed files with 62 additions and 40 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ dash
dcc
poetry.lock
homecontrol_dash.egg-info
__pycache__

View file

@ -81,8 +81,11 @@ class HCDash:
try:
url = f"{self.config.get('address')}/actor/get"
res = requests.get(url)
self.logger.debug(f"get_actors: {res}")
ret = res.json()
tmp = res.json()
# 10 minutes
for a in tmp:
if tmp[a].get("ts") > (time.time() - 60*10):
ret[a] = tmp[a]
self.logger.debug(f"get_actors: {ret}")
except Exception as ex:
self.logger.error(f"Exception get_actors, type: {type(ex).__name__}, args:\n{ex.args}")
@ -146,11 +149,12 @@ class HCDash:
]),
html.Div(id="tabs-content")
])
# tab callbacks
@app.callback(
Output("tabs-content", "children"),
[Input("tabs-select-class", "value")])
def updatefoo(value):
def update_tabs(value):
self.logger.debug(f"update_tabs: {value}")
if value == "sensors":
sensors = self.get_sensors()
sensor = next(iter(sensors), None)
@ -163,6 +167,36 @@ class HCDash:
dbc.Col(dcc.Tabs(id="tabs-select-sensor", value=sensor,
children=sensor_tabs))
]),
html.Div(id="sensor-data"),
])
elif value == "actors":
actors = self.get_actors()
actor = next(iter(actors), None)
actor_tabs = []
for a in actors:
actorType = actors[a]["actorType"]
actor_tabs.append(dcc.Tab(label=actorType, value=a))
return html.Div([
dbc.Row([
dbc.Col(dcc.Tabs(id="tabs-select-actor", value=actor,
children=actor_tabs))
]),
dbc.Row([
html.Div(id="actor-data"),
]),
])
else:
return html.Div([
html.H3("undefined")
]), None, None
@app.callback(
Output("sensor-data", "children"),
[Input("tabs-select-sensor", "value")])
def update_sensor(value):
self.logger.debug(f"update_sensor: {value}")
if value:
return [
dbc.Row([
dbc.Col(
dcc.Graph(
@ -198,35 +232,25 @@ class HCDash:
dcc.Slider(id='slider-limit', min=0, max=1000, step=10, value=0 ),
], style={'marginLeft': '3em', 'marginRight': '5em'}
),
]),
]),
])
elif value == "actors":
actors = self.get_actors()
actor = next(iter(actors), None)
actor_tabs = []
for a in actors:
actorType = actors[a]["actorType"]
actor_tabs.append(dcc.Tab(label=actorType, value=a))
return html.Div([
dbc.Row([
dbc.Col(dcc.Tabs(id="tabs-select-actor", value=actor,
children=actor_tabs))
]),
dbc.Row([
]
@app.callback(
Output("actor-data", "children"),
[Input("tabs-select-actor", "value")])
def update_actor(value):
self.logger.debug(f"update_actor: {value}")
if value:
return [
daq.ColorPicker(
id="color-picker",
label="Color Picker",
size=400,
size=360,
value=dict(hex="#268bd2")
),
html.P(id="empty")
]),
])
else:
return html.Div([
html.H3("undefined")
])
]
## sensor callbacks
@ -244,7 +268,6 @@ class HCDash:
if "values" in res:
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
return min_ts, cur_ts, max_ts
@app.callback(
@ -253,6 +276,7 @@ class HCDash:
def update_slider_min_txt(value):
return f"From: {self.pTime(value)}"
@app.callback(
[Output('slider-max', 'min'), Output('slider-max', 'value'),
Output('slider-max', 'max')],
@ -265,7 +289,6 @@ class HCDash:
if "values" in res:
min_ts = int(res["values"][0]["ts"])
sensorType = res["sensorType"]
return min_ts, max_ts, max_ts
@ -308,18 +331,16 @@ class HCDash:
'layout': { 'title': f'Data for sensor: {s} ({len(x)} elements)' }
}
## actor callbacks
@app.callback(
Output("empty", "value"),
[Input('tabs-select-actor', 'value'), Input('color-picker', 'value')])
def set_level(actorId, level):
rgb = level.get("rgb")
if rgb:
r = (rgb.get("r") or 0) << 16
g = (rgb.get("g") or 0) << 8
b = (rgb.get("b") or 0)
l = r + g + b
l = f"0x{level.get('hex').replace('#', '')}"
if l:
self.set_level(actorId, l)
self.logger.debug(f"hex: {l}")
return ""