2024-07-15 16:05:15 +08:00
|
|
|
package com.jsowell.netty.domain;
|
|
|
|
|
|
2024-08-03 16:59:31 +08:00
|
|
|
import com.jsowell.common.util.BytesUtil;
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import lombok.Builder;
|
|
|
|
|
import lombok.Data;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-07-15 17:10:48 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2024-08-03 16:59:31 +08:00
|
|
|
import java.util.Arrays;
|
2024-07-15 16:05:15 +08:00
|
|
|
|
2024-08-03 16:59:31 +08:00
|
|
|
@Slf4j
|
|
|
|
|
@Data
|
|
|
|
|
@NoArgsConstructor
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
@Builder
|
|
|
|
|
public class ChargingPileMessage {
|
|
|
|
|
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字节)
|
2024-07-15 17:10:48 +08:00
|
|
|
|
2024-08-03 16:59:31 +08:00
|
|
|
// 从字节数组解析消息
|
|
|
|
|
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
|
|
|
|
|
log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取包头
|
|
|
|
|
byte[] headerBytes = Arrays.copyOfRange(messageBytes, 0, 3);
|
|
|
|
|
System.out.println(new String(headerBytes, StandardCharsets.UTF_8));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取长度
|
|
|
|
|
byte[] lengthBytes = {messageBytes[3], messageBytes[4]};
|
|
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(lengthBytes));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取物理ID
|
|
|
|
|
byte[] physicalIdBytes = Arrays.copyOfRange(messageBytes, 5, 9);
|
|
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(physicalIdBytes));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取消息ID
|
|
|
|
|
byte[] messageIdBytes = {messageBytes[9], messageBytes[10]};
|
|
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(messageIdBytes));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取命令
|
2024-08-03 16:59:31 +08:00
|
|
|
byte command = messageBytes[11];
|
2024-08-05 10:35:19 +08:00
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(new byte[] {command}));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取数据
|
2024-08-03 16:59:31 +08:00
|
|
|
byte[] data = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
2024-08-05 10:35:19 +08:00
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(data));
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取校验
|
|
|
|
|
byte[] checksumBytes = {messageBytes[messageBytes.length - 2], messageBytes[messageBytes.length - 1]};
|
|
|
|
|
System.out.println(BytesUtil.bcd2StrLittle(checksumBytes));
|
2024-07-15 17:10:48 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
return null;
|
2024-07-15 17:10:48 +08:00
|
|
|
}
|
2024-08-03 16:59:31 +08:00
|
|
|
}
|