调整移位计算

This commit is contained in:
tiger_zhou 2023-06-13 10:05:29 +08:00
parent f475a82217
commit 2599d0bef3
2 changed files with 142 additions and 131 deletions

View File

@ -1,6 +1,6 @@
package club.joylink.xiannccda.ats.message.convertor;
import club.joylink.xiannccda.ats.message.rep.device.DeviceStatus;
import club.joylink.xiannccda.ats.message.line3.device.DeviceStatus;
import club.joylink.xiannccda.dto.protos.DeviceStatusProto;
import com.google.protobuf.GeneratedMessageV3;
@ -11,139 +11,150 @@ import java.lang.reflect.Method;
* 设备状态转换相关
*/
public class DeviceStatusConvertor {
/**
* Track状态proto
*/
public static void convertForTrack(final int statusBitMap,final DeviceStatusProto.Track.Builder to) throws Exception {
convert(DeviceStatus.TRACK.class,statusBitMap,to);
final int SPEED_LIMIT_MASK = 0xff000000;
final int SPEED_LIMIT_TYPE_MASK = 0x00f00000;
final int speedLimit = (statusBitMap&SPEED_LIMIT_MASK)>>6;
final int speedLimitType = (statusBitMap&SPEED_LIMIT_TYPE_MASK)>>5;
to.setSpeedLimit(speedLimit);
to.setLimitType(DeviceStatusProto.Track.LimitType.forNumber(speedLimitType));
}
/**
* Platform状态proto
*/
public static void convertForPlatform(final int statusBitMap,final int spare,final DeviceStatusProto.Platform.Builder to) throws Exception{
convert(DeviceStatus.PLATFORM.class,statusBitMap,to);
final int NEXT_SECTION_RUNTIME_MASK = 0xffff0000;
final int NEXT_SECTION_RUN_LEVEL_MASK = 0x0000ff00;
final int nextSectionRuntime = (spare&NEXT_SECTION_RUNTIME_MASK)>>4;
final int nextSectionRunLevel = (spare&NEXT_SECTION_RUN_LEVEL_MASK)>>2;
//
final int STOP_TIME_MASK = 0xffff0000;
final int stopTime = (statusBitMap&STOP_TIME_MASK)>>4;
//
to.setNextSectionRunTime(nextSectionRuntime);
to.setNextSectionRunLevel(nextSectionRunLevel);
to.setStopTime(stopTime);
}
/**
* Switch状态proto
*/
public static void convertForSwitch(final int statusBitMap,final int spare,final DeviceStatusProto.Switch.Builder to) throws Exception {
convert(DeviceStatus.SWITCH.class,statusBitMap,to);
final int SPEED_LIMIT_MASK = 0x000000ff;
final int speedLimit = spare&SPEED_LIMIT_MASK;
to.setSpeedLimit(speedLimit);
}
/**
* 把设备位图状态转换为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);
convert(deviceStatusEnum, statusBitMap, to);
return (T) to;
}
/**
* 把设备位图状态转换为proto定义的设备状态
*
* @param deviceStatusEnum 如DeviceStatus.SIGNAL.class
* @param statusBitMap 该设备的符合状态位图
* @param to proto设备状态
*/
public static void convert(final Class<?> deviceStatusEnum, final int statusBitMap, final GeneratedMessageV3.Builder to) throws Exception {
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));
}
}
/**
* 以信号机为例测试
*/
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()));
}
/**
* Track状态proto
*/
public static void convertForTrack(final int statusBitMap, final DeviceStatusProto.Track.Builder to) throws Exception {
convert(DeviceStatus.TRACK.class, statusBitMap, to);
final int SPEED_LIMIT_MASK = 0xff000000;
final int SPEED_LIMIT_TYPE_MASK = 0x00f00000;
// final int speedLimit = (statusBitMap & SPEED_LIMIT_MASK) >> 24;
// final int speedLimitType = (statusBitMap & SPEED_LIMIT_TYPE_MASK) >> 20;
private static GeneratedMessageV3.Builder findBy(final Class<?> deviceStatusEnum) {
final int speedLimit = (statusBitMap >> 24) & SPEED_LIMIT_MASK;
final int speedLimitType = (statusBitMap >> 20) & SPEED_LIMIT_TYPE_MASK;
to.setSpeedLimit(speedLimit);
to.setLimitType(DeviceStatusProto.Track.LimitType.forNumber(speedLimitType));
}
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()));
}
/**
* Platform状态proto
*/
public static void convertForPlatform(final int statusBitMap, final int spare, final DeviceStatusProto.Platform.Builder to) throws Exception {
convert(DeviceStatus.PLATFORM.class, statusBitMap, to);
final int NEXT_SECTION_RUNTIME_MASK = 0xffff0000;
final int NEXT_SECTION_RUN_LEVEL_MASK = 0x0000ff00;
// final int nextSectionRuntime = (spare & NEXT_SECTION_RUNTIME_MASK) >> 16;
// final int nextSectionRunLevel = (spare & NEXT_SECTION_RUN_LEVEL_MASK) >> 8;
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();
}
final int nextSectionRuntime = (spare >> 16) & NEXT_SECTION_RUNTIME_MASK;
final int nextSectionRunLevel = (spare >> 8) & NEXT_SECTION_RUN_LEVEL_MASK;
//
final int STOP_TIME_MASK = 0xffff0000;
final int stopTime = (statusBitMap >> 16) & STOP_TIME_MASK;
//
to.setNextSectionRunTime(nextSectionRuntime);
to.setNextSectionRunLevel(nextSectionRunLevel);
to.setStopTime(stopTime);
}
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) {
sb.append(aa[i]);
} else {
sb.append(aa[i].substring(0, 1)
.toUpperCase())
.append(aa[i].substring(1));
}
}
return sb.toString();
/**
* Switch状态proto
*/
public static void convertForSwitch(final int statusBitMap, final int spare, final DeviceStatusProto.Switch.Builder to) throws Exception {
convert(DeviceStatus.SWITCH.class, statusBitMap, to);
final int SPEED_LIMIT_MASK = 0x000000ff;
final int speedLimit = (spare << 24) & SPEED_LIMIT_MASK;
to.setSpeedLimit(speedLimit);
}
/**
* 把设备位图状态转换为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);
convert(deviceStatusEnum, statusBitMap, to);
return (T) to;
}
/**
* 把设备位图状态转换为proto定义的设备状态
*
* @param deviceStatusEnum 如DeviceStatus.SIGNAL.class
* @param statusBitMap 该设备的符合状态位图
* @param to proto设备状态
*/
public static void convert(final Class<?> deviceStatusEnum, final int statusBitMap, final GeneratedMessageV3.Builder to) throws Exception {
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));
}
}
/**
* 以信号机为例测试
*/
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 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 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) {
sb.append(aa[i]);
} else {
sb.append(aa[i].substring(0, 1)
.toUpperCase())
.append(aa[i].substring(1));
}
}
return sb.toString();
}
}

@ -1 +1 @@
Subproject commit d424b9466a5eeda8b5553a275a495eb7aa1f172a
Subproject commit aeb56f5de4298428f35f606892d5a402dbfbb98e