update 电单车协议

This commit is contained in:
Guoqs
2024-07-16 15:00:45 +08:00
parent c3908e2ca9
commit f0e0bcc63f
2 changed files with 9 additions and 3 deletions

View File

@@ -36,23 +36,29 @@ public class ChargingPileDecoder extends ByteToMessageDecoder {
// 读取物理ID
int physicalId = in.readInt();
log.info("physicalId:{}", physicalId);
// 读取消息ID
short messageId = in.readShort();
log.info("messageId:{}", messageId);
// 读取命令
byte command = in.readByte();
log.info("command:{}", command);
// 读取数据
int dataLength = length - 13; // 13 = 包头(3) + 长度(2) + 物理ID(4) + 消息ID(2) + 命令(1) + 校验(2)
byte[] data = new byte[dataLength];
in.readBytes(data);
log.info("data:{}", data);
// 读取校验和
short checksum = in.readShort();
log.info("checksum:{}", checksum);
// 验证校验和
short calculatedChecksum = calculateChecksum(in, length);
log.info("calculatedChecksum:{}", calculatedChecksum);
if (checksum != calculatedChecksum) {
log.info("校验和错误,丢弃此帧");
continue;
@@ -60,11 +66,12 @@ public class ChargingPileDecoder extends ByteToMessageDecoder {
// 创建消息对象并添加到输出列表
ChargingPileMessage message = new ChargingPileMessage(physicalId, messageId, command, data);
log.info("ChargingPileMessage:{}", message.toString());
out.add(message);
}
}
private boolean isValidHeader(byte[] header) {
private boolean isValidHeader(byte[] header) {
log.info("isValidHeader header:{}", header);
return header[0] == FRAME_HEADER[0] && header[1] == FRAME_HEADER[1] && header[2] == FRAME_HEADER[2];
}

View File

@@ -26,8 +26,7 @@ public class ElectricBicyclesServerChannelInitializer extends ChannelInitializer
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
// pipeline.addLast("frameDecoder",new CustomDecoder());
pipeline.addLast("frameDecoder", new StartAndLengthFieldFrameDecoder());
pipeline.addLast("frameDecoder", new ChargingPileDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控