From a6a20e8b49b4b0c8a3558e2b68c9ff4430487b46 Mon Sep 17 00:00:00 2001 From: tiger_zhou Date: Thu, 24 Aug 2023 11:10:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E9=A2=98=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiannccda/alert/AlertDetailFactory.java | 2 +- .../joylink/xiannccda/alert/NccAlertInfo.java | 23 ++++- .../xiannccda/alert/core/AlertDeviceType.java | 34 +++++++ .../xiannccda/alert/core/AlertManager.java | 11 ++- .../alert/core/AlertMonitoringTask.java | 3 + .../DeviceChangeStatusConvertor.java | 2 +- .../warn/AxleSwitchTrackLedAlertListener.java | 69 +++++++------- .../ats/warn/PlatformAlertMonitoringTask.java | 55 ++++-------- .../warn/SwitchLostAlertMonitoringTask.java | 75 +++++++--------- .../ats/warn/TrainModeAlertListener.java | 23 +++-- .../controller/AlertRecordController.java | 5 +- .../DeviceGuardConfigController.java | 4 +- .../dto/config/DeviceAreaConfigDto.java | 3 +- .../xiannccda/dto/protos/AlertConstProto.java | 25 +++++- .../joylink/xiannccda/entity/AlertRecord.java | 2 + .../xiannccda/entity/DeviceAreaConfig.java | 14 ++- .../xiannccda/service/AlertInfoService.java | 90 ++++++++++--------- .../xiannccda/service/AlertRecordService.java | 14 ++- .../config/DeviceAreaConfigService.java | 25 ++++++ .../config/DeviceGuardConfigService.java | 45 ++++------ .../xiannccda/config/DataConfigTest.java | 14 ++- .../device/DeviceStatusCheckTest.java | 2 +- xian-ncc-da-message | 2 +- 23 files changed, 321 insertions(+), 221 deletions(-) create mode 100644 src/main/java/club/joylink/xiannccda/alert/core/AlertDeviceType.java diff --git a/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java b/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java index ba1d015..923f749 100644 --- a/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java +++ b/src/main/java/club/joylink/xiannccda/alert/AlertDetailFactory.java @@ -127,7 +127,7 @@ public class AlertDetailFactory { .map(AlertTip::getId).orElse(null); return new NccAlertInfo(alertTime, alertType, alertTipId, lineId, alertObject, locatorDeviceId, - "", mock); + null, mock); } private String getId(MessageOrBuilder messageOrBuilder) { diff --git a/src/main/java/club/joylink/xiannccda/alert/NccAlertInfo.java b/src/main/java/club/joylink/xiannccda/alert/NccAlertInfo.java index 45a9d18..cd01d36 100644 --- a/src/main/java/club/joylink/xiannccda/alert/NccAlertInfo.java +++ b/src/main/java/club/joylink/xiannccda/alert/NccAlertInfo.java @@ -23,21 +23,36 @@ public class NccAlertInfo implements AlertInfo { @Setter // private AlertLocation alertLocation; private String alertLocation; + private Long alertLocationId; private boolean mock; @Setter private Integer alarmStatus; - public NccAlertInfo(LocalDateTime alertTime, AlertType alertType, Integer alertTipId, + public NccAlertInfo(LocalDateTime alertTime, AlertType alertType, Integer lineId, String alertObject, String locatorDeviceId, - String alertLocation, boolean mock) { + Long alertLocationId) { this.alertTime = alertTime; this.alertType = alertType; this.alertTipId = alertTipId; this.lineId = lineId; this.alertObject = alertObject; this.locatorDeviceId = locatorDeviceId; - this.alertLocation = alertLocation; - this.mock = mock; + this.alertLocationId = alertLocationId; + this.mock = false; + this.level = buildLevel(); + } + + public NccAlertInfo(LocalDateTime alertTime, AlertType alertType, Integer alertTipId, + Integer lineId, String alertObject, String locatorDeviceId, + Long alertLocationId) { + this.alertTime = alertTime; + this.alertType = alertType; + this.alertTipId = alertTipId; + this.lineId = lineId; + this.alertObject = alertObject; + this.locatorDeviceId = locatorDeviceId; + this.alertLocationId = alertLocationId; + this.mock = false; this.level = buildLevel(); } diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertDeviceType.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertDeviceType.java new file mode 100644 index 0000000..b0d38c7 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertDeviceType.java @@ -0,0 +1,34 @@ +package club.joylink.xiannccda.alert.core; + +import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import java.util.Arrays; +import lombok.Getter; + +public enum AlertDeviceType { + + /** + * 集中站 + */ + DEVICE_TYPE_RTU, + + /** + * 道岔 + */ + DEVICE_TYPE_SWITCH, + /** + * 轨道 + */ + DEVICE_TYPE_TRACK, + + /** + * 站台 + */ + DEVICE_TYPE_PLATFORM, + + /** + * 列车设备 + */ + DEVICE_TYPE_TRAIN + + +} diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertManager.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertManager.java index 3e93d40..18f7451 100644 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertManager.java +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertManager.java @@ -16,6 +16,7 @@ import lombok.Data; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; /** * 事件任务管理器 @@ -74,6 +75,9 @@ public class AlertManager extends EventEmitter { } public boolean needMostShow(String lineId, String customName, int val) { + if (StringUtils.isEmpty(customName)) { + return false; + } AlertTableDetail detail = DEVICE_ALTER_TABLE.get(lineId, customName); if (Objects.isNull(detail)) { detail = new AlertTableDetail(false); @@ -149,10 +153,11 @@ public class AlertManager extends EventEmitter { public boolean add(String deviceCode) { if (this.contains(deviceCode)) { - this.deviceCodes.add(deviceCode); - return true; + return false; + } - return false; + this.deviceCodes.add(deviceCode); + return true; } public boolean contains(String deviceCode) { diff --git a/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java b/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java index a8e05f8..c941dba 100644 --- a/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java +++ b/src/main/java/club/joylink/xiannccda/alert/core/AlertMonitoringTask.java @@ -12,4 +12,7 @@ public interface AlertMonitoringTask { */ void run(); + default boolean timeOver(Long receiveTime, int val) { + return System.currentTimeMillis() > (receiveTime + (val * 1000L)); + } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java index f897a9c..cde1384 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/collect/convertor/DeviceChangeStatusConvertor.java @@ -38,8 +38,8 @@ public class DeviceChangeStatusConvertor extends DefaultConvertor { } else if (builder instanceof Track.Builder trackBuild) { alertManager.emit(new SwitchAndTrackLedAlertEvent(trackBuild)); } else if (builder instanceof Switch.Builder switchBuild) { - alertManager.emit(new SwitchAndTrackLedAlertEvent(switchBuild)); alertManager.emit(new SwitchLostAlertEvent(switchBuild)); + alertManager.emit(new SwitchAndTrackLedAlertEvent(switchBuild)); } else if (builder instanceof Platform.Builder platformBuild) { alertManager.emit(new PlatformAlertEvent(platformBuild)); } diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/AxleSwitchTrackLedAlertListener.java b/src/main/java/club/joylink/xiannccda/ats/warn/AxleSwitchTrackLedAlertListener.java index 0a69121..5b3b777 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/AxleSwitchTrackLedAlertListener.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/AxleSwitchTrackLedAlertListener.java @@ -2,6 +2,7 @@ package club.joylink.xiannccda.ats.warn; import club.joylink.xiannccda.alert.AlertDetailFactory; import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.alert.core.AlertManager; import club.joylink.xiannccda.alert.core.AlertSourceEventListener; import club.joylink.xiannccda.alert.util.AlertUtil; @@ -15,8 +16,11 @@ import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto.GuardUnit; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Switch; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Track; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section; +import club.joylink.xiannccda.entity.DeviceAreaConfig; import club.joylink.xiannccda.entity.DeviceGuardConfig; +import club.joylink.xiannccda.service.AlertInfoService; import club.joylink.xiannccda.service.config.DeviceGuardConfigService; import club.joylink.xiannccda.service.config.Operator; import com.google.protobuf.GeneratedMessageV3; @@ -26,6 +30,7 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Map; import java.util.Objects; +import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @@ -33,14 +38,18 @@ import org.springframework.stereotype.Component; @Component public class AxleSwitchTrackLedAlertListener implements AlertSourceEventListener { - private AlertDetailFactory alertDetailFactory; - private DeviceGuardConfigService configService; - public AxleSwitchTrackLedAlertListener(AlertDetailFactory alertDetailFactory, DeviceGuardConfigService configService) { - this.alertDetailFactory = alertDetailFactory; + private DeviceGuardConfigService configService; + private AlertInfoService alertInfoService; + + public AxleSwitchTrackLedAlertListener(DeviceGuardConfigService configService, AlertInfoService alertInfoService) { + this.configService = configService; + this.alertInfoService = alertInfoService; } + private final static String DEFAULT_NAME = AlertType.AXLE_LED_RED.name(); + @Override public void accept(SwitchAndTrackLedAlertEvent event) { GeneratedMessageV3.Builder o = event.getSource(); @@ -83,47 +92,35 @@ public class AxleSwitchTrackLedAlertListener implements AlertSourceEventListener private void emit(boolean light, Builder build, Section section, AlertType alertType) { AlertManager alertManager = AlertManager.getDefault(); Integer lineId = DeviceStatusDataOperate.findFieldVal(build, "lineId", Integer.class); - Integer rtuId = DeviceStatusDataOperate.findFieldVal(build, "rtuId", Integer.class); String id = DeviceStatusDataOperate.findFieldVal(build, "id", String.class); - Long timestemp = DeviceStatusDataOperate.findFieldVal(build, "timestamp", Long.class); - String alertTypeName = String.format("%s-%s-%s", alertType.name(), lineId, rtuId); - String alertTypeMostName = String.format("%s-%s", alertTypeName, "counter"); - LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestemp), ZoneId.of("+8")); - - //TODO 检查对应的类型下数量是否超过阈值,超过阈值及达到大范围报警 - Map configMaper = configService.getGuardConfig(lineId, DeviceType.DEVICE_TYPE_TRACK); + GuardConfig guardConfig = configService.getGuardConfig(lineId); AlertType mostType = alertType == AlertType.AXLE_LED_RED ? AlertType.AXLE_LED_RED_MOST : AlertType.AXLE_LED_ORANGE_MOST; - DeviceGuardConfig guardConfig = this.getConfigOrDefault(mostType, configMaper); - /* if (alertManager.needMostShow(lineId.toString(), alertTypeMostName, guardConfig.getVal())) { - NccAlertInfo alertInfo = this.alertDetailFactory.getAlertDetail(createTime, alertType, lineId, false, section); - alertInfo.setAlertLocation(AlertUtil.findLocationByRtuCode(id).orElse(null)); - alertManager.emit(alertInfo); + int overNums = guardConfig.getRedLedMostNums(); + if (mostType == AlertType.AXLE_LED_ORANGE_MOST) { + overNums = guardConfig.getOrangeLedMostNums(); } + Optional alertInfoOptional = this.alertInfoService.findAreaDevice(alertType, AlertDeviceType.DEVICE_TYPE_TRACK, section.getCommon().getId(), lineId); + Optional alertInfoMostOptional = this.alertInfoService.findAreaDevice(mostType, AlertDeviceType.DEVICE_TYPE_TRACK, section.getCommon().getId(), lineId); + String ledName = alertInfoOptional.isPresent() ? alertInfoOptional.get().getAreaName() : alertType.name(); + String ledMostName = alertInfoMostOptional.map(DeviceAreaConfig::getAreaName).orElse(null); if (light) { - if (alertManager.putAlterDevice(lineId.toString(), alertTypeName, id)) { - NccAlertInfo alertInfo = this.alertDetailFactory.getAlertDetail(createTime, alertType, lineId, false, section); - alertInfo.setAlertLocation(AlertUtil.findLocationByRtuCode(id).orElse(null)); + if (alertManager.putAlterDevice(lineId.toString(), ledName, id)) { + NccAlertInfo alertInfo = this.alertInfoService.createAlert2(alertInfoOptional, alertType, build); alertManager.emit(alertInfo); } + if (Objects.nonNull(ledMostName) && alertManager.needMostShow(lineId.toString(), ledMostName, overNums)) { + NccAlertInfo alertInfoMost = this.alertInfoService.createAlert2(alertInfoOptional, alertType, build); + alertManager.emit(alertInfoMost); + } } else { - alertManager.removeAlterDevice(lineId.toString(), alertTypeName, id); - }*/ + alertManager.removeAlterDevice(lineId.toString(), ledName, id); + if (StringUtils.isNotEmpty(ledMostName)) { + alertManager.removeAlterDevice(lineId.toString(), ledMostName, id); + + } + } } - private DeviceGuardConfig getConfigOrDefault(AlertType mostType, Map configMap) { - AlertType configDeviceType = mostType == AlertType.AXLE_LED_RED_MOST ? AlertType.AXLE_LED_RED_MOST : AlertType.AXLE_LED_ORANGE_MOST; - DeviceGuardConfig guardConfig = configMap.get(configDeviceType); - if (Objects.nonNull(guardConfig)) { - return guardConfig; - } - - if (configDeviceType == AlertType.AXLE_LED_RED_MOST) { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_TRACK, configDeviceType, Operator.GE, GuardUnit.NUMS, 5); - } else { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_TRACK, AlertType.AXLE_LED_ORANGE_MOST, Operator.GE, GuardUnit.NUMS, 5); - } - return guardConfig; - } } diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/PlatformAlertMonitoringTask.java b/src/main/java/club/joylink/xiannccda/ats/warn/PlatformAlertMonitoringTask.java index e9cd005..a9db8d4 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/PlatformAlertMonitoringTask.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/PlatformAlertMonitoringTask.java @@ -2,20 +2,16 @@ package club.joylink.xiannccda.ats.warn; import club.joylink.xiannccda.alert.AlertDetailFactory; import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.alert.core.AlertManager; import club.joylink.xiannccda.alert.core.AlertMonitoringTask; import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; -import club.joylink.xiannccda.ats.message.line3.device.DeviceType; -import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto.GuardUnit; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Platform; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto; -import club.joylink.xiannccda.entity.DeviceGuardConfig; +import club.joylink.xiannccda.service.AlertInfoService; import club.joylink.xiannccda.service.config.DeviceGuardConfigService; -import club.joylink.xiannccda.service.config.Operator; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; @@ -26,19 +22,21 @@ import org.springframework.stereotype.Component; @Slf4j public class PlatformAlertMonitoringTask implements AlertMonitoringTask { - private AlertDetailFactory alertDetailFactory; - private DeviceGuardConfigService configService; + private final AlertDetailFactory alertDetailFactory; + private final DeviceGuardConfigService configService; + private final AlertInfoService alertInfoService; - public PlatformAlertMonitoringTask(AlertDetailFactory alertDetailFactory, DeviceGuardConfigService configService) { + public PlatformAlertMonitoringTask(AlertDetailFactory alertDetailFactory, DeviceGuardConfigService configService, AlertInfoService alertInfoService) { this.alertDetailFactory = alertDetailFactory; this.configService = configService; + this.alertInfoService = alertInfoService; } private AlertManager alertManager = AlertManager.getDefault(); - private final static String DEVICE_TYPE_NAME = Platform.getDescriptor().getName(); Map platformMap = new ConcurrentHashMap<>(); + private final static String CUSTOM_NAME = AlertDeviceType.DEVICE_TYPE_PLATFORM.name(); public void putSwitchIfNotExist(Platform.Builder platformBuild) { Platform.Builder saveBuilder = this.platformMap.get(platformBuild.getId()); @@ -54,7 +52,7 @@ public class PlatformAlertMonitoringTask implements AlertMonitoringTask { public void removeSwitch(Platform.Builder platformBuild) { platformMap.remove(platformBuild.getId()); - alertManager.removeAlterDevice(String.valueOf(platformBuild.getLineId()), DEVICE_TYPE_NAME, platformBuild.getId()); + alertManager.removeAlterDevice(String.valueOf(platformBuild.getLineId()), CUSTOM_NAME, platformBuild.getId()); } @Override @@ -67,40 +65,21 @@ public class PlatformAlertMonitoringTask implements AlertMonitoringTask { public void run() { for (Platform.Builder build : platformMap.values()) { String lineId = String.valueOf(build.getLineId()); - Map configMaper = configService.getGuardConfig(build.getLineId(), DeviceType.DEVICE_TYPE_PLATFORM); - DeviceGuardConfig guardConfig = this.getConfigOrDefault(AlertType.PLATFORM_DOOR_CANNOT_OPEN, configMaper); + GuardConfig guardConfig = configService.getGuardConfig(build.getLineId()); AlertType alertType = AlertType.PLATFORM_DOOR_CANNOT_OPEN; + int timeOut = guardConfig.getCanNotOpenTimes(); if (build.getPsdOpen()) { - guardConfig = this.getConfigOrDefault(AlertType.PLATFORM_DOOR_CANNOT_CLOSE, configMaper); alertType = AlertType.PLATFORM_DOOR_CANNOT_CLOSE; + timeOut = guardConfig.getCanNotCloseTimes(); } - if (/*guardConfig.getDeviceOperator().contrast(guardConfig.getDeviceUnit(), build.getReceiveTime(), guardConfig.getVal()) - &&*/ alertManager.putAlterDevice(lineId, "", build.getId())) { - NccAlertInfo alertInfo = this.createAlertTip(build, alertType); + if (this.timeOver(build.getReceiveTime(), timeOut) + && alertManager.putAlterDevice(lineId, CUSTOM_NAME, build.getId())) { + LayoutGraphicsProto.Platform platform = LineGraphicDataRepository.getDeviceByCode(build.getLineId(), build.getId(), LayoutGraphicsProto.Platform.class); + NccAlertInfo alertInfo = this.alertInfoService.createAlert(alertType, AlertDeviceType.DEVICE_TYPE_PLATFORM, platform.getCommon().getId(), build); alertManager.emit(alertInfo); } } } - private NccAlertInfo createAlertTip(Platform.Builder platformBuild, AlertType alertType) { - LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(platformBuild.getTimestamp()), ZoneId.of("+8")); - LayoutGraphicsProto.Platform platform = LineGraphicDataRepository.getDeviceByCode(platformBuild.getLineId(), platformBuild.getId(), LayoutGraphicsProto.Platform.class); - NccAlertInfo alertInfo = this.alertDetailFactory.getAlertDetail(createTime, alertType, platformBuild.getLineId(), false, platform); - return alertInfo; - } - - private DeviceGuardConfig getConfigOrDefault(AlertType configDeviceType, Map configMap) { - DeviceGuardConfig guardConfig = configMap.get(configDeviceType); - if (Objects.nonNull(guardConfig)) { - return guardConfig; - } - if (configDeviceType == AlertType.PLATFORM_DOOR_CANNOT_OPEN) { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_SWITCH, configDeviceType, Operator.GE, GuardUnit.TIMES, 30); - } else { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_SWITCH, AlertType.PLATFORM_DOOR_CANNOT_CLOSE, Operator.GE, GuardUnit.TIMES, 60); - } - return guardConfig; - } - } diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/SwitchLostAlertMonitoringTask.java b/src/main/java/club/joylink/xiannccda/ats/warn/SwitchLostAlertMonitoringTask.java index 9fb1388..e7f6fef 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/SwitchLostAlertMonitoringTask.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/SwitchLostAlertMonitoringTask.java @@ -2,6 +2,7 @@ package club.joylink.xiannccda.ats.warn; import club.joylink.xiannccda.alert.AlertDetailFactory; import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.alert.core.AlertManager; import club.joylink.xiannccda.alert.core.AlertMonitoringTask; import club.joylink.xiannccda.alert.util.AlertUtil; @@ -11,7 +12,9 @@ import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto.GuardUnit; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Switch; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Switch.Builder; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Turnout; +import club.joylink.xiannccda.entity.DeviceAreaConfig; import club.joylink.xiannccda.entity.DeviceGuardConfig; import club.joylink.xiannccda.service.AlertInfoService; import club.joylink.xiannccda.service.config.DeviceGuardConfigService; @@ -21,21 +24,22 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; @Component @Slf4j public class SwitchLostAlertMonitoringTask implements AlertMonitoringTask { - private AlertDetailFactory alertDetailFactory; private DeviceGuardConfigService configService; private AlertInfoService alertInfoService; - public SwitchLostAlertMonitoringTask(AlertDetailFactory alertDetailFactory, DeviceGuardConfigService configService, AlertInfoService alertInfoService) { - this.alertDetailFactory = alertDetailFactory; + public SwitchLostAlertMonitoringTask(DeviceGuardConfigService configService, AlertInfoService alertInfoService) { + this.configService = configService; this.alertInfoService = alertInfoService; } @@ -63,52 +67,37 @@ public class SwitchLostAlertMonitoringTask implements AlertMonitoringTask { return "SWITCH_LOST_ALTER"; } -/* private NccAlertInfo createAlterTip(Switch.Builder switchBuild, String lineId, AlertType alertType) { - LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(switchBuild.getTimestamp()), ZoneId.of("+8")); - NccAlertInfo alertInfo = this.alertDetailFactory.getAlertDetail(createTime, alertType, Integer.valueOf(lineId), false, switchBuild); - alertInfo.setAlertLocation(AlertUtil.findLocationByRtuCode(String.valueOf(switchBuild.getRtuId())).orElse(null)); - return alertInfo; - }*/ @Override public void run() { -// for (Builder savedSwitchBuild : this.deviceMap.values()) { -// String lineIdStr = String.valueOf(savedSwitchBuild.getLineId()); -// Map configMaper = configService.getGuardConfig(savedSwitchBuild.getLineId(), DeviceType.DEVICE_TYPE_SWITCH); -// DeviceGuardConfig guardConfig = this.getConfigOrDefault(AlertType.SWITCH_LOST, configMaper); -// boolean saveIsLost = savedSwitchBuild.getIpSingleSwitchStusLostIndication(); -// if (saveIsLost && guardConfig.getDeviceOperator().contrast(guardConfig.getDeviceUnit(), savedSwitchBuild.getReceiveTime(), guardConfig.getVal())) { -// Turnout turnout = LineGraphicDataRepository.getDeviceByCode(savedSwitchBuild.getLineId(), savedSwitchBuild.getId(), Turnout.class); -// //失表超时 -// if (alertManager.putAlterDevice(lineIdStr, DEVICE_TYPE_NAME, savedSwitchBuild.getId())) { -// NccAlertInfo alertInfo = this.alertInfoService.createAlert(AlertType.SWITCH_LOST, DeviceType.DEVICE_TYPE_SWITCH, turnout.getCommon().getId(), -// savedSwitchBuild); -// alertManager.emit(alertInfo); -// } -// //检测大面积失表 -// DeviceGuardConfig mostGuardConfig = this.getConfigOrDefault(AlertType.SWITCH_LOST_MOST, configMaper); -// if (alertManager.needMostShow(lineIdStr, "", mostGuardConfig.getVal())) { -// NccAlertInfo alertInfo = this.alertInfoService.createAlert(AlertType.SWITCH_LOST_MOST, DeviceType.DEVICE_TYPE_SWITCH, turnout.getCommon().getId(), -// savedSwitchBuild); -// alertManager.emit(alertInfo); -// } -// } -// -// } - } + for (Builder savedSwitchBuild : this.deviceMap.values()) { + String lineIdStr = String.valueOf(savedSwitchBuild.getLineId()); + GuardConfig guardConfig = configService.getGuardConfig(savedSwitchBuild.getLineId()); + Turnout turnout = LineGraphicDataRepository.getDeviceByCode(savedSwitchBuild.getLineId(), savedSwitchBuild.getId(), Turnout.class); - private DeviceGuardConfig getConfigOrDefault(AlertType configDeviceType, Map configMap) { - DeviceGuardConfig guardConfig = configMap.get(configDeviceType); - if (Objects.nonNull(guardConfig)) { - return guardConfig; - } + Optional alertInfoOpt = this.alertInfoService.findAreaDevice(AlertType.SWITCH_LOST, AlertDeviceType.DEVICE_TYPE_SWITCH, turnout.getCommon().getId(), + savedSwitchBuild.getLineId()); + + String oneName = alertInfoOpt.isPresent() ? alertInfoOpt.get().getAreaName() : AlertType.SWITCH_LOST.name(); + + boolean saveIsLost = savedSwitchBuild.getIpSingleSwitchStusLostIndication(); + if (saveIsLost && this.timeOver(savedSwitchBuild.getReceiveTime(), guardConfig.getSwitchLostTimes())) { + //失表超时 + if (alertManager.putAlterDevice(lineIdStr, oneName, savedSwitchBuild.getId())) { + NccAlertInfo alertInfo = this.alertInfoService.createAlert2(alertInfoOpt, AlertType.SWITCH_LOST, savedSwitchBuild); + alertManager.emit(alertInfo); + } + Optional alertInfoMostOpt = this.alertInfoService.findAreaDevice(AlertType.SWITCH_LOST_MOST, AlertDeviceType.DEVICE_TYPE_SWITCH, turnout.getCommon().getId(), + savedSwitchBuild.getLineId()); + //检测大面积失表 + if (alertInfoMostOpt.isPresent() && alertManager.needMostShow(lineIdStr, alertInfoMostOpt.get().getAreaName(), guardConfig.getSwitchLostMostNums())) { + + NccAlertInfo alertInfo = this.alertInfoService.createAlert2(alertInfoMostOpt, AlertType.SWITCH_LOST_MOST, savedSwitchBuild); + alertManager.emit(alertInfo); + } + } - if (configDeviceType == AlertType.SWITCH_LOST) { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_SWITCH, configDeviceType, Operator.GE, GuardUnit.TIMES, 13); - } else { - guardConfig = this.configService.buildDefault(DeviceType.DEVICE_TYPE_SWITCH, AlertType.SWITCH_LOST_MOST, Operator.GE, GuardUnit.NUMS, 2); } - return guardConfig; } diff --git a/src/main/java/club/joylink/xiannccda/ats/warn/TrainModeAlertListener.java b/src/main/java/club/joylink/xiannccda/ats/warn/TrainModeAlertListener.java index 891bc22..6c88574 100644 --- a/src/main/java/club/joylink/xiannccda/ats/warn/TrainModeAlertListener.java +++ b/src/main/java/club/joylink/xiannccda/ats/warn/TrainModeAlertListener.java @@ -1,11 +1,19 @@ package club.joylink.xiannccda.ats.warn; import club.joylink.xiannccda.alert.AlertDetailFactory; +import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.alert.core.AlertManager; import club.joylink.xiannccda.alert.core.AlertSourceEventListener; +import club.joylink.xiannccda.ats.cache.LineGraphicDataRepository; +import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.Switch; import club.joylink.xiannccda.dto.protos.DeviceStatusProto.TrainMode; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section; +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Turnout; import club.joylink.xiannccda.dto.protos.TrainProto.TrainInfo; +import club.joylink.xiannccda.service.AlertInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -13,14 +21,15 @@ import org.springframework.stereotype.Component; @Component public class TrainModeAlertListener implements AlertSourceEventListener { - private AlertDetailFactory alertDetailFactory; + private final AlertInfoService alertInfoService; - public TrainModeAlertListener(AlertDetailFactory alertDetailFactory) { - this.alertDetailFactory = alertDetailFactory; + public TrainModeAlertListener(AlertInfoService alertInfoService) { + this.alertInfoService = alertInfoService; } private AlertManager alertManager = AlertManager.getDefault(); + private final static String CUSTOM_NAME = AlertType.TRAIN_SIGNAL.name(); @Override public void accept(TrainAlertEvent event) { @@ -28,9 +37,13 @@ public class TrainModeAlertListener implements AlertSourceEventListener initData(@PathVariable("lineId") Integer lineId) { + public DeviceGuardConfig initData(@PathVariable("lineId") Integer lineId) { LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceGuardConfig.class); qw.eq(DeviceGuardConfig::getLineId, lineId); - return this.iDeviceGuardConfigRepository.list(qw); + return this.iDeviceGuardConfigRepository.getOne(qw, false); } @SecurityRequirement(name = "jwt") diff --git a/src/main/java/club/joylink/xiannccda/dto/config/DeviceAreaConfigDto.java b/src/main/java/club/joylink/xiannccda/dto/config/DeviceAreaConfigDto.java index 8ca22d5..77c3d19 100644 --- a/src/main/java/club/joylink/xiannccda/dto/config/DeviceAreaConfigDto.java +++ b/src/main/java/club/joylink/xiannccda/dto/config/DeviceAreaConfigDto.java @@ -1,5 +1,6 @@ package club.joylink.xiannccda.dto.config; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import java.util.List; import lombok.Data; @@ -10,7 +11,7 @@ public class DeviceAreaConfigDto { private Long id; private Integer lineId; private String areaName; - private DeviceType deviceType; + private AlertDeviceType deviceType; private List data; private List alertTypes; } diff --git a/src/main/java/club/joylink/xiannccda/dto/protos/AlertConstProto.java b/src/main/java/club/joylink/xiannccda/dto/protos/AlertConstProto.java index eb4e8ba..1969c41 100644 --- a/src/main/java/club/joylink/xiannccda/dto/protos/AlertConstProto.java +++ b/src/main/java/club/joylink/xiannccda/dto/protos/AlertConstProto.java @@ -327,6 +327,14 @@ public final class AlertConstProto { * SWITCH_LOST_MOST = 17; */ SWITCH_LOST_MOST(17), + /** + *
+     *信号故障
+     * 
+ * + * TRAIN_SIGNAL = 18; + */ + TRAIN_SIGNAL(18), UNRECOGNIZED(-1), ; @@ -454,6 +462,14 @@ public final class AlertConstProto { * SWITCH_LOST_MOST = 17; */ public static final int SWITCH_LOST_MOST_VALUE = 17; + /** + *
+     *信号故障
+     * 
+ * + * TRAIN_SIGNAL = 18; + */ + public static final int TRAIN_SIGNAL_VALUE = 18; public final int getNumber() { @@ -496,6 +512,7 @@ public final class AlertConstProto { case 15: return AXLE_LED_ORANGE; case 16: return AXLE_LED_ORANGE_MOST; case 17: return SWITCH_LOST_MOST; + case 18: return TRAIN_SIGNAL; default: return null; } } @@ -564,7 +581,7 @@ public final class AlertConstProto { "\n\020alertConst.proto\022\005alert*g\n\rAlertLocati" + "on\022\032\n\026ALERT_LOCATION_UNKNOWN\020\000\022\006\n\002QX\020\001\022\013" + "\n\007YHZ_LSQ\020\002\022\013\n\007HJM_LSQ\020\003\022\013\n\007BCT_LSQ\020\004\022\013\n" + - "\007BSQ_LSQ\020\005*\200\003\n\tAlertType\022\026\n\022ALERT_TYPE_U" + + "\007BSQ_LSQ\020\005*\222\003\n\tAlertType\022\026\n\022ALERT_TYPE_U" + "NKNOWN\020\000\022\020\n\014BLUE_DISPLAY\020\001\022\021\n\rTRAIN_DELA" + "Y_2\020\002\022\022\n\016TRAIN_DELAY_10\020\003\022\'\n#PLATFORM_DO" + "OR_WITHOUT_LOCKED_SIGNAL\020\004\022\035\n\031PLATFORM_D" + @@ -573,9 +590,9 @@ public final class AlertConstProto { "_LOST\020\010\022\022\n\016SWITCH_DW_LOST\020\t\022\022\n\016SWITCH_FW" + "_LOST\020\n\022\020\n\014AXLE_LED_RED\020\r\022\025\n\021AXLE_LED_RE" + "D_MOST\020\016\022\023\n\017AXLE_LED_ORANGE\020\017\022\030\n\024AXLE_LE" + - "D_ORANGE_MOST\020\020\022\024\n\020SWITCH_LOST_MOST\020\021B4\n" + - "!club.joylink.xiannccda.dto.protosB\017Aler" + - "tConstProtob\006proto3" + "D_ORANGE_MOST\020\020\022\024\n\020SWITCH_LOST_MOST\020\021\022\020\n" + + "\014TRAIN_SIGNAL\020\022B4\n!club.joylink.xiannccd" + + "a.dto.protosB\017AlertConstProtob\006proto3" }; descriptor = com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, diff --git a/src/main/java/club/joylink/xiannccda/entity/AlertRecord.java b/src/main/java/club/joylink/xiannccda/entity/AlertRecord.java index 2aefcd6..0f4d671 100644 --- a/src/main/java/club/joylink/xiannccda/entity/AlertRecord.java +++ b/src/main/java/club/joylink/xiannccda/entity/AlertRecord.java @@ -36,6 +36,7 @@ public class AlertRecord { private LocalDateTime alertTime; private String alertLocation; + private Long alertLocationId; private Long alertTipId; private Integer alarmStatus; @@ -50,6 +51,7 @@ public class AlertRecord { public static final String ALERT_TIME = "alert_time"; public static final String ALERT_LOCATION = "alert_location"; + public static final String ALERT_LOCATION_ID = "alert_location_id"; public static final String ALERT_TIP_ID = "alert_tip_id"; public static final String ALARM_STATUS = "alarm_status"; } diff --git a/src/main/java/club/joylink/xiannccda/entity/DeviceAreaConfig.java b/src/main/java/club/joylink/xiannccda/entity/DeviceAreaConfig.java index 484b50c..5b8a380 100644 --- a/src/main/java/club/joylink/xiannccda/entity/DeviceAreaConfig.java +++ b/src/main/java/club/joylink/xiannccda/entity/DeviceAreaConfig.java @@ -1,13 +1,18 @@ package club.joylink.xiannccda.entity; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; +import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.google.common.base.Splitter; import io.swagger.v3.oas.annotations.media.Schema; +import java.util.List; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; +import org.apache.commons.lang3.StringUtils; /** *

@@ -31,7 +36,7 @@ public class DeviceAreaConfig { private String areaName; - private DeviceType deviceType; + private AlertDeviceType deviceType; private String data; private String alertTypes; @@ -46,4 +51,11 @@ public class DeviceAreaConfig { public static final String DATA = "data"; public static final String ALERT_TYPES = "alert_types"; + + public boolean findAlertTypes(AlertType at) { + if (StringUtils.isEmpty(this.alertTypes)) { + return false; + } + return Splitter.on(",").splitToStream(this.alertTypes).anyMatch(d -> StringUtils.equalsIgnoreCase(d, at.name())); + } } diff --git a/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java b/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java index bbb830f..9868c73 100644 --- a/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java +++ b/src/main/java/club/joylink/xiannccda/service/AlertInfoService.java @@ -1,6 +1,7 @@ package club.joylink.xiannccda.service; import club.joylink.xiannccda.alert.NccAlertInfo; +import club.joylink.xiannccda.alert.core.AlertDeviceType; import club.joylink.xiannccda.ats.message.collect.DeviceStatusDataOperate; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; @@ -9,6 +10,7 @@ import club.joylink.xiannccda.entity.DeviceAreaConfig; import club.joylink.xiannccda.repository.IAlertTipRepository; import club.joylink.xiannccda.repository.IDeviceAreaConfigRepository; import club.joylink.xiannccda.repository.IDeviceGuardConfigRepository; +import club.joylink.xiannccda.service.config.DeviceAreaConfigService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -19,12 +21,14 @@ import com.google.protobuf.MessageOrBuilder; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.DeleteMapping; @Service public class AlertInfoService { @@ -32,31 +36,55 @@ public class AlertInfoService { private final AlertTipService alertTipService; private final IDeviceAreaConfigRepository areaConfigRepository; + private final DeviceAreaConfigService deviceAreaConfigService; - public AlertInfoService(AlertTipService alertTipService, IDeviceAreaConfigRepository areaConfigRepository) { + public AlertInfoService(AlertTipService alertTipService, IDeviceAreaConfigRepository areaConfigRepository, DeviceAreaConfigService deviceAreaConfigService) { this.alertTipService = alertTipService; this.areaConfigRepository = areaConfigRepository; + this.deviceAreaConfigService = deviceAreaConfigService; } + private List findDevice(AlertType alertType, AlertDeviceType dt, int lineId) { + List areaConfigList = this.deviceAreaConfigService.getCache(lineId, dt); + if (CollectionUtils.isEmpty(areaConfigList)) { + return Collections.emptyList(); + } + List findList = areaConfigList.stream().filter(d -> d.findAlertTypes(alertType)).toList(); - private List findDevice(AlertType alertType, DeviceType dt, int lineId) { - LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceAreaConfig.class); + /*LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceAreaConfig.class); qw.eq(DeviceAreaConfig::getDeviceType, dt.name()); qw.eq(DeviceAreaConfig::getLineId, lineId); qw.last(String.format(" and find_in_set('%s',%s) > 0 ", alertType.name(), DeviceAreaConfig.ALERT_TYPES)); - List areaConfigList = this.areaConfigRepository.list(qw); - return areaConfigList; - + List areaConfigList = this.areaConfigRepository.list(qw);*/ + return findList; } - public NccAlertInfo createAlert(AlertType alertType, DeviceType dt, String layoutId, MessageOrBuilder mb) { + public NccAlertInfo createAlert(AlertType alertType, AlertDeviceType dt, String layoutId, MessageOrBuilder mb) { + Integer lineId = DeviceStatusDataOperate.findFieldVal(mb, "lineId", Integer.class); + Optional areaConfigOptional = this.findAreaDevice(alertType, dt, layoutId, lineId); + return this.createAlert2(areaConfigOptional, alertType, mb); + } + + public NccAlertInfo createAlert2(Optional areaConfigOpt, AlertType alertType, MessageOrBuilder mb) { Long timestamp = DeviceStatusDataOperate.findFieldVal(mb, "timestamp", Long.class); - int lineId = DeviceStatusDataOperate.findFieldVal(mb, "lineId", Integer.class); + Integer lineId = DeviceStatusDataOperate.findFieldVal(mb, "lineId", Integer.class); + String deviceCode = DeviceStatusDataOperate.findFieldVal(mb, "id", String.class); + LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.of("+8")); + DeviceAreaConfig areaConfig = areaConfigOpt.orElse(null); + if (Objects.isNull(areaConfig)) { + return new NccAlertInfo(createTime, alertType, lineId, deviceCode, deviceCode, null); + } + return new NccAlertInfo(createTime, alertType, lineId, String.format("%-设备[%s]", areaConfig.getAreaName(), deviceCode), deviceCode, areaConfig.getId()); + } + + @Deprecated + public NccAlertInfo createAlert(DeviceAreaConfig areaConfig, AlertType alertType, MessageOrBuilder mb) { + Long timestamp = DeviceStatusDataOperate.findFieldVal(mb, "timestamp", Long.class); + Integer lineId = DeviceStatusDataOperate.findFieldVal(mb, "lineId", Integer.class); String deviceCode = DeviceStatusDataOperate.findFieldVal(mb, "id", String.class); - List areaConfigList = this.findDevice(alertType, dt, lineId); - DeviceAreaConfig areaConfig = this.findAreaDevice(areaConfigList, dt, layoutId); Optional alertTipOptional; + if (Objects.isNull(areaConfig)) { alertTipOptional = this.alertTipService.queryOne2(alertType.name(), null); } else { @@ -65,10 +93,16 @@ public class AlertInfoService { AlertTip at = alertTipOptional.orElse(null); LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.of("+8")); - return new NccAlertInfo(createTime, alertType, Objects.nonNull(at) ? at.getId() : null, lineId, deviceCode, deviceCode, Objects.nonNull(areaConfig) ? areaConfig.getAreaName() : null, false); + return new NccAlertInfo(createTime, alertType, Objects.nonNull(at) ? at.getId() : null, lineId, deviceCode, deviceCode, null); } - private DeviceAreaConfig findAreaDevice(List areaConfigList, DeviceType dt, String layoutId) { + public Optional findAreaDevice(AlertType alertType, AlertDeviceType dt, String layoutId, int lineId) { + List areaConfigList = this.findDevice(alertType, dt, lineId); + DeviceAreaConfig areaConfig = this.findAreaDevice(areaConfigList, dt, layoutId); + return Optional.ofNullable(areaConfig); + } + + private DeviceAreaConfig findAreaDevice(List areaConfigList, AlertDeviceType dt, String layoutId) { if (CollectionUtils.isEmpty(areaConfigList)) { //TODO return null; @@ -85,36 +119,4 @@ public class AlertInfoService { } - public interface AreaConfigFinder { - - DeviceType deviceType(); - - boolean matcher(DeviceAreaConfig areaConfig, String deviceCode); - } - - public static class SwitchFinder implements AreaConfigFinder { - - @Override - public DeviceType deviceType() { - return DeviceType.DEVICE_TYPE_SWITCH; - } - - @Override - public boolean matcher(DeviceAreaConfig areaConfig, String deviceCode) { - return false; - } - } - - public static class TrackFinder implements AreaConfigFinder { - - @Override - public DeviceType deviceType() { - return DeviceType.DEVICE_TYPE_TRACK; - } - - @Override - public boolean matcher(DeviceAreaConfig areaConfig, String deviceCode) { - return false; - } - } } diff --git a/src/main/java/club/joylink/xiannccda/service/AlertRecordService.java b/src/main/java/club/joylink/xiannccda/service/AlertRecordService.java index b7de2aa..8117744 100644 --- a/src/main/java/club/joylink/xiannccda/service/AlertRecordService.java +++ b/src/main/java/club/joylink/xiannccda/service/AlertRecordService.java @@ -9,6 +9,7 @@ import club.joylink.xiannccda.entity.AlertTip; import club.joylink.xiannccda.exception.BusinessExceptionAssertEnum; import club.joylink.xiannccda.repository.IAlertRecordRepository; import club.joylink.xiannccda.repository.impl.AlertTipRepository; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; @@ -35,9 +36,14 @@ public class AlertRecordService { this.alertTipRepository = alertTipRepository; } - public AlertTip confirm(Long recordId, String tipType) { - AlertTip tip = alertTipRepository.getOne(Wrappers.lambdaQuery(AlertTip.class).eq(AlertTip::getAlertType, tipType)); - BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(tip)); + public AlertTip confirm(Long recordId, String tipType, Long locationId) { + LambdaQueryWrapper qw = Wrappers.lambdaQuery(AlertTip.class).eq(AlertTip::getAlertType, tipType); + if (Objects.nonNull(locationId)) { + qw.eq(AlertTip::getAreaConfigId, locationId); + } + AlertTip tip = alertTipRepository.getOne(qw); + + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(tip), "未找到对应的决策辅助信息"); this.alertRecordRepository.update(Wrappers.lambdaUpdate(AlertRecord.class) .set(AlertRecord::getAlertTipId, tip.getId()).set(AlertRecord::getAlarmStatus, 1) .eq(AlertRecord::getId, recordId).isNull(AlertRecord::getAlarmStatus)); @@ -59,7 +65,7 @@ public class AlertRecordService { for (AlertRecord record : ars) { NccAlertInfo nccAlertInfo = new NccAlertInfo(record.getAlertTime(), AlertType.valueOf(record.getAlertType()), Objects.isNull(record.getAlertTipId()) ? null : record.getAlertTipId().intValue(), record.getLineId(), - record.getAlertObject(), "", "", false); + record.getAlertObject(), "", record.getAlertLocationId()); nccAlertInfo.setId(record.getId()); nccAlertInfo.setAlarmStatus(record.getAlarmStatus()); alertInfos.add(nccAlertInfo); diff --git a/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java b/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java index 7fb9b47..62c3da8 100644 --- a/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java +++ b/src/main/java/club/joylink/xiannccda/service/config/DeviceAreaConfigService.java @@ -1,7 +1,10 @@ package club.joylink.xiannccda.service.config; +import club.joylink.xiannccda.alert.core.AlertDeviceType; +import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.dto.config.DeviceAreaConfigDto; import club.joylink.xiannccda.dto.config.DeviceAreaConfigQueryDto; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.entity.DeviceAreaConfig; import club.joylink.xiannccda.exception.BusinessExceptionAssertEnum; import club.joylink.xiannccda.repository.impl.DeviceAreaConfigRepository; @@ -12,10 +15,16 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import com.google.common.collect.Lists; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; import java.util.stream.Collectors; import lombok.Data; import org.apache.commons.lang3.StringUtils; @@ -28,6 +37,21 @@ public class DeviceAreaConfigService { @Autowired private DeviceAreaConfigRepository deviceAreaConfigRepository; + private final static Cache>> AREA_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(5 * 60, TimeUnit.SECONDS).build(); + + + public List getCache(int lineId, AlertDeviceType dt) { + try { + Map> configList = AREA_CONFIG_CACHE.get(lineId, () -> { + List configs = deviceAreaConfigRepository.list(Wrappers.lambdaQuery(DeviceAreaConfig.class).eq(DeviceAreaConfig::getLineId, lineId)); + Map> maper = configs.stream().collect(Collectors.groupingBy(DeviceAreaConfig::getDeviceType)); + return maper; + }); + return configList.get(dt); + } catch (ExecutionException e) { + return Collections.emptyList(); + } + } public void saveOrUpdate(DeviceAreaConfigDto dto) { BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(CollectionUtils.isNotEmpty(dto.getAlertTypes()), "绑定数据类型不能为空"); @@ -40,6 +64,7 @@ public class DeviceAreaConfigService { areaConfig.setId(dto.getId()); } this.deviceAreaConfigRepository.saveOrUpdate(areaConfig); + AREA_CONFIG_CACHE.invalidate(dto.getLineId()); } private DeviceAreaConfig convertDB(DeviceAreaConfigDto dto) { diff --git a/src/main/java/club/joylink/xiannccda/service/config/DeviceGuardConfigService.java b/src/main/java/club/joylink/xiannccda/service/config/DeviceGuardConfigService.java index f990cc8..2eeb577 100644 --- a/src/main/java/club/joylink/xiannccda/service/config/DeviceGuardConfigService.java +++ b/src/main/java/club/joylink/xiannccda/service/config/DeviceGuardConfigService.java @@ -5,8 +5,10 @@ import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto; import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto.GuardUnit; import club.joylink.xiannccda.dto.config.DeviceGuardConfigQueryDto; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.entity.DeviceGuardConfig; import club.joylink.xiannccda.exception.BusinessExceptionAssertEnum; +import club.joylink.xiannccda.exception.ExceptionAssert; import club.joylink.xiannccda.repository.IDeviceGuardConfigRepository; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -33,42 +35,26 @@ public class DeviceGuardConfigService { @Autowired private IDeviceGuardConfigRepository configRepository; - private final static Cache> GUARD_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(10 * 60, TimeUnit.SECONDS).build(); + private final static Cache GUARD_CONFIG_CACHE = CacheBuilder.newBuilder().expireAfterWrite(10 * 60, TimeUnit.SECONDS).build(); - public Map getGuardConfig(int lineId, DeviceType deviceType) { -/* try { - Table cacheTable = GUARD_CONFIG_CACHE.get(lineId, () -> { + public GuardConfig getGuardConfig(int lineId) { + try { + GuardConfig guardConfig = GUARD_CONFIG_CACHE.get(lineId, () -> { LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceGuardConfig.class); - qw.eq(DeviceGuardConfig::getLineId, lineId).eq(DeviceGuardConfig::getDeviceType, deviceType) - .eq(DeviceGuardConfig::getConfigType, DeviceGuardConfig.CONFIG_TYPE_USER); - List configList = this.configRepository.list(qw); - Table table = HashBasedTable.create(); - for (DeviceGuardConfig guardConfig : configList) { - table.put(guardConfig.getDeviceType(), guardConfig.getDeviceConfigType(), guardConfig); - } - return table; + qw.eq(DeviceGuardConfig::getLineId, lineId); + DeviceGuardConfig deviceGuardConfig = configRepository.getOne(qw, false); + BusinessExceptionAssertEnum.DATA_NOT_EXIST.assertTrue(Objects.nonNull(deviceGuardConfig), "未找到对应的配置数据"); + GuardConfig gc = GuardConfig.parseFrom(deviceGuardConfig.getConfigData()); + return gc; + }); - if (Objects.isNull(cacheTable) || cacheTable.isEmpty()) { - return Collections.emptyMap(); - } - return cacheTable.row(deviceType); + return guardConfig; } catch (ExecutionException e) { log.error("获取设备保护配置信息出错 msg:[{}]", e.getMessage(), e); - return Collections.emptyMap(); - }*/ - return null; + return GuardConfig.newBuilder().build(); + } } - public DeviceGuardConfig buildDefault(DeviceType deviceType, AlertType configDeviceType, Operator operator, GuardUnit unit, int val) { -/* DeviceGuardConfig guardConfig = new DeviceGuardConfig(); - guardConfig.setDeviceConfigType(configDeviceType); - guardConfig.setDeviceType(deviceType); - guardConfig.setDeviceUnit(unit); - guardConfig.setVal(val); - guardConfig.setDeviceOperator(operator); - return guardConfig;*/ - return null; - } public void saveOrUpdate(DeviceGuardConfig dto) { /* LambdaQueryWrapper qw = Wrappers.lambdaQuery(DeviceGuardConfig.class); @@ -86,6 +72,7 @@ public class DeviceGuardConfigService { Long typeCounter = this.configRepository.count(qw); BusinessExceptionAssertEnum.UNIQUE_FIELD_REPEAT.assertTrue */ this.configRepository.saveOrUpdate(dto); + GUARD_CONFIG_CACHE.invalidateAll(); } public Page page(DeviceGuardConfigQueryDto queryDTO) { diff --git a/src/test/java/club/joylink/xiannccda/config/DataConfigTest.java b/src/test/java/club/joylink/xiannccda/config/DataConfigTest.java index a37f16e..c26abb1 100644 --- a/src/test/java/club/joylink/xiannccda/config/DataConfigTest.java +++ b/src/test/java/club/joylink/xiannccda/config/DataConfigTest.java @@ -3,6 +3,7 @@ package club.joylink.xiannccda.config; import club.joylink.xiannccda.ats.message.line3.device.DeviceType; import club.joylink.xiannccda.dto.config.DeviceGuardConfigDto.GuardUnit; import club.joylink.xiannccda.dto.protos.AlertConstProto.AlertType; +import club.joylink.xiannccda.dto.protos.GuardConfigProto.GuardConfig; import club.joylink.xiannccda.entity.DeviceGuardConfig; import club.joylink.xiannccda.repository.IDeviceGuardConfigRepository; import club.joylink.xiannccda.service.config.Operator; @@ -18,7 +19,18 @@ public class DataConfigTest { @Test public void test() { - + GuardConfig.Builder gb = GuardConfig.newBuilder(); + gb.setCanNotCloseTimes(60); + gb.setCanNotOpenTimes(30); + gb.setOrangeLedMostNums(2); + gb.setSwitchLostTimes(13); + gb.setRedLedMostNums(5); + gb.setOrangeLedMostNums(5); + gb.setSwitchLostMostNums(2); + DeviceGuardConfig gc = new DeviceGuardConfig(); + gc.setLineId(3); + gc.setConfigData(gb.build().toByteArray()); + this.iDeviceGuardConfigRepository.save(gc); } } diff --git a/src/test/java/club/joylink/xiannccda/device/DeviceStatusCheckTest.java b/src/test/java/club/joylink/xiannccda/device/DeviceStatusCheckTest.java index 7be4f15..c03032d 100644 --- a/src/test/java/club/joylink/xiannccda/device/DeviceStatusCheckTest.java +++ b/src/test/java/club/joylink/xiannccda/device/DeviceStatusCheckTest.java @@ -35,7 +35,7 @@ public class DeviceStatusCheckTest { DeviceStatusProto.Switch.Builder s2 = DeviceStatusConvertor.convert(DeviceStatus.SWITCH.class, 134217992); System.out.println(s2); System.out.println("-----------------"); - DeviceStatusProto.Switch.Builder s3 = DeviceStatusConvertor.convert(DeviceStatus.SWITCH.class, 72); + DeviceStatusProto.Switch.Builder s3 = DeviceStatusConvertor.convert(DeviceStatus.SWITCH.class, 2); System.out.println(s3); } diff --git a/xian-ncc-da-message b/xian-ncc-da-message index 5b065e1..aec7512 160000 --- a/xian-ncc-da-message +++ b/xian-ncc-da-message @@ -1 +1 @@ -Subproject commit 5b065e1951829ad21e7ee7310b480547f9d32cd5 +Subproject commit aec7512cf09e451aca396de2f6e8b98e9b68c624