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,71 +9,72 @@
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> {
    @Override
    protected void encode(ChannelHandlerContext ctx, OuterPacket packet, ByteBuf out) throws Exception {
        if (packet == null) {
            return;
        }
        int gnsscenterId;
        if (ctx.channel().hasAttr(Jtt809Constant.NettyAttribute.GNSS_CENTER_ID)) {
            gnsscenterId = Integer.parseInt(ctx.channel().attr(Jtt809Constant.NettyAttribute.GNSS_CENTER_ID).get());
        } else {
            gnsscenterId = packet.getGnsscenterId();
        }
        byte[] body = packet.getBody();
        if (body == null) {
            body = new byte[0];
        }
        // 32 = 头标识[1] + 数据头[30 = 长度[4] + 序列号[4] + 数据类型[2] + 接入码[4] + 版本号[3] + 加密标识[1] + 密钥[4]] + 时间[8] + 尾标识[1]
        int len = body.length + 32;
        out.markReaderIndex();
        // 数据长度
        out.writeInt(len);
        // 序列号
        out.writeInt(GnssCenterService.getInstance().serialNo(gnsscenterId));
        // 业务数据类型
        out.writeShort(packet.getId());
        // 下级平台接入码
        out.writeInt(gnsscenterId);
        // 版本号
        out.writeByte(1);
        out.writeByte(0);
        out.writeByte(0);
        // 报文加密标识位
        out.writeByte(0);
        // 数据加密的密钥
        out.writeInt(0);
        // 数据体
        out.writeBytes(body);
        // 校验码
        byte[] crcBytes = new byte[out.readableBytes()];
        out.readBytes(crcBytes);
        out.writeShort(CRC16CCITT.crc16(crcBytes));
        // 转义
        out.resetReaderIndex();
        byte[] escapeBytes = new byte[out.readableBytes()];
        out.readBytes(escapeBytes);
        // 重置下标
        out.setIndex(0, 0);
        // 包头标识
        out.writeByte(Jtt809Constant.PACKET_HEAD_FLAG);
        // 数据内容
        out.writeBytes(Jtt809Util.escape(escapeBytes));
        // 包尾标识
        out.writeByte(Jtt809Constant.PACKET_END_FLAG);
    }
   @Override
   protected void encode(ChannelHandlerContext ctx, OuterPacket packet, ByteBuf out) throws Exception {
      if (packet == null) {
         return;
      }
      int gnsscenterId;
      if (ctx.channel().hasAttr(Jtt809Constant.NettyAttribute.GNSS_CENTER_ID)) {
         gnsscenterId = Integer.parseInt(ctx.channel().attr(Jtt809Constant.NettyAttribute.GNSS_CENTER_ID).get());
      } else {
         gnsscenterId = packet.getGnsscenterId();
      }
      byte[] body = packet.getBody();
      if (body == null) {
         body = new byte[0];
      }
      // 26 = 头标识[1] + 数据头[22 = 长度[4] + 序列号[4] + 数据类型[2] + 接入码[4] + 版本号[3] + 加密标识[1] + 密钥[4]] + 校验码[2] + 尾标识[1]
      int len = body.length + 26;
      out.markReaderIndex();
      // 数据长度
      out.writeInt(len);
      // 序列号
      out.writeInt(GnssCenterService.getInstance().serialNo(gnsscenterId));
      // 业务数据类型
      out.writeShort(packet.getId());
      // 下级平台接入码
      out.writeInt(gnsscenterId);
      // 版本号
      out.writeByte(1);
      out.writeByte(0);
      out.writeByte(0);
      // 报文加密标识位
      out.writeByte(0);
      // 数据加密的密钥
      out.writeInt(0);
      // 数据体
      out.writeBytes(body);
      // 校验码
      byte[] crcBytes = new byte[out.readableBytes()];
      out.readBytes(crcBytes);
      out.writeShort(CRC16CCITT.crc16(crcBytes));
      // 转义
      out.resetReaderIndex();
      byte[] escapeBytes = new byte[out.readableBytes()];
      out.readBytes(escapeBytes);
      // 重置下标
      out.setIndex(0, 0);
      // 包头标识
      out.writeByte(Jtt809Constant.PACKET_HEAD_FLAG);
      // 数据内容
      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));
   }
}