update config by POST to script

This commit is contained in:
Chris Oloff 2025-11-21 16:18:03 +02:00
parent 09edceb73c
commit 3b3b8f1ac1
3 changed files with 43 additions and 4 deletions

View file

@ -6,8 +6,15 @@ from pydantic import BaseModel
from watt42_viewlib import attach_w42_state
panel.extension('echarts', 'ace', 'jsoneditor')
SYSTEM_ID = "79476e53-dea6-44fa-976c-eff6260baeb6"
API_TOKEN = "d0vA6CsrY69N3JAOGtuZMEb9QpbJWPcoxhxRyBXZn8SIisB3weLKjMZwQRo8c2k9BRDtK0qHYnsvUMnqeO7Xog"
w42_state = panel.rx(None)
attach_w42_state(rx_var=w42_state, system_id="79476e53-dea6-44fa-976c-eff6260baeb6")
# TODO: must pass token, otherwise subscription should fail
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)
@ -27,6 +34,11 @@ class LoadForecast(BaseModel):
at: datetime = datetime.now()
slots: list[float] = []
class Configuration(BaseModel):
inverter_sid: str = ""
location_lat: float = 0.0
location_lon: float = 0.0
class SystemState(BaseModel):
load_forecast: LoadForecast = LoadForecast()
pv_forecast: PvForecast = PvForecast()
@ -34,6 +46,7 @@ class SystemState(BaseModel):
sell_prices: list[float] = []
buy_prices: list[float] = []
output: Output | None = None
config: Configuration = Configuration()
state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state)
@ -139,10 +152,34 @@ prices_chart_rx = panel.rx(prices_chart)(state)
datetime_fmt = "%Y-%m-%d %H:%M:%S"
value = w42_state.rx.value
config_editor = panel.widgets.JSONEditor(value=state.config.model_dump(), mode='form', height=200, width=400, menu=False)
SCRIPT_ID = "23a6d15a-f7e3-4ff1-b3c1-94d297c972c9"
async def process_config_change(_btn):
from httpx import AsyncClient
async with AsyncClient() as client:
response = await client.post(
f"http://localhost:8000/api/scripts/{SCRIPT_ID}/execute?system_guid={SYSTEM_ID}",
json={
'signal': 'update_config',
'request_body': config_editor.value,
},
headers={
"Content-Type": "application/json",
"x-api-token": API_TOKEN
}
)
print(f"Config update response: {response.status_code} - {response.text}")
# _ = panel.rx(process_config_change)(config_editor.value)
config_submit = panel.widgets.Button(name="Update Configuration", button_type="primary", width=200)
config_submit.on_click(process_config_change)
_ = panel.template.FastListTemplate(
title="Sample W42 View App",
title="Sample W42 App",
sidebar=[panel.pane.Markdown("This is a sample sidebar.")],
main=[
# panel.pane.Markdown(state_as_text, sizing_mode='stretch_width'),
@ -157,6 +194,7 @@ _ = panel.template.FastListTemplate(
height=300
),
w42_state,
panel.Column(config_editor, config_submit, sizing_mode='stretch_width'),
panel.pane.Markdown(panel.bind(lambda s: f"State at {s.now.strftime(datetime_fmt)}, Load Forecast Time: {s.load_forecast.at}", state), sizing_mode='stretch_width'),
],
).servable()