diff --git a/sample2.py b/sample2.py index 96b4463..fc25d90 100644 --- a/sample2.py +++ b/sample2.py @@ -14,7 +14,7 @@ API_TOKEN = "d0vA6CsrY69N3JAOGtuZMEb9QpbJWPcoxhxRyBXZn8SIisB3weLKjMZwQRo8c2k9BRD w42_state = panel.rx(None) # TODO: must pass token, otherwise subscription should fail -attach_w42_state(rx_var=w42_state, system_id=SYSTEM_ID, token=API_TOKEN) +attach_w42_state(rx_var=w42_state, system_id=SYSTEM_ID) state_as_text = panel.bind(lambda s: f"W42 State:\n\n```\n{json.dumps(s, indent=2)}\n```\n\nReplace this with some awesome visuals", w42_state) @@ -39,9 +39,6 @@ class Configuration(BaseModel): location_lat: float = 0.0 location_lon: float = 0.0 -class Stats(BaseModel): - grid_cost_total: float = 0.0 - class SystemState(BaseModel): load_forecast: LoadForecast = LoadForecast() pv_forecast: PvForecast = PvForecast() @@ -50,7 +47,6 @@ class SystemState(BaseModel): buy_prices: list[float] = [] output: Output | None = None config: Configuration = Configuration() - stats: Stats = Stats() state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state) @@ -182,16 +178,11 @@ async def process_config_change(_btn): config_submit = panel.widgets.Button(name="Update Configuration", button_type="primary", width=200) config_submit.on_click(process_config_change) -def summary(state: SystemState) -> str: - total_cost = state.stats.grid_cost_total - return f"## Summary\n\nGrid {'cost' if total_cost > 0 else 'income'} total: {abs(state.stats.grid_cost_total):.2f} ZAR" - _ = panel.template.FastListTemplate( title="Sample W42 App", sidebar=[panel.pane.Markdown("This is a sample sidebar.")], main=[ # panel.pane.Markdown(state_as_text, sizing_mode='stretch_width'), - panel.pane.Markdown(panel.rx(summary)(state), sizing_mode='stretch_width'), panel.pane.ECharts( load_fc_chart_rx, sizing_mode='stretch_width', diff --git a/watt42_viewlib/__init__.py b/watt42_viewlib/__init__.py index 7f733d0..ac1a9a0 100644 --- a/watt42_viewlib/__init__.py +++ b/watt42_viewlib/__init__.py @@ -8,7 +8,7 @@ from websockets.asyncio.client import connect logger = logging.getLogger(__name__) -def attach_w42_state(rx_var: panel.rx, system_id: str, token: str): +def attach_w42_state(rx_var: panel.rx, system_id: str): WS_URL = "ws://localhost:8000/ws/systems" # TODO: make configurable @@ -23,17 +23,12 @@ def attach_w42_state(rx_var: panel.rx, system_id: str, token: str): logger.info(f"Connected to {WS_URL}") send_response = await websocket.send(json.dumps({ "action": "subscribe", - "system_id": system_id, - "token": token + "system_id": system_id })) logger.info(f"Subscribed to system {system_id}, waiting for messages..., send_response={send_response}") async for message in websocket: as_json = json.loads(message) - if as_json.get('error'): - logger.error(f"Error from websocket: {as_json['error']}") - rx_var.rx.value = as_json - else: - rx_var.rx.value = as_json['change']['state'] + rx_var.rx.value = as_json['change']['state'] except ConnectionClosed: if must_reconnect: logger.info("connection closed, retrying...")