(() => { const nav=document.createElement('button');nav.dataset.page='terminal';nav.innerHTML='▸_ Terminale';nav.onclick=()=>navigate('terminal');$('nav').append(nav); const section=document.createElement('section');section.id='terminal';section.className='page';section.hidden=true;section.innerHTML=`

Terminale

BPQ · Telnet

BPQ / REMOTE TERMINALOFFLINE
TELNET · BPQ LOCALE↑ ↓ cronologia · Invio per inviare
`;$('main').append(section); let cursor=0,busy=false,state='offline',buffer='',following=true,history=[],historyIndex=0,draft='',sending=false,generation=0; const area=$('#terminal-screen'),output=$('#terminal-output'),input=$('#terminal-input'); const previousNavigate=navigate;navigate=function(next){previousNavigate(next);if(next==='terminal'){$('#breadcrumb').textContent='Stazione / Terminale';poll();}}; function bottom(){area.scrollTop=area.scrollHeight;following=true;$('#terminal-follow').hidden=true;} function append(text){buffer=(buffer+text).slice(-200000);output.textContent=buffer;if(following)bottom();} function status(data){const wasOnline=state==='online';state=data.state;const online=state==='online',connecting=state==='connecting';$('#terminal-state').textContent=data.error||({online:'ONLINE',connecting:'COLLEGAMENTO…',offline:'OFFLINE',error:'ERRORE'}[state]||state);$('#terminal-led').classList.toggle('online',online);$('#terminal-connect').hidden=online||connecting;$('#terminal-stop').hidden=!online&&!connecting;input.disabled=!online;$('#terminal-send').disabled=!online||sending;input.placeholder=online?'Scrivi un comando…':'Collega il terminale per iniziare';$('#terminal-endpoint').textContent='TELNET · 127.0.0.1:'+data.port;if(online&&!wasOnline&&page==='terminal')input.focus();} async function poll(){if(!csrf||busy||!station.features.terminal)return;busy=true;const current=generation;try{const d=await api('/terminal?after='+cursor,{busy:false});if(current!==generation)return;if(d.latest{const button=$('#terminal-connect');button.disabled=true;try{const d=await api('/terminal/connect',{method:'POST',body:'{}'});buffer='';output.textContent='';following=true;cursor=0;status(d);await poll();}catch(e){toast(e.message);}finally{button.disabled=false;}}; $('#terminal-stop').onclick=async()=>{try{status(await api('/terminal/disconnect',{method:'POST',body:'{}'}));await poll();}catch(e){toast(e.message);}}; $('#terminal-form').onsubmit=async e=>{e.preventDefault();if(sending||state!=='online')return;const text=input.value;sending=true;$('#terminal-send').disabled=true;try{await api('/terminal/send',{method:'POST',body:JSON.stringify({text})});if(text&&history.at(-1)!==text)history.push(text);history=history.slice(-100);historyIndex=history.length;draft='';input.value='';await poll();}catch(e){toast(e.message);}finally{sending=false;$('#terminal-send').disabled=state!=='online';if(state==='online')input.focus();}}; input.addEventListener('keydown',e=>{if(e.key==='ArrowUp'){e.preventDefault();if(historyIndex===history.length)draft=input.value;if(historyIndex>0)input.value=history[--historyIndex];}else if(e.key==='ArrowDown'){e.preventDefault();if(historyIndex{following=area.scrollHeight-area.scrollTop-area.clientHeight<40;$('#terminal-follow').hidden=following;});$('#terminal-follow').onclick=bottom; $('#terminal-clear').onclick=()=>{buffer='';output.textContent='';bottom();input.focus();}; $('#terminal-export').onclick=()=>{if(!buffer){toast('Il terminale è vuoto');return;}const url=URL.createObjectURL(new Blob([buffer],{type:'text/plain;charset=utf-8'})),a=document.createElement('a');a.href=url;a.download='BPQ-terminal-'+new Date().toISOString().replace(/[:.]/g,'-')+'.txt';a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}; document.addEventListener('station-ready',()=>{generation++;cursor=0;buffer='';output.textContent='';history=[];historyIndex=0;nav.hidden=!station.features.terminal;if(page==='terminal')poll();}); const logout=$('#logout').onclick;$('#logout').onclick=async()=>{generation++;buffer='';output.textContent='';history=[];input.value='';await logout();}; setInterval(()=>{if(csrf&&page==='terminal')poll();},700); window.addEventListener('hashchange',()=>{if(location.hash==='#terminal'&&page!=='terminal')navigate('terminal');});if(location.hash==='#terminal')navigate('terminal'); })();