const state = { result: null, frequencies: null, health: null, }; function byId(id) { return document.getElementById(id); } function fmt(value, digits = 6) { if (value === null || value === undefined) return "-"; if (typeof value !== "number") return String(value); return Number.isFinite(value) ? value.toFixed(digits) : String(value); } async function getJson(url) { const res = await fetch(url); const data = await res.json().catch(() => ({})); if (!res.ok) { throw new Error(data.error || data.status || `HTTP ${res.status}`); } return data; } async function postJson(url, payload) { const res = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); const data = await res.json().catch(() => ({})); if (!res.ok) { throw new Error(data.error || data.status || `HTTP ${res.status}`); } return data; } function render() { const data = state.result?.data; const delivery = state.result?.output_delivery || state.frequencies?.output_delivery; byId("updated-at").textContent = `updated: ${state.result?.updated_at_utc || "n/a"}`; byId("health-status").textContent = `health: ${state.health?.status || "n/a"}`; byId("delivery-status").textContent = `delivery: ${delivery?.status || "n/a"}`; if (!data) { byId("selected-freq").textContent = "-"; byId("pos-x").textContent = "-"; byId("pos-y").textContent = "-"; byId("pos-z").textContent = "-"; byId("rmse").textContent = "-"; byId("receivers-list").textContent = "Нет данных"; byId("delivery-details").textContent = JSON.stringify(delivery || {}, null, 2); byId("freq-table").querySelector("tbody").innerHTML = ""; return; } byId("selected-freq").textContent = fmt(data.selected_frequency_hz, 1); byId("pos-x").textContent = fmt(data.position?.x); byId("pos-y").textContent = fmt(data.position?.y); byId("pos-z").textContent = fmt(data.position?.z); byId("rmse").textContent = fmt(data.rmse_m); const receivers = data.receivers || []; byId("receivers-list").textContent = JSON.stringify(receivers, null, 2); byId("delivery-details").textContent = JSON.stringify(delivery || {}, null, 2); const rows = data.frequency_table || []; const tbody = byId("freq-table").querySelector("tbody"); tbody.innerHTML = rows .map( (row) => `