cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.1541: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-09 06:36:15 +02:00
1 parent 05802d6f87
commit 94f46db8f8
19 files changed
+863 -60

No files matched your search

+49 -4
View File
@@ -24,6 +24,18 @@ like every command) with string args:
the battery's SoC bounds, which win (optimizer.py
Model.battery_plan). Kept with its schedule.
The binding (2026-11-09): a box that took a battery_plan stays bound to
that building after the plan ends ("battery_bound" in building.json: the
building, the level, since, the last plan's until). A bound battery's step
that no plan covers (the plan lapsed: the coordinator sent no renewal; or
beyond its 24 h within the box's 48 h) is self-consumption only: charged
from the house's PV surplus, discharged for the house's own use, never
charged from the grid or discharged into it for the box's own tariff
(optimizer.py Model.battery_plan, docs/methods.md A9.4). The battery
serves the building's flats, which the box's own plan cannot see. Ended
only when the box leaves that building (membership.json without it) or
the owner lifts a flexible battery plan by hand.
Rules: always within the flat's own floors (a cap below the comfort floor
of the owner or the tenant is refused: "floor"); a newer command for the
same control replaces the older; each is dropped at `until`. The level sits
@@ -60,6 +72,7 @@ PENALTY = { # EUR per unit across a building's request in the plan
# link checked (edge/link/gridorder.go): nobody overrides it; never a battery_plan
"emergency": {"load_limit": 500.0, "shift_request": 50.0, "thermostat_cap": 500.0},
}
BOUND_KEY = "battery_bound" # building.json: the binding of the battery to a building (kept after its plan ends)
SCHEDULE_STEP_S = 900 # a battery_plan's step: the plans' 15 minutes
SCHEDULE_MAX_W = 1e6 # |W| per step at most: a sanity bound on the numbers, not a battery's rating
@@ -69,9 +82,14 @@ class Building:
self.path, self.clock = os.path.join(state_dir, "building.json"), clock
self.membership_path = os.path.join(state_dir, "membership.json")
self.active: dict[str, dict] = {} # control -> the command's args (parsed)
# the building the battery is bound to (a battery_plan taken; kept after it ends: the module's doc)
self.bound: dict | None = None
try:
with open(self.path) as f:
self.active = {k: v for k, v in json.load(f).items() if k in CONTROLS}
raw = json.load(f)
self.active = {k: v for k, v in raw.items() if k in CONTROLS}
b = raw.get(BOUND_KEY)
self.bound = b if isinstance(b, dict) and b.get("building") else None
except (OSError, ValueError, AttributeError):
pass
self.drop_ended()
@@ -89,9 +107,21 @@ class Building:
def _save(self):
tmp = self.path + ".tmp"
with open(tmp, "w") as f:
json.dump(self.active, f)
json.dump({**self.active, **({BOUND_KEY: self.bound} if self.bound else {})}, f)
os.replace(tmp, self.path)
def battery_bound(self) -> dict | None:
"""The building the battery is bound to, or None (never bound, or the
box left that building: membership.json without it ends the binding)."""
if not self.bound:
return None
members = self.memberships()
if members is not None and str(self.bound["building"]) not in members:
self.bound = None
self._save()
return None
return self.bound
def drop_ended(self) -> list[dict]:
now = self.clock()
ended = [c for c in self.active.values() if now >= c["until"]]
@@ -165,22 +195,37 @@ class Building:
return False, f"floor: {cid}'s limit is {hi:g} °C, the cap {c['value']:g} °C"
old = self.active.get(c["control"])
self.active[c["control"]] = c
if c["control"] == "battery_plan":
b = self.bound if self.bound and self.bound.get("building") == c["building"] else {}
self.bound = {"building": c["building"], "level": c["level"], "since": b.get("since", int(self.clock())),
"until": c["until"], "id": c["id"]}
self._save()
replaced = f" (replaces {old['id']})" if old and old["id"] != c["id"] else ""
return True, f"{c['control']} {c['value']:g} {c['level']} until {time.strftime('%H:%M UTC', time.gmtime(c['until']))}{replaced}"
def lift(self, control: str):
"""The owner overrode a flexible request: it ends."""
"""The owner overrode a flexible request: it ends (a battery plan's
binding with it: the owner took the battery back)."""
if self.active.pop(control, None) is not None:
if control == "battery_plan":
self.bound = None
self._save()
def for_model(self, start: int, step_s: int, steps: int) -> dict:
"""The requests as the optimiser takes them: per step, or a window.
A battery_plan per model step (the slot at the step's middle; the
model may start after `from`: past slots skipped), None where the
command doesn't cover the step; from_step/to_step its covered span."""
command doesn't cover the step; from_step/to_step its covered span.
A bound battery (battery_bound) adds "battery_bound": {building,
level, until (the last plan's end), lapsed (no plan in force: it ended)}:
the steps no plan covers are self-consumption only."""
self.drop_ended()
out = {}
bound = self.battery_bound()
if bound:
out["battery_bound"] = {"building": bound["building"], "level": bound.get("level", "binding"),
"until": int(bound.get("until") or 0),
"lapsed": "battery_plan" not in self.active}
for ctl, c in self.active.items():
if ctl == "battery_plan":
ws, k_s = c.get("schedule") or [], int(c.get("step_s") or SCHEDULE_STEP_S)