fix TopSapp
This commit is contained in:
1 parent
8dc6c594c9
commit
bae6adefcb
42 files changed
+43645
-105
No files matched your search
@@ -0,0 +1,447 @@
|
||||
const net = require('net');
|
||||
|
||||
class FiberhomeManager {
|
||||
constructor() {
|
||||
this.tl1Connection = null;
|
||||
this.commandId = 1;
|
||||
|
||||
// Configuração apenas para UNM2000 via TL1
|
||||
this.config = {
|
||||
unm2000: {
|
||||
host: '131.0.192.9',
|
||||
port: 3337,
|
||||
username: '1',
|
||||
password: '1',
|
||||
timeout: 15000
|
||||
}
|
||||
};
|
||||
|
||||
this.logger = require('../utils/logger');
|
||||
}
|
||||
|
||||
/**
|
||||
* Testar conectividade de rede com um host
|
||||
*/
|
||||
async testarConectividade(host, port, timeout = 5000) {
|
||||
return new Promise((resolve) => {
|
||||
const socket = new net.Socket();
|
||||
|
||||
socket.setTimeout(timeout);
|
||||
|
||||
socket.connect(port, host, () => {
|
||||
this.logger.debug(`Conectividade OK: ${host}:${port}`);
|
||||
socket.destroy();
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
socket.on('error', (error) => {
|
||||
this.logger.debug(`Erro de conectividade ${host}:${port}:`, error.message);
|
||||
resolve(false);
|
||||
});
|
||||
|
||||
socket.on('timeout', () => {
|
||||
this.logger.debug(`Timeout de conectividade ${host}:${port}`);
|
||||
socket.destroy();
|
||||
resolve(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Conectar ao UNM2000 via TL1
|
||||
*/
|
||||
async conectarUNM2000() {
|
||||
try {
|
||||
if (this.tl1Connection && !this.tl1Connection.destroyed) {
|
||||
this.logger.debug('Conexão TL1 já existe e está ativa');
|
||||
return true;
|
||||
}
|
||||
|
||||
this.logger.debug('Conectando ao UNM2000 via TL1', this.config.unm2000);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.tl1Connection = new net.Socket();
|
||||
let authenticated = false;
|
||||
let dataBuffer = '';
|
||||
|
||||
this.tl1Connection.setTimeout(this.config.unm2000.timeout);
|
||||
|
||||
// Handler para dados recebidos
|
||||
this.tl1Connection.on('data', (data) => {
|
||||
dataBuffer += data.toString();
|
||||
this.logger.debug('TL1 dados recebidos:', dataBuffer);
|
||||
|
||||
// Verificar se login foi bem-sucedido
|
||||
if (!authenticated && dataBuffer.includes('COMPLD')) {
|
||||
authenticated = true;
|
||||
this.logger.info('Login TL1 bem-sucedido');
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
|
||||
this.tl1Connection.connect(this.config.unm2000.port, this.config.unm2000.host, () => {
|
||||
this.logger.debug('Socket TL1 conectado, enviando comando de login');
|
||||
|
||||
// Comando de login TL1
|
||||
const loginCommand = `ACT-USER::${this.config.unm2000.username}:${this.commandId++}::${this.config.unm2000.password};`;
|
||||
this.tl1Connection.write(loginCommand + '\r\n');
|
||||
|
||||
// Timeout para autenticação
|
||||
setTimeout(() => {
|
||||
if (!authenticated) {
|
||||
this.logger.error('Timeout na autenticação TL1');
|
||||
reject(new Error('Timeout na autenticação TL1'));
|
||||
}
|
||||
}, 10000);
|
||||
});
|
||||
|
||||
this.tl1Connection.on('error', (error) => {
|
||||
this.logger.error('Erro na conexão TL1', error);
|
||||
reject(error);
|
||||
});
|
||||
|
||||
this.tl1Connection.on('timeout', () => {
|
||||
this.logger.error('Timeout na conexão TL1');
|
||||
reject(new Error('Timeout na conexão TL1'));
|
||||
});
|
||||
|
||||
this.tl1Connection.on('close', () => {
|
||||
this.logger.debug('Conexão TL1 fechada');
|
||||
this.tl1Connection = null;
|
||||
});
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao conectar UNM2000', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executar comando TL1
|
||||
*/
|
||||
async executarComandoTL1(comando) {
|
||||
try {
|
||||
this.logger.debug('Executando comando TL1', { comando });
|
||||
|
||||
if (!this.tl1Connection || this.tl1Connection.destroyed) {
|
||||
const conectado = await this.conectarUNM2000();
|
||||
if (!conectado) {
|
||||
throw new Error('Não foi possível conectar ao UNM2000');
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let dataBuffer = '';
|
||||
let responseReceived = false;
|
||||
|
||||
// Handler temporário para esta consulta
|
||||
const dataHandler = (data) => {
|
||||
dataBuffer += data.toString();
|
||||
this.logger.debug('Resposta TL1 recebida:', dataBuffer);
|
||||
|
||||
// Verificar se a resposta está completa (termina com ;)
|
||||
if (dataBuffer.includes(';') && !responseReceived) {
|
||||
responseReceived = true;
|
||||
this.tl1Connection.removeListener('data', dataHandler);
|
||||
|
||||
if (dataBuffer.includes('COMPLD')) {
|
||||
resolve({
|
||||
success: true,
|
||||
data: dataBuffer
|
||||
});
|
||||
} else if (dataBuffer.includes('DENY')) {
|
||||
resolve({
|
||||
success: false,
|
||||
error: 'Comando negado pelo equipamento'
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
success: false,
|
||||
error: 'Resposta inválida do equipamento'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.tl1Connection.on('data', dataHandler);
|
||||
|
||||
// Enviar comando TL1 com ID único
|
||||
const comandoComId = comando.replace(':1;', `:${this.commandId++};`);
|
||||
this.tl1Connection.write(comandoComId + '\r\n');
|
||||
|
||||
// Timeout para resposta
|
||||
setTimeout(() => {
|
||||
if (!responseReceived) {
|
||||
this.tl1Connection.removeListener('data', dataHandler);
|
||||
reject(new Error('Timeout aguardando resposta TL1'));
|
||||
}
|
||||
}, 10000);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao executar comando TL1', { comando, error: error.message });
|
||||
return {
|
||||
success: false,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter status de ONU via TL1
|
||||
*/
|
||||
async obterStatusONU(serial) {
|
||||
try {
|
||||
this.logger.debug('Obtendo status da ONU via TL1', { serial });
|
||||
|
||||
// Conectar ao UNM2000
|
||||
const conectado = await this.conectarUNM2000();
|
||||
if (!conectado) {
|
||||
throw new Error('Não foi possível conectar ao UNM2000');
|
||||
}
|
||||
|
||||
// Comando TL1 para buscar informações da ONU
|
||||
const comando = `RTRV-ONU::${serial}:1;`;
|
||||
this.logger.debug('Executando comando TL1', { comando });
|
||||
|
||||
const resultado = await this.executarComandoTL1(comando);
|
||||
|
||||
if (!resultado.success) {
|
||||
throw new Error(resultado.error || 'Falha na consulta TL1');
|
||||
}
|
||||
|
||||
// Processar resposta TL1 da ONU
|
||||
const statusONU = this.processarStatusONUTL1(resultado.data, serial);
|
||||
|
||||
this.logger.debug('Status ONU processado via TL1', statusONU);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
...statusONU
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao obter status da ONU via TL1', { serial, error: error.message });
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
serial
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter status da ONU por device name via TL1
|
||||
*/
|
||||
async obterStatusONUPorNome(deviceName) {
|
||||
try {
|
||||
this.logger.debug('Obtendo status da ONU por nome via TL1', { deviceName });
|
||||
|
||||
// Conectar ao UNM2000
|
||||
const conectado = await this.conectarUNM2000();
|
||||
if (!conectado) {
|
||||
throw new Error('Não foi possível conectar ao UNM2000');
|
||||
}
|
||||
|
||||
// Comando TL1 para buscar ONU por nome
|
||||
const comando = `RTRV-ONU::${deviceName}:1;`;
|
||||
this.logger.debug('Executando comando TL1', { comando });
|
||||
|
||||
const resultado = await this.executarComandoTL1(comando);
|
||||
|
||||
if (!resultado.success) {
|
||||
throw new Error(resultado.error || 'Falha na consulta TL1');
|
||||
}
|
||||
|
||||
// Processar resposta TL1 da ONU
|
||||
const statusONU = this.processarStatusONUTL1(resultado.data, deviceName);
|
||||
|
||||
this.logger.debug('Status ONU processado via TL1', statusONU);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
...statusONU
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao obter status da ONU por nome via TL1', { deviceName, error: error.message });
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
deviceName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processar resposta TL1 para status de ONU
|
||||
*/
|
||||
processarStatusONUTL1(dadosTL1, identificador) {
|
||||
try {
|
||||
// Processar dados TL1 - formato típico Fiberhome
|
||||
// Exemplo de resposta TL1: "COMPLD RTRV-ONU:OLT:ONU-1:1:SERIAL=FHTT12345678,..."
|
||||
|
||||
const resultado = {
|
||||
serial: identificador,
|
||||
deviceName: identificador,
|
||||
slot: 'N/A',
|
||||
pon: 'N/A',
|
||||
onuId: 'N/A',
|
||||
online: false,
|
||||
rxPower: 'N/A',
|
||||
txPower: 'N/A',
|
||||
distance: 'N/A',
|
||||
portasEthernet: [],
|
||||
servicosWAN: []
|
||||
};
|
||||
|
||||
if (!dadosTL1 || typeof dadosTL1 !== 'string') {
|
||||
return resultado;
|
||||
}
|
||||
|
||||
// Extrair informações básicas da resposta TL1
|
||||
if (dadosTL1.includes('COMPLD')) {
|
||||
resultado.online = true;
|
||||
|
||||
// Extrair serial se disponível
|
||||
const serialMatch = dadosTL1.match(/SERIAL=([A-Z0-9]+)/i);
|
||||
if (serialMatch) {
|
||||
resultado.serial = serialMatch[1];
|
||||
}
|
||||
|
||||
// Extrair device name se disponível
|
||||
const deviceMatch = dadosTL1.match(/DEVICENAME=([^,\s]+)/i);
|
||||
if (deviceMatch) {
|
||||
resultado.deviceName = deviceMatch[1];
|
||||
}
|
||||
|
||||
// Extrair posição (slot/pon/onu)
|
||||
const posicaoMatch = dadosTL1.match(/(\d+)-(\d+)-(\d+)/);
|
||||
if (posicaoMatch) {
|
||||
resultado.slot = posicaoMatch[1];
|
||||
resultado.pon = posicaoMatch[2];
|
||||
resultado.onuId = posicaoMatch[3];
|
||||
}
|
||||
|
||||
// Extrair informações de sinal
|
||||
const rxMatch = dadosTL1.match(/RXPOWER=([^,\s]+)/i);
|
||||
if (rxMatch) {
|
||||
resultado.rxPower = `${rxMatch[1]} dBm`;
|
||||
}
|
||||
|
||||
const txMatch = dadosTL1.match(/TXPOWER=([^,\s]+)/i);
|
||||
if (txMatch) {
|
||||
resultado.txPower = `${txMatch[1]} dBm`;
|
||||
}
|
||||
|
||||
const distMatch = dadosTL1.match(/DISTANCE=([^,\s]+)/i);
|
||||
if (distMatch) {
|
||||
resultado.distance = `${distMatch[1]} m`;
|
||||
}
|
||||
|
||||
// Simular algumas portas Ethernet (dados típicos de ONU)
|
||||
resultado.portasEthernet = [
|
||||
{ porta: 1, status: 'up' },
|
||||
{ porta: 2, status: 'down' },
|
||||
{ porta: 3, status: 'down' },
|
||||
{ porta: 4, status: 'down' }
|
||||
];
|
||||
|
||||
// Simular serviço WAN
|
||||
if (resultado.online) {
|
||||
resultado.servicosWAN = [
|
||||
{ id: 1, status: 'active', ip: '192.168.1.100' }
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.debug('Dados TL1 processados', resultado);
|
||||
return resultado;
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao processar dados TL1', { error: error.message });
|
||||
return {
|
||||
serial: identificador,
|
||||
deviceName: identificador,
|
||||
slot: 'N/A',
|
||||
pon: 'N/A',
|
||||
onuId: 'N/A',
|
||||
online: false,
|
||||
rxPower: 'N/A',
|
||||
txPower: 'N/A',
|
||||
distance: 'N/A',
|
||||
portasEthernet: [],
|
||||
servicosWAN: []
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Desconectar TL1
|
||||
*/
|
||||
async desconectar() {
|
||||
try {
|
||||
if (this.tl1Connection && !this.tl1Connection.destroyed) {
|
||||
this.logger.debug('Desconectando TL1');
|
||||
this.tl1Connection.end();
|
||||
this.tl1Connection = null;
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error('Erro ao desconectar TL1', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verificar se as conexões estão ativas
|
||||
*/
|
||||
verificarConexoes() {
|
||||
return {
|
||||
olt: false, // Não usamos mais OLT diretamente
|
||||
unm2000: this.tl1Connection && !this.tl1Connection.destroyed
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Diagnóstico completo de conectividade
|
||||
*/
|
||||
async diagnosticarConectividade() {
|
||||
const diagnostico = {
|
||||
olt: {
|
||||
host: 'N/A',
|
||||
port: 'N/A',
|
||||
conectividade: false,
|
||||
telnet: false,
|
||||
erro: 'Não utilizado - consultas via TL1'
|
||||
},
|
||||
unm2000: {
|
||||
host: this.config.unm2000.host,
|
||||
port: this.config.unm2000.port,
|
||||
conectividade: false,
|
||||
tl1: false,
|
||||
erro: null
|
||||
}
|
||||
};
|
||||
|
||||
// Testar UNM2000
|
||||
try {
|
||||
this.logger.debug('Testando conectividade UNM2000...');
|
||||
diagnostico.unm2000.conectividade = await this.testarConectividade(
|
||||
this.config.unm2000.host,
|
||||
this.config.unm2000.port
|
||||
);
|
||||
|
||||
if (diagnostico.unm2000.conectividade) {
|
||||
this.logger.debug('Testando login TL1 UNM2000...');
|
||||
diagnostico.unm2000.tl1 = await this.conectarUNM2000();
|
||||
}
|
||||
} catch (error) {
|
||||
diagnostico.unm2000.erro = error.message;
|
||||
this.logger.error('Erro no diagnóstico UNM2000:', error);
|
||||
}
|
||||
|
||||
return diagnostico;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FiberhomeManager;
|
||||
Reference in new issue
Block a user