diff --git a/BACKEND/app/services/line_connection.ts b/BACKEND/app/services/line_connection.ts index 9702f03..cd8741e 100644 --- a/BACKEND/app/services/line_connection.ts +++ b/BACKEND/app/services/line_connection.ts @@ -94,6 +94,7 @@ export default class LineConnection { this.client.on('data', (data) => { if (this.connecting) return let message = data.toString() + let rawData = '' if (this.isRunningScript) { this.waitingScenario = true this.outputBuffer += message @@ -111,9 +112,10 @@ export default class LineConnection { this.config.output = this.config.output.slice(0, -1) // message = message.slice(0, -1) } else { - this.config.output += cleanData(char) + rawData += char } } + this.config.output += cleanData(rawData) this.config.output = this.config.output.slice(-15000) this.socketIO.emit('line_output', { stationId, diff --git a/BACKEND/app/ultils/helper.ts b/BACKEND/app/ultils/helper.ts index 8523a5f..f32463c 100644 --- a/BACKEND/app/ultils/helper.ts +++ b/BACKEND/app/ultils/helper.ts @@ -7,12 +7,23 @@ import path from 'node:path' * @returns {string} - The cleaned data. */ export const cleanData = (data: string) => { - return data - .replace(/--More--\s*BS\s*BS\s*BS\s*BS\s*BS\s*BS/g, '') - .replace(/\s*--More--\s*/g, '') - .replace(/\x1b\[[0-9;]*m/g, '') // Remove ANSI escape codes - .replace(/\x08/g, '') - .replace(/[^\x20-\x7E\r\n]/g, '') // Remove non-printable characters + return ( + data + // 1️⃣ Xóa chuỗi "--More--" (Cisco/Unix pager) + .replace(/--More--[\s\x08\x1b\[K]*/g, '') + + // 2️⃣ Xóa toàn bộ chuỗi ANSI escape sequences + // Ví dụ: ESC[2J, ESC[K, ESC[?25h, ESC[0m, ... + .replace(/\x1B\[[0-9;?]*[A-Za-z]/g, '') + + // 3️⃣ Xóa ký tự Backspace (BS) hoặc Delete (DEL) + .replace(/[\x08\x7F]/g, '') + + // 4️⃣ Xóa ký tự NUL và các control char khác (trừ \r, \n, \t) + .replace(/[^\x09\x0A\x0D\x20-\x7E]/g, '') + ) + + // 5️⃣ Chuẩn hóa xuống dòng nếu cần // .replace(/\r\n/g, '\n') }