shjung vor 2 Jahren
Ursprung
Commit
5824a64440

+ 6 - 0
pom.xml

@@ -36,6 +36,12 @@
 
     <dependencies>
 
+<!--        <dependency>-->
+<!--            <groupId>org.apache.clerezza.ext</groupId>-->
+<!--            <artifactId>org.json.simple</artifactId>-->
+<!--            <version>0.4</version>-->
+<!--        </dependency>-->
+
         <!-- Cache like map -->
         <dependency>
             <groupId>net.jodah</groupId>

+ 10 - 0
src/main/java/com/its/op/controller/ResourceMonitoringController.java

@@ -5,6 +5,7 @@ import com.its.op.dto.WebsocketMonitoringDto;
 import com.its.op.service.ResourceMonitoringService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
@@ -66,4 +67,13 @@ public class ResourceMonitoringController {
         List<WebsocketMonitoringDto> result = this.resourceMonitoringService.getWebsocketList();
         return ResponseEntity.ok(result);
     }
+    @ApiOperation(value = "웹소켓 디버그 조회", response = WebsocketMonitoringDto.class, responseContainer = "ArrayList")
+    @GetMapping(path="/websocket/debug/{flag}", produces = MediaType.APPLICATION_JSON_VALUE)
+    public ResponseEntity<String> getWebsocketDebug(
+            @ApiParam(name = "id", value = "디버그 설정(0 or 1", example = "0", required = true)
+            @PathVariable("flag") String flag
+    ) {
+        String result = this.resourceMonitoringService.getWebsocketDebug(flag);
+        return ResponseEntity.ok(result);
+    }
 }

+ 7 - 0
src/main/java/com/its/op/service/ResourceMonitoringService.java

@@ -128,4 +128,11 @@ public class ResourceMonitoringService {
         });
         return result;
     }
+
+    public String getWebsocketDebug(String flag) {
+        String before = this.itsWebSocketSessionManager.isDebug() ? "before: true" : "before: false";
+        this.itsWebSocketSessionManager.setDebug("1".equals(flag));
+        String after = this.itsWebSocketSessionManager.isDebug() ? "after: true" : "after: false";
+        return before + "\n" + after;
+    }
 }

+ 19 - 0
src/main/java/com/its/op/websocket/ItsWebSocketSessionManager.java

@@ -19,6 +19,7 @@ public class ItsWebSocketSessionManager {
     private volatile boolean serverRun;
     private Counter sessions;
     private ConcurrentHashMap<WebSocketSession, ItsWebSocketSession> sessionMap;
+    private boolean isDebug = false;
 
 //    public static ItsWebSocketSessionManager getInstance() {
 //        if (_instance == null) {
@@ -36,6 +37,15 @@ public class ItsWebSocketSessionManager {
         this.sessionMap = new ConcurrentHashMap<>();
     }
 
+
+    public boolean isDebug() {
+        return this.isDebug;
+    }
+
+    public void setDebug(boolean debug) {
+        this.isDebug = debug;
+    }
+
     public ConcurrentHashMap<WebSocketSession, ItsWebSocketSession> getMap() {
         return this.sessionMap;
     }
@@ -67,6 +77,9 @@ public class ItsWebSocketSessionManager {
     public int sendBroadcastMessage(String command, TextMessage message) {
         this.sessionMap.forEach((key, session) -> {
             session.sendMessage(command, message);
+            if (this.isDebug) {
+                log.info("sendBroadcastMessage: {}, to: {}, {}", command, session.getSession().getRemoteAddress(), message);
+            }
         });
         return this.sessionMap.size();
     }
@@ -77,6 +90,9 @@ public class ItsWebSocketSessionManager {
             if ("wall".equals(session.getGroupId())) {
                 session.sendMessage(command, message);
                 broadcastCount.getAndIncrement();
+                if (this.isDebug) {
+                    log.info("sendBroadcastSigPhaseMessage: {}, to: {}, {}", command, session.getSession().getRemoteAddress(), message);
+                }
             }
         });
         return broadcastCount.get();
@@ -88,6 +104,9 @@ public class ItsWebSocketSessionManager {
             if (!"wall".equals(session.getGroupId())) {
                 session.sendMessage(command, message);
                 broadcastCount.getAndIncrement();
+                if (this.isDebug) {
+                    log.info("sendBroadcastUserMessage: {}, to: {}, {}", command, session.getSession().getRemoteAddress(), message);
+                }
             }
         });
         return broadcastCount.get();

+ 42 - 0
src/test/java/com/its/op/ItsOpServerApplicationTests.java

@@ -28,6 +28,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
+
 import static org.assertj.core.api.Assertions.assertThat;
 
 @Slf4j
@@ -43,6 +44,47 @@ public class ItsOpServerApplicationTests {
     @Autowired
     private SqlSessionFactory sqlSessionFactory;
 
+    @Test
+    void testJsonParse() {
+        //String text = "{\"evt\":{\"uid\":6\tid:366773\tstat:\"4\"\ttm:\"20230412T162709.408\"\tetm:\"20230412T162710.531\"\ttype:7\tobj:{\"id\":2164885\ttype:\"2\"\tvattr:\"4\"\tbox:[0.336\t0.697\t0.081\t0.164]\tspeed:26.899}}}";
+        //String text = "{\"evt\":{\"uid\":6,id:366773,stat:\"4\",tm:\"20230412T162709.408\",etm:\"20230412T162710.531\",type:7,obj:{\"id\":2164885,type:\"2\",vattr:\"4\",box:[0.336,0.697,0.081,0.164],speed:26.899}}}";
+        String text = "{\"evt\":{\"uid\":6,\"id\":366773,\"stat\":\"4\",\"tm\":\"20230412T162709.408\",\"etm\":\"20230412T162710.531\",\"type\":7,\"obj\":{\"id\":2164885,\"type\":\"2\",\"vattr\":\"4\",\"box\":[0.336,0.697,0.081,0.164],\"speed\":26.899}}}";
+//        JSONParser jsonParser = new JSONParser();
+//        JSONObject jsonObject = null;
+//        try {
+//            jsonObject = (JSONObject)jsonParser.parse(text);
+//        } catch (ParseException e) {
+//            log.error("XXXXXXXXXXXXXXX: {}", e.toString());
+//            return;
+//        }
+//        log.info("{}", jsonObject.toString());
+//        Iterator<String> iterator = jsonObject.keySet().iterator();
+//        while(iterator.hasNext()) {
+//            String key = iterator.next();
+//            log.info("{}", key);
+//            JSONObject objList = (JSONObject)jsonObject.get(key);
+//            log.info("{}", objList);
+//            if ("evt".equals(key)) {
+//                log.info("Receive Event Data");
+//                String uid = objList.get("uid").toString();
+//                String stat = objList.get("stat").toString();
+//                String tm = objList.get("tm").toString();
+//                String id = objList.get("id").toString();
+//                String etm = objList.get("etm").toString();
+//                String type = objList.get("type").toString();
+//                log.info("uid: {}", uid);
+//                log.info("stat: {}", stat);
+//                log.info("tm: {}", tm);
+//                log.info("id: {}", id);
+//                log.info("etm: {}", etm);
+//                log.info("type: {}", type);
+//                if ("7".equals(type) || "13".equals(type)) {
+//                    JSONObject itemList = (JSONObject) objList.get("obj");
+//                    log.info("obj: {}", itemList.toString());
+//                }
+//            }
+//        }
+    }
     @Test
     void status_check_return_ok() {
         TestRestTemplate rest = new TestRestTemplate();