diff --git a/backend/csms.py b/backend/csms.py index b0779de..26a264b 100644 --- a/backend/csms.py +++ b/backend/csms.py @@ -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() diff --git a/backend/store.py b/backend/store.py index 517a110..5580fc3 100644 --- a/backend/store.py +++ b/backend/store.py @@ -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), ) diff --git a/frontend/src/pages/History.tsx b/frontend/src/pages/History.tsx index 3d4b1a8..2036a5e 100644 --- a/frontend/src/pages/History.tsx +++ b/frontend/src/pages/History.tsx @@ -36,7 +36,7 @@ export default function History() {
{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 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."}
); diff --git a/log.json b/log.json new file mode 100644 index 0000000..83e4e6d --- /dev/null +++ b/log.json @@ -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"}]}]}] \ No newline at end of file