Files
jsowell-charger-web/jsowell-netty/src/main/java/com/jsowell/netty/domain/ChargingPileMessage.java

47 lines
1.6 KiB
Java
Raw Normal View History

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));
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
int length = ((messageBytes[3] & 0xFF) << 8) | (messageBytes[4] & 0xFF);
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
int physicalId = ((messageBytes[5] & 0xFF) << 24) | ((messageBytes[6] & 0xFF) << 16) |
((messageBytes[7] & 0xFF) << 8) | (messageBytes[8] & 0xFF);
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
int messageId = ((messageBytes[9] & 0xFF) << 8) | (messageBytes[10] & 0xFF);
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
byte command = messageBytes[11];
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
byte[] data = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
2024-08-03 17:09:11 +08:00
2024-08-03 16:59:31 +08:00
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
}