ncc协议x--设备位图复合状态转换为proto定义的状态

This commit is contained in:
xzb 2023-06-09 15:37:51 +08:00
parent d47214cc32
commit f930f97a08
2 changed files with 77 additions and 26 deletions

View File

@ -6,42 +6,93 @@ import com.google.protobuf.GeneratedMessageV3;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* 把设备位图状态转换为proto定义的设备状态
* 设备状态转换相关
*/
public class DeviceStatusConvertor {
public static void convert(final Class<?> deviceStatusEnum,final int statusBitMap,final GeneratedMessageV3.Builder to) throws Exception {
final Method enumValues = deviceStatusEnum.getMethod("values");
final Object values = enumValues.invoke(deviceStatusEnum);
final int len = Array.getLength(values);
for(int i=0;i<len;i++){
final Object enumUnit = Array.get(values,i);
final String enumUnitName = enumUnit.toString();
}
/**
* 把设备位图状态转换为proto定义的设备状态
* @param deviceStatusEnum 如DeviceStatus.SIGNAL.class
* @param statusBitMap 该设备的符合状态位图
*/
public static <T extends GeneratedMessageV3.Builder> T convert(final Class<?> deviceStatusEnum, final int statusBitMap) throws Exception {
final GeneratedMessageV3.Builder to = findBy(deviceStatusEnum);
final Class<?> toClass = to.getClass();
final Method enumValues = deviceStatusEnum.getMethod("values");
final Object values = enumValues.invoke(deviceStatusEnum);
final int len = Array.getLength(values);
for (int i = 0; i < len; i++) {
final DeviceStatus.Status devStateEnum = (DeviceStatus.Status) Array.get(values, i);
final String enumUnitName = devStateEnum.toString();
final int devStateMask = devStateEnum.mask();
final String setName = toSetName(enumUnitName);
final Method setMethod = toClass.getMethod(setName, boolean.class);
setMethod.invoke(to, devStateMask == (statusBitMap & devStateMask));
}
return (T) to;
}
public static void main(String[]args) throws Exception {
convert(DeviceStatus.TRAIN_MODE.class,11,null);
//setGreenOpen
DeviceStatusProto.Signal.Builder signal = DeviceStatusProto.Signal.newBuilder();
System.out.println(signal.getGreenOpen());
final Method setGreenOpen = signal.getClass().getMethod("setGreenOpen",boolean.class);
setGreenOpen.invoke(signal,true);
System.out.println(signal.getGreenOpen());
/**
* 以信号机为例测试
*/
public static void main(String[] args) throws Exception {
final int bitMap = DeviceStatus.SIGNAL.GREEN_OPEN.mask() | DeviceStatus.SIGNAL.GREEN_FLASH.mask();
final DeviceStatusProto.Signal.Builder signal = convert(DeviceStatus.SIGNAL.class, bitMap);
System.out.println(String.format("==>>greenOpen = %s greenFlash = %s blueFlash = %s", signal.getGreenOpen(), signal.getGreenFlash(), signal.getBlueFlash()));
}
private static void setFieldValue(final GeneratedMessageV3.Builder to,final int statusBitMap){
private static GeneratedMessageV3.Builder findBy(final Class<?> deviceStatusEnum) {
if (deviceStatusEnum.isAssignableFrom(DeviceStatus.RTU.class)) {
return DeviceStatusProto.Rtu.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.STATION.class)) {
return DeviceStatusProto.Station.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.SIGNAL.class)) {
return DeviceStatusProto.Signal.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.ENTRY.class)) {
return DeviceStatusProto.Entry.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.SWITCH.class)) {
return DeviceStatusProto.Switch.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.TRACK.class)) {
return DeviceStatusProto.Track.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.PLATFORM.class)) {
return DeviceStatusProto.Platform.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.SCADA.class)) {
return DeviceStatusProto.Scada.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.WATERPROOF_DOOR.class)) {
return DeviceStatusProto.WaterProofDoor.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.WORK_AREA.class)) {
return DeviceStatusProto.WorkArea.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.GAMA.class)) {
return DeviceStatusProto.Gama.newBuilder();
} else if (deviceStatusEnum.isAssignableFrom(DeviceStatus.TRAIN_MODE.class)) {
return DeviceStatusProto.TrainMode.newBuilder();
}
throw new RuntimeException(String.format("枚举类型%s对应的proto不存在", deviceStatusEnum.getName()));
}
private static String toFieldName(final String src){
final String[] aa = src.toLowerCase().split("_");
private static String toSetName(final String src) {
final String fieldName = toFieldName(src);
final StringBuilder sb = new StringBuilder();
sb.append("set");
sb.append(fieldName.substring(0, 1)
.toUpperCase());
sb.append(fieldName.substring(1));
return sb.toString();
}
private static String toFieldName(final String src) {
final String[] aa = src.toLowerCase()
.split("_");
final StringBuilder sb = new StringBuilder();
for(int i=0;i<aa.length;i++){
if(i==0){
for (int i = 0; i < aa.length; i++) {
if (i == 0) {
sb.append(aa[i]);
}else{
sb.append(aa[i].substring(0,1).toUpperCase()).append(aa[i].substring(1));
} else {
sb.append(aa[i].substring(0, 1)
.toUpperCase())
.append(aa[i].substring(1));
}
}
return sb.toString();

@ -1 +1 @@
Subproject commit a8afc44290373cc61ad855327637032102fd10a0
Subproject commit 4bc980e0e9fb511279828a3ff474b88e2ca12604