Files
bpq_gui/monitor.mjs
T

18 lines
3.4 KiB
JavaScript

import net from 'node:net';
// BPQTermTCP: FF ESC colour + decoded frame + FE; FF FF port definitions.
export class MonitorDecoder {
constructor(onFrame,onPorts){this.onFrame=onFrame;this.onPorts=onPorts;this.buffer=Buffer.alloc(0);}
push(chunk){this.buffer=Buffer.concat([this.buffer,chunk]);while(this.buffer.length){const start=this.buffer.indexOf(255);if(start<0){this.buffer=Buffer.alloc(0);return;}if(start)this.buffer=this.buffer.subarray(start);if(this.buffer.length<2)return;
if(this.buffer[1]===255){const sep=this.buffer.indexOf(124,2);if(sep<0)return;const count=Number(this.buffer.subarray(2,sep).toString());if(!Number.isInteger(count)||count<0||count>64){this.buffer=this.buffer.subarray(2);continue;}let end=sep;for(let i=0;i<count;i++){end=this.buffer.indexOf(124,end+1);if(end<0)return;}const ports=this.buffer.subarray(sep+1,end).toString('utf8').split('|').map(s=>{const m=s.match(/^(\d+) (.*)$/);return m?{id:Number(m[1]),name:m[2]}:null}).filter(Boolean);this.onPorts(ports.filter(p=>p.id>0));this.buffer=this.buffer.subarray(end+1);continue;}
const end=this.buffer.indexOf(254,2);if(end<0){if(this.buffer.length>65536)this.buffer=Buffer.alloc(0);return;}const frame=this.buffer.subarray(1,end);const direction=frame[0]===27?(frame[1]===91?'TX':'RX'):'RX';const text=frame.subarray(frame[0]===27?2:0).toString('utf8').replace(/\r\n?/g,'\n').replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,'').trimEnd();const port=Number(text.match(/\bPort=(\d+)/)?.[1])||null;if(text)this.onFrame({text,direction,port});this.buffer=this.buffer.subarray(end+1);
}}
}
export class Monitor {
constructor(credentials){this.credentials=credentials;this.state='offline';this.events=[];this.seq=0;this.ports=[];this.lastAccess=0;this.timer=setInterval(()=>{if(this.socket&&Date.now()-this.lastAccess>120000)this.disconnect()},15000);this.timer.unref();}
snapshot(after=0){this.lastAccess=Date.now();return {state:this.state,error:this.error,ports:this.ports,latest:this.seq,first:this.events[0]?.id||0,events:this.events.filter(e=>e.id>after)};}
async connect(){this.lastAccess=Date.now();if(this.pending)return this.pending;if(this.socket&&!this.socket.destroyed)return this.snapshot();this.pending=this.open();try{return await this.pending}finally{this.pending=null;}}
async open(){const credentials=await this.credentials();this.state='connecting';this.error=null;this.auth='';const socket=net.createConnection({host:'127.0.0.1',port:8101});this.socket=socket;const decoder=new MonitorDecoder(frame=>{this.events.push({...frame,id:++this.seq,at:new Date().toISOString()});if(this.events.length>1000)this.events.shift()},ports=>this.ports=ports);socket.setTimeout(15000);socket.on('connect',()=>socket.write(credentials.user+'\r'+credentials.password+'\rBPQTermTCP\r'));socket.on('timeout',()=>socket.destroy(new Error('Timeout monitor BPQ')));socket.on('error',()=>{this.state='error';this.error='Monitor BPQ non disponibile'});socket.on('close',()=>{if(this.state!=='error')this.state='offline'});socket.on('data',chunk=>{if(this.state==='connecting'){this.auth=(this.auth+chunk.toString('utf8')).slice(-4096);if(/Connected to TelnetServer/i.test(this.auth)){this.state='online';this.auth='';socket.setTimeout(0);socket.write('\\'.repeat(4)+'7fffffffffffffff 1 1 1 1 0 1 1\r');}else if(/invalid|attempts|failed/i.test(this.auth)){socket.destroy(new Error('Accesso monitor fallito'));}}decoder.push(chunk)});return this.snapshot();}
disconnect(){this.socket?.destroy();this.state='offline';}
}