From ca1b1661d179cb2778cf41c0ded08f58dadffe86 Mon Sep 17 00:00:00 2001
From: hongawen <83944980@qq.com>
Date: Wed, 6 Aug 2025 13:36:18 +0800
Subject: [PATCH] =?UTF-8?q?websocket=E4=BC=98=E5=8C=96=E8=B0=83=E6=95=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../SocketContrastResponseService.java | 1 +
.../handler/SocketDevResponseService.java | 2 +-
.../handler/SocketSourceResponseService.java | 2 +-
.../service/impl/PreDetectionServiceImpl.java | 45 +-
.../detection/util/socket/CnSocketUtil.java | 1 +
.../util/socket/WebServiceManager.java | 86 ----
.../util/socket/cilent/NettyClient.java | 2 +-
.../socket/cilent/NettyDevClientHandler.java | 2 +-
.../util/socket/web/WebSocketHandler.java | 169 --------
.../util/socket/web/WebSocketInitializer.java | 63 ---
.../util/socket/web/WebSocketService.java | 94 -----
.../socket/websocket/WebServiceManager.java | 261 ++++++++++++
.../socket/websocket/WebSocketConstants.java | 49 +++
.../socket/websocket/WebSocketHandler.java | 389 ++++++++++++++++++
.../websocket/WebSocketInitializer.java | 184 +++++++++
.../socket/websocket/WebSocketService.java | 237 +++++++++++
.../src/test/java/com/njcn/BaseJunitTest.java | 2 -
.../security/MyUserDetailsService.java | 1 -
.../user/user/controller/AuthController.java | 4 +-
19 files changed, 1170 insertions(+), 424 deletions(-)
delete mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java
delete mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java
delete mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketInitializer.java
delete mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketService.java
create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebServiceManager.java
create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketConstants.java
create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java
create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketInitializer.java
create mode 100644 detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketService.java
diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketContrastResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketContrastResponseService.java
index 834da0b0..4f0c960f 100644
--- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketContrastResponseService.java
+++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketContrastResponseService.java
@@ -15,6 +15,7 @@ import com.njcn.gather.detection.pojo.vo.SocketMsg;
import com.njcn.gather.detection.pojo.vo.WebSocketVO;
import com.njcn.gather.detection.util.DetectionUtil;
import com.njcn.gather.detection.util.socket.*;
+import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
import com.njcn.gather.device.pojo.vo.PreDetection;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.device.service.IPqStandardDevService;
diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java
index d47eee53..2f05bd39 100644
--- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java
+++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java
@@ -1,6 +1,5 @@
package com.njcn.gather.detection.handler;
-import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
@@ -19,6 +18,7 @@ import com.njcn.gather.detection.pojo.vo.*;
import com.njcn.gather.detection.service.impl.DetectionServiceImpl;
import com.njcn.gather.detection.util.DetectionUtil;
import com.njcn.gather.detection.util.socket.*;
+import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
import com.njcn.gather.device.pojo.enums.CommonEnum;
import com.njcn.gather.device.pojo.po.PqDevSub;
import com.njcn.gather.device.pojo.vo.PreDetection;
diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java
index d5e6cce4..37220161 100644
--- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java
+++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java
@@ -1,6 +1,5 @@
package com.njcn.gather.detection.handler;
-import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
@@ -14,6 +13,7 @@ import com.njcn.gather.detection.pojo.vo.WebSocketVO;
import com.njcn.gather.detection.util.socket.*;
import com.njcn.gather.detection.util.socket.cilent.NettyClient;
import com.njcn.gather.detection.util.socket.cilent.NettyDevClientHandler;
+import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
import com.njcn.gather.device.pojo.vo.PreDetection;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.script.pojo.po.SourceIssue;
diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java
index b7601ec2..d32a6178 100644
--- a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java
+++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java
@@ -22,8 +22,9 @@ import com.njcn.gather.detection.util.business.DetectionCommunicateUtil;
import com.njcn.gather.detection.util.socket.CnSocketUtil;
import com.njcn.gather.detection.util.socket.FormalTestManager;
import com.njcn.gather.detection.util.socket.SocketManager;
-import com.njcn.gather.detection.util.socket.WebServiceManager;
+import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
import com.njcn.gather.detection.util.socket.cilent.NettyClient;
+import com.njcn.web.utils.RequestUtil;
import com.njcn.gather.detection.util.socket.cilent.NettyContrastClientHandler;
import com.njcn.gather.detection.util.socket.cilent.NettySourceClientHandler;
import com.njcn.gather.device.pojo.po.PqDev;
@@ -146,8 +147,28 @@ public class PreDetectionServiceImpl implements PreDetectionService {
}
+ /**
+ * 发送源通信校验Socket连接(数字式和模拟式检测模式)
+ *
+ *
该方法用于建立与程控源设备的Socket连接,进行源通信校验。主要流程:
+ *
+ * - 1. 存储检测参数到全局管理器
+ * - 2. 根据计划ID获取计划源信息
+ * - 3. 获取源设备初始化参数
+ * - 4. 初始化设备和源响应服务列表
+ * - 5. 组装Socket请求报文
+ * - 6. 建立Netty客户端连接
+ *
+ *
+ * @param param 预检测参数,包含计划ID、用户ID等信息
+ * @throws BusinessException 当计划源信息不存在或源初始化参数为空时抛出
+ *
+ * @see SourceOperateCodeEnum#YJC_YTXJY 源通信校验操作码
+ * @see SourceOperateCodeEnum#INIT_GATHER 初始化采集操作码
+ */
private void sendYtxSocket(PreDetectionParam param) {
- WebServiceManager.addPreDetectionParam(param);
+ String loginName = RequestUtil.getLoginNameByToken();
+ WebServiceManager.addPreDetectionParam(loginName, param);
AdPlanSource planSource = adPlanSourceService.getOne(new LambdaQueryWrapper().eq(AdPlanSource::getPlanId, param.getPlanId()));
param.setSourceId(planSource.getSourceId());
if (ObjectUtil.isNotNull(planSource)) {
@@ -172,10 +193,28 @@ public class PreDetectionServiceImpl implements PreDetectionService {
}
}
+ /**
+ * 发送源通信校验Socket连接(仿真模式)
+ *
+ * 该方法专门用于仿真检测模式下的源通信校验。与普通模式的区别:
+ *
+ * - 直接使用传入的sourceId获取源初始化参数
+ * - 不需要通过计划ID查询计划源信息
+ * - 适用于独立的源设备通信测试
+ *
+ *
+ * @param param 预检测参数,必须包含sourceId和userPageId
+ * @throws BusinessException 当源初始化参数为空时抛出
+ *
+ * @see #sendYtxSocket(PreDetectionParam) 普通检测模式的源通信校验
+ * @see SourceOperateCodeEnum#YJC_YTXJY 源通信校验操作码
+ * @see SourceOperateCodeEnum#INIT_GATHER 初始化采集操作码
+ */
private void sendYtxSocketSimulate(PreDetectionParam param) {
SourceInitialize sourceParam = pqSourceService.getSourceInitializeParam(param.getSourceId());
param.setSourceId(sourceParam.getSourceId());
- WebServiceManager.addPreDetectionParam(param);
+ String loginName = RequestUtil.getLoginNameByToken();
+ WebServiceManager.addPreDetectionParam(loginName, param);
if (ObjectUtil.isNotNull(sourceParam)) {
SocketMsg socketMsg = new SocketMsg<>();
socketMsg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue());
diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java
index 76ee7074..954fd1e7 100644
--- a/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java
+++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/CnSocketUtil.java
@@ -6,6 +6,7 @@ import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
import com.njcn.gather.detection.pojo.vo.WebSocketVO;
+import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;
/**
* @Author: cdf
diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java
deleted file mode 100644
index be8ebf1f..00000000
--- a/detection/src/main/java/com/njcn/gather/detection/util/socket/WebServiceManager.java
+++ /dev/null
@@ -1,86 +0,0 @@
-package com.njcn.gather.detection.util.socket;
-
-import com.alibaba.fastjson.JSON;
-import com.njcn.gather.detection.pojo.param.PreDetectionParam;
-import com.njcn.gather.detection.pojo.vo.WebSocketVO;
-import io.netty.channel.Channel;
-import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.poi.ss.formula.functions.T;
-
-import java.time.LocalDateTime;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * @Description: webSocket存储的通道
- * @Author: wr
- * @Date: 2024/12/11 13:04
- */
-@Slf4j
-public class WebServiceManager {
-
- //key:页面 value:channel
- private static final Map userSessions = new ConcurrentHashMap<>();
-
- // 检测参数。key固定为preDetectionParam, value:检测参数
- private static final Map preDetectionParamMap = new ConcurrentHashMap<>();
-
- public static void addUser(String userId, Channel channel) {
- userSessions.put(userId, channel);
- }
-
-
- public static void removeChannel(String channelId) {
- // 遍历并删除
- Iterator> iterator = userSessions.entrySet().iterator();
- while (iterator.hasNext()) {
- Map.Entry entry = iterator.next();
- if (entry.getValue().id().toString().equals(channelId)) {
- iterator.remove();
- }
- }
- }
-
- public static Channel getChannelByUserId(String userId) {
- return userSessions.get(userId);
- }
-
- public static void sendMsg(String userId,String msg) {
- Channel channel = userSessions.get(userId);
- if(Objects.nonNull(channel) && channel.isActive()){
- TextWebSocketFrame wd = new TextWebSocketFrame(msg);
- channel.writeAndFlush(wd);
- }else {
- log.error("{}-websocket推送消息失败;当前用户-{}-客户端已经断开连接", LocalDateTime.now(),userId);
-// PreDetectionParam param = preDetectionParamMap.get("preDetectionParam");
-// CnSocketUtil.quitSend(param);
-// CnSocketUtil.quitSendSource(param);
- }
-
- }
-
- public static void sendMessage(String userId, WebSocketVO