Commit 65b50318 authored by p x's avatar p x
Browse files

解析DTO

parent f2e07035
......@@ -36,7 +36,7 @@ public class VehicleInfoDto {
//档位
@Order(7)
private byte tapPos ;
private short tapPos ;
//方向盘转角
@Order(8)
private double steeringAngle;
......@@ -80,7 +80,7 @@ public class VehicleInfoDto {
//制动主缸压力
@Order(19)
private int brakePressure;
private float brakePressure;
//油耗
@Order(20)
......
......@@ -7,6 +7,8 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
......@@ -18,28 +20,44 @@ public class Parse {
public static <T> T parse(ByteBuf data, T t) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
Field [] fields = t.getClass().getDeclaredFields();
Field[] fields = t.getClass().getDeclaredFields();
List<Field> fList = new ArrayList<>();
SortedMap<Integer,Field> rstMap = new TreeMap<>();
for(Field field : fields) {
SortedMap<Integer, Field> rstMap = new TreeMap<>();
for (Field field : fields) {
if (field.isAnnotationPresent(Order.class)) {
fList.add(field);
}
}
Collections.sort(fList, new Comparator<Field>() {
@Override
public int compare(Field f1, Field f2) {
return f1.getAnnotation(Order.class).value() - f2.getAnnotation(Order.class).value();
}
});
for (Field field : fList) {
field.setAccessible(true);
if (field.isAnnotationPresent(Order.class)) {
String fieldName = field.getName();
Order order = field.getAnnotation(Order.class);
System.out.println(fieldName);
int num = order.value();
//基础数据类型 且 不是数组
if(field.getType().isPrimitive() && !field.getType().isArray() ) {
if (field.getType().isPrimitive() && !field.getType().isArray()) {
// data.re
// field.set(fList, rstMap);
readBuf(t,field,data);
}else if(field.getClass().isPrimitive() && field.getType().isArray()){
readBuf(t, field, data);
} else if (field.getClass().isPrimitive() && field.getType().isArray()) {
// 基础数据类型 且 是数组
}else if(List.class.isAssignableFrom(field.getType())) {
} else if (List.class.isAssignableFrom(field.getType())) {
// 集合类型
//读取属性的注解是否引用其他字段的长度
if( field.isAnnotationPresent(RefNumFlag.class)) {
if (field.isAnnotationPresent(RefNumFlag.class)) {
RefNumFlag refNumFlag = field.getAnnotation(RefNumFlag.class);
String refField = refNumFlag.value();
......@@ -53,18 +71,18 @@ public class Parse {
Type[] actualTypes = pt.getActualTypeArguments();
if (actualTypes.length > 0) {
actualTypes[0].getClass();
int len= (int) nameField.get(t);
for(int i=0;i<len;i++) {
int len = (int) nameField.get(t);
for (int i = 0; i < len; i++) {
actualTypes[0].getClass();
}
}
}
}
}else if(field.getType()==String.class) {
} else if (field.getType() == String.class) {
//类型为字符串
readBuf(t,field,data);
}else {
readBuf(t, field, data);
} else {
//其他对象类型
Class<?> cls = Class.forName(field.getType().getName());
Object obj = cls.newInstance();
......@@ -72,7 +90,7 @@ public class Parse {
parse(data, obj);
field.set(t, obj);
}
rstMap.put(num,field);
rstMap.put(num, field);
}
}
return t;
......@@ -80,45 +98,48 @@ public class Parse {
/**
* 读取字节流
*
* @param t
* @param field
* @param data
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void readBuf(Object t, Field field,ByteBuf data ) throws IllegalArgumentException, IllegalAccessException {
if(data.readerIndex()==data.maxCapacity() )
return ;
FieldDef fieldDef =null;
if(field.isAnnotationPresent(FieldDef.class)) {
public static void readBuf(Object t, Field field, ByteBuf data) throws IllegalArgumentException, IllegalAccessException {
if (data.readerIndex() == data.maxCapacity())
return;
FieldDef fieldDef = null;
if (field.isAnnotationPresent(FieldDef.class)) {
fieldDef = field.getAnnotation(FieldDef.class);
}
Class<?> type = field.getType();
if(type==String.class && fieldDef!=null &&fieldDef.type().equals("BYTE")) {
byte[]by = new byte[fieldDef.length()];
if (type == String.class && fieldDef != null && fieldDef.type().equals("BYTE")) {
byte[] by = new byte[fieldDef.length()];
data.readBytes(by);
String str = new String(by, StandardCharsets.UTF_8);
field.set(t, str);
}else if(type==short.class) {
} else if (type == short.class) {
field.set(t, data.readUnsignedByte());
}else if(type==int.class) {
} else if (type == int.class) {
field.set(t, data.readUnsignedShort());
}else if(type==long.class && fieldDef==null ) {
} else if (type == long.class && fieldDef == null) {
field.set(t, data.readUnsignedInt());
}else if(type==long.class && fieldDef!=null &&fieldDef.type().equals("TIMESTAMP") && fieldDef.length()==8 ) {
} else if (type == long.class && fieldDef != null && fieldDef.type().equals("TIMESTAMP") && fieldDef.length() == 8) {
field.set(t, data.readLong());
}else if(type==long.class && fieldDef!=null &&fieldDef.type().equals("BYTE") ) {
} else if (type == long.class && fieldDef != null && fieldDef.type().equals("BYTE")) {
field.set(t, data.readLong());
}
}
public static byte[] longToBytes(long value) {
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.order(ByteOrder.BIG_ENDIAN); // 指定小端模式
buffer.putLong(value); // 将long值存入buffer
return buffer.array(); // 获取byte数组
}
public static byte[] longToBytes1(long value) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN); // 指定小端模式
......@@ -147,6 +168,7 @@ public class Parse {
// buffer.putLong(value); // 将long值存入buffer
// return buffer.array(); // 获取byte数组
}
public static String long2Hex(long num, int len) {
String hex = Long.toHexString(num);
int padLength = len * 2;
......@@ -157,7 +179,7 @@ public class Parse {
}
public static void main(String [] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
String hexString = "F20000000015018cb864640000000000";
......@@ -167,9 +189,9 @@ public class Parse {
//3600000000
// 1164916946
// byte [] bs = Long.toHexString(l);
String s = long2Hex(l,4);
String s = long2Hex(l, 4);
//longToBytes1(l);
int i = 990;
int i = 33;
String hex = Integer.toHexString(i);
String vid = "T1023459";
......@@ -191,102 +213,104 @@ public class Parse {
}
public static void convoterBean( Object source , Object target) throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException, NoSuchFieldException, SecurityException {
Field [] fields = source.getClass().getDeclaredFields();
List<Field> fList = new ArrayList<>();
SortedMap<Integer,Field> rstMap = new TreeMap<>();
for(Field field : fields) {
public static void convoterBean(Object source, Object target) {
Field field;
Field tField;
try {
Field[] fields = source.getClass().getDeclaredFields();
//List<Field> fList = new ArrayList<>();
//SortedMap<Integer,Field> rstMap = new TreeMap<>();
for (Field cfield : fields) {
field = cfield;
field.setAccessible(true);
Object o= field.get(source);
if(o==null) {
Object o = field.get(source);
if (o == null) {
continue;
}
OffsetDef offsetDef =null;
if(field.isAnnotationPresent(OffsetDef.class)) {
OffsetDef offsetDef = null;
if (field.isAnnotationPresent(OffsetDef.class)) {
offsetDef = field.getAnnotation(OffsetDef.class);
}
//获取目标对象指定的属性
Field tField = target.getClass().getDeclaredField(field.getName());
tField = target.getClass().getDeclaredField(field.getName());
tField.setAccessible(true);
if (field.isAnnotationPresent(Order.class)) {
if(field.getType().isPrimitive() && !field.getType().isArray() ) {
if (field.getType().isPrimitive() && !field.getType().isArray()) {
//如果属性添加了自定义注解 offset
Class<?> type = field.getType();
if(offsetDef!=null) {
double offset =offsetDef.value();
int ofsetType =offsetDef.type();
int minValidLength =offsetDef.minValidLength();
if (offsetDef != null) {
double offset = offsetDef.value();
int type1 = offsetDef.type();
int minValidLength = offsetDef.minValidLength();
//startindex
if(type==byte.class) {
if (type == byte.class) {
tField.set(target, o);
}else if(type==short.class) {
} else if (type == short.class) {
if(minValidLength>0 ) {
int len = String.valueOf((Short)o).length();
if(len<minValidLength) {
int subtract = minValidLength-len;
double f =(Short)o*Math.pow(10, subtract)*offset;
tField.set(target, (float)f);
}else {
double f =(Short)o*offset;
tField.set(target, (float)f);
if (minValidLength > 0) {
int len = String.valueOf((Short) o).length();
if (len < minValidLength) {
int subtract = minValidLength - len;
double f = (Short) o * Math.pow(10, subtract) * offset;
tField.set(target, (float) f);
} else {
double f = (Short) o * offset;
tField.set(target, (float) f);
}
}else {
double f =(Short)o*offset;
tField.set(target, (float)f);
} else {
double f = (Short) o * offset;
tField.set(target, (short) f);
}
}else if(type==int.class) {
if(minValidLength>0 ) {
int len = String.valueOf((Integer)o).length();
if(len<minValidLength) {
int subtract = minValidLength-len;
double f =(Integer)o*Math.pow(10, subtract)*offset;
tField.set(target, (float)f);
}else {
double f =(Integer)o*offset;
tField.set(target, (float)f);
} else if (type == int.class) {
if (minValidLength > 0) {
int len = String.valueOf((Integer) o).length();
if (len < minValidLength) {
int subtract = minValidLength - len;
double f = (Integer) o * Math.pow(10, subtract) * offset;
tField.set(target, (float) f);
} else {
double f = (Integer) o * offset;
tField.set(target, (float) f);
}
}else {
// double f =(Integer)o*offset;
float f = (float) ((Integer)o*offset);
tField.set(target, (float)f);
} else {
double f = (Integer) o * offset;
tField.set(target, (float) f);
}
}else if(type==long.class) {
if(minValidLength>0 ) {
int len = String.valueOf((Long)o).length();
if(len<minValidLength) {
int subtract = minValidLength-len;
double f =(Long)o*Math.pow(10, subtract)*offset;
tField.set(target, (float)f);
}else {
double f =(Long)o*offset;
tField.set(target, (float)f);
} else if (type == long.class) {
if (minValidLength > 0) {
int len = String.valueOf((Long) o).length();
if (len < minValidLength) {
int subtract = minValidLength - len;
double f = (Long) o * Math.pow(10, subtract) * offset;
tField.set(target, (float) f);
} else {
double f = (Long) o * offset;
tField.set(target, (float) f);
}
}else {
double d =(Long)o*offset;
} else {
double d = (Long) o * offset;
tField.set(target, d);
tField.set(target, (float)d);
}
}
}else {
} else {
tField.set(target, o);
}
}else if(field.getClass().isPrimitive() && field.getType().isArray()){
} else if (field.getClass().isPrimitive() && field.getType().isArray()) {
// 基础数据类型 且 是数组
}else if(List.class.isAssignableFrom(field.getType())) {
} else if (List.class.isAssignableFrom(field.getType())) {
// 集合类型
//读取属性的注解是否引用其他字段的长度
if( field.isAnnotationPresent(RefNumFlag.class)) {
if (field.isAnnotationPresent(RefNumFlag.class)) {
// RefNumFlag refNumFlag = field.getAnnotation(RefNumFlag.class);
// String refField = refNumFlag.value();
......@@ -308,7 +332,7 @@ public class Parse {
// }
}
}else if(field.getType()==String.class) {
} else if (field.getType() == String.class) {
//类型为字符串
// try {
// String s = (String)o;
......@@ -317,7 +341,7 @@ public class Parse {
// }catch(Exception e) {
// e.printStackTrace();
// }
}else {
} else {
//其他对象类型
Class<?> cls = Class.forName(tField.getType().getName());
......@@ -328,7 +352,29 @@ public class Parse {
}
}
}
} catch (IllegalArgumentException e) {
System.out.println("e = " + e.getMessage());
} catch (IllegalAccessException e) {
System.out.println("e = " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("e = " + e.getMessage());
} catch (InstantiationException e) {
System.out.println("e = " + e.getMessage());
} catch (NoSuchFieldException e) {
System.out.println("e = " + e.getMessage());
} catch (SecurityException e) {
System.out.println("e = " + e.getMessage());
}
}
public static byte[] convoter(Byte[] bytes) {
byte[] bs = new byte[bytes.length];
int i = 0;
for (Byte b : bytes) {
bs[i++] = b;
}
return bs;
}
}
......@@ -6,10 +6,10 @@ import com.cusc.adas.v2x.utils.Order;
public class Position2D {
//经度
@Order(1)
@OffsetDef(value=10000000)
@OffsetDef(value=0.0000001,minValidLength=10)
private long longitude;
//纬度
@Order(2)
@OffsetDef(value=10000000)
@OffsetDef(value=0.0000001,minValidLength=9)
private long latitude;
}
......@@ -7,69 +7,74 @@ import com.cusc.adas.v2x.utils.NumFlag;
import com.cusc.adas.v2x.utils.OffsetDef;
import com.cusc.adas.v2x.utils.Order;
import com.cusc.adas.v2x.utils.RefNumFlag;
/**
* 车辆基础信息
* @author huangml
*
* @author huangml
*/
public class VehicleInfo {
//车辆编号
@Order(1)
@FieldDef(type="BYTE",isArray=true,length=8)
@FieldDef(type = "BYTE", isArray = true, length = 8)
private String vehicleId;
//消息编号
@Order(2)
@FieldDef(type="BYTE",isArray=true,length=8)
@FieldDef(type = "BYTE", isArray = true, length = 8)
private long messageId;
//GNSS 时间戳
@Order(3)
@FieldDef(type="TIMESTAMP",isArray=false,length=8)
@FieldDef(type = "TIMESTAMP", isArray = false, length = 8)
private long timestampGNSS;
//GNSS 速度 单位:0.01 m/s
@Order(4)
@OffsetDef(value=0.036,type=2)
@OffsetDef(value = 0.036, type = 2)
private int velocityGNSS;
//位置
@Order(5)
private Position position;
//航向角 单位为 1e-4 °
/****[0..3600000],正北方向顺时针旋转至与车辆当前运动方向重合所转 过的角度,单位为 1e-4 ° , 不可缺省,0xFFFFFFFF 表示异常**/
@Order(6)
@OffsetDef(value=0.0001,type = 2)
private int heading;
@OffsetDef(value = 0.0001)
private long heading;
//档位
/**
* 档位 枚举类型:[0..50],0:数据失效;1-20:表示手动档车辆前进档对 应档位,1 表示 1 档,2 表示 2 档,以此类推;21-30:表示手动挡车 辆倒档对应档位,
* 21 表示 R1 档,22 表示 R2 档,以此类推;31:D 档 (前进档);32:R 档(倒档);33:P 档(驻车档);34:N 档(空 档);
* 35:S 档(运动模式);36:L 档(低速档);37:H 档;38; HL 档;39-50:预留,不可缺省,0xFF 表示异常
*/
@Order(7)
private byte tapPos ;
private short tapPos;
//方向盘转角 单位为 1e-4 °
@Order(8)
@OffsetDef(value=0.0001)
@OffsetDef(value = 0.0001)
private long steeringAngle;
//当前车速 单位:0.01m/s
/**[0..20000],CAN 总线数据中的行驶速度,单位:0.01m/s,0xFFFF 表 示缺省***/
@Order(9)
@OffsetDef(value=0.036,type=2)
@OffsetDef(value = 0.036, type = 2)
private int velocity;
//纵向加速度 单位:0.01m/s
@Order(10)
@OffsetDef(value=36,type=2)
@OffsetDef(value = 0.036, type = 2)
private int accelerationLon;
//横向加速度 单位:0.01m/s
@Order(11)
@OffsetDef(value=36,type=2)
@OffsetDef(value = 0.036, type = 2)
private int accelerationLat;
//垂向加速度 单位:0.01m/s
@Order(12)
@OffsetDef(value=36,type=2)
@OffsetDef(value = 0.036, type = 2)
private int accelerationVer;
//横摆角速度 单位:0.01m/s
@Order(13)
@OffsetDef(value=36,type=2)
@OffsetDef(value = 0.036, type = 2)
private int yawRate;
//油门开度 单位:0.1%
@Order(14)
private int accelPos;
//发动机输出转速 单位:r/min
......@@ -98,7 +103,7 @@ public class VehicleInfo {
//车辆驾驶模式
@Order(21)
private byte driveMode;
private short driveMode;
//目的地位置
@Order(22)
......@@ -110,15 +115,8 @@ public class VehicleInfo {
private short passPointsNum;
//途经点
@Order(24)
@RefNumFlag(value="passPointsNum")
@RefNumFlag(value = "passPointsNum")
private List<Position2D> passPoints;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment