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") tx = kwargs.get("transaction_id")
if tx is not None: if tx is not None:
for entry in meter_value or []: for entry in meter_value or []:
reading = {} reading, context = {}, None
for sv in entry.get("sampled_value") or []: for sv in entry.get("sampled_value") or []:
# measurand defaults to the energy register when omitted # measurand defaults to the energy register when omitted
measurand = sv.get("measurand", "Energy.Active.Import.Register") measurand = sv.get("measurand", "Energy.Active.Import.Register")
context = sv.get("context", context) # Sample.Periodic / Transaction.Begin / .End
try: try:
value = float(sv.get("value")) value = float(sv.get("value"))
except (TypeError, ValueError): except (TypeError, ValueError):
@@ -207,10 +208,13 @@ class ChargePoint(OcppChargePoint):
reading["power_w"] = value * 1000 if sv.get("unit") == "kW" else value reading["power_w"] = value * 1000 if sv.get("unit") == "kW" else value
elif measurand == "SoC": elif measurand == "SoC":
reading["soc"] = value 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: if reading:
store.add_sample(tx, entry.get("timestamp", now()), store.add_sample(tx, entry.get("timestamp", now()),
reading.get("energy_wh"), reading.get("power_w"), context=context, **reading)
reading.get("soc"))
log.info("MeterValues: connector=%s values=%r", connector_id, meter_value) log.info("MeterValues: connector=%s values=%r", connector_id, meter_value)
return call_result.MeterValues() return call_result.MeterValues()
+16 -6
View File
@@ -51,7 +51,10 @@ CREATE TABLE IF NOT EXISTS samples (
time TEXT NOT NULL, time TEXT NOT NULL,
energy_wh REAL, energy_wh REAL,
power_w 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_samples_tx ON samples (tx);
CREATE INDEX IF NOT EXISTS idx_sessions_started ON sessions (started); CREATE INDEX IF NOT EXISTS idx_sessions_started ON sessions (started);
@@ -64,10 +67,14 @@ def _db():
con = sqlite3.connect(DB_PATH) con = sqlite3.connect(DB_PATH)
con.row_factory = sqlite3.Row con.row_factory = sqlite3.Row
con.executescript(_SCHEMA) 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)")] cols = [row[1] for row in con.execute("PRAGMA table_info(sessions)")]
if "method" not in cols: if "method" not in cols:
con.execute("ALTER TABLE sessions ADD COLUMN method TEXT") 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) 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], def add_sample(tx: int, time: str, energy_wh: Optional[float] = None,
power_w: Optional[float], soc: 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: with _db() as con, con:
con.execute( con.execute(
"INSERT INTO samples (tx, time, energy_wh, power_w, soc) VALUES (?, ?, ?, ?, ?)", "INSERT INTO samples (tx, time, energy_wh, power_w, soc, voltage_v, current_a, context)"
(tx, time, energy_wh, power_w, soc), " VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(tx, time, energy_wh, power_w, soc, voltage_v, current_a, context),
) )
+1 -1
View File
@@ -36,7 +36,7 @@ export default function History() {
<p className="empty"> <p className="empty">
{admin {admin
? "No charging sessions yet. The first card tap or in-app start will appear here with who, when, how, and how many kWh." ? "No charging sessions yet. The first card tap or in-app start will appear here with who, when, how, and how many kWh."
: "No charging sessions of yours yet. Sessions you start — by card or in-app — appear here."} : "No charging sessions yet. Sessions you start — by card or in-app — appear here."}
</p> </p>
); );
+1
View File
@@ -0,0 +1 @@
[2,"2901b39c-8ec0-4b2e-9402-ecf19aeda2f9","MeterValues",{"connectorId":1,"transactionId":2,"meterValue":[{"timestamp":"2026-07-12T07:22:13.558Z","sampledValue":[{"value":"0.0","context":"Transaction.Begin","format":"Raw","measurand":"Current.Import","location":"Outlet","unit":"A"},{"value":"405731","context":"Transaction.Begin","format":"Raw","measurand":"Energy.Active.Import.Register","location":"Outlet","unit":"Wh"},{"value":"0","context":"Transaction.Begin","format":"Raw","measurand":"Power.Active.Import","location":"Outlet","unit":"W"},{"value":"0","context":"Transaction.Begin","format":"Raw","measurand":"SoC","location":"Outlet","unit":"Percent"},{"value":"0.0","context":"Transaction.Begin","format":"Raw","measurand":"Voltage","location":"Outlet","unit":"V"}]}]}]