From 694f12bc29973dd2ed60a8b654c1d5cca395097e Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Wed, 10 Sep 2025 08:26:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=95=E6=B3=A2=E5=8A=9F=E8=83=BD=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SocketContrastResponseService.java | 59 ++++++++++------- .../service/impl/DetectionServiceImpl.java | 3 + .../detection/util/socket/SocketManager.java | 66 ++++++++++--------- .../util/socket/cilent/NettyClient.java | 6 +- .../cilent/NettyContrastClientHandler.java | 4 +- .../socket/websocket/WebSocketHandler.java | 6 +- .../device/mapper/mapping/PqDevMapper.xml | 4 +- .../mapper/mapping/PqStandardDevMapper.xml | 4 +- .../njcn/gather/device/pojo/po/PqDevSub.java | 15 ----- .../gather/device/pojo/vo/PreDetection.java | 3 + .../gather/monitor/pojo/po/PqMonitor.java | 15 +++++ .../plan/controller/AdPlanController.java | 2 +- .../gather/plan/pojo/param/AdPlanParam.java | 1 + .../gather/plan/service/IAdPlanService.java | 4 +- .../plan/service/impl/AdPlanServiceImpl.java | 17 ++--- .../gather/result/pojo/param/ResultParam.java | 8 +++ .../gather/result/pojo/vo/FormContentVO.java | 5 ++ .../service/impl/ResultServiceImpl.java | 64 +++++++++++++----- .../gather/type/pojo/param/DevTypeParam.java | 3 + .../com/njcn/gather/type/pojo/po/DevType.java | 5 ++ .../service/ContrastHarmonicService.java | 9 +-- .../service/ContrastNonHarmonicService.java | 20 ++++-- .../impl/ContrastHarmonicServiceImpl.java | 26 ++++++-- .../impl/ContrastNonHarmonicServiceImpl.java | 26 ++++++-- 24 files changed, 245 insertions(+), 130 deletions(-) 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 a749be61..dbb0a0d4 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 @@ -1013,7 +1013,8 @@ public class SocketContrastResponseService { webSend.setCode(SourceResponseCodeEnum.ALL_SUCCESS.getCode());//最终成功推送 WebServiceManager.sendMsg(param.getUserPageId(), JSON.toJSONString(webSend)); // 修改设备状态 - pqDevService.updateResult(true, param.getDevIds(), adTypes, code, param.getUserId(), param.getTemperature(), param.getHumidity()); + + pqDevService.updateResult(true, param.getDevIds(), adTypes, code, param.getUserId(), param.getTemperature(), param.getHumidity()); } } } @@ -1121,7 +1122,7 @@ public class SocketContrastResponseService { FormalTestManager.pairsIpMap, FormalTestManager.devIdMapComm, FormalTestManager.testItemMap.keySet().stream().collect(Collectors.toList()), - errorSysId, DictDataEnum.Own_value, FormalTestManager.numMap, code, FormalTestManager.waveNum); + errorSysId, FormalTestManager.dataRule, FormalTestManager.numMap, code, FormalTestManager.waveNum); Map> deviceIdMap = allResultList.stream().collect(Collectors.groupingBy(DevLineTestResult::getDeviceId, Collectors.toList())); deviceIdMap.forEach((deviceId, devLineTestResultList) -> { @@ -1190,7 +1191,7 @@ public class SocketContrastResponseService { List adTypes = FormalTestManager.testItemMap.values().stream().collect(Collectors.toList()); // 修改设备状态 - pqDevService.updateResult(true, param.getDevIds(), adTypes, code, param.getUserId(), param.getTemperature(), param.getHumidity()); + //pqDevService.updateResult(true, param.getDevIds(), adTypes, code, param.getUserId(), param.getTemperature(), param.getHumidity()); // 最终将检测结果推送到前端 webSend.setCode(SourceResponseCodeEnum.ALL_SUCCESS.getCode()); @@ -2177,7 +2178,8 @@ public class SocketContrastResponseService { WaveCommandDTO waveCommandDTO = new WaveCommandDTO(); waveCommandDTO.setIp(preDetection.getDevIP()); waveCommandDTO.setPort(preDetection.getPort()); - waveCommandDTO.setOper("RDRE1$CO$RcdTrg$Oper"); + waveCommandDTO.setOper(preDetection.getWaveCmd()); +// waveCommandDTO.setOper("RDRE1$CO$RcdTrg$Oper"); waveCommandDTO.setLine(monitorListDTO.getLine()); waveCommandDTOList.add(waveCommandDTO); }); @@ -2187,7 +2189,8 @@ public class SocketContrastResponseService { WaveCommandDTO waveCommandDTO = new WaveCommandDTO(); waveCommandDTO.setIp(preDetection.getDevIP()); waveCommandDTO.setPort(preDetection.getPort()); - waveCommandDTO.setOper("RDRE1$CO$RcdTrg$Oper"); + waveCommandDTO.setOper(preDetection.getWaveCmd()); +// waveCommandDTO.setOper("RDRE1$CO$RcdTrg$Oper"); waveCommandDTO.setLine(monitorListDTO.getLine()); waveCommandDTOList.add(waveCommandDTO); }); @@ -2199,12 +2202,12 @@ public class SocketContrastResponseService { } private List> analyzeWaveFile(WaveResultDTO devWaveResultDTO, WaveResultDTO stdDevWaveResultDTO, PreDetection.MonitorListDTO monitorListDTO, String devMonitorId, String stdDevMonitorId, List adTypeCodeList) { -// String stdDevCfgPath = stdDevWaveResultDTO.getPath().replaceAll("\\\\", "/").replace(".CFG",".cfg"); -// String devCfgPath = devWaveResultDTO.getPath().replaceAll("\\\\", "/").replace(".CFG",".cfg"); + String stdDevCfgPath = stdDevWaveResultDTO.getPath().replaceAll("\\\\", "/").replace(".CFG", ".cfg"); + String devCfgPath = devWaveResultDTO.getPath().replaceAll("\\\\", "/").replace(".CFG", ".cfg"); - String stdDevCfgPath = "D:\\数据比对\\统计数据1\\B码\\216\\PQMonitor_PQM1_000001_20200430_113404_845.cfg"; +// String stdDevCfgPath = "D:\\数据比对\\统计数据1\\B码\\216\\PQMonitor_PQM1_000001_20200430_113404_845.cfg"; String stdDevDatPath = stdDevCfgPath.replaceAll(".cfg", ".dat"); - String devCfgPath = "D:\\数据比对\\统计数据1\\B码\\217\\PQMonitor_PQM1_000001_20200430_113407_075.cfg"; +// String devCfgPath = "D:\\数据比对\\统计数据1\\B码\\217\\PQMonitor_PQM1_000001_20200430_113407_075.cfg"; String devDatPath = devCfgPath.replaceAll(".cfg", ".dat"); // 验证文件是否存在 @@ -2297,16 +2300,16 @@ public class SocketContrastResponseService { String code = adTypeCode.replace(DetectionCodeEnum.REAL_PREFIX.getCode(), ""); DetectionCodeEnum detectionCodeEnum = DetectionCodeEnum.getDetectionCodeByCode(code); switch (detectionCodeEnum) { - case FREQ: - // 频率 - sqlDataDTO = new DevData.SqlDataDTO(); - sqlDataDTO.setDesc(DetectionCodeEnum.FREQ.getCode()); - sqlDataDTO.setType("real"); - listDTO = new DevData.SqlDataDTO.ListDTO(); - listDTO.setT((double) pointDevData.getFreq()[0]); - sqlDataDTO.setList(listDTO); - sqlDataDTOS.add(sqlDataDTO); - break; +// case FREQ: +// // 频率 +// sqlDataDTO = new DevData.SqlDataDTO(); +// sqlDataDTO.setDesc(DetectionCodeEnum.FREQ.getCode()); +// sqlDataDTO.setType("real"); +// listDTO = new DevData.SqlDataDTO.ListDTO(); +// listDTO.setT((double) pointDevData.getFreq()[0]); +// sqlDataDTO.setList(listDTO); +// sqlDataDTOS.add(sqlDataDTO); +// break; case VRMS: // 相电压有效值 sqlDataDTO = new DevData.SqlDataDTO(); @@ -2407,9 +2410,9 @@ public class SocketContrastResponseService { sqlDataDTO.setDesc(DetectionCodeEnum.I1.getCode()); sqlDataDTO.setType("real"); listDTO = new DevData.SqlDataDTO.ListDTO(); - listDTO.setA((double) pointDevData.getFuHarm()[3][0]); - listDTO.setB((double) pointDevData.getFuHarm()[4][0]); - listDTO.setC((double) pointDevData.getFuHarm()[5][0]); + listDTO.setA((double) pointDevData.getInHarm()[3][0]); + listDTO.setB((double) pointDevData.getInHarm()[4][0]); + listDTO.setC((double) pointDevData.getInHarm()[5][0]); sqlDataDTO.setList(listDTO); sqlDataDTOS.add(sqlDataDTO); @@ -2496,6 +2499,16 @@ public class SocketContrastResponseService { case PSV_1_49: break; case SI_1_49: + sqlDataDTO = new DevData.SqlDataDTO(); + sqlDataDTO.setDesc(DetectionCodeEnum.I1.getCode()); + sqlDataDTO.setType("real"); + listDTO = new DevData.SqlDataDTO.ListDTO(); + listDTO.setA((double) pointDevData.getInHarm()[3][0]); + listDTO.setB((double) pointDevData.getInHarm()[4][0]); + listDTO.setC((double) pointDevData.getInHarm()[5][0]); + sqlDataDTO.setList(listDTO); + sqlDataDTOS.add(sqlDataDTO); + // 间谐波电流幅值 sqlDataHarmDTOI = new DevData.SqlDataHarmDTO(); sqlDataHarmDTOI.setDesc(DetectionCodeEnum.SI_1_49.getCode()); @@ -2507,7 +2520,7 @@ public class SocketContrastResponseService { cListI = new ArrayList<>(); // 前50次间谐波 - for (int harm = 0; harm < 50; harm++) { + for (int harm = 1; harm < 51; harm++) { // 间谐波电流 aListI.add(String.format("%.7f", pointDevData.getInHarm()[3][harm])); bListI.add(String.format("%.7f", pointDevData.getInHarm()[4][harm])); diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java index a3f06158..68b60679 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java @@ -1150,6 +1150,9 @@ public class DetectionServiceImpl { break; case AVG_VALUE: newValue.addAll(DetectionUtil.getAvgDoubles(value)); + case Own_value: + case AT_WILL_VALUE: + newValue.addAll(value); break; } value.clear(); diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java index 8bd10557..f8b813c2 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java @@ -7,15 +7,14 @@ import com.alibaba.fastjson.JSONObject; import com.njcn.gather.detection.pojo.param.ContrastDetectionParam; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.util.socket.cilent.NettyClient; +import com.njcn.gather.detection.util.socket.cilent.NettyContrastClientHandler; import com.njcn.gather.detection.util.socket.config.SocketConnectionConfig; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - import com.njcn.gather.plan.pojo.enums.DataSourceEnum; import com.njcn.gather.script.pojo.po.SourceIssue; import io.netty.channel.Channel; import io.netty.channel.nio.NioEventLoopGroup; import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.HashMap; @@ -28,13 +27,13 @@ import java.util.concurrent.CopyOnWriteArrayList; /** * Socket连接管理器 * 提供Socket连接的生命周期管理、消息发送、检测任务管理等功能 - * + *

* 包含以下主要功能: * 1. 基础连接管理:addUser, removeUser, sendMsg等 * 2. 智能消息发送:smartSendToSource, smartSendToDevice等(新增) * 3. 检测任务管理:targetMap, sourceIssueList等管理 * 4. 连接状态监控:getConnectionStatus等(新增) - * + * * @Description: webSocket存储的通道 * @Author: wr, hongawen * @Date: 2024/12/11 13:04 @@ -61,21 +60,21 @@ public class SocketManager { } public static void addGroup(String userId, NioEventLoopGroup group) { - socketGroup.put(userId, group); + socketGroup.put(userId, group); } public static void removeUser(String userId) { Channel channel = socketSessions.get(userId); - if(ObjectUtil.isNotNull(channel)){ + if (ObjectUtil.isNotNull(channel)) { try { channel.close().sync(); } catch (InterruptedException e) { e.printStackTrace(); } NioEventLoopGroup eventExecutors = socketGroup.get(userId); - if(ObjectUtil.isNotNull(eventExecutors)){ + if (ObjectUtil.isNotNull(eventExecutors)) { eventExecutors.shutdownGracefully(); - System.out.println(userId+"__"+channel.id()+"关闭了客户端"); + System.out.println(userId + "__" + channel.id() + "关闭了客户端"); } } socketSessions.remove(userId); @@ -90,12 +89,12 @@ public class SocketManager { return socketGroup.get(userId); } - public static void sendMsg(String userId,String msg) { + public static void sendMsg(String userId, String msg) { Channel channel = socketSessions.get(userId); - if(ObjectUtil.isNotNull(channel)){ - channel.writeAndFlush(msg+'\n'); + if (ObjectUtil.isNotNull(channel)) { + channel.writeAndFlush(msg + '\n'); log.info("{}__{}往{}发送数据:{}", userId, channel.id(), channel.remoteAddress(), msg); - }else{ + } else { log.warn("{}__发送数据:失败通道不存在{}", userId, msg); } } @@ -106,9 +105,9 @@ public class SocketManager { * 智能发送消息到程控源设备 * 自动从配置文件读取IP和PORT,开发者无需关心网络配置 * 如果连接不存在且requestId需要建立连接,会自动建立连接后发送 - * + * * @param param 检测参数,包含用户页面ID等信息 - * @param msg 要发送的消息内容(JSON格式,包含requestId字段) + * @param msg 要发送的消息内容(JSON格式,包含requestId字段) */ public void smartSendToSource(PreDetectionParam param, String msg) { String requestId = extractRequestId(msg); @@ -127,7 +126,7 @@ public class SocketManager { return; } } - + // 连接已存在或不需要建立连接,直接发送消息 log.info("直接发送消息到程控源: userId={}, requestId={}", userId, requestId); sendMsg(userId, msg); @@ -137,9 +136,9 @@ public class SocketManager { * 智能发送消息到被检设备 * 自动从配置文件读取IP和PORT,开发者无需关心网络配置 * 如果连接不存在且requestId需要建立连接,会自动建立连接后发送 - * + * * @param param 检测参数,包含用户页面ID等信息 - * @param msg 要发送的消息内容(JSON格式,包含requestId字段) + * @param msg 要发送的消息内容(JSON格式,包含requestId字段) */ public void smartSendToDevice(PreDetectionParam param, String msg) { String requestId = extractRequestId(msg); @@ -158,7 +157,7 @@ public class SocketManager { return; } } - + // 连接已存在或不需要建立连接,直接发送消息 log.info("直接发送消息到被检设备: userId={}, requestId={}", userId, requestId); sendMsg(userId, msg); @@ -171,7 +170,7 @@ public class SocketManager { * 如果连接不存在且requestId需要建立连接,会自动建立连接后发送 * * @param param 检测参数,包含用户页面ID等信息 - * @param msg 要发送的消息内容(JSON格式,包含requestId字段) + * @param msg 要发送的消息内容(JSON格式,包含requestId字段) */ public void smartSendToContrast(ContrastDetectionParam param, String msg) { String requestId = extractRequestId(msg); @@ -188,6 +187,13 @@ public class SocketManager { NettyClient.connectToContrastDeviceStatic(ip, port, param, msg); }); return; + } else { + PreDetectionParam preDetectionParam = new PreDetectionParam(); + preDetectionParam.setUserPageId(param.getLoginName()); + preDetectionParam.setTestItemList(param.getTestItemList()); + preDetectionParam.setDevIds(param.getDevIds()); + preDetectionParam.setUserId(param.getUserId()); + NettyContrastClientHandler.param = preDetectionParam; } } @@ -197,14 +203,12 @@ public class SocketManager { } - - // =================== 私有工具方法 =================== /** * 从消息中提取requestId * 支持JSON格式的消息解析 - * + * * @param msg 消息内容 * @return String requestId,如果解析失败返回"unknown" */ @@ -217,13 +221,13 @@ public class SocketManager { if (StrUtil.isNotBlank(requestId)) { return requestId; } - + // 如果没有requestId字段,尝试解析request_id字段 requestId = jsonObject.getString("request_id"); if (StrUtil.isNotBlank(requestId)) { return requestId; } - + // 如果没有JSON字段,尝试从普通字符串中匹配 if (msg.contains("requestId=")) { String[] parts = msg.split("requestId="); @@ -236,13 +240,13 @@ public class SocketManager { } catch (Exception e) { log.warn("解析消息中的requestId失败: msg={}, error={}", msg, e.getMessage()); } - + return "unknown"; } /** * 检查指定用户的Channel是否活跃 - * + * * @param userId 用户ID * @return boolean true:连接活跃, false:连接不存在或不活跃 */ @@ -271,7 +275,7 @@ public class SocketManager { /** * 用于存储每个测试小项超时时长key key:检测项 value:时间秒 */ - public static volatile Map clockMap = new ConcurrentHashMap<>(); + public static volatile Map clockMap = new ConcurrentHashMap<>(); /** * 用于存储比对式测试时间。 @@ -279,7 +283,6 @@ public class SocketManager { public static volatile Map contrastClockMap = new ConcurrentHashMap<>(); - public static void addSourceList(List sList) { sourceIssueList = sList; } @@ -302,8 +305,8 @@ public class SocketManager { targetMap = map; } - public static void addTargetMap(String scriptType,Long count) { - targetMap.put(scriptType,count); + public static void addTargetMap(String scriptType, Long count) { + targetMap.put(scriptType, count); } public static Long getSourceTarget(String scriptType) { @@ -311,6 +314,5 @@ public class SocketManager { } - } \ No newline at end of file diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java index a33ad10c..eb74ccb3 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyClient.java @@ -128,7 +128,9 @@ public class NettyClient { preDetectionParam.setTestItemList(param.getTestItemList()); preDetectionParam.setDevIds(param.getDevIds()); preDetectionParam.setUserId(param.getUserId()); - NettyContrastClientHandler handler = new NettyContrastClientHandler(preDetectionParam, socketContrastResponseService); + NettyContrastClientHandler handler = new NettyContrastClientHandler(); + handler.param = preDetectionParam; + handler.socketContrastResponseService = socketContrastResponseService; executeSocketConnection(ip, port, preDetectionParam, msg, handler); } @@ -265,7 +267,7 @@ public class NettyClient { PreDetectionParam param, SimpleChannelInboundHandler handler) { // 基础编解码器:处理数据格式转换和粘包拆包 // 按行分割,最大20KB - pipeline.addLast(new LineBasedFrameDecoder(10240*2)) + pipeline.addLast(new LineBasedFrameDecoder(10240 * 2)) // 字节转字符串 .addLast(new StringDecoder(CharsetUtil.UTF_8)) // 字符串转字节 diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyContrastClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyContrastClientHandler.java index 407d91af..5b705507 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyContrastClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettyContrastClientHandler.java @@ -36,8 +36,8 @@ import java.util.Objects; public class NettyContrastClientHandler extends SimpleChannelInboundHandler { - private final PreDetectionParam param; - private final SocketContrastResponseService socketContrastResponseService; + public static PreDetectionParam param; + public static SocketContrastResponseService socketContrastResponseService; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java index ecfd0e02..fed0818e 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/websocket/WebSocketHandler.java @@ -374,9 +374,11 @@ public class WebSocketHandler extends SimpleChannelInboundHandler + +