update 电单车协议

This commit is contained in:
Guoqs
2024-08-03 16:59:31 +08:00
parent 01770b2123
commit 6345314725
6 changed files with 117 additions and 170 deletions

View File

@@ -1,35 +1,40 @@
package com.jsowell.netty.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.jsowell.common.util.BytesUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ChargingPileMessage {
private int physicalId;
private short messageId;
private byte command;
private byte[] data;
private String header; // 包头 (3字节)
private int length; // 长度 (2字节)
private int physicalId; // 物理ID (4字节)
private int messageId; // 消息ID (2字节)
private byte command; // 命令 (1字节)
private byte[] data; // 数据 (n字节)
private int checksum; // 校验 (2字节)
public ChargingPileMessage(int physicalId, short messageId, byte command, byte[] data) {
this.physicalId = physicalId;
this.messageId = messageId;
this.command = command;
this.data = data;
// 从字节数组解析消息
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
String header = new String(Arrays.copyOfRange(messageBytes, 0, 3));
int length = ((messageBytes[3] & 0xFF) << 8) | (messageBytes[4] & 0xFF);
int physicalId = ((messageBytes[5] & 0xFF) << 24) | ((messageBytes[6] & 0xFF) << 16) |
((messageBytes[7] & 0xFF) << 8) | (messageBytes[8] & 0xFF);
int messageId = ((messageBytes[9] & 0xFF) << 8) | (messageBytes[10] & 0xFF);
byte command = messageBytes[11];
byte[] data = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
int checksum = ((messageBytes[messageBytes.length - 2] & 0xFF) << 8) | (messageBytes[messageBytes.length - 1] & 0xFF);
return new ChargingPileMessage(header, length, physicalId, messageId, command, data, checksum);
}
// Getters
public int getPhysicalId() { return physicalId; }
public short getMessageId() { return messageId; }
public byte getCommand() { return command; }
public byte[] getData() { return data; }
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.JSON_STYLE)
.append("physicalId", physicalId)
.append("messageId", messageId)
.append("command", command)
.append("data", data)
.toString();
}
}
}