diff --git a/BACKEND/app/services/line_connection.ts b/BACKEND/app/services/line_connection.ts index 39d5bd9..489c069 100644 --- a/BACKEND/app/services/line_connection.ts +++ b/BACKEND/app/services/line_connection.ts @@ -138,7 +138,7 @@ export default class LineConnection { this.client.on('close', () => { console.log(`🔌 Line ${lineNumber} disconnected`) this.config.status = 'disconnected' - this.config.inventory = undefined + // this.config.inventory = undefined this.socketIO.emit('line_disconnected', { stationId, lineId: id, diff --git a/BACKEND/providers/socket_io_provider.ts b/BACKEND/providers/socket_io_provider.ts index 581ebfd..7a31d54 100644 --- a/BACKEND/providers/socket_io_provider.ts +++ b/BACKEND/providers/socket_io_provider.ts @@ -110,6 +110,13 @@ export class WebSocketIo { socket.on('disconnect', () => { console.log(`FE disconnected: ${socket.id}`) this.userConnecting.delete(userId) + const listLineS = Array.from(this.lineMap.values()).map((el) => el.config) + listLineS.forEach((el) => { + if (el.userOpenCLI === userName) { + const line = this.lineMap.get(el.id) + if (line) line.userCloseCLI() + } + }) setTimeout(() => { io.emit('user_connecting', Array.from(this.userConnecting.values())) }, 200) @@ -559,7 +566,15 @@ export class WebSocketIo { const username = station.switch_control_username as string const password = station.switch_control_password as string - if (!ip || !port || !password) throw new Error(`Missing Switch configuration`) + if (!ip || !port || !password) { + socket.emit('switch_output', { + stationId: station.id, + portGroups: [], + status: 'DISCONNECTED', + message: `Missing Switch configuration`, + }) + throw new Error(`Missing Switch configuration`) + } // Tạo APC Controller instance const infoSwitch = new SwitchController({ diff --git a/FRONTEND/src/App.tsx b/FRONTEND/src/App.tsx index 2014b44..dc250a2 100644 --- a/FRONTEND/src/App.tsx +++ b/FRONTEND/src/App.tsx @@ -364,6 +364,17 @@ function App() { setSelectedLine(data); }; + useEffect(() => { + return () => { + console.log("CLEANUP SOCKET CLI", selectedLine); + if (selectedLine) + socket?.emit("close_cli", { + lineId: selectedLine?.id, + stationId: selectedLine?.station_id, + }); + }; + }, []); + return ( = ({ } }, [open]); + useEffect(() => { + if (loading) + setTimeout(() => { + setLoading(false); + }, 15000); + }, [loading]); + useEffect(() => { socket?.on("switch_output", (data) => { if (data.stationId !== stationAPI.id) return; diff --git a/FRONTEND/src/components/ModalTerminal.tsx b/FRONTEND/src/components/ModalTerminal.tsx index 3e7c3fd..c8d66fe 100644 --- a/FRONTEND/src/components/ModalTerminal.tsx +++ b/FRONTEND/src/components/ModalTerminal.tsx @@ -64,15 +64,15 @@ const ModalTerminal = ({ useEffect(() => { if ( typeof dataRequestTakeOver?.userName !== "undefined" && - line?.userEmailOpenCLI === user?.email && - dataRequestTakeOver?.userName !== user?.email + line?.userOpenCLI === user?.userName && + dataRequestTakeOver?.userName !== user?.userName ) { if (dataRequestTakeOver?.userName) { intervalTakeOverRef.current = setInterval(() => { socket?.emit("open_cli", { lineId: line?.id, stationId: line?.station_id, - userEmail: user?.email, + userEmail: user?.userName, userName: user?.userName, }); socket?.emit("request_take_over", { @@ -96,7 +96,7 @@ const ModalTerminal = ({ opened={opened} onClose={() => { onClose(); - if (line?.userEmailOpenCLI === user?.email) + if (line?.userOpenCLI === user?.userName) socket?.emit("close_cli", { lineId: line?.id, stationId: line?.station_id, @@ -133,7 +133,9 @@ const ModalTerminal = ({ }} > {line?.userOpenCLI - ? line?.userOpenCLI + " is using" + ? (line?.userOpenCLI === user?.userName + ? "You are" + : line?.userOpenCLI + " is") + " using" : "Terminal is used"} @@ -170,8 +172,8 @@ const ModalTerminal = ({ line_id={Number(line?.id)} station_id={Number(stationItem?.id)} isDisabled={ - typeof line?.userEmailOpenCLI !== "undefined" && - line?.userEmailOpenCLI !== user?.email + typeof line?.userOpenCLI !== "undefined" && + line?.userOpenCLI !== user?.userName } line_status={line?.status || ""} /> @@ -194,8 +196,8 @@ const ModalTerminal = ({ @@ -274,8 +276,8 @@ const ModalTerminal = ({ - {`${ - dataRequestTakeOver?.userName - ? `${dataRequestTakeOver?.userName} (${dataRequestTakeOver?.userEmail})` + {`${`${dataRequestTakeOver?.userName} ${ + dataRequestTakeOver?.userEmail + ? `(${dataRequestTakeOver?.userEmail})` : "" - } want to take over this line? ${ + }`} want to take over this line? ${ countDownRequest > 0 && typeof dataRequestTakeOver?.userName !== "undefined" && - line?.userEmailOpenCLI === user?.email + line?.userOpenCLI === user?.userName ? `(${countDownRequest}s)` : "" }`} diff --git a/FRONTEND/src/components/TerminalXTerm.tsx b/FRONTEND/src/components/TerminalXTerm.tsx index fb08b83..c23e159 100644 --- a/FRONTEND/src/components/TerminalXTerm.tsx +++ b/FRONTEND/src/components/TerminalXTerm.tsx @@ -123,7 +123,7 @@ const TerminalCLI: React.FC = ({ }, [cliOpened]); useEffect(() => { - if (cliOpened) { + if (cliOpened && isInit) { if (terminal.current) setTimeout(() => terminal.current?.write(content), 200);