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

解析DTO

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