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-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));
|
|
|
|
|
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);
|
2024-07-15 17:10:48 +08:00
|
|
|
|
2024-08-03 16:59:31 +08:00
|
|
|
return new ChargingPileMessage(header, length, physicalId, messageId, command, data, checksum);
|
2024-07-15 17:10:48 +08:00
|
|
|
}
|
2024-08-03 16:59:31 +08:00
|
|
|
}
|