package com.zy.acs.gateway.domain;
|
|
import com.zy.acs.framework.common.Cools;
|
import com.zy.acs.gateway.constant.AgvConstant;
|
import com.zy.acs.gateway.constant.PacErrorType;
|
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBufUtil;
|
|
import java.util.Objects;
|
|
/**
|
* AGV报文模型
|
* Created by vincent on 2023/3/10
|
*/
|
public class AgvPackage implements PackageSupport {
|
|
/**
|
* 源数据包缓冲区(引用)
|
*/
|
private ByteBuf sourceBuff;
|
|
/**
|
* 原始消息对应的16进制字符串
|
*/
|
private String sourceHexStr;
|
|
/**
|
* 消息头部
|
*/
|
private PacHeader header;
|
|
/**
|
* 消息体
|
*/
|
private PacBody body;
|
|
/**
|
* 消息的校正码
|
*/
|
private int validCode;
|
|
/**
|
* 是否为校验异常包
|
*/
|
private boolean errorPac;
|
|
/**
|
* 校验异常类型
|
*/
|
private PacErrorType pacErrorType;
|
|
public static AgvPackage valueOfEmpty() {
|
AgvPackage agvPackage = new AgvPackage();
|
agvPackage.setHeader(new PacHeader())
|
.setBody(new PacBody())
|
.setValidCode((short) 0)
|
.setErrorPac(false);
|
return agvPackage;
|
}
|
|
public String toLogString() {
|
StringBuilder pacSb = new StringBuilder();
|
pacSb.append("AGV(").append(getHeader().getUniqueNo()).append(")")
|
.append("上传")
|
.append("[").append(getHeader().getProtocolType().getDes()).append("]消息.")
|
.append("原始消息[").append(getSourceHexStr()).append("]");
|
return pacSb.toString();
|
}
|
|
public ByteBuf getSourceBuff() {
|
// 因为处理buffer时,总是希望拿到一个重置了readerIndex的buf
|
if (Objects.nonNull(this.sourceBuff)) {
|
this.sourceBuff.resetReaderIndex();
|
}
|
return this.sourceBuff;
|
}
|
|
public AgvPackage setSourceBuff(ByteBuf sourceBuff) {
|
this.sourceBuff = sourceBuff;
|
if (Cools.isEmpty(this.sourceHexStr)) {
|
this.sourceBuff.resetReaderIndex();
|
this.sourceHexStr = ByteBufUtil.hexDump(this.sourceBuff).toUpperCase();
|
this.sourceBuff.resetReaderIndex();
|
}
|
return this;
|
}
|
|
public PacHeader getHeader() {
|
return header;
|
}
|
|
public AgvPackage setHeader(PacHeader header) {
|
this.header = header;
|
return this;
|
}
|
|
public PacBody getBody() {
|
return body;
|
}
|
|
public AgvPackage setBody(PacBody body) {
|
this.body = body;
|
return this;
|
}
|
|
public int getValidCode() {
|
return validCode;
|
}
|
|
public AgvPackage setValidCode(int validCode) {
|
this.validCode = validCode;
|
return this;
|
}
|
|
public boolean isErrorPac() {
|
return errorPac;
|
}
|
|
public AgvPackage setErrorPac(boolean errorPac) {
|
this.errorPac = errorPac;
|
return this;
|
}
|
|
public String getSourceHexStr() {
|
return sourceHexStr;
|
}
|
|
public AgvPackage setSourceHexStr(String sourceHexStr) {
|
this.sourceHexStr = sourceHexStr;
|
return this;
|
}
|
|
public PacErrorType getPacErrorType() {
|
return pacErrorType;
|
}
|
|
public AgvPackage setPacErrorType(PacErrorType pacErrorType) {
|
this.pacErrorType = pacErrorType;
|
return this;
|
}
|
|
public ByteBuf convert(ByteBuf byteBuf){
|
byteBuf.writeByte(this.getHeader().getStartSymbol())
|
.writeShort(this.getHeader().getContentLength())
|
.writeByte(this.getHeader().getProtocolType().getCode())
|
.writeBytes(this.getHeader().getUniqueNo().getBytes(AgvConstant.CHARSET_GBK))
|
.writeByte(this.getHeader().getEncryptType().getCode())
|
.writeBytes(this.getBody().getContent())
|
.writeByte(this.getValidCode());
|
// 计算并设置校验码
|
// this.setValidCode(ValidUtil.caculateValidByteFromBuff(byteBuf));
|
byteBuf.resetReaderIndex();
|
byteBuf.writerIndex(byteBuf.readableBytes() - 1).writeByte(this.getValidCode());
|
return byteBuf;
|
}
|
|
}
|