let modemTelemetry=null,modemPolling=false;
const radioCard=$('.radio-card');radioCard.innerHTML=`
BPQ MODEM—
STAZIONE IN ASCOLTONODO LOCALESTAZIONE
◉ALIASBPQ
Porta radio BPQ
Modem BPQSNR ultima sessione —TX — / RX —
`;
const missionHead=document.createElement('div');missionHead.className='mission-heading';missionHead.innerHTML='RADIO OPERATIONS
STAZIONE / BPQ
STAZIONE BPQ
';
$('#home .page-heading').replaceWith(missionHead);
function graph(history,keys,id,minimum=1){const width=350,height=85,pad=5;const values=history.flatMap(s=>keys.map(k=>s[k])).filter(v=>typeof v==='number'&&Number.isFinite(v));if(!values.length)return 'Nessun campione disponibile
';const min=Math.min(0,...values),max=Math.max(minimum,...values),range=max-min||1;const x=i=>pad+i*(width-pad*2)/Math.max(1,history.length-1),y=v=>height-pad-(v-min)*(height-pad*2)/range;const lines=keys.map((key,j)=>{let path='',breakLine=true;history.forEach((s,i)=>{const value=s[key];if(typeof value!=='number'||!Number.isFinite(value)){breakLine=true;return;}path+=(breakLine?'M':'L')+x(i).toFixed(2)+' '+y(value).toFixed(2)+' ';breakLine=false;});return ``}).join('');return ``;}
function renderTelemetry(data){modemTelemetry=data;const c=data.current;if(!c)return;const history=data.history.slice(-120);$('#radio-status').textContent=window.bpqRadioControl?.txSuspended?'TX SOSPESO':!c.available?'OFFLINE':c.active?'LINK ATTIVO':'IN ASCOLTO';$('#modem-session').classList.toggle('idle',!c.active);$('#modem-session').classList.toggle('connected',!!c.active);$('#session-label').textContent=window.bpqRadioControl?.txSuspended?'PORTE RADIO IN PAUSA':c.connecting?'CHIAMATA IN CORSO':c.active?'CONNESSIONE RADIO':'STAZIONE IN ASCOLTO';$('#caller-call').textContent=c.active?c.caller||'—':station.nodeCall;$('#called-call').textContent=c.active?c.called||'—':station.alias;$('#call-arrow').textContent=c.active?'→':'◉';$('#caller-label').textContent=c.active?'CHIAMANTE':'NODO LOCALE';$('#called-label').textContent=c.active?'CHIAMATO':'ALIAS';$('#session-detail').textContent=c.active?(c.connecting?'Attendo risposta':(c.caller||'').replace(/-\d+$/,'')===station.call.replace(/-\d+$/,'')?'Chiamata in uscita':'Chiamata in ingresso'):station.radio?.name||'Nessuna porta modem configurata';$('#radio-freq').textContent=c.frequency||'—';$('#radio-scan').textContent=c.scan?'● Scansione attiva':c.active?'● Frequenza bloccata':'—';$('#radio-channel').textContent=c.available?(c.busy?'Canale occupato':'Canale libero'):'—';$('#radio-modem').textContent=c.available?station.radio?.name||'Modem BPQ':'Modem non disponibile';$('#radio-snr').textContent=c.lastSnr!=null?c.lastSnr.toFixed(1)+' dB':'—';$('#bps-value').textContent=c.available?Math.round((c.txBps||0)+(c.rxBps||0)).toLocaleString('it-IT'):'—';$('#bps-caption').textContent='TX '+Math.round(c.txBps||0).toLocaleString('it-IT')+' / RX '+Math.round(c.rxBps||0).toLocaleString('it-IT')+' bit/s · dati BPQ';$('#snr-value').textContent=c.snr!=null?c.snr.toFixed(1)+' dB':'—';$('#vara-cpu-value').textContent=c.varaCpu!=null?c.varaCpu.toFixed(1)+'%':'—';$('#system-cpu-value').textContent=c.cpu!=null?c.cpu.toFixed(1)+'%':'—';$('#snr-caption').textContent=c.active?'SNR esposto da BPQ':'In attesa della prossima connessione';$('#modem-traffic').textContent='TX '+size(c.sent||0)+' / RX '+size(c.received||0);$('#bps-chart').innerHTML=graph(history,['txBps','rxBps'],'traffico BPQ TX RX',100);$('#snr-chart').innerHTML=graph(history,['snr'],'SNR',10);$('#vara-cpu-chart').innerHTML=graph(history,['varaCpu'],'CPU VARA',5);$('#system-cpu-chart').innerHTML=graph(history,['cpu'],'CPU sistema',10);}
async function pollTelemetry(){if(!csrf||modemPolling)return;modemPolling=true;try{renderTelemetry(await api('/telemetry'))}catch{}finally{modemPolling=false}}
const missionDashboard=loadDashboard;loadDashboard=async function(){await missionDashboard();if(modemTelemetry)renderTelemetry(modemTelemetry);pollTelemetry();};setInterval(()=>{if(csrf&&!document.hidden)pollTelemetry()},2000);pollTelemetry();
const fullscreenButton=document.createElement('button');fullscreenButton.id='fullscreen-toggle';fullscreenButton.className='icon-button';fullscreenButton.textContent='⛶';fullscreenButton.setAttribute('aria-label','Schermo intero');fullscreenButton.title='Schermo intero';$('#refresh').after(fullscreenButton);
fullscreenButton.onclick=async()=>{try{if(document.fullscreenElement)await document.exitFullscreen();else await document.documentElement.requestFullscreen();}catch{toast('Schermo intero non disponibile in questo browser. Prova F11.');}};
document.addEventListener('fullscreenchange',()=>{const active=!!document.fullscreenElement;fullscreenButton.textContent=active?'⊡':'⛶';fullscreenButton.title=active?'Torna alla finestra':'Schermo intero';fullscreenButton.setAttribute('aria-label',fullscreenButton.title);fullscreenButton.setAttribute('aria-pressed',String(active));});