diff --git a/.gitignore b/.gitignore index ba64065..47e3095 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ dash dcc poetry.lock homecontrol_dash.egg-info +__pycache__ diff --git a/homecontrol_dash/__pycache__/hcdash.cpython-38.pyc b/homecontrol_dash/__pycache__/hcdash.cpython-38.pyc deleted file mode 100644 index 0428126..0000000 Binary files a/homecontrol_dash/__pycache__/hcdash.cpython-38.pyc and /dev/null differ diff --git a/homecontrol_dash/hcdash.py b/homecontrol_dash/hcdash.py index a3d86cf..d282ab2 100755 --- a/homecontrol_dash/hcdash.py +++ b/homecontrol_dash/hcdash.py @@ -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( @@ -181,8 +215,8 @@ class HCDash: dcc.Slider(id='slider-min', min=0, max=round(time.time()), step=600, value=0 ), ], style={'marginLeft': '5em', 'marginRight': '3em'} - ), - ]), + ), + ]), dbc.Col([ html.Div(id='slider-max-txt', style={'marginLeft': '3em', 'marginRight': '3em'}), @@ -190,43 +224,33 @@ class HCDash: dcc.Slider(id='slider-max', min=0, max=round(time.time()), step=600, value=round(time.time())), ], style={'marginLeft': '3em', 'marginRight': '3em'} - ), - ]), + ), + ]), dbc.Col([ html.Div(id='slider-limit-txt', style={'marginLeft': '3em', 'marginRight': '5em'}), html.Div([ 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,19 +331,17 @@ 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) - 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"))