Files
Live_RC/src/decoder_diagnostics_view.js
T
larssand 35eb3eb059 feat(decoder): warn when the decoder's clock is off (1.42.4)
The decoder's time is never changed. Passings (also during warm-up)
measure it against the computer's clock, and Race Day's preflight and
the decoder diagnostics warn when it is over a minute off, e.g. after
the switch to winter time.
2026-10-25 18:40:35 +01:00

89 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { decoderDiagnostics, DIAGNOSTIC_ROLES } from "./decoder_diagnostics.js";
import { decoderClockCheck, describeDecoderClockSkew } from "./decoder_clock_check.js";
let stopPrevious = () => {};
export function decoderDiagnosticsMarkup(language) {
return `<details id="decoderDiagnostics" class="panel mt-16"><summary class="panel-header">${language === "sv" ? "Decoder / socket-diagnostik" : "Decoder / socket diagnostics"}</summary><div class="panel-body" data-diagnostics-body></div></details>`;
}
export function mountDecoderDiagnostics(root, getSettings) {
stopPrevious();
if (!root) return;
const sv = getSettings().language === "sv";
const tr = (svText, enText) => sv ? svText : enText;
const body = root.querySelector("[data-diagnostics-body]");
const note = document.createElement("p");
note.textContent = tr("Endast denna webbläsare. Ansluten socket betyder inte att passeringar kommer in. Loggen sparas inte och nollställs vid omladdning.", "This browser only. A connected socket does not guarantee passings. Logs are not saved and reset on reload.");
body.append(note);
const controls = document.createElement("div");
controls.className = "decoder-diagnostics-controls";
const select = document.createElement("select");
select.setAttribute("aria-label", tr("Decoderkanal", "Decoder channel"));
const names = ["Main", tr("Mellantid", "Split"), tr("Depå in", "Pit entry"), tr("Depå ut", "Pit exit")];
DIAGNOSTIC_ROLES.forEach((role, i) => select.add(new Option(names[i], role)));
controls.append(select);
const button = (label, action) => {
const node = document.createElement("button");
node.type = "button"; node.className = "btn"; node.textContent = label;
node.addEventListener("click", action); controls.append(node); return node;
};
let paused = false;
const pause = button(tr("Pausa visning", "Pause display"), () => {
paused = !paused;
pause.textContent = paused ? tr("Fortsätt visning", "Resume display") : tr("Pausa visning", "Pause display");
update();
});
button(tr("Rensa vald kanal", "Clear selected channel"), () => { decoderDiagnostics.clear(select.value); update(true); });
const output = document.createElement("textarea");
output.readOnly = true; output.hidden = true; output.rows = 6;
output.setAttribute("aria-label", tr("Logg att kopiera", "Log to copy"));
button(tr("Kopiera logg", "Copy log"), async () => {
const text = JSON.stringify(decoderDiagnostics.snapshot().find(entry => entry.role === select.value), null, 2);
try {
if (!navigator.clipboard?.writeText) throw new Error("Clipboard unavailable");
await navigator.clipboard.writeText(text);
copyStatus.textContent = tr("Kopierat", "Copied");
} catch {
output.hidden = false; output.value = text; output.focus(); output.select();
copyStatus.textContent = tr("Kopiera den markerade loggen manuellt.", "Copy the selected log manually.");
}
});
const copyStatus = document.createElement("span");
copyStatus.setAttribute("role", "status"); controls.append(copyStatus);
const cards = document.createElement("div"); cards.className = "decoder-diagnostics-cards";
const nodes = DIAGNOSTIC_ROLES.map((role, i) => {
const card = document.createElement("pre"); card.setAttribute("data-channel", role);
cards.append(card); return card;
});
const log = document.createElement("pre"); log.className = "decoder-diagnostics-log";
body.append(controls, cards, log, output);
const urlKeys = ["wsUrl", "splitWsUrl", "pitEntryWsUrl", "pitExitWsUrl"];
const date = value => value == null ? "-" : new Date(value).toLocaleTimeString(sv ? "sv-SE" : "en-GB");
const statuses = sv ? {connected:"Ansluten", connecting:"Ansluter", disconnected:"Frånkopplad", error:"Anslutningsfel"} : {};
function update(force = false) {
if (!force && (paused || !root.open)) return;
const settings = getSettings();
const entries = decoderDiagnostics.snapshot();
// The decoder's clock against the computer's, measured on passings (decoder_clock_check.js).
const clockLine = (role) => {
const skew = decoderClockCheck.skewMs(role);
if (skew == null) return `${tr("Decoderklocka", "Decoder clock")}: -`;
const off = describeDecoderClockSkew(skew);
if (!off) return `${tr("Decoderklocka", "Decoder clock")}: ${tr("stämmer med datorn", "matches the computer")}`;
const amount = `${off.hours ? `${off.hours} h ` : ""}${off.minutes ? `${off.minutes} min` : ""}`.trim();
return `${tr("Decoderklocka", "Decoder clock")}: ${amount} ${off.ahead ? tr("före", "ahead of") : tr("efter", "behind")} ${tr("datorn – ställ in rätt tid i decodern", "the computer – set the right time in the decoder")}`;
};
entries.forEach((entry, i) => {
const enabled = i === 0 || (i === 1 ? settings.splitDecoderEnabled : settings.pitDecoderEnabled);
nodes[i].textContent = `${names[i]}: ${enabled ? statuses[entry.status] || entry.status : tr("Avstängd", "Disabled")}\n${tr("Konfigurerad", "Configured")}: ${settings[urlKeys[i]] || "-"}\n${tr("Socket-adress", "Socket address")}: ${entry.url || "-"}\n${tr("Senast ansluten", "Last connected")}: ${date(entry.connectedAt)}\n${tr("Senast mottaget", "Last received")}: ${date(entry.lastReceivedAt)} (${entry.lastReceivedAt == null ? "-" : Math.max(0, Math.floor((Date.now() - entry.lastReceivedAt) / 1000))} s)\nFrames: ${entry.frames} | PASSING: ${entry.passings}\n${tr("Hanterade", "Handled")}: ${entry.handled} | ${tr("Ignorerade", "Ignored")}: ${entry.ignored}\n${tr("Ogiltiga varv", "Invalid laps")}: ${entry.invalid} | JSON ${tr("fel", "errors")}: ${entry.parseErrors}\n${clockLine(entry.role)}`;
});
const entry = entries.find(item => item.role === select.value);
log.textContent = `${tr("Senaste råmeddelande (max 512 tecken)", "Latest raw frame (max 512 characters)")}:\n${entry.lastFrame || "-"}\n\n${tr("Senaste 50 händelser; orsakskoder", "Latest 50 events; reason codes")}:\n${entry.log.map(item => `${date(item.at)} ${item.reason} ${item.sample}`).join("\n")}`;
}
select.addEventListener("change", () => update(true));
root.addEventListener("toggle", () => update());
const timer = setInterval(() => { if (!root.isConnected) clearInterval(timer); else update(); }, 1000);
stopPrevious = () => clearInterval(timer);
update();
}