diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataRepository.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataRepository.java index 94ceb47..682de58 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataRepository.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataRepository.java @@ -1,12 +1,16 @@ package club.joylink.xiannccda.ats.message.collect; +import club.joylink.xiannccda.dto.protos.DeviceStatusProto; +import com.google.protobuf.Descriptors; import com.google.protobuf.GeneratedMessageV3; import com.google.protobuf.Message; import lombok.Getter; +import org.apache.commons.lang3.StringUtils; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; /** @@ -27,7 +31,7 @@ public class StatusDataRepository { /** * 当前所有设备状态信息 */ - private Map statusDataVOMap = new ConcurrentHashMap<>(); + private Map statusDataVOMap = new ConcurrentHashMap<>(); /** * 初始化时,放入设备状态 @@ -44,7 +48,7 @@ public class StatusDataRepository { * @return 包装的设备状态 */ public void addDeviceStatus(GeneratedMessageV3.Builder builder) { - deviceStatusBuilder.put(StatusDataVO.getIdVal(ID_NAME, builder), builder); + deviceStatusBuilder.put(getIdVal(ID_NAME, builder), builder); } @@ -58,9 +62,9 @@ public class StatusDataRepository { deviceStatusBuilder.forEach((k,v) -> { Message message = null; if (statusDataVOMap.containsKey(k)) { - message = statusDataVOMap.get(k).compareAndChange(v); + message = compareAndChange(statusDataVOMap.get(k), v); } else { - statusDataVOMap.put(k, new StatusDataVO(ID_NAME, v)); + statusDataVOMap.put(k, v); message = v.build(); } if (message != null) { @@ -69,4 +73,54 @@ public class StatusDataRepository { }); return resultMessage; } + + /** + * 对比设备状态目前的 + * + * @param newStatus 新状态 + * @return 变化的字段 + */ + public static Message compareAndChange(GeneratedMessageV3.Builder oldStatus, GeneratedMessageV3.Builder newStatus) { + Descriptors.Descriptor descriptor = oldStatus.getDescriptorForType(); + String curStatusIdVal = getIdVal(ID_NAME, oldStatus); + String newStatusIdVal = getIdVal(ID_NAME, newStatus); + if (StringUtils.isNotEmpty(curStatusIdVal) + && StringUtils.isNotEmpty(newStatusIdVal) && !Objects.equals(newStatusIdVal, curStatusIdVal)) { + return null; + } + // 添加主键标识 + GeneratedMessageV3.Builder resultBuilder = oldStatus.clone().clear(); + resultBuilder.setField(descriptor.findFieldByName(ID_NAME), curStatusIdVal); + // 循环判断字段信息是否一直 + boolean change = false; + List fieldDescriptorList = descriptor.getFields().stream() + .filter(f -> !f.getName().equals(ID_NAME)).toList(); + for (Descriptors.FieldDescriptor field : fieldDescriptorList) { + if (!Objects.equals(oldStatus.getField(field), newStatus.getField(field))) { + change = true; + oldStatus.setField(field, newStatus.getField(field)); + resultBuilder.setField(field, newStatus.getField(field)); + } + } + return change ? resultBuilder.build() : null; + } + + /** + * 获取主键值 + * @param idName 主键字段标识 + * @param statusProto 数据信息 + * @return 主键 + */ + public static String getIdVal(String idName, GeneratedMessageV3.Builder statusProto) { + return String.valueOf(statusProto.getField(statusProto.getDescriptorForType().findFieldByName(idName))); + } + + + public static void main(String[] args) { + DeviceStatusProto.Signal.Builder signal1 = DeviceStatusProto.Signal.newBuilder(); + signal1.setApproachLock(false); + DeviceStatusProto.Signal.Builder signal2 = DeviceStatusProto.Signal.newBuilder(); + signal2.setApproachLock(true); + System.out.println(compareAndChange(signal1, signal2)); + } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataVO.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataVO.java deleted file mode 100644 index 29539aa..0000000 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/StatusDataVO.java +++ /dev/null @@ -1,67 +0,0 @@ -package club.joylink.xiannccda.ats.message.collect; - -import com.google.protobuf.Descriptors; -import com.google.protobuf.GeneratedMessageV3; -import com.google.protobuf.Message; -import lombok.Getter; -import org.apache.commons.lang3.StringUtils; - -import java.util.List; -import java.util.Objects; - -/** - * 状态数据 - */ -@Getter -public record StatusDataVO(String idName, GeneratedMessageV3.Builder statusProto) { - - /** - * 状态数据 - * - * @param idName 主键标识 - * @param statusProto 状态信息 - */ - public StatusDataVO { - } - - /** - * 对比设备状态目前的 - * - * @param newStatus 新状态 - * @return 变化的字段 - */ - public Message compareAndChange(GeneratedMessageV3.Builder newStatus) { - Descriptors.Descriptor descriptor = statusProto.getDescriptorForType(); - String curStatusIdVal = getIdVal(idName, statusProto); - String newStatusIdVal = getIdVal(idName, newStatus); - if (StringUtils.isNotEmpty(curStatusIdVal) - && StringUtils.isNotEmpty(newStatusIdVal) && !Objects.equals(newStatusIdVal, curStatusIdVal)) { - return null; - } - // 添加主键标识 - GeneratedMessageV3.Builder resultBuilder = statusProto.clone().clear(); - resultBuilder.setField(descriptor.findFieldByName(idName), curStatusIdVal); - // 循环判断字段信息是否一直 - boolean change = false; - List fieldDescriptorList = descriptor.getFields().stream() - .filter(f -> !f.getName().equals(idName)).toList(); - for (Descriptors.FieldDescriptor field : fieldDescriptorList) { - if (!Objects.equals(statusProto.getField(field), newStatus.getField(field))) { - change = true; - statusProto.setField(field, newStatus.getField(field)); - resultBuilder.setField(field, newStatus.getField(field)); - } - } - return change ? resultBuilder.build() : null; - } - - /** - * 获取主键值 - * @param idName 主键字段标识 - * @param statusProto 数据信息 - * @return 主键 - */ - public static String getIdVal(String idName, GeneratedMessageV3.Builder statusProto) { - return String.valueOf(statusProto.getField(statusProto.getDescriptorForType().findFieldByName(idName))); - } -}