[626][adas] 能否启动自驾检测接口新增原始数据字段

This commit is contained in:
xinfengkun
2023-12-13 18:16:50 +08:00
parent af7d211ad0
commit f1113767c1
19 changed files with 170 additions and 32 deletions

View File

@@ -0,0 +1,93 @@
package com.zhjt.mogo.adas.data.bean;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.zhjt.mogo.adas.utils.ByteUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import function_state_management.FSMStatusReasonQueryOuterClass;
import system_master.SsmInfo;
import system_master.SystemStatusInfo;
/**
* 检测到不能启动自动驾驶的数据
* 此类目前主要用于启动自驾命令下发后的域控数据记录
*/
public class UnableLaunchData {
/**
* 所使用的检测版本
*/
public final String abilityVersion;
/**
* 老底盘数据
* null表示未用到此数据
* TODO 不需要底盘数据,如果底盘数据存在不能启动自驾的原因会限制启动自驾命令的下发
*/
// public final VehicleStateOuterClass.VehicleState vehicleState;
/**
* 新底盘数据
* null表示未用到此数据
* TODO 不需要底盘数据,如果底盘数据存在不能启动自驾的原因会限制启动自驾命令的下发
*/
// public final ChassisStatesOuterClass.ChassisStates chassisStates;
/**
* 老SSM数据
* null表示未用到此数据
*/
public final SystemStatusInfo.StatusInfo statusInfo;
/**
* 新SSM数据
* null表示未用到此数据
*/
public final SsmInfo.SsmStatusInf ssmInfo;
public final FSMStatusReasonQueryOuterClass.FSMStatusReasonRespond fsmStatusReasonRespond;//FSM数据
public UnableLaunchData(String abilityVersion,
SystemStatusInfo.StatusInfo statusInfo,
SsmInfo.SsmStatusInf ssmInfo,
FSMStatusReasonQueryOuterClass.FSMStatusReasonRespond fsmStatusReasonRespond) {
this.abilityVersion = abilityVersion;
this.statusInfo = statusInfo;
this.ssmInfo = ssmInfo;
this.fsmStatusReasonRespond = fsmStatusReasonRespond;
}
public String getJson() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("ability_version", abilityVersion);
JSONArray array = new JSONArray();
if (statusInfo != null) {
arrayPut(array, statusInfo);
}
if (ssmInfo != null) {
arrayPut(array, ssmInfo);
}
if (fsmStatusReasonRespond != null) {
arrayPut(array, fsmStatusReasonRespond);
}
if (array.length() > 0) {
jsonObject.put("data", array);
}
} catch (JSONException | InvalidProtocolBufferException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
private void arrayPut(JSONArray array, GeneratedMessageV3 message) throws JSONException, InvalidProtocolBufferException {
JSONObject object = new JSONObject();
object.put("name", message.getClass().getName());
object.put("original", ByteUtil.byteArrToHex(message.toByteArray(), false));
object.put("parse", new JSONObject(JsonFormat.printer().print(message)));
array.put(object);
}
}

View File

@@ -0,0 +1,377 @@
package com.zhjt.mogo.adas.utils;
import java.math.BigInteger;
public class ByteUtil {
private static final String TAG = ByteUtil.class.getSimpleName();
public static byte[] set8bitUnsignedValue(int num) {
return new byte[]{(byte) (num)};
}
public static byte[] set16bitUnsignedValue(int num) {
byte[] bytes = new byte[2];
bytes[0] = (byte) (num >> 8);
bytes[1] = (byte) num;
return bytes;
}
public static byte[] set32bitUnsignedValue(long num) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (num >> 24);
bytes[1] = (byte) (num >> 16);
bytes[2] = (byte) (num >> 8);
bytes[3] = (byte) num;
return bytes;
}
public static byte[] set64bitUnsignedValue(long num) {
byte[] bytes = new byte[8];
bytes[0] = (byte) (num >> 56);
bytes[1] = (byte) (num >> 48);
bytes[2] = (byte) (num >> 40);
bytes[3] = (byte) (num >> 32);
bytes[4] = (byte) (num >> 24);
bytes[5] = (byte) (num >> 16);
bytes[6] = (byte) (num >> 8);
bytes[7] = (byte) num;
return bytes;
}
/**
* 无符号
* 1 byte
* 使用后将 Index中的index移动到下一个数据的开始位
*
* @param index 开始下标
* @param data
* @return
*/
public static int get8bitUnsignedValue(int index, byte[] data) {
int value = 0;
if (data.length > index) {
value = data[index] & 0xFF;
}
return value;
}
/**
* 无符号
* 合并 2 byte
* 使用后将 Index中的index移动到下一个数据的开始位
*
* @param index 开始下标
* @param data
* @return
*/
public static int get16bitUnsignedValue(int index, byte[] data) {
// String str = "byte[]= %02X %02X";
// L.i("Index=" + index + String.format(str, data[index], data[index + 1]));
int value = 0;
if (data.length > index + 1) {
value = data[index++] & 0xFF;
value = value << 8;
value = value | (data[index] & 0xFF);
}
return value;
}
/**
* 无符号
* 合并 4 byte
* 使用后将 Index中的index移动到下一个数据的开始位
* *
*
* @param index 开始下标
* @param data
* @return
*/
public static int get32bitUnsignedValue(int index, byte[] data) {
int value = 0;
if (data.length > index + 3) {
value = data[index++] & 0xFF;
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index] & 0xFF);
}
return value;
}
/**
* 无符号
* 合并 8 byte
* 使用后将 Index中的index移动到下一个数据的开始位
*
* @param index 开始下标
* @param data
* @return
*/
/**
* 超过{@link Long#MAX_VALUE} 会产生有符号值,目前 Unit Mask 值未超过最大值
*
* @param index
* @param data
* @return
*/
public static long get64bitUnsignedValue(int index, byte[] data) {
long value = 0;
if (data.length > index + 7) {
value = data[index++] & 0xFF;
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index++] & 0xFF);
value = value << 8;
value = value | (data[index] & 0xFF);
}
return value;
}
/**
* 合并 8 byte
* 使用后将 Index中的index移动到下一个数据的开始位
*
* @param index 开始下标
* @param data
* @return
*/
private static final BigInteger TWO_COMPL_REF = BigInteger.ONE.shiftLeft(64);
public static String get64bitUnsignedString(int index, byte[] data) {
BigInteger bigInteger = null;
if (data.length > index + 7) {
byte[] bytes = new byte[8];
System.arraycopy(data, index, bytes, 0, 8);
bigInteger = new BigInteger(bytes);
if (bigInteger.compareTo(BigInteger.ZERO) < 0)
bigInteger = bigInteger.add(TWO_COMPL_REF);
}
return bigInteger == null ? null : bigInteger.toString();
}
/**
* 有符号
* 1 byte
*
* @param index 开始下标
* @param data
* @return
*/
public static int get8bitSignedValue(int index, byte[] data) {
int value = 0;
if (data.length > index) {
value = data[index];
}
return value;
}
/**
* 有符号
* 合并 2 byte
*
* @param index 开始下标
* @param data
* @return
*/
public static int get16bitSignedValue(int index, byte[] data) {
int value = 0;
if (data.length > index + 1) {
value = data[index++];
value = value << 8;
value = value | data[index];
}
return value;
}
/**
* 有符号
* 合并 4 byte
*
* @param index 开始下标
* @param data
* @return
*/
public static long get32bitSignedValue(int index, byte[] data) {
long value = 0L;
if (data.length > index + 3) {
value = data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index];
}
return value;
}
/**
* 有符号
* 合并 8 byte
*
* @param index 开始下标
* @param data
* @return
*/
public static long get64bitSignedValue(int index, byte[] data) {
long value = 0;
if (data.length > index + 7) {
value = data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index++];
value = value << 8;
value = value | data[index];
}
return value;
}
/**
* 判断奇数或偶数位运算最后一位是1则为奇数为0是偶数
*
* @param num
* @return
*/
public static int isOdd(int num) {
return num & 0x1;
}
/**
* Hex字符串转byte
*
* @param inHex
* @return
*/
public static byte hexToByte(String inHex) {
return (byte) Integer.parseInt(inHex, 16);
}
/**
* 转hex字符串转字节数组
*
* @param inHex
* @return
*/
public static byte[] hexToByteArr(String inHex) {
inHex = inHex.replaceAll(" ", "");
int hexlen = inHex.length();
byte[] result;
if (isOdd(hexlen) == 1) {//奇数
hexlen++;
result = new byte[(hexlen / 2)];
inHex = "0" + inHex;
} else {//偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2) {
result[j] = hexToByte(inHex.substring(i, i + 2));
j++;
}
return result;
}
/**
* 1字节转2个Hex字符 无符号 不足两位
*
* @param inByte
* @return
*/
public static String byte2Hex(int inByte) {
String hex = Integer.toHexString(inByte & 0xFF).toUpperCase();
return (hex.length() == 1) ? "0" + hex : hex;
}
/**
* 字节数组转转hex字符串
*
* @param inBytArr
* @return
*/
public static String byteArrToHex(byte[] inBytArr) {
return byteArrToHex(inBytArr, true);
}
public static String byteArrToHex(byte[] inBytArr, boolean isAddSpacing) {
if (inBytArr == null)
return null;
StringBuilder strBuilder = new StringBuilder();
for (byte b : inBytArr) {
strBuilder.append(byte2Hex(b));
if (isAddSpacing)
strBuilder.append(" ");
}
return strBuilder.toString().toUpperCase().trim();
}
/**
* 拼接两个byte[]
*
* @param
* @param
* @return
*/
public static byte[] concatBytes(byte[] bt1, byte[] bt2) {
if (bt1 == null) {
return bt2;
}
if (bt2 == null) {
return bt1;
}
byte[] bt3 = new byte[bt1.length + bt2.length];
System.arraycopy(bt1, 0, bt3, 0, bt1.length);
System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
return bt3;
}
//翻转
public static String byteToBit(byte b) {
return "" + (byte) ((b >> 0) & 0x1) +
(byte) ((b >> 1) & 0x1) +
(byte) ((b >> 2) & 0x1) +
(byte) ((b >> 3) & 0x1) +
(byte) ((b >> 4) & 0x1) +
(byte) ((b >> 5) & 0x1) +
(byte) ((b >> 6) & 0x1) +
(byte) ((b >> 7) & 0x1);
}
}