From fd5f1d86f334e814ccb8c2d679373fa7e373cb5e Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 04 Mar 2025 15:05:21 +0800
Subject: [PATCH] 优化

---
 ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java |   25 ++++++++-----------------
 1 files changed, 8 insertions(+), 17 deletions(-)

diff --git a/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java b/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java
index 2e86ed3..3ff7b6c 100644
--- a/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java
+++ b/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java
@@ -1,29 +1,24 @@
 package com.ard.tianyu.netty.handler;// MessageProtocolEncoder.java
 
-import com.ard.tianyu.netty.command.BaseCommand;
+import com.ard.common.utils.ByteUtils;
+import com.ard.tianyu.netty.command.BaseCmd;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 import lombok.extern.slf4j.Slf4j;
 
-import java.util.Arrays;
-
 @Slf4j
-public class MessageProtocolEncoder extends MessageToByteEncoder<BaseCommand> {
+public class MessageProtocolEncoder extends MessageToByteEncoder<BaseCmd> {
     @Override
-    protected void encode(ChannelHandlerContext ctx, BaseCommand command, ByteBuf out) throws Exception {
-        out.writeShort(command.getDataHeader()); // 写入帧头
-        out.writeShort(command.getDataLength()); // 写入数据长度
-        if(command.getBoxSn() != null) {
-            out.writeBytes(command.getBoxSn());
-        }// 写入指令编号
-        out.writeByte(command.getCmdCode()); // 写入指令编号
+    protected void encode(ChannelHandlerContext ctx, BaseCmd command, ByteBuf out) throws Exception {
+
         command.encode(out); // 编码具体负载
         // 打印
         if(command.getCmdCode()!=0x02) {
             printHex(out);
         }
     }
+    // 打印编码后的字节数组
     private void printHex(ByteBuf out)
     {
         // 打印编码后的字节数组
@@ -31,11 +26,7 @@
         out.getBytes(out.readerIndex(), encodedBytes); // 读取ByteBuf中的字节到字节数组中
         // log.warn("Encoded message bytes: {}", Arrays.toString(encodedBytes));
         // 将字节数组转换为十六进制格式
-        StringBuilder hexString = new StringBuilder();
-        for (byte b : encodedBytes) {
-            hexString.append(String.format("%02X ", b)); // 将每个字节转换为两位十六进制并加上空格
-        }
-
-        log.warn("Encoded message in hex: {}", hexString.toString().trim());
+        String hexString = ByteUtils.bytesToHexWithSpaces(encodedBytes);
+        log.warn("Encoded message in hex: {}", hexString);
     }
 }

--
Gitblit v1.9.3