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 dcc
poetry.lock poetry.lock
homecontrol_dash.egg-info homecontrol_dash.egg-info
__pycache__

View file

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