feat(backend): capture voltage, current and sample context in meter samples

The charger sends Voltage/Current.Import in every MeterValues frame but
we only stored energy/power/SoC. samples now also record voltage_v,
current_a and the OCPP context (Sample.Periodic vs Transaction.Begin/End
snapshots, which are legitimately all-zero). Existing dbs migrate via
ALTER TABLE on open.
This commit is contained in:
2026-07-12 09:32:32 +02:00
parent 9562fdaf6b
commit 8d8affe6ff
4 changed files with 25 additions and 10 deletions
+7 -3
View File
@@ -192,10 +192,11 @@ class ChargePoint(OcppChargePoint):
tx = kwargs.get("transaction_id")
if tx is not None:
for entry in meter_value or []:
reading = {}
reading, context = {}, None
for sv in entry.get("sampled_value") or []:
# measurand defaults to the energy register when omitted
measurand = sv.get("measurand", "Energy.Active.Import.Register")
context = sv.get("context", context) # Sample.Periodic / Transaction.Begin / .End
try:
value = float(sv.get("value"))
except (TypeError, ValueError):
@@ -207,10 +208,13 @@ class ChargePoint(OcppChargePoint):
reading["power_w"] = value * 1000 if sv.get("unit") == "kW" else value
elif measurand == "SoC":
reading["soc"] = value
elif measurand == "Voltage":
reading["voltage_v"] = value * 1000 if sv.get("unit") == "kV" else value
elif measurand == "Current.Import":
reading["current_a"] = value
if reading:
store.add_sample(tx, entry.get("timestamp", now()),
reading.get("energy_wh"), reading.get("power_w"),
reading.get("soc"))
context=context, **reading)
log.info("MeterValues: connector=%s values=%r", connector_id, meter_value)
return call_result.MeterValues()
+16 -6
View File
@@ -51,7 +51,10 @@ CREATE TABLE IF NOT EXISTS samples (
time TEXT NOT NULL,
energy_wh REAL,
power_w REAL,
soc REAL
soc REAL,
voltage_v REAL,
current_a REAL,
context TEXT
);
CREATE INDEX IF NOT EXISTS idx_samples_tx ON samples (tx);
CREATE INDEX IF NOT EXISTS idx_sessions_started ON sessions (started);
@@ -64,10 +67,14 @@ def _db():
con = sqlite3.connect(DB_PATH)
con.row_factory = sqlite3.Row
con.executescript(_SCHEMA)
# migration for dbs created before the audit column existed
# migrations for dbs created before later columns existed
cols = [row[1] for row in con.execute("PRAGMA table_info(sessions)")]
if "method" not in cols:
con.execute("ALTER TABLE sessions ADD COLUMN method TEXT")
sample_cols = [row[1] for row in con.execute("PRAGMA table_info(samples)")]
for col, ddl in (("voltage_v", "REAL"), ("current_a", "REAL"), ("context", "TEXT")):
if col not in sample_cols:
con.execute(f"ALTER TABLE samples ADD COLUMN {col} {ddl}")
return closing(con)
@@ -114,12 +121,15 @@ def end_session(tx: int, ended: str, meter_stop: float,
)
def add_sample(tx: int, time: str, energy_wh: Optional[float],
power_w: Optional[float], soc: Optional[float]) -> None:
def add_sample(tx: int, time: str, energy_wh: Optional[float] = None,
power_w: Optional[float] = None, soc: Optional[float] = None,
voltage_v: Optional[float] = None, current_a: Optional[float] = None,
context: Optional[str] = None) -> None:
with _db() as con, con:
con.execute(
"INSERT INTO samples (tx, time, energy_wh, power_w, soc) VALUES (?, ?, ?, ?, ?)",
(tx, time, energy_wh, power_w, soc),
"INSERT INTO samples (tx, time, energy_wh, power_w, soc, voltage_v, current_a, context)"
" VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(tx, time, energy_wh, power_w, soc, voltage_v, current_a, context),
)