feat(sim): start and stop the decoder simulator from Settings (1.65.0)

This commit is contained in:
larssand committed 2026-11-05 18:48:37 +01:00
1 parent 0ce7280cbf
commit e4874fd558
14 files changed
+309 -110

No files matched your search

+16
View File
@@ -2279,6 +2279,14 @@ const TRANSLATIONS = {
"events.step_count": "{done}/{total} körda",
"events.assignments": "Tilldelningar",
"events.na": "ej relevant",
"sim.title": "Decoder-simulator (för test)",
"sim.hint": "Kör bilar runt banan utan decoder och AMMC, för att labba och prova UI. Den kör transpondrarna i det aktiva heatet (sponsorheat: bilarna) och följer med när du byter heat. Starta heatet på Race Day så kommer varven. Den riktiga decoderadressen sparas och sätts tillbaka när du stoppar.",
"sim.lap_sec": "Varvtid (s)",
"sim.spread_sec": "Spridning mellan bilar (s)",
"sim.start": "Starta simulator",
"sim.stop": "Stoppa simulator",
"sim.running": "Igång på {url}. Transpondrar: {cars}. Passeringar: {passings}.",
"sim.stopped": "Avstängd.",
"sponsor.title": "Sponsor event - enkel setup",
"sponsor.step_drivers": "1. Förare (ett namn per rad)",
"sponsor.guests_placeholder": "Anna\nBosse\nCissi",
@@ -4303,6 +4311,14 @@ const TRANSLATIONS = {
"events.step_count": "{done}/{total} run",
"events.assignments": "Assignments",
"events.na": "n/a",
"sim.title": "Decoder simulator (for testing)",
"sim.hint": "Drives cars around the track without a decoder or AMMC, to try things out. It drives the transponders of the active heat (sponsor heats: the cars) and follows when you switch heat. Start the heat on Race Day and the laps come in. The real decoder address is kept and put back when you stop it.",
"sim.lap_sec": "Lap time (s)",
"sim.spread_sec": "Spread between cars (s)",
"sim.start": "Start simulator",
"sim.stop": "Stop simulator",
"sim.running": "Running on {url}. Transponders: {cars}. Passings: {passings}.",
"sim.stopped": "Off.",
"sponsor.title": "Sponsor event - quick setup",
"sponsor.step_drivers": "1. Drivers (one name per line)",
"sponsor.guests_placeholder": "Anna\nBob\nCarla",
+58
View File
@@ -0,0 +1,58 @@
// Settings → Decoder simulator: start/stop the server's fake decoder (server/decoder_sim.js)
// and point the app at it. The real decoder address is kept and put back on Stop.
const SIM_PORT = 9099;
export function mountDecoderSimSettings(root, { state, t, escapeHtml, getBackendUrl, saveState, connectDecoder, renderView }) {
if (!root) return;
const call = async (path, body) => {
const response = await fetch(`${getBackendUrl()}/api/sim/${path}`, body ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } : {});
const payload = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(payload.error || `HTTP ${response.status}`);
return payload.status || payload;
};
const simUrl = () => `ws://${window.location.hostname || "127.0.0.1"}:${SIM_PORT}`;
const render = (status, error = "") => {
const running = Boolean(status?.running);
root.innerHTML = `
<section class="panel mt-16">
<div class="panel-header"><h3>${t("sim.title")}</h3></div>
<div class="panel-body">
<p class="hint">${t("sim.hint")}</p>
<div class="form-grid cols-4">
<label><span>${t("sim.lap_sec")}</span><input type="number" id="simLapSec" min="3" max="120" step="0.5" value="${escapeHtml(String(status?.lapSec || 18))}" ${running ? "disabled" : ""} /></label>
<label><span>${t("sim.spread_sec")}</span><input type="number" id="simSpreadSec" min="0" max="20" step="0.5" value="${escapeHtml(String(status?.spreadSec ?? 1.5))}" ${running ? "disabled" : ""} /></label>
</div>
<div class="actions mt-8">
<button id="simToggle" class="btn ${running ? "btn-danger" : "btn-primary"}" type="button">${running ? t("sim.stop") : t("sim.start")}</button>
</div>
<p id="simStatus">${running
? escapeHtml(t("sim.running", { url: simUrl(), cars: (status.transponders || []).join(", ") || "-", passings: status.passings || 0 }))
: escapeHtml(t("sim.stopped"))}</p>
${error ? `<p class="form-error" role="alert">${escapeHtml(error)}</p>` : ""}
</div>
</section>`;
root.querySelector("#simToggle")?.addEventListener("click", async () => {
try {
if (running) {
await call("stop", {});
// Back to the real decoder address.
state.settings.wsUrl = state.settings.simPreviousWsUrl || state.settings.wsUrl;
delete state.settings.simPreviousWsUrl;
} else {
await call("start", { lapSec: Number(root.querySelector("#simLapSec").value), spreadSec: Number(root.querySelector("#simSpreadSec").value) });
if (state.settings.wsUrl !== simUrl()) state.settings.simPreviousWsUrl = state.settings.wsUrl;
state.settings.wsUrl = simUrl();
}
saveState();
connectDecoder();
renderView();
} catch (failure) {
render(status, failure instanceof Error ? failure.message : String(failure));
}
});
};
render(null);
call("status").then((status) => render(status)).catch(() => render(null));
}
+2
View File
@@ -132,6 +132,8 @@ export function applyPersistedStateHelper(persisted, deps) {
state.raceDays = normalizeRaceDays(persisted.raceDays);
state.settings = {
wsUrl: persisted.settings?.wsUrl || state.settings.wsUrl || "ws://127.0.0.1:9000",
// The real decoder address while the decoder simulator is on (decoder_sim_view.js).
...(persisted.settings?.simPreviousWsUrl ? { simPreviousWsUrl: String(persisted.settings.simPreviousWsUrl) } : {}),
splitDecoderEnabled: persisted.settings?.splitDecoderEnabled === true,
splitWsUrl: String(persisted.settings?.splitWsUrl || state.settings.splitWsUrl || "ws://127.0.0.1:9001"),
pitDecoderEnabled: persisted.settings?.pitDecoderEnabled === true,
+3
View File
@@ -2,6 +2,7 @@ import { normalizePublicBaseUrl } from "./public_url.js";
import { decoderDiagnosticsMarkup, mountDecoderDiagnostics } from "./decoder_diagnostics_view.js";
import { mountAuthSettings } from "./auth_view.js";
import { mountTtsSettings } from "./tts_settings_view.js";
import { mountDecoderSimSettings } from "./decoder_sim_view.js";
import { formatSpokenLapTime, formatShortLapCall } from "./race_audio.js";
let activeSettingsTab = "connection";
@@ -453,6 +454,7 @@ export function renderSettings(deps) {
)}</pre>
</div>
</section>
<div id="decoderSimSettings"></div>
</div>
<div data-settings-panel="system">
@@ -619,6 +621,7 @@ Erik Eriksson;654321;4WD;Associated;;</code></pre>
}
mountAuthSettings(document.getElementById("authSettings"), { t, escapeHtml });
mountTtsSettings(document.getElementById("ttsSettings"), { state, t, escapeHtml, saveState, speakText });
mountDecoderSimSettings(document.getElementById("decoderSimSettings"), { state, t, escapeHtml, getBackendUrl, saveState, connectDecoder, renderView });
const applyRaceAudioFormValues = (form) => {
const events = {};