Skip to content

Commit 7e17717

Browse files
committed
#31 FE : [FEAT] 전력제어 모달창 구현
1 parent 8a3d8bf commit 7e17717

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

client/src/pages/Device.tsx

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React, { useEffect, useState } from "react";
2+
import { Client } from "@stomp/stompjs";
3+
import SockJS from "sockjs-client";
24
import { FaImages, FaBuilding } from "react-icons/fa";
35
import Sidebar from "./Sidebar";
46
import "./Device.css";
@@ -16,16 +18,44 @@ const Device: React.FC = () => {
1618
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
1719
const [isLayoutModalOpen, setIsLayoutModalOpen] = useState(false);
1820
const [isCommandModalOpen, setIsCommandModalOpen] = useState(false);
19-
const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false); // 성공 모달 상태
21+
const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false);
2022
const [currentDeviceName, setCurrentDeviceName] = useState<string>("");
2123
const [command, setCommand] = useState<boolean>(true);
24+
const [stompClient, setStompClient] = useState<Client | null>(null);
2225

2326
const layoutImage =
2427
"https://github.com/CSID-DGU/2024-1-CECD1-1921-3/blob/develop/data/IoTImg/%EB%B0%B0%EC%B9%98%EB%8F%84-%EC%8B%A0%EA%B3%B5%ED%95%99%EA%B4%805145%ED%98%B8.png?raw=true";
2528

2629
const [building, setBuilding] = useState<string>("신공학관");
2730
const [room, setRoom] = useState<string>("5145호");
2831

32+
// WebSocket 초기화
33+
useEffect(() => {
34+
const client = new Client({
35+
webSocketFactory: () => new SockJS("https://www.dgu1921.p-e.kr/ws"),
36+
onConnect: () => {
37+
console.log("WebSocket connected");
38+
client.subscribe("/topic/commands", (message) => {
39+
const data = JSON.parse(message.body);
40+
console.log("Received message via WebSocket:", data);
41+
setCurrentDeviceName(data.deviceName);
42+
setCommand(data.command);
43+
setIsCommandModalOpen(true); // WebSocket 수신 시 모달창 표시
44+
});
45+
},
46+
onDisconnect: () => {
47+
console.log("WebSocket disconnected");
48+
},
49+
});
50+
51+
client.activate();
52+
setStompClient(client);
53+
54+
return () => {
55+
client.deactivate();
56+
};
57+
}, []);
58+
2959
// IoT 기기 데이터 가져오기
3060
useEffect(() => {
3161
fetch(
@@ -44,9 +74,26 @@ const Device: React.FC = () => {
4474
setIsLayoutModalOpen(false);
4575
};
4676

77+
const sendWebSocketCommand = (deviceName: string) => {
78+
if (stompClient && stompClient.connected) {
79+
const payload = {
80+
deviceName: deviceName,
81+
command: command,
82+
};
83+
stompClient.publish({
84+
destination: "/app/socket/control",
85+
body: JSON.stringify(payload),
86+
});
87+
console.log("Command sent via WebSocket:", payload);
88+
} else {
89+
console.error("WebSocket is not connected.");
90+
}
91+
};
92+
4793
const openCommandModal = (deviceName: string) => {
4894
setCurrentDeviceName(deviceName);
4995
setIsCommandModalOpen(true);
96+
sendWebSocketCommand(deviceName); // WebSocket 메시지 전송
5097
};
5198

5299
const closeCommandModal = () => {
@@ -178,7 +225,7 @@ const Device: React.FC = () => {
178225
onClick={(e) => e.stopPropagation()}
179226
>
180227
<p>
181-
현재 '<strong className="device-name">{currentDeviceName}</strong>
228+
현재 '<strong className="device-name">{currentDeviceName || "스마트 IoT 기기"}</strong>
182229
' 는 {command ? "ON" : "OFF"} 상태입니다. 제어 명령을 전송하시겠습니까?
183230
</p>
184231
<div className="command-toggle">

0 commit comments

Comments
 (0)