提交YKCBaseMessage

This commit is contained in:
Guoqs
2024-10-30 14:19:19 +08:00
parent 0ff1896a44
commit cb9ba2139e

View File

@@ -0,0 +1,106 @@
package com.jsowell.common.core.domain.ykc;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.YKCUtils;
import io.netty.channel.ChannelId;
import lombok.*;
import org.apache.poi.ss.formula.functions.T;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* 云快充报文基本信息
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class YKCBaseMessage implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 起始标志 1字节
*/
protected String header;
/**
* 数据长度 1字节
*/
protected int dataLength;
/**
* 序列号域 2字节
*/
protected int serialNumber;
/**
* 加密标志 1 字节
*/
protected int encryptFlag;
/**
* 帧类型标志 1 字节
*/
protected String frameType;
/**
* 数据信息
* 参考不同帧类型的数据字段
*/
protected T dataInfo;
/**
* 帧校验域 2字节
*/
protected String frameCheckSequence;
/**
* shortChannelId
*/
protected ChannelId channelId;
public YKCBaseMessage(byte[] messageBytes) {
if (messageBytes == null || messageBytes.length < 8 || messageBytes.length > 204) {
throw new IllegalArgumentException("Invalid message bytes");
}
// 读取包头
int startIndex = 0;
int length = 1;
byte[] headerBytes = BytesUtil.copyBytes(messageBytes, startIndex, length);
this.header = new String(headerBytes, StandardCharsets.UTF_8);
// 读取长度
startIndex += length;
length = 1;
byte[] lengthBytes = BytesUtil.copyBytes(messageBytes, startIndex, length);
this.dataLength = BytesUtil.bytesToIntLittle(lengthBytes);
// 读取序列号
startIndex += length;
length = 2;
byte[] serialNumberBytes = BytesUtil.copyBytes(messageBytes, startIndex, length);
this.serialNumber = BytesUtil.bytesToIntLittle(serialNumberBytes);
// 读取加密标志
startIndex += length;
length = 1;
byte[] encryptFlagBytes = BytesUtil.copyBytes(messageBytes, startIndex, length);
this.encryptFlag = BytesUtil.bytesToIntLittle(encryptFlagBytes);
// 读取帧类型
startIndex += length;
length = 1;
byte[] frameTypeBytes = BytesUtil.copyBytes(messageBytes, startIndex, length);
this.frameType = YKCUtils.frameType2Str(frameTypeBytes);
// 读取校验码
byte[] frameCheckSequenceBytes = Arrays.copyOfRange(messageBytes, messageBytes.length - 2, messageBytes.length);
this.frameCheckSequence = String.format("%04x", BytesUtil.bytesToInt(frameCheckSequenceBytes, 0));
}
}