Files
bpq_gui/terminal.mjs
T
alberto c98c67e18b Add detached service windows and selectable terminal themes
Support standalone APRS, Monitor, Chat and Terminal views with courtesy screens, window focus and return controls, shared sessions and suspended embedded polling.

Add five persistent phosphor palettes, remove Console terminal idle disconnections, and display decoded Mic-E movement and altitude with expandable original packets.

Document the new behavior and add regression coverage. Validation: 182 tests passed; the known hanging PowerShell startup readiness test was excluded.
2026-10-19 22:15:15 +02:00

25 lines
3.2 KiB
JavaScript

import net from 'node:net';
import {StringDecoder} from 'node:string_decoder';
import {TelnetText,TelnetLogin} from './telnet-login.mjs';
export {TelnetText} from './telnet-login.mjs';
export class Terminal {
constructor(credentials,options={}){this.credentials=credentials;this.options=options;this.state='offline';this.events=[];this.seq=0;}
event(text){if(!text)return;this.events.push({id:++this.seq,text});let size=this.events.reduce((n,e)=>n+e.text.length,0);while(this.events.length>500||size>200000)size-=this.events.shift().text.length;}
snapshot(after=0){return {state:this.state,events:this.events.filter(e=>e.id>after),latest:this.seq,port:this.options.port,error:this.error||null};}
async connect(){if(this.state==='connecting'||this.socket&&!this.socket.destroyed)return this.snapshot();this.state='connecting';let credentials;try{credentials=await this.credentials();}catch{this.state='error';throw new Error('Credenziali Telnet BPQ non disponibili');}if(this.state!=='connecting')return this.snapshot();this.error=null;this.events=[];this.stage=0;this.escape='';this.pendingCR=false;this.decoder=new StringDecoder('utf8');const socket=net.createConnection({host:'127.0.0.1',port:this.options.port});this.socket=socket;const telnet=new TelnetText(b=>socket.write(b));socket.setTimeout(15000);
socket.on('timeout',()=>{if(this.state==='connecting')socket.destroy(new Error('Accesso Telnet non confermato'));});
socket.on('error',e=>{if(this.socket!==socket)return;this.state='error';this.error=['Accesso rifiutato','Accesso Telnet non confermato'].includes(e.message)?e.message:'Connessione Telnet BPQ non disponibile';this.event('\n[Connessione Telnet non disponibile]\n');});
socket.on('close',()=>{if(this.socket!==socket)return;this.login?.close();if(this.pendingCR){this.event('\n');this.pendingCR=false;}if(this.state!=='error')this.state='offline';this.event('\n[Sessione terminata]\n');});
this.login=new TelnetLogin(socket,credentials,this.options,text=>{if(this.socket!==socket)return;this.stage=3;this.state='online';socket.setTimeout(0);this.output(text);});
socket.on('data',chunk=>{if(this.socket!==socket)return;const text=this.decoder.write(telnet.read(chunk));if(this.stage<3){this.login.read(text);return;}this.output(text);});return this.snapshot();}
output(text){let out='';for(const c of text){if(this.pendingCR){out+='\n';this.pendingCR=false;if(c==='\n'||c==='\0')continue;}
if(this.escape){this.escape+=c;if(this.escape.length>128||/^\x1b\[[0-?]*[ -/]*[@-~]$/.test(this.escape)||this.escape.length===2&&c!=='[')this.escape='';continue;}
if(c==='\x1b'){this.escape=c;continue;}if(c==='\r'){this.pendingCR=true;continue;}if(c==='\b'){out=out.slice(0,-1);continue;}if(c==='\n'||c==='\t'||c.charCodeAt(0)>=32)out+=c;
}this.event(out);}
send(text){if(this.state!=='online'||!this.socket||this.socket.destroyed)throw new Error('Collega prima il terminale');if(typeof text!=='string'||text.length>1000||/[\x00-\x1f\x7f]/.test(text))throw new Error('Invia un solo comando, massimo 1000 caratteri');this.event(text+'\n');this.socket.write(text+'\r');}
disconnect(reason='Disconnessione richiesta'){this.login?.close();if(this.socket&&!this.socket.destroyed)this.socket.destroy();this.state='offline';this.event('\n['+reason+']\n');}
close(){this.disconnect();}
}