Files
reflector-dashboard/lastheard_live_view.js
bjorne cb8b9af20e sync från dev (svx-web): lastheard paginering + separat live-feed, listen-vy, UI-uppdateringar
- lastheard 24h: client-side paginering (lib/pager.js), dynamisk sidstorlek
- lastheard: separat live-feed (lastheard_live_view.js), 24h loggar varje key-up
- listen_view.js + auth/admin_listen.*: Listen Live
- genomgående UI/auth-uppdateringar synkade från dev
Inga hårdkodade secrets/IP/login — endast hostnamn.
2026-11-18 22:07:16 +01:00

133 lines
5.2 KiB
JavaScript

'use strict';
/* Lastheard — LIVE. Shows ALL talkers heard in the last few seconds — local,
* trunk AND satellite alike — exactly like the 24h view, just a short live
* window. Driven by /api/lastheard (the SAME feed the 24h view uses), NOT by
* status.nodes[].isTalker, which only exists on local nodes and therefore misses
* every trunk/satellite talker (the bug). Newest on top; a talker lingers
* LIVE_WINDOW seconds after their over. 4 stat tiles + "● LIVE" dot. */
const LIVE_WINDOW = 10; // seconds a talker lingers after their over ends
let __lhEvents = [];
let __lhLiveTimer = null;
function lhLiveIsBridge(n){ return !!(n && (n.remoteTrunk || n.sat_id)); }
function lhLiveParseEvents(payload){
// LIVE reads the SEPARATE `live` feed (who is keyed NOW), NOT the 24h `events`
// history — the two are deliberately not mixed. Fall back to `events` only for an
// older server that does not yet serve `live`.
const arr = (payload && payload.live && payload.live.length) ? payload.live
: (payload && payload.events ? payload.events : []);
return arr
.map(e => {
if (typeof e === 'string'){ try { return JSON.parse(e); } catch { return null; } }
return e;
})
.filter(Boolean);
}
async function refreshLastheardLive(){
lhLiveStartTimer();
try {
const r = await apiFetch('/svxportal/api/lastheard?limit=50'); // lib/api.js
if (r && r.ok && r.json) __lhEvents = lhLiveParseEvents(r.json);
} catch(_){ /* keep last events */ }
renderLastheardLive();
}
// Direction: local transmit vs trunk/bridge inbound (RX). Everything that is not
// a plain local keyup is received over a trunk/satellite — shown as RX.
function lhLiveDir(net){
const b = String(net || '').toLowerCase();
return (b === '' || b === 'local' || b === 'svxlink') ? 'TX' : 'RX';
}
function updateLhLiveDot(){
const dot = document.getElementById('lastheardLiveDot');
if (!dot) return;
if (window.__svxSSE === 'down'){ dot.style.color = '#ff4040'; dot.innerHTML = '● RECONNECTING'; }
else { dot.style.color = '#0f0'; dot.innerHTML = '● LIVE'; }
}
function renderLastheardLive(){
const tbody = document.getElementById('lastheardLiveRows');
if (!tbody) return;
const esc = window.escHtml || (s => String(s));
const status = window.__svxportal_lastStatus || {};
const nodes = status.nodes || {};
const bn = status.bridge_nodes || {};
const now = Math.floor(Date.now()/1000);
updateLhLiveDot();
// Live talkers within the window (all sources), newest first, ONE row per
// talker: dedupe on callsign+TG so repeated overs don't stack up as doubles.
const seenKey = new Set();
const live = __lhEvents
.filter(e => (now - Number(e.ts || 0)) <= LIVE_WINDOW)
.sort((a,b) => Number(b.ts || 0) - Number(a.ts || 0))
.filter(e => {
const k = `${String(e.callsign||'').toUpperCase()}|${Number(e.tg||0)}`;
if (seenKey.has(k)) return false;
seenKey.add(k); return true;
});
// ---- 4 stat tiles ----
let repeaters = 0, clients = 0, bridges = 0;
for (const cs of Object.keys(nodes)){
const n = nodes[cs]; if (!n) continue;
if (lhLiveIsBridge(n)) bridges++;
else if (String(n.nodeClass||'').toLowerCase() === 'repeater') repeaters++;
else clients++;
}
const set = (id,v)=>{ const el=document.getElementById(id); if(el) el.textContent=String(v); };
set('lhLiveStatActive', live.length);
set('lhLiveStatRepeaters', repeaters);
set('lhLiveStatClients', clients);
set('lhLiveStatBridges', bridges);
// ---- Live Talkers table ----
if (live.length === 0){
tbody.innerHTML = '<tr><td colspan="5" class="text-secondary small">No active calls.</td></tr>';
return;
}
tbody.innerHTML = live.map(e => {
const age = now - Number(e.ts || 0);
const bi = bn[String(e.callsign || '').toUpperCase()];
const net = e.network || (bi && bi.network) || '';
const dir = lhLiveDir(net) === 'RX'
? '<span class="badge bg-info">RX</span>'
: '<span class="badge bg-success">TX</span>';
const mode = net ? `<span class="mono">${esc(net)}</span>` : '<span class="text-secondary">&mdash;</span>';
const loc = esc(e.location || (bi && bi.system) || '');
// Fresh (just now) = green on air; older within the window = grey lingering.
const bg = age <= 3 ? 'rgba(25,135,84,0.85)' : 'rgba(108,117,125,0.65)';
return `<tr style="background:${bg};">
<td>${dir}</td>
<td>${mode}</td>
<td class="mono"><b>${esc(e.callsign || '')}</b></td>
<td class="wrap">${loc}</td>
<td class="mono">${Number(e.tg || 0)}</td>
</tr>`;
}).join('');
}
// 2 s poll while the panel is open: re-fetch events so trunk/satellite talkers
// appear fast, and let lingering rows age out of the window.
function lhLiveStartTimer(){
if (__lhLiveTimer) return;
__lhLiveTimer = setInterval(() => {
if (document.getElementById('lastheardLivePanel')?.style.display === 'none'){
lhLiveStopTimer(); return;
}
refreshLastheardLive();
}, 2000);
}
function lhLiveStopTimer(){ if (__lhLiveTimer){ clearInterval(__lhLiveTimer); __lhLiveTimer = null; } }
window.refreshLastheardLive = refreshLastheardLive;
window.renderLastheardLive = renderLastheardLive;
window.updateLhLiveDot = updateLhLiveDot;