// --- VARIABILI GLOBALI E UI VIEW --- const HANG_TIME_MS = 3000; let clients = []; let me = { logged_in: false, user: '', role: '', allowed_nodes: '' }; let isAuthenticated = false; let usersCache = {}; let globalHealthData = {}; let globalVersionsData = {}; let editingUserId = null; let currentResetHatId = null; let confirmActionCallback = null; let currentFilter = 'all'; let searchQuery = ''; let viewMode = localStorage.getItem('fleetViewMode') || 'grid'; let fleetMap = null; let mapMarkers = {}; function switchView(viewName) { document.getElementById('view-home').style.display = 'none'; document.getElementById('view-nodes').style.display = 'none'; document.getElementById('view-map').style.display = 'none'; document.getElementById('tab-home').classList.remove('active'); document.getElementById('tab-nodes').classList.remove('active'); document.getElementById('tab-map').classList.remove('active'); document.getElementById(`view-${viewName}`).style.display = 'block'; document.getElementById(`tab-${viewName}`).classList.add('active'); localStorage.setItem('fleetDefaultView', viewName); if (viewName === 'nodes') { applyViewMode(); } else if (viewName === 'map') { setTimeout(() => { initOrUpdateMap(); }, 100); } } window.addEventListener('DOMContentLoaded', () => { const savedView = localStorage.getItem('fleetDefaultView') || 'home'; switchView(savedView); initUI(); checkPushStatus(); setInterval(refreshStates, 15000); setInterval(() => { refreshLogs(); refreshStats(); }, 60000); checkServerUpdate(); setInterval(checkServerUpdate, 3600000); }); // --- FUNZIONI FILTRO E LAYOUT --- function setFilter(filter) { currentFilter = filter; document.querySelectorAll('.filter-pill').forEach(btn => { if (btn.getAttribute('onclick') === `setFilter('${filter}')`) btn.classList.add('active'); else btn.classList.remove('active'); }); applyFilters(); } function toggleViewMode() { viewMode = viewMode === 'grid' ? 'list' : 'grid'; localStorage.setItem('fleetViewMode', viewMode); applyViewMode(); } function applyViewMode() { const gridDiv = document.getElementById('client-grid'); const btnToggle = document.getElementById('view-toggle-btn'); if (viewMode === 'list') { gridDiv.className = 'list-view'; btnToggle.innerText = 'πŸ“„ LIST'; } else { gridDiv.className = 'grid'; btnToggle.innerText = 'πŸ”² GRID'; } } function applyFilters() { const searchEl = document.getElementById('node-search'); if(!searchEl) return; searchQuery = searchEl.value.toLowerCase(); clients.forEach(c => { const card = document.getElementById(`card-${c.id}`); if (!card) return; let show = true; let status = card.dataset.status || 'offline'; let gatewaysStr = card.dataset.gateways || ''; let hasAlert = card.dataset.hasWarn === 'true'; if (currentFilter === 'online' && status !== 'online') show = false; if (currentFilter === 'offline' && status === 'online') show = false; if (currentFilter === 'alerts' && !hasAlert) show = false; if (['dmr', 'ysf', 'd-star', 'nxdn'].includes(currentFilter)) { if (!gatewaysStr.includes(currentFilter)) show = false; } if (searchQuery !== '') { const searchStr = `${c.id.toLowerCase()} ${c.name.toLowerCase()} ${(card.dataset.loc || '').toLowerCase()}`; if (!searchStr.includes(searchQuery)) show = false; } card.style.display = show ? 'flex' : 'none'; }); } function fmtAge(sec) { sec = Math.max(0, Math.round(sec)); if (sec < 60) return sec + 's'; if (sec < 3600) return Math.floor(sec / 60) + 'm'; if (sec < 86400) return Math.floor(sec / 3600) + 'h'; return Math.floor(sec / 86400) + 'd'; } function seenItem(label, age, tip, warn) { const cls = warn ? 'warn' : (age !== undefined && age < 300 ? 'ok' : 'idle'); const txt = age === undefined ? 'β€”' : fmtAge(age); return `${warn ? '⚠ ' : ''}${label} ${txt}`; } function renderSeen(c, data, isOnline) { const div = document.getElementById(`seen-${c.id}`); if (!div) return; const it = currentLang === 'it'; const ages = (data.last_seen && data.last_seen[c.id.toLowerCase()]) || {}; const upSec = data.uptime || 0; let html = ''; const warnM = ages.mmdvm === undefined && isOnline && upSec > 600; const tipM = it ? `Ultimo evento dal topic mmdvm/${c.id}/json (TX, cambio modo...). Su un nodo tranquillo puΓ² passare molto tempo.` : `Last event from topic mmdvm/${c.id}/json (TX, mode change...). It can be a long time on a quiet node.`; const tipMW = it ? `Nessun evento MMDVM ricevuto da quando il server Γ¨ attivo (${fmtAge(upSec)}): controlla che MMDVMHost sia connesso al broker.` : `No MMDVM event received since the server started (${fmtAge(upSec)}): check that MMDVMHost is connected to the broker.`; html += seenItem('MMDVM', ages.mmdvm, warnM ? tipMW : tipM, warnM); if (ages.gateway !== undefined) { html += seenItem('GW', ages.gateway, it ? 'Ultimo evento dai gateway (DMR/NXDN/YSF/P25/D-Star)' : 'Last event from the gateways (DMR/NXDN/YSF/P25/D-Star)', false); } const hObj = (data.health && data.health[c.id.toLowerCase()]) || {}; const iv = Number(hObj.interval) > 0 ? Number(hObj.interval) : 60; const warnA = isOnline && (ages.health === undefined ? upSec > Math.max(300, iv * 5) : ages.health > Math.max(180, iv * 3)); const tipA = warnA ? (it ? 'L\'agent del nodo non invia piΓΉ lo stato (devices//services): connessione bloccata?' : 'The node agent stopped sending its status (devices//services): stuck connection?') : (it ? 'Ultimo stato ricevuto dall\'agent del nodo (devices//services)' : 'Last status received from the node agent (devices//services)'); html += seenItem('AGENT', ages.health, tipA, warnA); div.innerHTML = html; div.style.display = 'flex'; } function sendCommand(clientId, type, customTitle, customMsg, customColor) { const title = customTitle || t('titleAction'); const msg = customMsg || `${t('confOp')}${clientId.toUpperCase()}?`; const color = customColor || "var(--primary)"; customConfirm(title, msg, color, async () => { try { const res = await fetch('/api/command', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId, type }) }); const data = await res.json(); if(!data.success) { customAlert(t('titleError'), esc(data.error), true); } refreshStates(); } catch (e) { console.error(e); } }); } function confirmSwitch(id, mode) { const data = globalHealthData[id.toLowerCase()]; let profileName = mode === 'A' ? "PROFILE A" : "PROFILE B"; if (data && data.profiles && data.profiles[mode]) profileName = data.profiles[mode]; const title = `πŸ”„ ${t('titleSwitch')}`; const msg = `${t('confSwitch')} ${esc(profileName)} -> ${id.toUpperCase()}?`; const color = mode === 'A' ? "var(--accent)" : "#eab308"; sendCommand(id, mode, title, msg, color); } function confirmReboot(id) { sendCommand(id, 'REBOOT', `πŸ”„ ${t('titleBoot')}`, `${t('confBoot')}${id.toUpperCase()}?`, "var(--danger)"); } function confirmUpdateAgent(id) { sendCommand(id, 'UPDATE_AGENT', `⬇️ UPDATE OTA`, `Confermi l'aggiornamento dello script tramite git pull sul nodo ${id.toUpperCase()}?`, "#2563eb"); } function sendTgCommand(clientId, comando) { const msg = (comando === 'TG:ON') ? t('confTgOn') : t('confTgOff'); customConfirm("πŸ’¬ TELEGRAM", `${msg}${clientId.toUpperCase()}?`, "var(--primary)", async () => { try { const res = await fetch('/api/command', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ clientId: clientId, type: comando }) }); const data = await res.json(); if(!data.success) { customAlert(t('titleError'), esc(data.error), true); } refreshStates(); } catch (e) { console.error(e); } }); } async function initUI() { applyTranslations(); try { try { const meRes = await fetch('/api/me'); if (meRes.ok) me = await meRes.json(); } catch (e) { console.error(e); } isAuthenticated = !!me.logged_in; const response = await fetch('/api/clients'); clients = await response.json(); clients = clients.filter(c => /^[A-Za-z0-9_\-]+$/.test(String(c.id))); const statSelect = document.getElementById('stat-node-filter'); if (statSelect) { let opts = ``; clients.forEach(c => { opts += ``; }); statSelect.innerHTML = opts; } const grid = document.getElementById('client-grid'); const authContainer = document.getElementById('auth-container'); const role = me.role || ''; const allowedList = String(me.allowed_nodes || '').split(',').map(x => x.trim().toLowerCase()).filter(Boolean); if (isAuthenticated) { let adminBtn = role === 'admin' ? ` ` : ''; authContainer.innerHTML = `${adminBtn} πŸ‘€ ${esc(String(me.user).toUpperCase())} `; } else { authContainer.innerHTML = ``; } grid.innerHTML = clients.map(c => { let canControl = isAuthenticated && (role === 'admin' || allowedList.includes('all') || allowedList.includes(c.id.toLowerCase())); let showReboot = (role === 'admin'); return `
${esc(c.name)}
ID: ${c.id.toUpperCase()}
Offline
TS1: ...
TS2: ...
${t('waitNet')}
${canControl ? `
` : ''} ${showReboot ? `
βš™οΈ Admin Tools
` : ''}
`; }).join(''); const miniGrid = document.getElementById('home-mini-nodes'); if (miniGrid) { miniGrid.innerHTML = clients.map(c => `
${esc(c.name)}
ID: ${c.id.toUpperCase()}
OFFLINE
`).join(''); } refreshStates(); refreshLogs(); refreshStats(); } catch (e) { console.error(e); } } async function performLogin() { const user = document.getElementById('modal-username').value; const pass = document.getElementById('modal-password').value; if (!user || !pass) return; const res = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ user, pass }) }); const data = await res.json(); if (res.ok) { location.reload(); } else if (res.status === 429) { customAlert(t('titleError'), esc(data.error || t('msgLoginFail')), true); } else { customAlert(t('titleError'), t('msgLoginFail'), true); } } async function logout() { try { await fetch('/api/logout', { method: 'POST' }); } catch (e) { console.error(e); } sessionStorage.clear(); location.reload(); } // --- LOGICA FLEET MAP V5.0 --- let latestStatesData = null; function initOrUpdateMap() { if (!fleetMap) { fleetMap = L.map('map-container').setView([46.06, 13.23], 9); L.tileLayer('https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap France | © OpenStreetMap', maxZoom: 19, className: 'dark-tiles' }).addTo(fleetMap); } else { fleetMap.invalidateSize(); } if (!latestStatesData) return; let bounds = []; clients.forEach(c => { const cid = c.id.toLowerCase(); const info = latestStatesData.info && latestStatesData.info[cid]; const gen = latestStatesData.general && latestStatesData.general[cid]; const state = latestStatesData.states[cid] || "OFFLINE"; const isOnline = !state.toUpperCase().includes("OFF") && state !== ""; const health = latestStatesData.health && latestStatesData.health[cid]; const card = document.getElementById(`card-${c.id}`); const isTx = card && card.dataset.lastTx && (Date.now() - parseInt(card.dataset.lastTx) < 3000); const warns = (latestStatesData.warnings && latestStatesData.warnings[cid]) || []; if (info && info.lat && info.lon && parseFloat(info.lat) !== 0 && parseFloat(info.lon) !== 0) { const lat = parseFloat(info.lat); const lon = parseFloat(info.lon); bounds.push([lat, lon]); let pinClass = isOnline ? (warns.length > 0 ? 'pin-warn' : 'pin-online') : 'pin-offline'; if (isTx) pinClass += ' pin-tx'; let iconSymbol = (gen && gen.duplex === "0") ? "πŸ“»" : "πŸ“‘"; if (warns.length > 0 && !isTx) iconSymbol = "⚠️"; const customIcon = L.divIcon({ className: 'custom-div-icon', html: `
${iconSymbol}
`, iconSize: [32, 32], iconAnchor: [16, 16] }); const call = (gen && gen.callsign) ? gen.callsign : c.name; const warnsHtml = warns.length > 0 ? `
${warns.map(w => esc(w)).join('
')}
` : ''; const popupContent = `
${esc(call)}
ID: ${c.id.toUpperCase()} | ${isOnline ? 'ONLINE' : 'OFFLINE'}
${warnsHtml}
πŸ“ ${esc(info.loc)}
TX: ${esc(info.tx)}
RX: ${esc(info.rx)}
${health ? `
🌑️ Temp: ${health.temp}°C | CPU: ${health.cpu}%
` : ''}
`; if (!mapMarkers[cid]) { mapMarkers[cid] = L.marker([lat, lon], { icon: customIcon }).addTo(fleetMap).bindPopup(popupContent); } else { mapMarkers[cid].setLatLng([lat, lon]); mapMarkers[cid].setIcon(customIcon); mapMarkers[cid].setPopupContent(popupContent); } } }); if (bounds.length > 0 && !fleetMap._hasFitted) { fleetMap.fitBounds(bounds, { padding: [40, 40] }); fleetMap._hasFitted = true; } } async function refreshStates() { try { const res = await fetch('/api/states'); const data = await res.json(); latestStatesData = data; globalVersionsData = data.versions || {}; clients.forEach(c => { const statusDiv = document.getElementById(`status-${c.id}`); const cardDiv = document.getElementById(`card-${c.id}`); if (!statusDiv || !cardDiv) return; let stateValue = data.states[c.id.toLowerCase()] || data.states[c.id] || "OFFLINE"; stateValue = String(stateValue).trim().toUpperCase(); statusDiv.innerText = stateValue; const isOnline = !stateValue.includes("OFF") && stateValue !== ""; cardDiv.dataset.status = isOnline ? 'online' : 'offline'; renderSeen(c, data, isOnline); const warns = (data.warnings && data.warnings[c.id.toLowerCase()]) || []; const diagDiv = document.getElementById(`diag-warn-${c.id}`); cardDiv.dataset.hasWarn = (warns.length > 0) ? 'true' : 'false'; if (diagDiv) { if (warns.length > 0 && isOnline) { diagDiv.style.display = 'block'; diagDiv.innerHTML = warns.map(w => `
${esc(w)}
`).join(''); } else { diagDiv.style.display = 'none'; diagDiv.innerHTML = ''; } } let genObj = data.general && data.general[c.id.toLowerCase()]; const nameSpan = cardDiv.querySelector('.client-name'); const idBadge = cardDiv.querySelector('.badge-id'); const typeBadge = document.getElementById(`type-badge-${c.id}`); if (genObj && isOnline) { nameSpan.innerText = genObj.callsign; idBadge.innerText = `ID: ${genObj.radio_id}`; idBadge.style.background = "var(--primary)"; idBadge.style.color = "#ffffff"; idBadge.style.borderColor = "var(--primary)"; if (typeBadge && genObj.duplex !== undefined) { typeBadge.style.display = 'inline-block'; if (genObj.duplex === "1") { typeBadge.innerText = "REPEATER"; typeBadge.className = "type-badge type-repeater"; } else { typeBadge.innerText = "HOTSPOT"; typeBadge.className = "type-badge type-hotspot"; } } } else { nameSpan.innerText = c.name; idBadge.innerText = `ID: ${c.id.toUpperCase()}`; idBadge.style.background = "rgba(255,255,255,0.1)"; idBadge.style.color = "var(--text-muted)"; idBadge.style.borderColor = "var(--border-color)"; if (typeBadge) typeBadge.style.display = 'none'; } let telemetryObj = data.telemetry[c.id.toLowerCase()] || data.telemetry[c.id] || { ts1: "...", ts2: "...", alt: "", idle: true }; if (typeof telemetryObj === 'string') telemetryObj = { ts1: telemetryObj, ts2: "...", alt: "", idle: true }; const tsContainer = document.getElementById(`ts-container-${c.id}`); const ts1Div = document.getElementById(`dmr-ts1-${c.id}`); const ts2Div = document.getElementById(`dmr-ts2-${c.id}`); const altDiv = document.getElementById(`dmr-alt-${c.id}`); let isTx = false; let activeModeColor = "var(--success)"; let isIdle = telemetryObj.idle === true; if (telemetryObj.alt && telemetryObj.alt !== "") { if (tsContainer) tsContainer.style.display = "none"; if (altDiv) { altDiv.style.display = "block"; altDiv.innerText = telemetryObj.alt; let altText = telemetryObj.alt.toUpperCase(); if (altText.includes("NXDN")) activeModeColor = "#10b981"; else if (altText.includes("YSF")) activeModeColor = "#8b5cf6"; else if (altText.includes("D-STAR")) activeModeColor = "#06b6d4"; else if (altText.includes("P25")) activeModeColor = "#f59e0b"; isTx = altText.includes("🟒") || altText.includes("🟣") || altText.includes("πŸ”΅") || altText.includes("🟠"); altDiv.style.setProperty('--pulse-color', activeModeColor); if (isTx) { altDiv.classList.add('tx-active-unified'); } else { altDiv.classList.remove('tx-active-unified'); altDiv.style.setProperty('color', 'var(--text-muted)', 'important'); altDiv.style.setProperty('border-left', `4px solid var(--border-color)`, 'important'); } } } else { if (altDiv) altDiv.style.display = "none"; if (tsContainer) tsContainer.style.display = "flex"; activeModeColor = "var(--primary)"; if (ts1Div && ts2Div) { let isSimplex = (genObj && genObj.duplex === "0"); ts1Div.style.display = isSimplex ? 'none' : 'block'; [ts1Div, ts2Div].forEach((div, idx) => { if (isSimplex && idx === 0) return; const val = String((idx === 0 ? telemetryObj.ts1 : telemetryObj.ts2) ?? ''); const fullLabel = `TS${idx + 1}`; div.innerText = `${fullLabel}: ${val}`; div.style.setProperty('--pulse-color', activeModeColor); if (val.includes("πŸŽ™οΈ")) { isTx = true; div.classList.add('tx-active-unified'); } else { div.classList.remove('tx-active-unified'); div.style.setProperty('color', 'var(--text-muted)', 'important'); div.style.setProperty('border-left', '4px solid var(--border-color)', 'important'); } }); } } let healthObj = data.health && data.health[c.id.toLowerCase()]; let infoObj = data.info && data.info[c.id.toLowerCase()]; const freqContainer = document.getElementById(`freq-container-${c.id}`); const freqTx = document.getElementById(`freq-tx-${c.id}`); const freqRx = document.getElementById(`freq-rx-${c.id}`); const metaContainer = document.getElementById(`meta-container-${c.id}`); const locSpan = document.getElementById(`loc-${c.id}`); const latSpan = document.getElementById(`lat-${c.id}`); const lonSpan = document.getElementById(`lon-${c.id}`); if (infoObj && isOnline) { if (freqContainer) { freqContainer.style.display = 'flex'; freqTx.innerText = infoObj.tx; freqRx.innerText = infoObj.rx; } if (metaContainer) { metaContainer.style.display = 'block'; locSpan.innerText = infoObj.loc; cardDiv.dataset.loc = infoObj.loc; if(latSpan) latSpan.innerText = infoObj.lat; if(lonSpan) lonSpan.innerText = infoObj.lon; } } else { if (freqContainer) freqContainer.style.display = 'none'; if (metaContainer) metaContainer.style.display = 'none'; } const healthContainer = document.getElementById(`health-${c.id}`); const cpuSpan = document.getElementById(`cpu-${c.id}`); const tempSpan = document.getElementById(`temp-${c.id}`); const ramSpan = document.getElementById(`ram-${c.id}`); const diskSpan = document.getElementById(`disk-${c.id}`); const gwBar = document.getElementById(`gw-bar-${c.id}`); if (gwBar && healthObj) { let gwHtml = ""; let gwData = data.gateway_states && data.gateway_states[c.id.toLowerCase()] ? data.gateway_states[c.id.toLowerCase()] : {}; let processes = healthObj.processes || {}; let activeGatewaysStr = ""; const gwMap = [ { id: 'dmrgateway', label: 'DMR', color: '#3b82f6', tint: 'rgba(59, 130, 246, 0.15)', proto: 'DMR' }, { id: 'ysfgateway', label: 'YSF', color: '#8b5cf6', tint: 'rgba(139, 92, 246, 0.15)', proto: 'YSF' }, { id: 'dstargateway', label: 'D-STAR', color: '#06b6d4', tint: 'rgba(6, 182, 212, 0.15)', proto: 'D-STAR' }, { id: 'nxdngateway', label: 'NXDN', color: '#10b981', tint: 'rgba(16, 185, 129, 0.15)', proto: 'NXDN' }, { id: 'p25gateway', label: 'P25', color: '#f59e0b', tint: 'rgba(245, 158, 11, 0.15)', proto: 'P25' } ]; if (isOnline) { gwMap.forEach(gw => { let isActiveProc = processes[gw.id] && processes[gw.id].toLowerCase() === 'online'; if (isActiveProc) { activeGatewaysStr += gw.proto.toLowerCase() + ","; let stateInfo = gwData[gw.proto] || { action: 'IDLE', dest: '---' }; let action = (stateInfo.action || 'IDLE').toLowerCase(); let dest = stateInfo.dest && stateInfo.dest !== '---' ? stateInfo.dest : ''; let badgeBg, badgeColor, badgeBorder, suffix = '', extraClass = ''; if (action === 'linking' || action === 'linked' || (dest !== '' && action !== 'unlinked' && action !== 'failed')) { badgeBg = gw.color; badgeColor = 'white'; badgeBorder = gw.color; if (dest) suffix = `: ${dest}`; else suffix = ` [LINKING]`; } else if (action === 'failed') { badgeBg = 'var(--danger)'; badgeColor = 'white'; badgeBorder = 'var(--danger)'; suffix = ' [FAILED]'; extraClass = 'blink'; } else if (action === 'unlinked') { badgeBg = 'transparent'; badgeColor = 'var(--text-muted)'; badgeBorder = 'var(--border-color)'; suffix = ' [UNLINKED]'; } else { badgeBg = gw.tint; badgeColor = gw.color; badgeBorder = gw.color; } if (gw.proto === 'DMR' && gwData['DMR_NETS'] && gwData['DMR_NETS'].length > 0) { gwData['DMR_NETS'].forEach(netName => { let netText = `DMR: ${esc(netName)}${esc(suffix)}`; gwHtml += `${netText}`; }); } else { let text = `${gw.label}${esc(suffix)}`; gwHtml += `${text}`; } } }); } cardDiv.dataset.gateways = activeGatewaysStr; gwBar.innerHTML = gwHtml; } else if (gwBar) { gwBar.innerHTML = ""; cardDiv.dataset.gateways = ""; } if (healthObj && isOnline) { healthContainer.style.display = 'flex'; let cpu = healthObj.cpu; cpuSpan.innerText = cpu; cpuSpan.style.color = cpu < 50 ? 'var(--success)' : (cpu < 80 ? '#f59e0b' : 'var(--danger)'); let tempVal = healthObj.temp; tempSpan.innerText = tempVal; tempSpan.style.color = tempVal < 55 ? 'var(--success)' : (tempVal < 70 ? '#f59e0b' : 'var(--danger)'); ramSpan.innerText = healthObj.ram; let d = healthObj.disk; diskSpan.innerText = d; diskSpan.style.color = d < 85 ? 'inherit' : (d < 95 ? '#f59e0b' : 'var(--danger)'); let upSpan = document.getElementById(`up-${c.id}`); if (upSpan && healthObj.uptime) { upSpan.innerText = fmtAge(healthObj.uptime); } let agentBadge = document.getElementById(`agent-badge-${c.id}`); if (agentBadge && healthObj.agent_version) { agentBadge.innerText = `Agent v${healthObj.agent_version}`; agentBadge.style.display = 'inline-block'; } const profBtnsDiv = document.getElementById(`prof-btns-${c.id}`); const profKeys = JSON.stringify(healthObj.profiles || {}); if (profBtnsDiv && profBtnsDiv.dataset.keys !== profKeys) { profBtnsDiv.dataset.keys = profKeys; if (healthObj.profiles && Object.keys(healthObj.profiles).length > 0) { profBtnsDiv.style.display = 'flex'; let html = ''; const colors = ["var(--accent)", "#eab308", "var(--success)", "var(--primary)"]; let idx = 0; for (const [pKey, pLabel] of Object.entries(healthObj.profiles)) { let col = colors[idx % colors.length]; html += ``; idx++; } profBtnsDiv.innerHTML = html; } else { profBtnsDiv.style.display = 'none'; profBtnsDiv.innerHTML = ''; } } } else { if (healthContainer) healthContainer.style.display = 'none'; let agentBadge = document.getElementById(`agent-badge-${c.id}`); if (agentBadge) agentBadge.style.display = 'none'; } globalHealthData[c.id.toLowerCase()] = healthObj; let hasOfflineService = false; if (healthObj && healthObj.processes) { for (const [svcName, svcStatus] of Object.entries(healthObj.processes)) { if (svcStatus.toLowerCase() !== 'online') { hasOfflineService = true; break; } } } const warnBadge = document.getElementById(`svc-warn-${c.id}`); const btnSvc = document.getElementById(`btn-svc-${c.id}`); if (warnBadge) warnBadge.style.display = hasOfflineService ? 'block' : 'none'; if (btnSvc) { if (hasOfflineService) { btnSvc.style.background = 'var(--danger)'; btnSvc.classList.add('blink'); btnSvc.innerHTML = t('btnSvcKo'); } else { btnSvc.style.background = '#334155'; btnSvc.classList.remove('blink'); btnSvc.innerHTML = t('btnSvc'); } } let btnOta = document.getElementById(`btn-ota-${c.id}`); let adminSummary = document.getElementById(`admin-summary-${c.id}`); if (btnOta) { if (healthObj && healthObj.agent_update_available) { btnOta.style.setProperty('background', 'var(--danger)', 'important'); btnOta.style.setProperty('color', 'white', 'important'); btnOta.style.setProperty('border', '1px solid var(--danger)', 'important'); btnOta.style.animation = 'danger-glow 1.5s infinite'; if (adminSummary) { adminSummary.innerHTML = "⚠️ ADMIN (UPDATE OTA!)"; adminSummary.style.setProperty('color', 'var(--danger)', 'important'); adminSummary.style.setProperty('border-color', 'var(--danger)', 'important'); adminSummary.style.animation = 'danger-glow 1.5s infinite'; } } else { btnOta.style.setProperty('background', '#2563eb', 'important'); btnOta.style.setProperty('color', 'white', 'important'); btnOta.style.setProperty('border', '1px solid transparent', 'important'); btnOta.style.animation = 'none'; if (adminSummary) { adminSummary.innerHTML = "βš™οΈ Admin Tools"; adminSummary.style.setProperty('color', 'var(--text-muted)', 'important'); adminSummary.style.setProperty('border-color', 'var(--border-color)', 'important'); adminSummary.style.animation = 'none'; } } } if (isOnline) { cardDiv.classList.add('online'); statusDiv.classList.remove('status-offline'); cardDiv.style.opacity = "1"; cardDiv.style.filter = "none"; if (isTx) cardDiv.dataset.lastTx = Date.now(); let timeSinceTx = Date.now() - (parseInt(cardDiv.dataset.lastTx) || 0); let showActiveColor = isTx || (!isIdle && timeSinceTx < HANG_TIME_MS); cardDiv.style.setProperty('border-top-color', showActiveColor ? activeModeColor : "var(--border-color)", 'important'); if(isTx) { let shadowRGB = '59, 130, 246'; if (activeModeColor === '#10b981') shadowRGB = '16, 185, 129'; else if (activeModeColor === '#8b5cf6') shadowRGB = '139, 92, 246'; else if (activeModeColor === '#06b6d4') shadowRGB = '6, 182, 212'; else if (activeModeColor === '#f59e0b') shadowRGB = '245, 158, 11'; cardDiv.style.boxShadow = `0 0 20px rgba(${shadowRGB}, 0.5) !important`; } else { cardDiv.style.boxShadow = '0 4px 12px rgba(0,0,0,0.2) !important'; } } else { cardDiv.classList.remove('online'); statusDiv.classList.add('status-offline'); cardDiv.style.opacity = "0.7"; cardDiv.style.filter = "grayscale(80%)"; cardDiv.style.setProperty('border-top-color', '#64748b', 'important'); cardDiv.style.boxShadow = 'none !important'; } const miniCard = document.getElementById(`mini-card-${c.id}`); const miniStatus = document.getElementById(`mini-status-${c.id}`); const miniName = document.getElementById(`mini-name-${c.id}`); const miniId = document.getElementById(`mini-id-${c.id}`); if (miniCard && miniStatus) { if (genObj && isOnline) { if (miniName) miniName.innerText = genObj.callsign; if (miniId) miniId.innerText = `ID: ${genObj.radio_id}`; } else { if (miniName) miniName.innerText = c.name; if (miniId) miniId.innerText = `ID: ${c.id.toUpperCase()}`; } if (isOnline) { miniCard.style.borderColor = (warns.length > 0) ? "#f59e0b" : "var(--success)"; miniCard.style.opacity = "1"; miniCard.style.filter = "none"; miniStatus.innerText = (warns.length > 0) ? "⚠️ ALERT" : stateValue; miniStatus.title = (warns.length > 0) ? warns.join(" | ") : stateValue; miniStatus.style.color = (warns.length > 0) ? "#f59e0b" : "var(--success)"; miniStatus.style.background = (warns.length > 0) ? "rgba(245, 158, 11, 0.15)" : "rgba(16, 185, 129, 0.1)"; miniStatus.style.borderColor = (warns.length > 0) ? "rgba(245, 158, 11, 0.4)" : "rgba(16, 185, 129, 0.2)"; } else { miniCard.style.borderColor = "var(--border-color)"; miniCard.style.opacity = "0.6"; miniCard.style.filter = "grayscale(80%)"; miniStatus.innerText = "OFFLINE"; miniStatus.title = "OFFLINE"; miniStatus.style.color = "var(--danger)"; miniStatus.style.background = "rgba(239, 68, 68, 0.1)"; miniStatus.style.borderColor = "rgba(239, 68, 68, 0.2)"; } } }); applyFilters(); if (document.getElementById('view-map').style.display === 'block') { initOrUpdateMap(); } document.getElementById('last-update').innerText = "Sync: " + new Date().toLocaleTimeString(); } catch (e) { console.error(e); } } async function refreshLogs() { try { const res = await fetch('/api/logs'); const logs = await res.json(); const tbody = document.getElementById('log-body'); let tableHTML = ""; let networkLogs = {}; logs.forEach(row => { const time = row[0] ? row[0].split(' ')[1] : "--:--"; const clientId = row[1]; const protocol = row[2] || "DMR"; let source = row[3] || "---"; const target = row[4] || "---"; const rawSlot = row[5]; const source_ext = row[8]; const rawRssi = row[9]; const sourceConn = row[10]; const ysfMode = row[11]; let sourceHtml = esc(source); if (source_ext && source_ext.trim() !== "") { sourceHtml += ` /${esc(source_ext)}`; } let slotDisplay = rawSlot && String(rawSlot).trim() !== "" && rawSlot !== "undefined" ? (protocol === "DMR" && !String(rawSlot).toUpperCase().startsWith("TS") ? `TS${rawSlot}` : rawSlot) : "---"; let protoColor = "#3b82f6"; if (protocol === "NXDN") protoColor = "#10b981"; else if (protocol === "YSF") protoColor = "#8b5cf6"; else if (protocol === "D-STAR") protoColor = "#06b6d4"; else if (protocol === "P25") protoColor = "#f59e0b"; let sourceBadge = sourceConn ? (sourceConn.toLowerCase() === 'rf' ? 'RF' : (sourceConn.toLowerCase() === 'network' ? 'NET' : `${esc(sourceConn).toUpperCase()}`)) : "---"; let modeDisplay = ysfMode && ysfMode.trim() !== "" ? `${esc(ysfMode).toUpperCase()}` : "---"; let rssiDisplay = '---'; if (rawRssi && rawRssi !== 0) { let sLabel = "S0"; if (rawRssi >= -93) { let over = Math.round((rawRssi + 93) / 10) * 10; sLabel = over > 0 ? `S9+${over}` : "S9"; } else { let sUnits = Math.floor((rawRssi + 141) / 6) + 1; sLabel = `S${Math.max(1, Math.min(9, sUnits))}`; } let activeSegs = 1; if (rawRssi >= -53) activeSegs = 9; else if (rawRssi >= -63) activeSegs = 8; else if (rawRssi >= -73) activeSegs = 7; else if (rawRssi >= -83) activeSegs = 6; else if (rawRssi >= -93) activeSegs = 5; else if (rawRssi >= -105) activeSegs = 4; else if (rawRssi >= -117) activeSegs = 3; else if (rawRssi >= -129) activeSegs = 2; let barsHtml = ''; const heights = [4, 5, 6, 7, 8, 10, 12, 14, 14]; for (let i = 0; i < 9; i++) { let color = "rgba(255,255,255,0.1)"; if (i < activeSegs) { if (i < 5) color = "var(--success)"; else if (i < 7) color = "#f59e0b"; else color = "var(--danger)"; } barsHtml += `
`; } rssiDisplay = `
${sLabel}
${barsHtml}
${esc(rawRssi)} dBm
`; } if (source.includes("🌐")) { if (!networkLogs[clientId]) networkLogs[clientId] = ""; networkLogs[clientId] += `
[${esc(time)}] [${esc(protocol)}] ${esc(source.replace("🌐", "").trim())}
`; } else { const selectedFilter = document.getElementById('stat-node-filter').value; if (selectedFilter === 'all' || String(clientId).toLowerCase() === selectedFilter) { tableHTML += ` ${esc(time)} ${esc(String(clientId).toUpperCase())} ${esc(protocol)} ${modeDisplay} ${sourceBadge} ${esc(slotDisplay)} ${sourceHtml} ${esc(target.trim())} ${esc(row[6])}s ${esc(row[7])}% ${rssiDisplay} `; } } }); tbody.innerHTML = tableHTML; clients.forEach(c => { const localDiv = document.getElementById(`sys-log-${c.id}`); if (localDiv && networkLogs[c.id]) { localDiv.innerHTML = networkLogs[c.id]; } }); } catch (e) { console.error(e); } } async function refreshStats() { try { const node = document.getElementById('stat-node-filter').value || 'all'; const res = await fetch(`/api/stats?node=${node}`); const data = await res.json(); document.getElementById('stat-tgs').innerHTML = data.top_tgs.map((t, i) => `
${i+1}. ${esc(t.target)} ${t.count} tx
`).join('') || t('statNoData'); document.getElementById('stat-calls').innerHTML = data.top_calls.map((c, i) => `
${i+1}. ${esc(c.call)} ${c.count} tx
`).join('') || t('statNoData'); document.getElementById('stat-avg').innerText = data.avg_duration; document.getElementById('stat-today').innerText = data.today_tx; } catch (e) { console.error("Errore statistiche:", e); } } async function subscribeToPush() { if (!('serviceWorker' in navigator) || !('PushManager' in window)) { customAlert("Error", "Push notifications are not supported by this browser.", true); return; } if (!me.logged_in) { openLoginModal(); return; } const permission = await Notification.requestPermission(); if (permission !== 'granted') { customAlert("Error", "Push notifications permission denied.", true); return; } try { const reg = await navigator.serviceWorker.ready; const res = await fetch('/api/vapid_public_key'); const { public_key } = await res.json(); const subscription = await reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlB64ToUint8Array(public_key) }); const subRes = await fetch('/api/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(subscription) }); if (!subRes.ok) throw new Error('subscribe failed: HTTP ' + subRes.status); customAlert("Success", "Push notifications enabled successfully!"); document.getElementById('push-btn').style.color = 'var(--success)'; } catch (e) { console.error(e); customAlert("Error", "Failed to enable notifications.", true); } } async function checkPushStatus() { if (!('serviceWorker' in navigator) || !('PushManager' in window)) return; try { const reg = await navigator.serviceWorker.ready; const subscription = await reg.pushManager.getSubscription(); const btn = document.getElementById('push-btn'); if (subscription && btn) { btn.style.color = 'var(--success)'; } } catch(e) { console.error(e); } } function urlB64ToUint8Array(base64String) { const padding = '='.repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/'); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) outputArray[i] = rawData.charCodeAt(i); return outputArray; } async function checkServerUpdate() { if (!isAuthenticated || me.role !== 'admin') return; try { const res = await fetch('/api/server_update/check'); const data = await res.json(); const btn = document.getElementById('server-update-btn'); if (btn) { btn.style.display = data.update_available ? 'inline-block' : 'none'; } } catch (e) { console.error(e); } } function confirmServerUpdate() { customConfirm("πŸ”„ UPDATE SERVER", "Vuoi scaricare l'ultima versione del server da Gitea/GitHub e riavviare il servizio backend?", "var(--danger)", async () => { try { const res = await fetch('/api/server_update/apply', { method: 'POST' }); const data = await res.json(); if (data.success) { customAlert("βœ… SUCCESS", "Aggiornamento in corso. La pagina si ricaricherΓ  a breve."); setTimeout(() => location.reload(), 5000); } else { customAlert("❌ ERRORE", esc(data.error), true); } } catch (e) { console.error(e); customAlert("❌ ERRORE", "Errore di rete.", true); } }); } function debounce(fn, ms) { let tm = null; return () => { clearTimeout(tm); tm = setTimeout(fn, ms); }; } const refreshStatesSoon = debounce(refreshStates, 300); const refreshLogsSoon = debounce(() => { refreshLogs(); refreshStats(); }, 500); if (typeof io !== 'undefined') { const socket = io(); socket.on('mqtt_status', function(data) { const badge = document.getElementById('mqtt-badge'); if (badge) { if (data.connected) { badge.innerText = "MQTT: ONLINE"; badge.className = "mqtt-badge mqtt-online"; } else { badge.innerText = "MQTT: OFFLINE"; badge.className = "mqtt-badge mqtt-offline"; } } }); socket.on('connect', () => { refreshStates(); refreshLogs(); refreshStats(); }); socket.on('dati_aggiornati', refreshStatesSoon); socket.on('log_aggiornato', refreshLogsSoon); }