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 51270a97..7c606026 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 @@ -26,6 +26,7 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; @@ -46,6 +47,8 @@ public class SocketDevResponseService { * 存储的装置相序数据 */ List devInfo = new ArrayList<>(); + + Map devDataMap = new HashMap<>(); /** * 成功结束的装置 */ @@ -67,7 +70,7 @@ public class SocketDevResponseService { /** - * 成功结束的装置 + * 保存成功的协议 */ List successXieyi = new ArrayList<>(); List successXieyi3 = new ArrayList<>(); @@ -139,12 +142,20 @@ public class SocketDevResponseService { switch (dictDataEnumByCode) { case SUCCESS: //通讯校验成功 - webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); + webSocketHandler.sendMsgToUser(param.getUserPageId(), MsgUtil.msgToWebData(socketDataMsg,devNameMapComm,1)); String mId = JSON.parseObject(msg).get("data").toString(); successComm.add(mId); System.out.println(successComm.size() + "=====" + monitorIdListComm.size()); if (successComm.size() == monitorIdListComm.size()) { + + SocketDataMsg temMsg = new SocketDataMsg(); + temMsg.setCode(SourceResponseCodeEnum.DEV_COMM_ALL_SUCCESS.getCode()); + temMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue()); + temMsg.setRequestId(SourceOperateCodeEnum.YJC_SBTXJY.getValue()); + webSocketHandler.sendMsgToUser(param.getUserPageId(),JSON.toJSONString(temMsg) ); + + //开始进行协议校验 List devList = iPqDevService.getDevInfo(param.getDevIds()); Map> map = new HashMap(1); @@ -162,14 +173,15 @@ public class SocketDevResponseService { break; case UNPROCESSED_BUSINESS: + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketDataMsg)); break; case DEV_ERROR: - webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketDataMsg)); break; case DEV_TARGET: - webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketDataMsg)); break; case RE_OPERATE: @@ -194,13 +206,9 @@ public class SocketDevResponseService { switch (dictDataEnumByCode) { case SUCCESS: - webSocketHandler.sendMsgToUser(param.getUserPageId(), msg); - - JSONObject jsonObject = JSON.parseObject(msg); - String mId = jsonObject.get("data").toString(); - String type = jsonObject.get("operateCode").toString(); - if (type.equals(SourceOperateCodeEnum.DEV_INIT_GATHER_02.getValue())) { - successXieyi.add(mId); + webSocketHandler.sendMsgToUser(param.getUserPageId(), MsgUtil.msgToWebData(socketDataMsg,devNameMapComm,2)); + if (socketDataMsg.getOperateCode().equals(SourceOperateCodeEnum.DEV_INIT_GATHER_02.getValue())) { + successXieyi.add(socketDataMsg.getData()); if (successXieyi.size() == monitorIdListComm.size()) { //协议3校验 successXieyi = new ArrayList<>(); @@ -218,10 +226,18 @@ public class SocketDevResponseService { SocketManager.sendMsg(s, json); } - } else if (type.equals(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue())) { - successXieyi3.add(mId); + } else if (socketDataMsg.getOperateCode().equals(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue())) { + successXieyi3.add(socketDataMsg.getData()); System.out.println(successXieyi3.size() + "=====" + monitorIdListComm.size()); if (successXieyi3.size() == monitorIdListComm.size()) { + SocketDataMsg temMsg = new SocketDataMsg(); + temMsg.setCode(SourceResponseCodeEnum.DEV_COMM_ALL_SUCCESS.getCode()); + temMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue()); + temMsg.setRequestId(SourceOperateCodeEnum.YJC_XYJY.getValue()); + webSocketHandler.sendMsgToUser(param.getUserPageId(),JSON.toJSONString(temMsg) ); + + + System.out.println("开始相序校验++++++++++"); System.out.println("开始相序校验,向源下参数++++++++++"); PqScriptIssueParam issueParam = new PqScriptIssueParam(); issueParam.setPlanId(param.getPlanId()); @@ -242,6 +258,8 @@ public class SocketDevResponseService { break; case UNPROCESSED_BUSINESS: + webSocketHandler.sendMsgToUser(param.getUserPageId(),msg); + break; case RE_OPERATE: //发起关闭操作 @@ -428,6 +446,7 @@ public class SocketDevResponseService { //初始化相序集合 this.devInfo = new ArrayList<>(); this.success = new ArrayList<>(); + this.devDataMap = new HashMap<>(); List pqDevList = iPqDevService.getDevInfo(param.getDevIds()); @@ -435,6 +454,7 @@ public class SocketDevResponseService { .map(PreDetection.MonitorListDTO::getLineId) .collect(Collectors.toList()); this.devNameMapComm = pqDevList.stream().collect(Collectors.toMap(PreDetection::getDevIP, PreDetection::getDevName)); + } 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 11750267..d3b3bfec 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 @@ -84,6 +84,9 @@ public class SocketSourceResponseService { webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketMsg)); SocketManager.removeUser(param.getUserPageId()+"_Source"); + //todo 前端推送收到的消息暂未处理好 + webSocketHandler.sendMsgToUser(param.getUserPageId(),JSON.toJSONString(socketDataMsg) ); + String s = param.getUserPageId() + "_Dev"; //开始设备通讯检测(发送设备初始化) List devList = iPqDevService.getDevInfo(param.getDevIds()); Map> map = new HashMap(1); @@ -93,9 +96,12 @@ public class SocketSourceResponseService { socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_01.getValue()); socketMsg.setData(jsonString); String json = JSON.toJSONString(socketMsg); + NettyClient.socketClient(ip, port, s,json, new NettyDevClientHandler(param, socketDevResponseService)); + // SocketManager.sendMsg(s,json); NettyClient.socketClient(ip, port, param.getUserPageId(), json, new NettyDevClientHandler(param, socketDevResponseService)); break; case UNPROCESSED_BUSINESS: + webSocketHandler.sendMsgToUser(param.getUserPageId(),JSON.toJSONString(socketDataMsg) ); break; default: socketMsg = new SocketMsg(); diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java index 80a4ae96..3b2b08d8 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/enums/SourceResponseCodeEnum.java @@ -34,7 +34,8 @@ public enum SourceResponseCodeEnum { NO_INIT_DEV(10556,"不存在上线的设备"), //自定义前端展示消息 - SOCKET_ERROR(20000,"服务端连接失败") + SOCKET_ERROR(25000,"服务端连接失败"), + DEV_COMM_ALL_SUCCESS(25001,"校验成功") diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/SocketDataMsg.java b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/SocketDataMsg.java index 0448a7b6..5dab1584 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/SocketDataMsg.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/SocketDataMsg.java @@ -11,6 +11,11 @@ import lombok.Data; @Data public class SocketDataMsg { + /** + * 标识不同业务 + */ + private String type = "aaa"; + /** * 请求id,确保接收到响应时,知晓是针对的哪次请求的应答 */ diff --git a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java index 2bd79d7a..755c6a86 100644 --- a/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java +++ b/detection/src/main/java/com/njcn/gather/detection/pojo/vo/WebSocketVO.java @@ -1,12 +1,18 @@ package com.njcn.gather.detection.pojo.vo; import com.njcn.gather.detection.pojo.enums.SourceResponseCodeEnum; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@AllArgsConstructor +@NoArgsConstructor public class WebSocketVO { + private String type; + private Integer code; private String message; @@ -14,18 +20,11 @@ public class WebSocketVO { private T data; - public WebSocketVO(SourceResponseCodeEnum sourceResponseCodeEnum){ + public WebSocketVO(SourceResponseCodeEnum sourceResponseCodeEnum,String type){ + this.type = type; this.code = sourceResponseCodeEnum.getCode(); this.message= sourceResponseCodeEnum.getMessage(); } - public WebSocketVO(Integer code, String message, T data) { - this.code = code; - this.message = message; - this.data = data; - } - public WebSocketVO() { - - } } 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 06c68fc8..842b590e 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 @@ -112,19 +112,8 @@ public class PreDetectionServiceImpl implements PreDetectionService { public boolean startTest(PreDetectionParam param) { socketDevResponseService.initList(param); + NettyClient.socketClient(ip, port, param.getUserPageId(),"start\n", new NettySourceClientHandler(param, sourceResponseService)); - Runnable runnable = new Runnable() { - @Override - public void run() { - String ddId = param.getUserPageId() + "_Source"; - Channel channel = SocketManager.getChannelByUserId(ddId); - if (channel == null || !channel.isActive()) { - NettyClient.socketClient(ip, port, param.getUserPageId(), "start\n", new NettySourceClientHandler(param, sourceResponseService)); - } - - } - }; - runnable.run(); return true; } } \ No newline at end of file diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/MsgUtil.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/MsgUtil.java index 8f6c15ff..1d0b024c 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/MsgUtil.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/MsgUtil.java @@ -1,9 +1,12 @@ package com.njcn.gather.detection.util.socket; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.njcn.gather.detection.pojo.vo.SocketDataMsg; +import java.util.Map; + /** * @author wr * @description @@ -36,4 +39,26 @@ public class MsgUtil { public static String toJsonWithNewLinePlain(Object obj) { return JSON.toJSONString(obj) + "\n"; } + + + public static String msgToWebData(SocketDataMsg socketDataMsg, Map devMap,Integer type){ + String data = socketDataMsg.getData(); + if (StrUtil.isNotBlank(data)) { + String[] parts = data.split("_"); + if (parts.length > 0) { + String key = parts[0]; + String newValue = devMap.get(key); + if (newValue != null) { + if(type == 0) { + socketDataMsg.setData(newValue); + }else { + socketDataMsg.setData(newValue+"_"+parts[1]+"路"); + } + } + } + } + return JSON.toJSONString(socketDataMsg); + } + + } 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 576c0b03..2c24c0fe 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 @@ -80,10 +80,11 @@ public class NettyClient { } } } catch (Exception e) { - System.out.println("连接socker服务端发送异常............" + e.getMessage()); + System.out.println("连接socket服务端发送异常............" + e.getMessage()); group.shutdownGracefully(); //TODO 通知页面 WebSocketVO webSocketVO = new WebSocketVO(); + webSocketVO.setType("aaa"); webSocketVO.setCode(SourceResponseCodeEnum.SOCKET_ERROR.getCode()); webSocketVO.setMessage(SourceResponseCodeEnum.SOCKET_ERROR.getMessage()); WebServiceManager.sendMsg(userPageId, JSON.toJSONString(webSocketVO)); diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java index e98d9bd1..1316eba5 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/web/WebSocketHandler.java @@ -123,7 +123,7 @@ public class WebSocketHandler extends SimpleChannelInboundHandler