From 1d0a90f2cf7da88a4b3d0fb86946306253ea774b Mon Sep 17 00:00:00 2001 From: nguyentrungthat <80239428+nguentrungthat@users.noreply.github.com> Date: Mon, 15 Dec 2025 11:33:45 +0700 Subject: [PATCH] Update config scenarios --- .../app/controllers/scenarios_controller.ts | 1 + BACKEND/app/services/line_connection.ts | 89 +++++++----- BACKEND/providers/socket_io_provider.ts | 25 ++-- FRONTEND/src/components/BottomToolBar.tsx | 135 ++++++++++++++---- FRONTEND/src/components/ButtonAction.tsx | 66 +++++++-- FRONTEND/src/components/CardLine.tsx | 15 ++ .../src/components/Modal/ModalScenario.tsx | 56 +++++--- FRONTEND/src/untils/types.ts | 2 + 8 files changed, 290 insertions(+), 99 deletions(-) diff --git a/BACKEND/app/controllers/scenarios_controller.ts b/BACKEND/app/controllers/scenarios_controller.ts index 7429a94..8c1a870 100644 --- a/BACKEND/app/controllers/scenarios_controller.ts +++ b/BACKEND/app/controllers/scenarios_controller.ts @@ -42,6 +42,7 @@ export default class ScenariosController { body: JSON.stringify(payload.body), timeout: payload.timeout, isReboot: payload.isReboot, + send_result: payload.send_result, }, { client: trx } ) diff --git a/BACKEND/app/services/line_connection.ts b/BACKEND/app/services/line_connection.ts index 567a920..ee51bfb 100644 --- a/BACKEND/app/services/line_connection.ts +++ b/BACKEND/app/services/line_connection.ts @@ -339,13 +339,20 @@ export default class LineConnection { console.log( `Run scenario "${script?.title}" to line ${this.config.lineNumber} of ${this.config.stationName}` ) - if (script?.title === 'DPELP') this.dataDPELP = '' this.isRunningScript = true this.socketIO.emit('running_scenario', { stationId: this.config.stationId, lineId: this.config.id, title: script?.title, }) + if (script?.send_result) this.dataDPELP = '' + if (script?.isReboot) { + await sleep(10000) + for (let index = 0; index < 30; index++) { + await sleep(1000) + this.breakSpam() + } + } const now = Date.now() this.outputScenario += `\n\n---start-scenarios---${now}---${userName}---${script?.title}---\n---scenario---${script?.title}---${now}---\n` appendLog( @@ -363,31 +370,34 @@ export default class LineConnection { let stepIndex = 0 return new Promise((resolve, reject) => { - const timeoutTimer = setTimeout(() => { - this.isRunningScript = false - this.socketIO.emit('running_scenario', { - stationId: this.config.stationId, - lineId: this.config.id, - title: '', - }) - this.outputBuffer = '' - this.outputScenario = '' - this.config.output += 'Timeout run scenario' - this.socketIO.emit('line_output', { - stationId: this.config.stationId, - lineId: this.config.id, - data: 'Timeout run scenario', - }) - this.outputScenario += `\n---end-scenarios---${now}---${userName}---\n` - appendLog( - `\n---end-scenarios---${now}---${userName}---\n`, - this.config.stationId, - this.config.stationName, - this.config.stationIp, - this.config.lineNumber - ) - // reject(new Error('Script timeout')) - }, script.timeout || 300000) + const timeoutTimer = setTimeout( + () => { + this.isRunningScript = false + this.socketIO.emit('running_scenario', { + stationId: this.config.stationId, + lineId: this.config.id, + title: '', + }) + this.outputBuffer = '' + this.outputScenario = '' + this.config.output += 'Timeout run scenario' + this.socketIO.emit('line_output', { + stationId: this.config.stationId, + lineId: this.config.id, + data: 'Timeout run scenario', + }) + this.outputScenario += `\n---end-scenarios---${now}---${userName}---\n` + appendLog( + `\n---end-scenarios---${now}---${userName}---\n`, + this.config.stationId, + this.config.stationName, + this.config.stationIp, + this.config.lineNumber + ) + // reject(new Error('Script timeout')) + }, + script.timeout ? Number(script.timeout) * 1000 : 300000 + ) const runStep = async (index: number) => { if (index >= steps.length) { @@ -447,11 +457,13 @@ export default class LineConnection { }, data, }) - if (script?.title === 'DPELP') this.dataDPELP = result - console.log( - `DPELP DATA line ${this.config.lineNumber} of ${this.config.stationName}:`, - this.dataDPELP - ) + if (script?.send_result) { + this.dataDPELP = result + console.log( + `DPELP DATA line ${this.config.lineNumber} of ${this.config.stationName}:`, + this.dataDPELP + ) + } if (this.config.latestScenario) this.config.latestScenario = { ...this.config.latestScenario, detectAI: detectLog } this.config.data = data @@ -473,6 +485,7 @@ export default class LineConnection { const step = steps[index] let repeatCount = Number(step.repeat) || 1 + const delay = step?.delay ? Number(step?.delay) * 1000 : 1000 const sendCommand = async () => { if (repeatCount <= 0) { // Done → next step @@ -493,12 +506,12 @@ export default class LineConnection { } repeatCount-- - setTimeout(() => sendCommand(), Number(step?.delay) || 500) + setTimeout(() => sendCommand(), delay) } // Nếu expect rỗng → gửi ngay if (!step?.expect || step?.expect.trim() === '') { - setTimeout(() => sendCommand(), Number(step?.delay) || 500) + setTimeout(() => sendCommand(), delay) return } @@ -506,12 +519,15 @@ export default class LineConnection { // await sleep(200) // if (this.outputBuffer.includes(step.expect)) { // this.outputBuffer = '' - // setTimeout(() => sendCommand(), Number(step?.delay) || 500) + // setTimeout(() => sendCommand(), delay) // } // } - const matched = await this.waitForExpect(step.expect, Number(step?.timeout) || 60000) - if (matched) setTimeout(() => sendCommand(), Number(step?.delay) || 500) + const matched = await this.waitForExpect( + step.expect.trim(), + script?.timeout ? Number(script?.timeout) * 1000 : 60000 + ) + if (matched) setTimeout(() => sendCommand(), delay) } runStep(stepIndex) @@ -562,6 +578,7 @@ export default class LineConnection { waitForExpect = async (expect: string, timeout = 60000) => { const start = Date.now() + console.log('[EXPECT]', expect, timeout) while (Date.now() - start < timeout) { if (this.outputBuffer.includes(expect)) { this.outputBuffer = '' diff --git a/BACKEND/providers/socket_io_provider.ts b/BACKEND/providers/socket_io_provider.ts index 1a7b670..b7b34b5 100644 --- a/BACKEND/providers/socket_io_provider.ts +++ b/BACKEND/providers/socket_io_provider.ts @@ -572,22 +572,23 @@ export class WebSocketIo { const linkWiki = process.env.LINK_WIKI || 'https://logs.danielvu.com/api/wiki/page/insert?title=Dev_test' - await axios.post(linkWiki, { - data: tableHTML, - titleAuto: `[DPELP] - ${stationName} - ` + dataFormat, - }) + // await axios.post(linkWiki, { + // data: tableHTML, + // titleAuto: `[DPELP] - ${stationName} - ` + dataFormat, + // }) await sendMessageToMail( 'andrew.ng@apactech.io', `[DPELP] - ${stationName} - ${dataFormat}`, - tableHTML, - ['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io'] - ) - await sendMessageToZulip( - 'stream', - 'ATC_Report', - station.name, - `\n\n---\n**[DPELP] - ${stationName} - ${dataFormat}**\n\n` + zulipMess + tableHTML + // , + // ['ips@ipsupply.com.au', 'kay@ipsupply.com.au', 'joseph@apactech.io'] ) + // await sendMessageToZulip( + // 'stream', + // 'ATC_Report', + // station.name, + // `\n\n---\n**[DPELP] - ${stationName} - ${dataFormat}**\n\n` + zulipMess + // ) } catch (error) { console.log(error) } diff --git a/FRONTEND/src/components/BottomToolBar.tsx b/FRONTEND/src/components/BottomToolBar.tsx index c9ff045..85e2723 100644 --- a/FRONTEND/src/components/BottomToolBar.tsx +++ b/FRONTEND/src/components/BottomToolBar.tsx @@ -59,6 +59,7 @@ const ScenarioCard = ({ socket, setOpenScenarioModal, setIsDisable, + station, }: { scenario: IScenario; index: number; @@ -68,6 +69,7 @@ const ScenarioCard = ({ socket: Socket | null; setOpenScenarioModal: (value: boolean) => void; setIsDisable: (value: boolean) => void; + station: TStation; }) => { const [isHovered, setIsHovered] = useState(false); const [overlayPosition, setOverlayPosition] = useState({ top: 0, left: 0 }); @@ -203,6 +205,45 @@ const ScenarioCard = ({ setIsDisable(false); }, 5000); + if (scenario?.isReboot || scenario?.is_reboot) { + const lineApc1 = selectedLines + ?.filter( + (el) => + el.outlet && + Number(el.outlet) > 0 && + (el.apcName === "apc_1" || el.apc_name === "apc_1") + ) + ?.map((el) => el.outlet); + const lineApc2 = selectedLines + ?.filter( + (el) => + el.outlet && + Number(el.outlet) > 0 && + (el.apcName === "apc_2" || el.apc_name === "apc_2") + ) + ?.map((el) => el.outlet); + if (lineApc1.length > 0) + socket?.emit("control_apc", { + outletNumbers: lineApc1, + station: { ...station, lines: [] }, + action: "restart", + apcName: "apc_1", + }); + if (lineApc2.length > 0) + socket?.emit("control_apc", { + outletNumbers: lineApc2, + station: { ...station, lines: [] }, + action: "restart", + apcName: "apc_2", + }); + } + if (scenario?.send_result) + socket?.emit("run_all_dpelp", { + lineIds: selectedLines?.map((el) => el.id), + stationName: station.name, + stationId: Number(station.id), + }); + selectedLines .filter( (el) => @@ -213,7 +254,13 @@ const ScenarioCard = ({ socket?.emit( "run_scenario", Object.assign(el, { - scenario: scenario, + scenario: { + ...scenario, + isReboot: + typeof scenario?.isReboot !== "undefined" + ? scenario?.isReboot + : scenario?.is_reboot, + }, }) ); }); @@ -347,7 +394,6 @@ const BottomToolBar = ({ return ( <> {/* Modal chọn Scenario - Custom Simple Modal */} - )} - + - + {isExpand ? ( { socket?.emit("open_cli", { lineId: line.id, - stationId: line.stationId || line.station_id, + stationId: + line.stationId || line.station_id, userEmail: user?.email, userName: user?.userName, }); @@ -557,7 +610,8 @@ const BottomToolBar = ({ selectedLines.forEach((line) => { socket?.emit("close_cli", { lineId: line?.id, - stationId: line.stationId || line.station_id, + stationId: + line.stationId || line.station_id, }); }); setSelectedLines([]); @@ -615,6 +669,9 @@ const BottomToolBar = ({ socket={socket} selectedLines={selectedLines} isDisable={isDisable || selectedLines.length === 0} + dataDPELP={scenarios?.find( + (el) => el.title.toUpperCase() === "DPELP" + )} onClick={() => { if ( selectedLines.length > 0 @@ -665,7 +722,13 @@ const BottomToolBar = ({ flexDirection: "column", }} > - + ) : ( - - + + {/* Danh sách Line - chiếm 1 phần, cố định width và height, hiển thị 2 hàng */} - + {selectedLines.map((el) => ( - + Line {el.lineNumber} @@ -766,8 +835,7 @@ const BottomToolBar = ({ selectedLines.forEach((line) => { socket?.emit("close_cli", { lineId: line?.id, - stationId: - line.stationId || line.station_id, + stationId: line.stationId || line.station_id, }); }); setSelectedLines([]); @@ -780,15 +848,18 @@ const BottomToolBar = ({ )} - + {/* Selected count - ở giữa */} - + Selected: {selectedLines.length} /{" "} {station.lines.length} - + {/* Input + Select All - chiếm 1 phần */} {/* Input */} - + - + {/* Select All */} - + - + {/* Content */}
))} @@ -977,7 +1060,7 @@ const BottomToolBar = ({
)} - + {/* Drawer Scenario để Add/Edit */} void; selectedLines: TLine[]; className?: string; + dataDPELP?: IScenario; }) => { return (