package com.sandu.ximon.admin.manager.iot.frame.inner.report;
|
|
import cn.hutool.core.util.HexUtil;
|
import cn.hutool.core.util.StrUtil;
|
import com.sandu.ximon.admin.manager.iot.frame.inner.BaseResponseInnerFrame;
|
import com.sandu.ximon.admin.manager.iot.rrpc.enums.C3ErrorEnum;
|
import com.sandu.ximon.admin.manager.iot.rrpc.util.CRC32Utils;
|
import com.sandu.ximon.admin.utils.StringUtil;
|
import lombok.Data;
|
import lombok.ToString;
|
|
/**
|
* C3充电桩故障上报帧
|
*/
|
@Data
|
@ToString(callSuper = true)
|
public class A5C3ErrorCodeReportInnerFrame extends BaseResponseInnerFrame<A5C3ErrorCodeReportInnerFrame> {
|
|
/**
|
* 目标地址 2
|
*/
|
private String destinationAddress;
|
/**
|
* 故障码
|
*/
|
private String errorMessage;
|
|
@Override
|
public A5C3ErrorCodeReportInnerFrame transformFrame(String hex) {
|
//F04500080000048E0000001CA2D92F64
|
if (StrUtil.isBlank(hex)) {
|
return null;
|
}
|
// MQTT通信方式(1)
|
setConnectType(hex.substring(0, 2));
|
// 功能码(1)
|
setFunctionCode(hex.substring(2, 4));
|
// 负荷长度(2)
|
setPayloadLength(hex.substring(4, 8));
|
|
setDestinationAddress(hex.substring(8, 16));
|
|
//故障信息
|
String errorCodeHex = hex.substring(16, hex.length() - 8);
|
String errorInfo = errorInfo(errorCodeHex);
|
if (!StringUtil.strIsNullOrEmpty(errorInfo)) {
|
setErrorMessage(errorInfo);
|
}
|
|
setCrc32(hex.substring(hex.length() - 8));
|
// 校验CRC32
|
String frame = hex.substring(2,hex.length()-8);
|
this.setValidate(CRC32Utils.validateFrame(frame, getCrc32()));
|
return this;
|
}
|
|
/**
|
* 收集故障信息
|
*
|
* @param hexStr
|
* @return
|
*/
|
public String errorInfo(String hexStr) {
|
String errorMsg = "";
|
String str = hexStr2BinStr(hexStr);
|
|
int index = str.indexOf("1");
|
while ((index = str.indexOf("1", index)) > 0) {
|
C3ErrorEnum[] values = C3ErrorEnum.values();
|
for (C3ErrorEnum value : values) {
|
if (value.getCode() == index) {
|
errorMsg = errorMsg + value.getMessage() + ";";
|
}
|
}
|
index = index + 1;
|
}
|
return errorMsg;
|
}
|
|
/**
|
* 16进制字符串转为二进制
|
*/
|
public String hexStr2BinStr(String hexStr) {
|
if (hexStr == null || hexStr.length() % 2 != 0) {
|
return null;
|
}
|
String bString = "", tmp;
|
for (int i = 0; i < hexStr.length(); i++) {
|
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexStr.substring(i, i + 1), 16));
|
bString += tmp.substring(tmp.length() - 4);
|
}
|
return bString;
|
}
|
}
|