update 电单车协议

This commit is contained in:
Guoqs
2024-08-15 11:25:47 +08:00
parent 415f7c4b36
commit a6a799f5f0
3 changed files with 8 additions and 17 deletions

View File

@@ -1,6 +1,6 @@
package com.jsowell.netty.decoder;
import com.jsowell.netty.domain.ChargingPileMessage;
import com.jsowell.netty.domain.EBikeMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
@@ -22,7 +22,7 @@ public class MessageDecode extends ByteToMessageDecoder {
in.readBytes(bytes);
// 解析字节数组
ChargingPileMessage message = ChargingPileMessage.parseMessage(bytes);
EBikeMessage message = EBikeMessage.parseMessage(bytes);
out.add(message);
}
}

View File

@@ -18,7 +18,7 @@ import java.util.Arrays;
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ChargingPileMessage {
public class EBikeMessage {
private String header; // 包头 (3字节)
private int length; // 长度 (2字节)
private int physicalId; // 物理ID (4字节)
@@ -40,12 +40,12 @@ public class ChargingPileMessage {
.toString();
}
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
public static EBikeMessage parseMessage(byte[] messageBytes) {
if (messageBytes == null || messageBytes.length < 14 || messageBytes.length > 256) {
throw new IllegalArgumentException("Invalid message bytes");
}
ChargingPileMessage message = new ChargingPileMessage();
EBikeMessage message = new EBikeMessage();
try {
// 读取包头

View File

@@ -1,27 +1,18 @@
package com.jsowell.netty.server.electricbicycles;
import com.alibaba.fastjson2.JSON;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.netty.domain.ChargingPileMessage;
import com.jsowell.netty.domain.EBikeMessage;
import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@ChannelHandler.Sharable
@Slf4j
@Component
public class ChargingPileHandler extends SimpleChannelInboundHandler<ChargingPileMessage> {
public class ChargingPileHandler extends SimpleChannelInboundHandler<EBikeMessage> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ChargingPileMessage msg) throws Exception {
protected void channelRead0(ChannelHandlerContext ctx, EBikeMessage msg) throws Exception {
log.info("收到消息, channelId:{}, msg:{}", ctx.channel().id().toString(), JSON.toJSONString(msg));
}