| | |
| | | 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; |
| | |
| | | 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> { |
| | | |
| | | |
| | |
| | | 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); |
| | |
| | | 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)); |
| | | } |
| | | |
| | | } |