From 9ab5b4a86e06562df41a452d0d2cc66efe24c4f6 Mon Sep 17 00:00:00 2001 From: htjcAdmin Date: Wed, 12 Mar 2025 16:15:11 +0800 Subject: [PATCH] =?UTF-8?q?/*=E8=B0=83=E6=95=B4=E6=97=A5=E5=BF=97=E7=9A=84?= =?UTF-8?q?=E6=89=93=E5=8D=B0=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E3=80=82*/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inspect/tcpserver/tcp/NettyServer.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java index 47a4b1d..f6910fd 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java +++ b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java @@ -140,7 +140,7 @@ public class NettyServer { } //发送消息 - public void sendMsg(String uuid, String clientKey, boolean request, String xml) { + public void flushMsgToDevice(String uuid, String clientKey, boolean request, String xml) { if (tcpClientMap.containsKey(clientKey) && !StringUtil.isNullOrEmpty(xml)) { ByteBuf byteBuf = Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8); int length = byteBuf.readableBytes(); @@ -155,11 +155,11 @@ public class NettyServer { allBuf.writeByte(0xEB); allBuf.writeByte(0x90); redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS); - logger.info("######## 会话:{}, 客户:{}, 向设备回送序列:{}, 接收端序列:{}, 消息########\n{}", uuid, clientKey, sendIndex, receiveIndex, xml); + logger.info(Color.MAGENTA + "######## => 会话:{}, 客户:{}, 向设备回送序列:{}, 接收端序列:{}, 消息########\n{}" + Color.END, uuid, tcpClientMap.get(clientKey), sendIndex, receiveIndex, xml); nettyServerHandler.sendMsg(uuid, clientKey, tcpClientMap.get(clientKey), allBuf); sendIndex++; } else { - logger.warn("######## 会话: {}, 设备端机器人: [{}] 离线!!! ########", uuid, clientKey); + logger.warn(Color.RED + "######## 会话: {}, 设备端机器人: [{}] 离线!!! ########" + Color.END, uuid, tcpClientMap.get(clientKey)); } } @@ -180,7 +180,7 @@ public class NettyServer { String msg = redisTemplate.opsForValue().get(String.valueOf(sendIndex)); if (!StringUtil.isNullOrEmpty(msg)) { ByteBuf allBuf = Unpooled.copiedBuffer(msg, CharsetUtil.US_ASCII); - sendMsg("", sendCode, true, msg); + flushMsgToDevice("", sendCode, true, msg); } } @@ -202,7 +202,7 @@ public class NettyServer { command = Integer.parseInt(root.element("Command").getText()); } - logger.info(Color.RED + "######## 会话: {}, 客户: {}, 消息类型: {}, 命令:{}, 消息体: ########\n{}" + Color.END, binaryModel.uuid, binaryModel.id, type, command, xml); + logger.info(Color.MAGENTA + "######## <= 会话: {}, 客户: {}, 消息类型: {}, 命令:{}, 消息体: ########\n{}" + Color.END, binaryModel.uuid, binaryModel.id, type, command, xml); //判断是否重发 if (type == SystemType.system) { if (command == SystemType.has_response || command == SystemType.no_response) { @@ -323,13 +323,15 @@ public class NettyServer { // send to BasedataMontDataMqAcceptHandle rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json); String receiveCode = root.element("ReceiveCode").getText(); - feedbackToDevice(binaryModel.uuid, receiveCode, sendCode); + boolean isHost = json.contains(aliasHost); + logger.info("######## 会话: {}, 客户: {}, isHost: {}", binaryModel.uuid, binaryModel.id, isHost); + sendResponseToDevice(binaryModel.uuid, receiveCode, sendCode, isHost); } } - public void feedbackToDevice(String uuid, String sendCode, String receiveCode) { + public void sendResponseToDevice(String uuid, String sendCode, String receiveCode, boolean isHost) { XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); - xStream.alias(aliasHost, ResponseControl.class); + xStream.alias(isHost ? aliasHost : aliasDevice, ResponseControl.class); xStream.autodetectAnnotations(true); ResponseControl responseControl = new ResponseControl(); responseControl.SendCode = sendCode; @@ -340,7 +342,7 @@ public class NettyServer { responseControl.Time = CommonUtils.GetNowDateString(); responseControl.Items = ""; String xml = xStream.toXML(responseControl); - sendMsg(uuid, receiveCode, false, xml); + flushMsgToDevice(uuid, receiveCode, false, xml); } //处理注册应答 @@ -457,11 +459,12 @@ public class NettyServer { logger.error("################ DOWN REGISTER 解析失败[PatrolDevice] {} ################", e2.getMessage()); } } - sendMsg(uuid, sendCode, false, xml); + flushMsgToDevice(uuid, sendCode, false, xml); } public void sendHeartBeat(final String uuid, String xml) { //logger.info("################ 设备端机器人系统心跳消息 ################\n{}", xml); + boolean isHost = true; BaseControl obj = new BaseControl(); try { XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); @@ -479,12 +482,13 @@ public class NettyServer { xStream.ignoreUnknownElements(); xStream.addPermission(AnyTypePermission.ANY); obj = (BaseControl) xStream.fromXML(xml); + isHost = false; } catch (com.thoughtworks.xstream.XStreamException e2) { logger.error("######## HEARTBEAT TO MQ 解析失败[PatrolDevice] {} ########", e2.getMessage()); } } - feedbackToDevice(uuid, obj.ReceiveCode, obj.SendCode); + sendResponseToDevice(uuid, obj.ReceiveCode, obj.SendCode, isHost); // 推送消息到mq JSONObject jsonObject = new JSONObject(); @@ -554,7 +558,7 @@ public class NettyServer { } if (!StringUtils.isEmpty(xml)) { //logger.info("################ 向设备端下发命令 ################\n{}", xml); - sendMsg("", receiveCode, true, xml); + flushMsgToDevice("", receiveCode, true, xml); } } }