069d544bb5
- non-admins now see ONLY their own sessions in /api/sessions (own app starts + own linked cards); gun scopes grant control, not visibility; admins see everything - Account page shows the logged-in user's permissions in plain words (Administrator badge, or the guns/relays they control), built solely from data already visible to them - Start button arms only when the gun reports Preparing (cable in the car); idle unplugged gun shows an explanatory hint instead - keep owner's app rename to 'Art IoT' - smoke test: 59 checks (gun-scoped user sees no foreign sessions)
256 lines
8.5 KiB
TypeScript
256 lines
8.5 KiB
TypeScript
import { api, scopeAllowed } from "../api";
|
|
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 && (
|
|
<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>
|
|
</dl>
|
|
)}
|
|
|
|
<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} />
|
|
</>
|
|
);
|
|
}
|