Browse Source

/*调整日志的打印,增加颜色。*/

master
htjcAdmin 9 months ago
parent
commit
9ab5b4a86e
1 changed files with 16 additions and 12 deletions
  1. +16
    -12
      src/main/java/com/inspect/tcpserver/tcp/NettyServer.java

+ 16
- 12
src/main/java/com/inspect/tcpserver/tcp/NettyServer.java View File

@ -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)) { if (tcpClientMap.containsKey(clientKey) && !StringUtil.isNullOrEmpty(xml)) {
ByteBuf byteBuf = Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8); ByteBuf byteBuf = Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8);
int length = byteBuf.readableBytes(); int length = byteBuf.readableBytes();
@ -155,11 +155,11 @@ public class NettyServer {
allBuf.writeByte(0xEB); allBuf.writeByte(0xEB);
allBuf.writeByte(0x90); allBuf.writeByte(0x90);
redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS); 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); nettyServerHandler.sendMsg(uuid, clientKey, tcpClientMap.get(clientKey), allBuf);
sendIndex++; sendIndex++;
} else { } 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)); String msg = redisTemplate.opsForValue().get(String.valueOf(sendIndex));
if (!StringUtil.isNullOrEmpty(msg)) { if (!StringUtil.isNullOrEmpty(msg)) {
ByteBuf allBuf = Unpooled.copiedBuffer(msg, CharsetUtil.US_ASCII); 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()); 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 (type == SystemType.system) {
if (command == SystemType.has_response || command == SystemType.no_response) { if (command == SystemType.has_response || command == SystemType.no_response) {
@ -323,13 +323,15 @@ public class NettyServer {
// send to BasedataMontDataMqAcceptHandle // send to BasedataMontDataMqAcceptHandle
rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json); rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json);
String receiveCode = root.element("ReceiveCode").getText(); 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 xStream = new XStream(new Xpp3Driver(new NoNameCoder()));
xStream.alias(aliasHost, ResponseControl.class);
xStream.alias(isHost ? aliasHost : aliasDevice, ResponseControl.class);
xStream.autodetectAnnotations(true); xStream.autodetectAnnotations(true);
ResponseControl responseControl = new ResponseControl(); ResponseControl responseControl = new ResponseControl();
responseControl.SendCode = sendCode; responseControl.SendCode = sendCode;
@ -340,7 +342,7 @@ public class NettyServer {
responseControl.Time = CommonUtils.GetNowDateString(); responseControl.Time = CommonUtils.GetNowDateString();
responseControl.Items = ""; responseControl.Items = "";
String xml = xStream.toXML(responseControl); 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()); 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) { public void sendHeartBeat(final String uuid, String xml) {
//logger.info("################ 设备端机器人系统心跳消息 ################\n{}", xml); //logger.info("################ 设备端机器人系统心跳消息 ################\n{}", xml);
boolean isHost = true;
BaseControl obj = new BaseControl(); BaseControl obj = new BaseControl();
try { try {
XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder()));
@ -479,12 +482,13 @@ public class NettyServer {
xStream.ignoreUnknownElements(); xStream.ignoreUnknownElements();
xStream.addPermission(AnyTypePermission.ANY); xStream.addPermission(AnyTypePermission.ANY);
obj = (BaseControl) xStream.fromXML(xml); obj = (BaseControl) xStream.fromXML(xml);
isHost = false;
} catch (com.thoughtworks.xstream.XStreamException e2) { } catch (com.thoughtworks.xstream.XStreamException e2) {
logger.error("######## HEARTBEAT TO MQ 解析失败[PatrolDevice] {} ########", e2.getMessage()); logger.error("######## HEARTBEAT TO MQ 解析失败[PatrolDevice] {} ########", e2.getMessage());
} }
} }
feedbackToDevice(uuid, obj.ReceiveCode, obj.SendCode);
sendResponseToDevice(uuid, obj.ReceiveCode, obj.SendCode, isHost);
// 推送消息到mq // 推送消息到mq
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
@ -554,7 +558,7 @@ public class NettyServer {
} }
if (!StringUtils.isEmpty(xml)) { if (!StringUtils.isEmpty(xml)) {
//logger.info("################ 向设备端下发命令 ################\n{}", xml); //logger.info("################ 向设备端下发命令 ################\n{}", xml);
sendMsg("", receiveCode, true, xml);
flushMsgToDevice("", receiveCode, true, xml);
} }
} }
} }

Loading…
Cancel
Save