25fa1f9fc5
- 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)
297 lines
9.9 KiB
TypeScript
297 lines
9.9 KiB
TypeScript
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";
|
|
import type { ActiveTx, Status } from "../types";
|
|
|
|
function fmtSince(iso: string): string {
|
|
const mins = Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 60000));
|
|
return mins >= 60 ? `${Math.floor(mins / 60)} h ${mins % 60} min` : `${mins} min`;
|
|
}
|
|
|
|
interface Gun {
|
|
cp: string;
|
|
connector: number;
|
|
label: string;
|
|
chargerLabel: string;
|
|
ocppStatus: string; // OCPP status, or "Offline" / "NoStatus" synthesized here
|
|
tx: (ActiveTx & { txId: string }) | null;
|
|
}
|
|
|
|
/** Every gun the system knows about is ALWAYS shown: the chargers registry
|
|
* defines the cards, live connector reports and transactions overlay state. */
|
|
function gunsFrom(status: Status): Gun[] {
|
|
const guns = new Map<string, Gun>();
|
|
|
|
for (const [cp, meta] of Object.entries(status.chargers)) {
|
|
const online = cp in status.connected;
|
|
for (const [conn, label] of Object.entries(meta.guns ?? {})) {
|
|
guns.set(`${cp}/${conn}`, {
|
|
cp,
|
|
connector: Number(conn),
|
|
label,
|
|
chargerLabel: meta.label ?? cp,
|
|
ocppStatus: online ? "NoStatus" : "Offline",
|
|
tx: null,
|
|
});
|
|
}
|
|
}
|
|
|
|
for (const [key, ocppStatus] of Object.entries(status.connectors)) {
|
|
const cp = key.slice(0, key.lastIndexOf("/"));
|
|
const connector = Number(key.slice(key.lastIndexOf("/") + 1));
|
|
if (connector <= 0) continue; // connector 0 = the whole charge point
|
|
const existing = guns.get(`${cp}/${connector}`);
|
|
if (existing) existing.ocppStatus = ocppStatus;
|
|
else
|
|
guns.set(`${cp}/${connector}`, {
|
|
cp,
|
|
connector,
|
|
label: `Gun ${connector}`,
|
|
chargerLabel: status.chargers[cp]?.label ?? cp,
|
|
ocppStatus,
|
|
tx: null,
|
|
});
|
|
}
|
|
|
|
for (const [txId, tx] of Object.entries(status.active_transactions)) {
|
|
const gun = guns.get(`${tx.cp}/${tx.connector}`);
|
|
if (gun) gun.tx = { ...tx, txId };
|
|
}
|
|
|
|
return [...guns.values()].sort(
|
|
(a, b) => a.cp.localeCompare(b.cp) || a.connector - b.connector,
|
|
);
|
|
}
|
|
|
|
/** Chip text + tone per the owner's state language. */
|
|
function gunState(gun: Gun): { label: string; cls: string } {
|
|
if (gun.tx) return { label: "Charging", cls: "charging" };
|
|
switch (gun.ocppStatus) {
|
|
case "Charging":
|
|
return { label: "Charging", cls: "charging" };
|
|
case "Preparing":
|
|
return { label: "Plugged in", cls: "plugged" };
|
|
case "SuspendedEV":
|
|
case "SuspendedEVSE":
|
|
return { label: "Paused", cls: "plugged" };
|
|
case "Finishing":
|
|
return { label: "Finishing", cls: "plugged" };
|
|
case "Available":
|
|
return { label: "Idle", cls: "idle" };
|
|
case "Offline":
|
|
return { label: "Offline", cls: "offline" };
|
|
case "NoStatus":
|
|
return { label: "No status yet", cls: "offline" };
|
|
case "Faulted":
|
|
return { label: "Fault", cls: "faulted" };
|
|
default:
|
|
return { label: gun.ocppStatus, cls: "idle" };
|
|
}
|
|
}
|
|
|
|
function GunBay({ gun, scopes }: { gun: Gun; scopes: string[] }) {
|
|
const toast = useApp((s) => s.toast);
|
|
const may = scopeAllowed(scopes, `charger:${gun.cp}:${gun.connector}`);
|
|
// Start arms ONLY with a cable in the car (Preparing / pulsing blue) —
|
|
// an idle, unplugged gun keeps the button disabled (owner, 2026-07-12)
|
|
const ready = !gun.tx && gun.ocppStatus === "Preparing";
|
|
|
|
const start = useAction(
|
|
() =>
|
|
api.post<{ remote_start: string }>(
|
|
`/charger/start?connector=${gun.connector}&cp=${encodeURIComponent(gun.cp)}`,
|
|
),
|
|
[["status"], ["sessions"]],
|
|
(r) => toast(`Start command: ${r.remote_start}`),
|
|
);
|
|
const stop = useAction(
|
|
() => api.post<{ remote_stop: string }>(`/charger/stop/${gun.tx!.txId}`),
|
|
[["status"], ["sessions"]],
|
|
(r) => toast(`Stop command: ${r.remote_stop}`),
|
|
);
|
|
|
|
const state = gunState(gun);
|
|
return (
|
|
<article className="bay">
|
|
<header className="bay__head">
|
|
<span className="bay__eyebrow">{gun.chargerLabel}</span>
|
|
<Lamp tone={statusTone(gun.ocppStatus, !!gun.tx)} title={state.label} />
|
|
</header>
|
|
<h2 className="bay__title">{gun.label}</h2>
|
|
<div className="bay__status">
|
|
<span className={`chip chip--${state.cls}`}>{state.label}</span>
|
|
{gun.ocppStatus === "Preparing" && !gun.tx && (
|
|
<span className="bay__hint">cable in the socket — ready to start</span>
|
|
)}
|
|
</div>
|
|
|
|
{gun.tx && (
|
|
<>
|
|
<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>
|
|
<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">
|
|
{gun.tx ? (
|
|
<button
|
|
className="btn btn--danger"
|
|
disabled={!may || stop.isPending}
|
|
onClick={() => stop.mutate()}
|
|
>
|
|
{stop.isPending ? "Stopping…" : "Stop charging"}
|
|
</button>
|
|
) : (
|
|
<button
|
|
className="btn btn--primary"
|
|
disabled={!may || !ready || start.isPending}
|
|
onClick={() => start.mutate()}
|
|
>
|
|
{start.isPending ? "Starting…" : "Start charging"}
|
|
</button>
|
|
)}
|
|
{!may && <span className="bay__hint">no permission for this gun</span>}
|
|
{may && !gun.tx && !ready && gun.ocppStatus === "Available" && (
|
|
<span className="bay__hint">plug the cable into the car to enable</span>
|
|
)}
|
|
</footer>
|
|
</article>
|
|
);
|
|
}
|
|
|
|
function Relays({ status, scopes }: { status: Status; scopes: string[] }) {
|
|
const toast = useApp((s) => s.toast);
|
|
const live = useLiveRelays();
|
|
const names = Object.keys(status.relays);
|
|
|
|
const toggle = useAction(
|
|
(name: string) => api.post<{ relay: string; on: boolean }>(`/relays/${encodeURIComponent(name)}/toggle`),
|
|
[["relays-live"]],
|
|
(r) => toast(`${r.relay}: ${r.on ? "on" : "off"}`),
|
|
);
|
|
|
|
if (!names.length) return null;
|
|
return (
|
|
<section className="panel">
|
|
<header className="panel__head">
|
|
<h2>Relays</h2>
|
|
<button
|
|
className="btn btn--ghost btn--sm"
|
|
onClick={() => live.refetch()}
|
|
disabled={live.isFetching}
|
|
>
|
|
{live.isFetching ? "Reading…" : "Read states"}
|
|
</button>
|
|
</header>
|
|
<ul className="relay-list">
|
|
{names.map((name) => {
|
|
const state = live.data?.[name];
|
|
const on = state && "on" in state ? state.on : undefined;
|
|
return (
|
|
<li key={name} className="relay">
|
|
<Lamp tone={on === undefined ? "off" : on ? "green" : "off"} />
|
|
<span className="relay__label">{status.relays[name].label}</span>
|
|
<span className="relay__state">
|
|
{state?.error ? "unreachable" : on === undefined ? "—" : on ? "on" : "off"}
|
|
</span>
|
|
<button
|
|
className="btn btn--ghost btn--sm"
|
|
disabled={!scopeAllowed(scopes, `relay:${name}`) || toggle.isPending}
|
|
onClick={() => toggle.mutate(name)}
|
|
>
|
|
Toggle
|
|
</button>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
export default function Dashboard() {
|
|
const me = useApp((s) => s.me)!;
|
|
const { data: status, isLoading, error } = useStatus();
|
|
|
|
if (isLoading) return <p className="empty">Reading charger status…</p>;
|
|
if (error || !status) return <p className="empty">Status unavailable: {(error as Error)?.message}</p>;
|
|
|
|
const guns = gunsFrom(status);
|
|
const offline = Object.keys(status.connected).length === 0;
|
|
|
|
return (
|
|
<>
|
|
{offline && (
|
|
<div className="panel panel--offline">
|
|
<Lamp tone="red" />
|
|
<div>
|
|
<h2>Charger offline</h2>
|
|
<p>Waiting for the charge point to dial in — controls enable as soon as it connects.</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{!guns.length && !offline && (
|
|
<p className="empty">Connected, waiting for the first gun status report…</p>
|
|
)}
|
|
<div className="bays">
|
|
{guns.map((g) => (
|
|
<GunBay key={`${g.cp}/${g.connector}`} gun={g} scopes={me.scopes} />
|
|
))}
|
|
</div>
|
|
<Relays status={status} scopes={me.scopes} />
|
|
</>
|
|
);
|
|
}
|