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-08-05 16:37:35 +08:00
|
|
|
import org.apache.commons.lang3.builder.ToStringBuilder;
|
|
|
|
|
import org.apache.commons.lang3.builder.ToStringStyle;
|
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-05 16:37:35 +08:00
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return new ToStringBuilder(this, ToStringStyle.JSON_STYLE)
|
|
|
|
|
.append("header", header)
|
|
|
|
|
.append("length", length)
|
|
|
|
|
.append("physicalId", physicalId)
|
|
|
|
|
.append("messageId", messageId)
|
|
|
|
|
.append("command", command)
|
|
|
|
|
.append("data", data)
|
|
|
|
|
.append("checksum", checksum)
|
|
|
|
|
.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-03 16:59:31 +08:00
|
|
|
// 从字节数组解析消息
|
|
|
|
|
public static ChargingPileMessage parseMessage(byte[] messageBytes) {
|
2024-08-05 16:37:35 +08:00
|
|
|
ChargingPileMessage message = new ChargingPileMessage();
|
|
|
|
|
// log.info("parseMessage:{}", BytesUtil.binary(messageBytes, 16));
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取包头
|
|
|
|
|
byte[] headerBytes = Arrays.copyOfRange(messageBytes, 0, 3);
|
2024-08-05 16:37:35 +08:00
|
|
|
String header = new String(headerBytes, StandardCharsets.UTF_8);
|
|
|
|
|
message.setHeader(header);
|
|
|
|
|
// System.out.println("包头:" + header);
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取长度
|
|
|
|
|
byte[] lengthBytes = {messageBytes[3], messageBytes[4]};
|
2024-08-05 16:37:35 +08:00
|
|
|
int length = BytesUtil.bytesToIntLittle(lengthBytes);
|
|
|
|
|
message.setLength(length);
|
|
|
|
|
// System.out.println("长度:" + length);
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取物理ID
|
|
|
|
|
byte[] physicalIdBytes = Arrays.copyOfRange(messageBytes, 5, 9);
|
2024-08-05 16:37:35 +08:00
|
|
|
int physicalId = BytesUtil.bytesToIntLittle(physicalIdBytes);
|
|
|
|
|
message.setPhysicalId(physicalId);
|
|
|
|
|
// System.out.println("物理ID:" + physicalId);
|
2024-08-03 17:09:11 +08:00
|
|
|
|
2024-08-05 10:35:19 +08:00
|
|
|
// 读取消息ID
|
|
|
|
|
byte[] messageIdBytes = {messageBytes[9], messageBytes[10]};
|
2024-08-05 16:37:35 +08:00
|
|
|
int messageId = BytesUtil.bytesToIntLittle(messageIdBytes);
|
|
|
|
|
message.setMessageId(messageId);
|
|
|
|
|
// System.out.println("消息ID:" + messageId);
|
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 16:37:35 +08:00
|
|
|
message.setChecksum(command);
|
|
|
|
|
// 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-05 16:37:35 +08:00
|
|
|
byte[] dataBytes = Arrays.copyOfRange(messageBytes, 12, messageBytes.length - 2);
|
|
|
|
|
message.setData(dataBytes);
|
|
|
|
|
String data = BytesUtil.bcd2StrLittle(dataBytes);
|
|
|
|
|
// System.out.println("数据:" + 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]};
|
2024-08-05 16:37:35 +08:00
|
|
|
int checksum = BytesUtil.bytesToIntLittle(checksumBytes);
|
|
|
|
|
message.setChecksum(checksum);
|
|
|
|
|
// System.out.println("校验:" + checksum);
|
|
|
|
|
log.info("报文:{}, parseMessage:{}", BytesUtil.binary(messageBytes, 16), message.toString());
|
|
|
|
|
return message;
|
2024-07-15 17:10:48 +08:00
|
|
|
}
|
2024-08-03 16:59:31 +08:00
|
|
|
}
|