mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 11:05:18 +08:00
update 电单车协议
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.jsowell.netty.domain;
|
||||
|
||||
import com.jsowell.common.YouDianUtils;
|
||||
import com.jsowell.common.util.BytesUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -39,51 +40,65 @@ public class ChargingPileMessage {
|
||||
.toString();
|
||||
}
|
||||
|
||||
// 从字节数组解析消息
|
||||
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
|
||||
if (messageBytes == null || messageBytes.length < 14 || messageBytes.length > 256) {
|
||||
throw new IllegalArgumentException("Invalid message bytes");
|
||||
}
|
||||
|
||||
ChargingPileMessage message = new ChargingPileMessage();
|
||||
// log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
|
||||
// 读取包头
|
||||
byte[] headerBytes = Arrays.copyOfRange(messageBytes, 0, 3);
|
||||
String header = new String(headerBytes, StandardCharsets.UTF_8);
|
||||
message.setHeader(header);
|
||||
// System.out.println("包头:" + header);
|
||||
|
||||
// 读取长度
|
||||
byte[] lengthBytes = {messageBytes[3], messageBytes[4]};
|
||||
int length = BytesUtil.bytesToIntLittle(lengthBytes);
|
||||
message.setLength(length);
|
||||
// System.out.println("长度:" + length);
|
||||
try {
|
||||
// 读取包头
|
||||
byte[] headerBytes = Arrays.copyOfRange(messageBytes, 0, 3);
|
||||
String header = new String(headerBytes, StandardCharsets.UTF_8);
|
||||
message.setHeader(header);
|
||||
|
||||
// 读取物理ID
|
||||
byte[] physicalIdBytes = Arrays.copyOfRange(messageBytes, 5, 9);
|
||||
int physicalId = BytesUtil.bytesToIntLittle(physicalIdBytes);
|
||||
message.setPhysicalId(physicalId);
|
||||
// System.out.println("物理ID:" + physicalId);
|
||||
// 读取长度
|
||||
byte[] lengthBytes = Arrays.copyOfRange(messageBytes, 3, 5);
|
||||
int length = BytesUtil.bytesToIntLittle(lengthBytes);
|
||||
message.setLength(length);
|
||||
|
||||
// 读取消息ID
|
||||
byte[] messageIdBytes = {messageBytes[9], messageBytes[10]};
|
||||
int messageId = BytesUtil.bytesToIntLittle(messageIdBytes);
|
||||
message.setMessageId(messageId);
|
||||
// System.out.println("消息ID:" + messageId);
|
||||
// 验证长度
|
||||
if (length != (messageBytes.length - 5)) {
|
||||
throw new IllegalArgumentException("Invalid message length");
|
||||
}
|
||||
|
||||
// 读取命令
|
||||
byte command = messageBytes[11];
|
||||
message.setCommand(BytesUtil.bcd2StrLittle(new byte[] {command}));
|
||||
// System.out.println("命令:" + BytesUtil.bcd2StrLittle(new byte[] {command}));
|
||||
// 读取物理ID
|
||||
byte[] physicalIdBytes = Arrays.copyOfRange(messageBytes, 5, 9);
|
||||
int physicalId = BytesUtil.bytesToIntLittle(physicalIdBytes);
|
||||
message.setPhysicalId(physicalId);
|
||||
|
||||
// 读取数据
|
||||
byte[] dataBytes = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
||||
message.setData(dataBytes);
|
||||
String data = BytesUtil.bcd2StrLittle(dataBytes);
|
||||
// System.out.println("数据:" + data);
|
||||
// 读取消息ID
|
||||
byte[] messageIdBytes = Arrays.copyOfRange(messageBytes, 9, 11);
|
||||
int messageId = BytesUtil.bytesToIntLittle(messageIdBytes);
|
||||
message.setMessageId(messageId);
|
||||
|
||||
// 读取校验
|
||||
byte[] checksumBytes = {messageBytes[messageBytes.length - 2], messageBytes[messageBytes.length - 1]};
|
||||
int checksum = BytesUtil.bytesToIntLittle(checksumBytes);
|
||||
message.setChecksum(checksum);
|
||||
// System.out.println("校验:" + checksum);
|
||||
log.info("报文:{}, parseMessage:{}", BytesUtil.binary(messageBytes, 16), message.toString());
|
||||
return message;
|
||||
// 读取命令
|
||||
byte commandByte = messageBytes[11];
|
||||
String command = BytesUtil.bcd2StrLittle(new byte[]{commandByte});
|
||||
message.setCommand(command);
|
||||
|
||||
// 读取数据
|
||||
byte[] dataBytes = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
||||
message.setData(dataBytes);
|
||||
String data = BytesUtil.bcd2StrLittle(dataBytes);
|
||||
|
||||
// 读取校验
|
||||
byte[] checksumBytes = Arrays.copyOfRange(messageBytes, messageBytes.length - 2, messageBytes.length);
|
||||
int checksum = BytesUtil.bytesToIntLittle(checksumBytes);
|
||||
message.setChecksum(checksum);
|
||||
|
||||
// 校验码验证
|
||||
if (!YouDianUtils.validateChecksum(messageBytes)) {
|
||||
throw new IllegalArgumentException("Checksum validation failed");
|
||||
}
|
||||
|
||||
log.info("报文:{}, parseMessage:{}", BytesUtil.binary(messageBytes, 16), message.toString());
|
||||
return message;
|
||||
} catch (Exception e) {
|
||||
log.error("Error parsing message", e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user