From 5cc542e6246154eb9c5bcc3f87d31d7e32c2ea1d Mon Sep 17 00:00:00 2001 From: wr <1754607820@qq.com> Date: Thu, 19 Dec 2024 17:32:33 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=BE=AE=E8=B0=83=E7=9B=B8=E5=BA=8F=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/SocketDevResponseService.java | 1 + .../handler/SocketSourceResponseService.java | 29 +++--- .../pojo/enums/DetectionResponseEnum.java | 24 +++++ .../pojo/param/PreDetectionParam.java | 5 ++ .../service/impl/PreDetectionServiceImpl.java | 88 ++++++++++--------- .../detection/util/socket/SocketManager.java | 10 ++- .../util/socket/cilent/NettyClient.java | 2 +- .../cilent/NettySourceClientHandler.java | 27 +++--- .../util/socket/web/WebSocketHandler.java | 2 +- 9 files changed, 121 insertions(+), 67 deletions(-) create mode 100644 detection/src/main/java/com/njcn/gather/detection/pojo/enums/DetectionResponseEnum.java 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 838b357e..3ce30382 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 @@ -235,6 +235,7 @@ public class SocketDevResponseService { System.out.println("开始相序校验++++++++++"); PqScriptIssueParam issueParam=new PqScriptIssueParam(); issueParam.setPlanId(param.getPlanId()); + issueParam.setSourceId(param.getScriptId()); issueParam.setIsPhaseSequence(true); issueParam.setDevIds(param.getDevIds()); List sourceIssues = pqScriptDtlsService.listSourceIssue(issueParam); 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 4077d8c9..f6072039 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 @@ -46,16 +46,21 @@ public class SocketSourceResponseService { public void deal(PreDetectionParam param, String msg){ SocketDataMsg socketDataMsg = MsgUtil.socketDataMsg(msg); SourceOperateCodeEnum enumByCode = SourceOperateCodeEnum.getDictDataEnumByCode(socketDataMsg.getRequestId()); - switch (enumByCode){ - case YJC_YTXJY: - detectionDev(param, socketDataMsg); - break; - case YJC_XUJY: - phaseSequenceDev(param, socketDataMsg); - break; + if(ObjectUtil.isNotNull(enumByCode)){ + switch (enumByCode){ + case YJC_YTXJY: + detectionDev(param, socketDataMsg); + break; + case YJC_XUJY: + phaseSequenceDev(param, socketDataMsg); + break; + } + }else{ + System.out.println("1"); } + } /** @@ -70,9 +75,9 @@ public class SocketSourceResponseService { switch (dictDataEnumByCode){ case SUCCESS: //todo 前端推送收到的消息暂未处理好 -// webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); + webSocketHandler.sendMsgToUser(param.getUserPageId(), "msg"); String s = param.getUserPageId() + "_Dev"; - //开始设备通讯检测 + //开始设备通讯检测(发送设备初始化) Channel channel = SocketManager.getChannelByUserId(s); if(channel==null || !channel.isActive()){ NettyClient.socketClient(ip, port, param.getUserPageId(), new NettyDevClientHandler(param, socketDevResponseService)); @@ -93,7 +98,7 @@ public class SocketSourceResponseService { socketMsg.setRequestId(socketDataMsg.getRequestId()); socketMsg.setOperateCode(socketDataMsg.getOperateCode()); socketMsg.setData(dictDataEnumByCode.getMessage()); -// webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); break; } } @@ -114,7 +119,7 @@ public class SocketSourceResponseService { socketMsg.setRequestId(socketDataMsg.getRequestId()); socketMsg.setOperateCode(socketDataMsg.getOperateCode()); socketMsg.setData(dictDataEnumByCode.getMessage()); -// webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); String s = param.getUserPageId() + "_Dev"; @@ -138,7 +143,7 @@ public class SocketSourceResponseService { socketMsg.setRequestId(socketDataMsg.getRequestId()); socketMsg.setOperateCode(socketDataMsg.getOperateCode()); socketMsg.setData(dictDataEnumByCode.getMessage()); -// webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); break; } } diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/enums/DetectionResponseEnum.java b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/DetectionResponseEnum.java new file mode 100644 index 00000000..9fced6b2 --- /dev/null +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/DetectionResponseEnum.java @@ -0,0 +1,24 @@ +package com.njcn.gather.detection.pojo.enums; + +import lombok.Getter; + +/** + * @author caozehui + * @data 2024/11/9 + */ +@Getter +public enum DetectionResponseEnum { + PLAN_PATTERN_NOT("A020001", "计划模式查询为空"), + SOURCE_INFO_NOT("A020002", "源表信息不存在"), + PLAN_AND_SOURCE_NOT("A020003", "计划和源关系不存在") + + ; + + private String code; + private String message; + + DetectionResponseEnum(String code, String message) { + this.code = code; + this.message = message; + } +} diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/param/PreDetectionParam.java b/detection/src/main/java/com/njcn/gather/detection/pojo/param/PreDetectionParam.java index 7155a514..f972d412 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/param/PreDetectionParam.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/param/PreDetectionParam.java @@ -27,4 +27,9 @@ public class PreDetectionParam { */ private List devIds; + /** + * 检测脚本Id + */ + private String scriptId; + } 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 e244efc3..ca631f79 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 @@ -1,11 +1,15 @@ package com.njcn.gather.detection.service.impl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.njcn.common.pojo.enums.response.CommonResponseEnum; +import com.njcn.common.pojo.exception.BusinessException; import com.njcn.gather.detection.handler.SocketDevResponseService; import com.njcn.gather.detection.handler.SocketSourceResponseService; +import com.njcn.gather.detection.pojo.enums.DetectionResponseEnum; import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum; import com.njcn.gather.detection.pojo.param.PreDetectionParam; import com.njcn.gather.detection.pojo.vo.SocketMsg; @@ -13,10 +17,9 @@ import com.njcn.gather.detection.service.PreDetectionService; import com.njcn.gather.detection.util.socket.SocketManager; 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.cilent.NettySourceClientHandler; import com.njcn.gather.device.device.service.IPqDevService; -import com.njcn.gather.device.script.pojo.param.PqScriptIssueParam; -import com.njcn.gather.device.script.pojo.po.SourceIssue; import com.njcn.gather.device.script.service.IPqScriptDtlsService; import com.njcn.gather.device.source.pojo.po.SourceInitialize; import com.njcn.gather.device.source.service.IPqSourceService; @@ -31,13 +34,15 @@ import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.util.*; +import java.util.HashMap; +import java.util.Map; + @Service @RequiredArgsConstructor public class PreDetectionServiceImpl implements PreDetectionService { - private final String source= "source"; + private final String source = "source"; private final IPqDevService iPqDevService; private final IDictDataService dictDataService; @@ -58,7 +63,6 @@ public class PreDetectionServiceImpl implements PreDetectionService { private final SocketSourceResponseService sourceResponseService; - @Override public void sourceCommunicationCheck(PreDetectionParam param) { /* @@ -75,51 +79,55 @@ public class PreDetectionServiceImpl implements PreDetectionService { sendYtxSocket(param); break; case CONTRAST: - //todo 对比式可以是多个源 - sendYtxSocket(param); break; default: - //todo 没有找到对应的模式 - break; + throw new BusinessException(DetectionResponseEnum.PLAN_PATTERN_NOT); } - - } else { - //todo 需要向前端推送消息查不到检测计划 } } - private void sendYtxSocket(PreDetectionParam param){ + private void sendYtxSocket(PreDetectionParam param) { AdPlanSource planSource = adPlanSourceService.getOne(new LambdaQueryWrapper() - .eq(AdPlanSource::getPlanId,param.getPlanId()) + .eq(AdPlanSource::getPlanId, param.getPlanId()) ); - if(ObjectUtil.isNotNull(planSource)){ + if (ObjectUtil.isNotNull(planSource)) { SourceInitialize sourceParam = pqSourceService.getSourceInitializeParam(planSource.getSourceId()); - if(ObjectUtil.isNotNull(sourceParam)){ + if (ObjectUtil.isNotNull(sourceParam)) { //开始组装socket报文请求头 - SocketMsg msg=new SocketMsg(); + SocketMsg msg ; + String s = param.getUserPageId() + "_Source"; + //PQC600A_192.168.1.133_6806 + Channel channel = SocketManager.getChannelByUserId(s); + socketDevResponseService.xuClear(); + socketDevResponseService.initList(param); + if(channel==null || !channel.isActive()){ + NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService)); + }else{ + //关闭源 + msg= new SocketMsg(); + msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue()); + msg.setOperateCode(SourceOperateCodeEnum.CLOSE_GATHER.getValue()); + Map map=new HashMap<>(1); + map.put("sourceId",sourceParam.getSourceId()); + msg.setData(JSON.toJSONString(map)); + SocketManager.sendMsg(s, JSON.toJSONString(msg)); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + msg= new SocketMsg(); msg.setRequestId(SourceOperateCodeEnum.YJC_YTXJY.getValue()); msg.setOperateCode(SourceOperateCodeEnum.INIT_GATHER.getValue()); msg.setData(JSON.toJSONString(sourceParam)); - String s = param.getUserPageId() + "_Source"; - NettyClient.socketClient(ip, port,param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService)); - SocketManager.sendMsg(s,JSON.toJSONString(msg)); - - - PqScriptIssueParam issueParam=new PqScriptIssueParam(); - issueParam.setScriptId(planSource.getSourceId()); - issueParam.setPlanId(param.getPlanId()); - issueParam.setSourceId(planSource.getSourceId()); - List sourceIssues = pqScriptDtlsService.listSourceIssue(issueParam); - for (SourceIssue sourceIssue : sourceIssues) { - String jsonString = JSON.toJSONString(sourceIssue); - } - - }else{ - //todo 提示处理源表信息不存在 + SocketManager.sendMsg(s, JSON.toJSONString(msg)); + } else { + throw new BusinessException(DetectionResponseEnum.SOURCE_INFO_NOT); } - }else{ - //todo 提示处理计划和源关系不存在 + } else { + throw new BusinessException(DetectionResponseEnum.PLAN_AND_SOURCE_NOT); } } @@ -129,17 +137,15 @@ public class PreDetectionServiceImpl implements PreDetectionService { socketDevResponseService.initList(param); - - Runnable runnable = new Runnable() { @Override public void run() { - String ddId = param.getUserPageId()+"_Source"; + String ddId = param.getUserPageId() + "_Source"; Channel channel = SocketManager.getChannelByUserId(ddId); - if( channel== null || !channel.isActive()){ - NettyClient.socketClient(ip, port,param.getUserPageId(),new NettySourceClientHandler(param, sourceResponseService)); + if (channel == null || !channel.isActive()) { + NettyClient.socketClient(ip, port, param.getUserPageId(), new NettySourceClientHandler(param, sourceResponseService)); } - SocketManager.sendMsg(ddId,"start\n"); + SocketManager.sendMsg(ddId, "start\n"); } }; runnable.run(); 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 afaa9706..0c63a968 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 @@ -1,5 +1,6 @@ package com.njcn.gather.detection.util.socket; +import cn.hutool.core.util.ObjectUtil; import io.netty.channel.Channel; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; @@ -29,8 +30,13 @@ public class SocketManager { public static void sendMsg(String userId,String msg) { Channel channel = userSessions.get(userId); - channel.writeAndFlush(msg); - System.out.println(userId+"__"+channel.id()+"往"+channel.remoteAddress()+"发送数据:"+msg); + if(ObjectUtil.isNotNull(channel)){ + channel.writeAndFlush(msg); + System.out.println(userId+"__"+channel.id()+"往"+channel.remoteAddress()+"发送数据:"+msg); + }else{ + System.out.println(userId+"__发送数据:失败通道不存在"+msg); + } + } } \ 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 38a35485..ce340608 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 @@ -53,7 +53,7 @@ public class NettyClient { //空闲状态的handler // 添加LineBasedFrameDecoder来按行分割数据 // .addLast(new LineBasedFrameDecoder(10240)) - // .addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS)) + .addLast(new IdleStateHandler(0, 5, 0, TimeUnit.SECONDS)) .addLast(new StringDecoder(CharsetUtil.UTF_8)) .addLast(new StringEncoder(CharsetUtil.UTF_8)) .addLast(handler); diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java index 6b601c91..ed7a936c 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/cilent/NettySourceClientHandler.java @@ -1,10 +1,15 @@ package com.njcn.gather.detection.util.socket.cilent; +import com.alibaba.fastjson.JSON; import com.njcn.gather.detection.handler.SocketSourceResponseService; +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.util.socket.SocketManager; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.timeout.IdleState; +import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.TimeoutException; import lombok.RequiredArgsConstructor; @@ -70,16 +75,18 @@ public class NettySourceClientHandler extends SimpleChannelInboundHandler