sample2 improvements

This commit is contained in:
Chris Oloff 2025-11-20 08:46:12 +02:00
parent 47b5263874
commit 15e99c9c4a

View file

@ -18,9 +18,17 @@ class LoadForecast(BaseModel):
async def update(self, now: datetime) -> None: async def update(self, now: datetime) -> None:
pass pass
class Output(BaseModel):
target_storage: float
target_geyser_temperature: float
storage: list[float]
grid_sell: list[float]
grid_buy: list[float]
class SystemState(BaseModel): class SystemState(BaseModel):
load_forecast: LoadForecast = LoadForecast() load_forecast: LoadForecast = LoadForecast()
now: datetime = datetime.now() now: datetime = datetime.now()
output: Output | None = None
state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state) state: SystemState = panel.rx(lambda s: SystemState.model_validate(s) if s else SystemState())(w42_state)
@ -39,12 +47,14 @@ def get_label(at: datetime, slot_index: int) -> str:
def load_fc_chart(state: SystemState) -> dict: def load_fc_chart(state: SystemState) -> dict:
slots = state.load_forecast.slots slots = state.load_forecast.slots
at = state.load_forecast.at at = state.load_forecast.at
storage = state.output.storage if state.output else [0] * len(slots)
battery = [storage[ix] - storage[ix-1] if ix > 0 else 0 for ix in range(len(slots))] if state.output else [0] * len(slots)
return { return {
'title': { 'title': {
'text': '24hr Forecast' 'text': '24hr Forecast'
}, },
'legend': { 'legend': {
'data': ['Load Forecast'] 'data': ['Load Forecast', 'Battery Storage', 'Battery in/out']
}, },
'tooltip': { 'tooltip': {
'trigger': 'axis' 'trigger': 'axis'
@ -56,11 +66,23 @@ def load_fc_chart(state: SystemState) -> dict:
'yAxis': { 'yAxis': {
'type': 'value' 'type': 'value'
}, },
'series': [{ 'series': [
{
'name': 'Load Forecast', 'name': 'Load Forecast',
'data': slots, 'data': slots,
'type': 'line' 'type': 'line'
}] }, {
'name': 'Battery Storage',
'data': storage,
'type': 'line',
'color': 'orange'
}, {
'name': 'Battery in/out',
'data': battery,
'type': 'line',
'color': 'lightblue'
}
]
} }
load_fc_chart_rx = panel.rx(load_fc_chart)(state) load_fc_chart_rx = panel.rx(load_fc_chart)(state)