feat: live telemetry on gun cards — SoC battery gauge, kW, V, A, delivered kWh

- backend keeps the latest meter sample per active transaction in memory
  and ships it as active_transactions[tx].telemetry on /api/status
  (Transaction.Begin snapshots skipped: taken before the BMS is read)
- Battery component: fill + color sweep red (empty) -> blue (half) ->
  green (full) per the owner's spec; aria meter semantics
- charging bays show the gauge, a prominent kW readout, and delivered
  kWh / voltage / current rows; hint while awaiting the first sample
- smoke: 60 checks (telemetry rides /api/status)
This commit is contained in:
2026-07-12 09:39:05 +02:00
parent 8d8affe6ff
commit 25fa1f9fc5
7 changed files with 149 additions and 15 deletions
+7
View File
@@ -215,6 +215,13 @@ class ChargePoint(OcppChargePoint):
if reading:
store.add_sample(tx, entry.get("timestamp", now()),
context=context, **reading)
# live telemetry for the panel: latest sample rides
# /api/status. Transaction.Begin is skipped — it's taken
# before the car's BMS is read (SoC 0, all zeros).
if context != "Transaction.Begin" and tx in ACTIVE_TX:
ACTIVE_TX[tx].setdefault("telemetry", {}).update(
{**reading, "time": entry.get("timestamp")}
)
log.info("MeterValues: connector=%s values=%r", connector_id, meter_value)
return call_result.MeterValues()
+41
View File
@@ -0,0 +1,41 @@
/** State-of-charge gauge: a battery whose fill sweeps the owner-specified
* gradient — red (empty) → blue (half) → green (full). */
type RGB = [number, number, number];
const RED: RGB = [191, 53, 39];
const BLUE: RGB = [33, 96, 196];
const GREEN: RGB = [23, 138, 92];
function mix(a: RGB, b: RGB, t: number): string {
const c = a.map((v, i) => Math.round(v + (b[i] - v) * t));
return `rgb(${c[0]}, ${c[1]}, ${c[2]})`;
}
export function socColor(soc: number): string {
const s = Math.max(0, Math.min(100, soc));
return s <= 50 ? mix(RED, BLUE, s / 50) : mix(BLUE, GREEN, (s - 50) / 50);
}
export default function Battery({ soc }: { soc: number | null | undefined }) {
const known = soc != null;
const pct = known ? Math.max(0, Math.min(100, soc)) : 0;
const color = known ? socColor(pct) : "transparent";
return (
<div
className="battery"
role="meter"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={known ? Math.round(pct) : undefined}
aria-label="Vehicle state of charge"
>
<span className="battery__shell">
<span className="battery__fill" style={{ width: `${pct}%`, background: color }} />
</span>
<span className="battery__cap" />
<span className="battery__pct mono" style={known ? { color: socColor(pct) } : undefined}>
{known ? `${Math.round(pct)}%` : "—"}
</span>
</div>
);
}
+54 -13
View File
@@ -1,4 +1,5 @@
import { api, scopeAllowed } from "../api";
import Battery from "../components/Battery";
import { Lamp, statusTone } from "../components/Lamp";
import { useAction, useLiveRelays, useStatus } from "../queries";
import { useApp } from "../store";
@@ -127,20 +128,60 @@ function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) {
</div>
{gun.tx && (
<dl className="bay__telemetry">
<div>
<dt>By</dt>
<dd>{gun.tx.who || gun.tx.id_tag}</dd>
<>
<div className="bay__charge">
<Battery soc={gun.tx.telemetry?.soc} />
<div className="bay__kw">
<span className="bay__kw-num mono">
{gun.tx.telemetry?.power_w != null
? (gun.tx.telemetry.power_w / 1000).toFixed(1)
: "—"}
</span>
<span className="bay__kw-unit">kW</span>
</div>
</div>
<div>
<dt>Via</dt>
<dd>{gun.tx.method === "app" ? "In-app" : gun.tx.method === "card" ? "Card" : gun.tx.method}</dd>
</div>
<div>
<dt>Running</dt>
<dd>{fmtSince(gun.tx.started)}</dd>
</div>
</dl>
<dl className="bay__telemetry">
<div>
<dt>By</dt>
<dd>{gun.tx.who || gun.tx.id_tag}</dd>
</div>
<div>
<dt>Via</dt>
<dd>{gun.tx.method === "app" ? "In-app" : gun.tx.method === "card" ? "Card" : gun.tx.method}</dd>
</div>
<div>
<dt>Running</dt>
<dd>{fmtSince(gun.tx.started)}</dd>
</div>
<div>
<dt>Delivered</dt>
<dd className="mono">
{gun.tx.telemetry?.energy_wh != null
? `${((gun.tx.telemetry.energy_wh - gun.tx.meter_start) / 1000).toFixed(2)} kWh`
: "—"}
</dd>
</div>
<div>
<dt>Voltage</dt>
<dd className="mono">
{gun.tx.telemetry?.voltage_v != null
? `${gun.tx.telemetry.voltage_v.toFixed(0)} V`
: "—"}
</dd>
</div>
<div>
<dt>Current</dt>
<dd className="mono">
{gun.tx.telemetry?.current_a != null
? `${gun.tx.telemetry.current_a.toFixed(1)} A`
: "—"}
</dd>
</div>
</dl>
{!gun.tx.telemetry && (
<span className="bay__hint">first meter reading arrives within a minute…</span>
)}
</>
)}
<footer className="bay__actions">
+32 -1
View File
@@ -256,8 +256,39 @@ button, input, select { font: inherit; color: inherit; }
.bay__status { display: flex; align-items: center; gap: 10px; min-height: 26px; }
.bay__hint { color: var(--ink-dim); font-size: 13px; }
.bay__charge {
display: flex; align-items: center; justify-content: space-between; gap: 14px;
padding: 12px 14px;
background: var(--panel-inset);
border-radius: 8px;
}
.bay__kw { display: flex; align-items: baseline; gap: 5px; }
.bay__kw-num { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; }
.bay__kw-unit {
font-family: var(--font-mono); font-size: 11px;
text-transform: uppercase; letter-spacing: 0.12em; color: var(--ink-dim);
}
/* ---------- battery gauge (SoC, red -> blue -> green) ---------- */
.battery { display: flex; align-items: center; gap: 2px; }
.battery__shell {
display: inline-block; width: 52px; height: 22px;
border: 2px solid var(--ink-dim); border-radius: 6px;
padding: 2px; background: var(--panel);
}
.battery__fill {
display: block; height: 100%; border-radius: 3px;
min-width: 2px;
transition: width 0.6s ease, background 0.6s ease;
}
.battery__cap {
width: 3px; height: 10px; border-radius: 0 2px 2px 0;
background: var(--ink-dim);
}
.battery__pct { font-size: 15px; font-weight: 600; margin-left: 8px; }
.bay__telemetry {
display: flex; gap: 22px;
display: flex; flex-wrap: wrap; gap: 10px 22px;
padding: 10px 12px;
background: var(--panel-inset);
border-radius: 8px;
+10
View File
@@ -4,6 +4,15 @@ export interface Me {
id_tag: string | null;
}
export interface Telemetry {
energy_wh?: number;
power_w?: number;
soc?: number;
voltage_v?: number;
current_a?: number;
time?: string;
}
export interface ActiveTx {
cp: string;
connector: number;
@@ -12,6 +21,7 @@ export interface ActiveTx {
method: string;
meter_start: number;
started: string;
telemetry?: Telemetry;
}
export interface ChargerMeta {
+1 -1
View File
@@ -1 +1 @@
{"root":["./src/app.tsx","./src/api.ts","./src/main.tsx","./src/queries.ts","./src/store.ts","./src/types.ts","./src/components/lamp.tsx","./src/components/layout.tsx","./src/pages/account.tsx","./src/pages/cards.tsx","./src/pages/dashboard.tsx","./src/pages/history.tsx","./src/pages/login.tsx","./src/pages/users.tsx"],"version":"5.9.3"}
{"root":["./src/app.tsx","./src/api.ts","./src/main.tsx","./src/queries.ts","./src/store.ts","./src/types.ts","./src/components/battery.tsx","./src/components/lamp.tsx","./src/components/layout.tsx","./src/pages/account.tsx","./src/pages/cards.tsx","./src/pages/dashboard.tsx","./src/pages/history.tsx","./src/pages/login.tsx","./src/pages/users.tsx"],"version":"5.9.3"}
+4
View File
@@ -91,3 +91,7 @@ Owner confirmed the React panel loads on the phone after backend restart. Change
## [2026-07-12] meta | Private history; permissions on Account; Start only when plugged; "Art IoT" rename
Owner (testing on the phone, second user created): (1) transactions are now PRIVATE — non-admins see only their own sessions (app starts + linked cards); gun scopes grant control, not visibility; admins see all. (2) Account page shows the user's own permissions in plain words (admin badge, or gun/relay list built from data they already see). (3) Start button arms ONLY when the cable is plugged (Preparing) — an idle unplugged gun shows "plug the cable into the car to enable". Owner also renamed the app to "Art IoT" (login, topbar, manifest, title) — kept. Smoke: 59 checks. Updated [[backend]], [[frontend]].
## [2026-07-12] meta | Live telemetry on the gun cards: SoC battery gauge + kW/V/A/delivered
Owner wants meter values visible per gun in Control. Backend: latest Sample.Periodic per active tx kept in memory (ACTIVE_TX telemetry) and shipped with /api/status (Transaction.Begin snapshots skipped — taken before the car's BMS is read). Also this morning's finding filed: begin/end snapshots are legitimately 0 A/0 V, and a 42 s session never gets a periodic sample. Frontend: battery gauge with red->blue->green SoC gradient + big kW readout + Delivered/Voltage/Current rows on charging bays; earlier commit added voltage_v/current_a/context columns to samples. Smoke: 60 checks. Updated [[frontend]], [[backend]] implicitly via log.