Pu Zhibing
2025-03-11 f56262ee1cb3c554878984e3536ab55a298bdedf
ruoyi-service/ruoyi-dataInterchange/src/main/java/com/ruoyi/dataInterchange/util/jtt809/encoder/Jtt809Encoder.java
@@ -1,5 +1,6 @@
package com.ruoyi.dataInterchange.util.jtt809.encoder;
import com.ruoyi.dataInterchange.util.jtt809.common.ByteArrayUtil;
import com.ruoyi.dataInterchange.util.jtt809.common.CRC16CCITT;
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Constant;
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Util;
@@ -8,14 +9,12 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import lombok.extern.slf4j.Slf4j;
/**
 * @author tucke
 */
@Slf4j
public class Jtt809Encoder extends MessageToByteEncoder<OuterPacket> {
@@ -34,8 +33,8 @@
        if (body == null) {
            body = new byte[0];
        }
        // 32 = 头标识[1] + 数据头[30 = 长度[4] + 序列号[4] + 数据类型[2] + 接入码[4] + 版本号[3] + 加密标识[1] + 密钥[4]] + 时间[8] + 尾标识[1]
        int len = body.length + 32;
      // 26 = 头标识[1] + 数据头[22 = 长度[4] + 序列号[4] + 数据类型[2] + 接入码[4] + 版本号[3] + 加密标识[1] + 密钥[4]] + 校验码[2] + 尾标识[1]
      int len = body.length + 26;
        out.markReaderIndex();
        // 数据长度
        out.writeInt(len);
@@ -73,6 +72,9 @@
        out.writeBytes(Jtt809Util.escape(escapeBytes));
        // 包尾标识
        out.writeByte(Jtt809Constant.PACKET_END_FLAG);
      byte[] readableBytes = new byte[out.readableBytes()];
      out.readBytes(readableBytes);
      log.info("下发数据包, packetLen : {}, packet : {}", readableBytes.length, ByteArrayUtil.bytes2HexStr(readableBytes));
    }
}