From 55a0e8a2f7b4e0e38f450fe3d578ca3b0c9038fe Mon Sep 17 00:00:00 2001 From: Rodrigo Martins Date: Sat, 25 Apr 2026 21:03:20 -0300 Subject: [PATCH] diagnostico: fix uptime Signed-off-by: Rodrigo Martins --- src/fiberhome/manager.js | 73 +++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/src/fiberhome/manager.js b/src/fiberhome/manager.js index 51833fe..09607ad 100644 --- a/src/fiberhome/manager.js +++ b/src/fiberhome/manager.js @@ -683,31 +683,58 @@ class FiberhomeManager { resultado.online = false; } - // Extrair LASTONTIME e calcular uptime - // Formato: "YYYY-MM-DD HH:MM:SS" ou "YYYY-MM-DD HH-MM-SS" (UTC+8 / Beijing) - const lastonMatch = dados.match(/LASTONTIME[\s\S]{0,80}?(\d{4}-\d{2}-\d{2}[\s]\d{2}[:\-]\d{2}[:\-]\d{2})/i); - if (lastonMatch) { - try { - // Normalizar separadores de tempo para ":" - const normalizado = lastonMatch[1].replace(/(\d{2})-(\d{2})-(\d{2})$/, '$1:$2:$3'); - // UNM2000 usa horário de Beijing (UTC+8) por padrão - const dtOnline = new Date(normalizado + '+08:00'); - const diffMs = Date.now() - dtOnline.getTime(); - if (!isNaN(dtOnline.getTime()) && diffMs >= 0) { - const totalMin = Math.floor(diffMs / 60000); - const dias = Math.floor(totalMin / 1440); - const horas = Math.floor((totalMin % 1440) / 60); - const mins = totalMin % 60; - resultado.uptime = dias > 0 - ? `${dias}d ${String(horas).padStart(2,'0')}:${String(mins).padStart(2,'0')}` - : `${String(horas).padStart(2,'0')}:${String(mins).padStart(2,'0')}`; - console.log(`[TL1-DIAG] ✅ LASTONTIME="${lastonMatch[1]}" → uptime="${resultado.uptime}"`); + // Calcular uptime a partir de LASTOFFTIME (tab-separated) + // O timestamp do servidor (linha de header do TL1) é usado como "agora" + // para evitar divergência de fuso — tudo no horário local do UNM2000. + try { + const linhasState = dados.replace(/\r/g, '').split('\n'); + + // Timestamp do servidor: linha "FH_xxx.xxx.xxx.xxx YYYY-MM-DD HH:MM:SS" + const srvMatch = dados.match(/FH_[\w.]+\s+(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})/); + const serverNow = srvMatch ? new Date(srvMatch[1]) : new Date(); + console.log(`[TL1-DIAG] serverNow="${srvMatch ? srvMatch[1] : 'Date.now()'}"`); + + // Header tab-separado: ONUID \t AdminState \t OperState \t ... \t LASTOFFTIME + const headerLine = linhasState.find(l => /\bONUID\b/i.test(l) && /\bLASTOFFTIME\b/i.test(l)); + if (headerLine) { + const headers = headerLine.trim().split('\t'); + const lastOffIdx = headers.findIndex(h => /LASTOFFTIME/i.test(h)); + console.log(`[TL1-DIAG] LASTOFFTIME col index=${lastOffIdx}`); + + // Linha de dados: tem \tUP\t e não é header/separador + const dataLine = linhasState.find(l => + l.trim() && + !/^[-\s]+$/.test(l) && + !/COMPLD|total_blocks|block_number|list of ONU|FH_/i.test(l) && + /\tUP\t/i.test(l) + ); + console.log(`[TL1-DIAG] dataLine: ${JSON.stringify(dataLine)}`); + + if (dataLine && lastOffIdx !== -1) { + const valores = dataLine.trim().split('\t'); + const lastOffStr = valores[lastOffIdx]; + console.log(`[TL1-DIAG] LASTOFFTIME="${lastOffStr}"`); + + if (lastOffStr && /\d{4}-\d{2}-\d{2}/.test(lastOffStr)) { + const dtOff = new Date(lastOffStr); + const diffMs = serverNow - dtOff; + if (!isNaN(dtOff) && diffMs >= 0) { + const totalMin = Math.floor(diffMs / 60000); + const dias = Math.floor(totalMin / 1440); + const horas = Math.floor((totalMin % 1440) / 60); + const mins = totalMin % 60; + resultado.uptime = dias > 0 + ? `${dias}d ${String(horas).padStart(2,'0')}:${String(mins).padStart(2,'0')}` + : `${String(horas).padStart(2,'0')}:${String(mins).padStart(2,'0')}`; + console.log(`[TL1-DIAG] ✅ uptime="${resultado.uptime}"`); + } + } } - } catch (e) { - console.log(`[TL1-DIAG] ⚠️ Falha ao calcular uptime: ${e.message}`); + } else { + console.log('[TL1-DIAG] ⚠️ Header com LASTOFFTIME não encontrado na resposta LST-ONUSTATE'); } - } else { - console.log('[TL1-DIAG] ⚠️ LASTONTIME não encontrado na resposta LST-ONUSTATE'); + } catch (e) { + console.log(`[TL1-DIAG] ⚠️ Falha ao calcular uptime: ${e.message}`); } }