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:
+16
-6
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user