diff --git a/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/convert/LcPowerEnvMonitorMetricEntityConvert.java b/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/convert/LcPowerEnvMonitorMetricEntityConvert.java index 7148d0e..94c87f7 100644 --- a/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/convert/LcPowerEnvMonitorMetricEntityConvert.java +++ b/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/convert/LcPowerEnvMonitorMetricEntityConvert.java @@ -121,7 +121,13 @@ public interface LcPowerEnvMonitorMetricEntityConvert { Map controlEnumValue = JSONUtil.toBean(metric.getControlEnumValue(), new TypeReference>() { }, true); deviceMetric.put(metric.getPropertyCode() + "View", controlEnumValue.get(metric.getValue())); - if (StrUtil.equals(metric.getValue(), "true")) deviceMetric.put("alarm", Boolean.TRUE); + + // 当值为true且属性不是开关,或者值为false且属性是开关时,触发告警 + boolean isAlarm = ("true".equals(metric.getValue()) && !"switch".equals(metric.getPropertyCode())) || + ("false".equals(metric.getValue()) && "switch".equals(metric.getPropertyCode())); + if (isAlarm) { + deviceMetric.put("alarm", Boolean.TRUE); + } } }); return deviceMetric; diff --git a/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/listener/LcPowerEnvMetricEventListener.java b/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/listener/LcPowerEnvMetricEventListener.java index 75545f3..6dc8cc4 100644 --- a/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/listener/LcPowerEnvMetricEventListener.java +++ b/jeelowcode-module/jeelowcode-module-biz/src/main/java/com/jeelowcode/module/biz/listener/LcPowerEnvMetricEventListener.java @@ -47,17 +47,25 @@ public class LcPowerEnvMetricEventListener { List recentData = metricService.getRecentData(metric.getDeviceUid(), metric.getPropertyCode(), metric.getCreateTime().minusMinutes(3)); LcPowerEnvAlertMessageEntity alertMessage = alertService.getLatestAlertMessage(metric.getDeviceUid(), metric.getPropertyCode()); - if ((StrUtil.equals(metric.getMetricValue(), "true") && !StrUtil.equals(metric.getPropertyCode(), "switch")) || - (StrUtil.equals(metric.getMetricValue(), "false") && StrUtil.equals(metric.getPropertyCode(), "switch"))) { - // 判断三分钟内是否有告警,如果有,则延续告警消息,延长告警消息时间,如果没有则创建新的告警消息 - if (CollUtil.isNotEmpty(recentData) - && StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) { - // 更新告警消息 - Optional.ofNullable(alertMessage).ifPresent(alert -> { - alert.setEndTime(metric.getUpdateTime()); - alertService.updateById(alert); - }); - } else { + // 根据属性编码和当前值判断是否需要触发告警 + boolean isSwitchProperty = StrUtil.equals(metric.getPropertyCode(), "switch"); + boolean shouldTriggerAlert; + + if (isSwitchProperty) { + // switch 开关状态正好是反过来,true是正常,代表在线,false是异常,代表离线。 + shouldTriggerAlert = StrUtil.equals(metric.getMetricValue(), "false"); + } else { + shouldTriggerAlert = StrUtil.equals(metric.getMetricValue(), "true"); + } + + // 获取最近一条数据的值用于比较 + String previousValue = CollUtil.isNotEmpty(recentData) ? CollUtil.getFirst(recentData).getMetricValue() : null; + + if (shouldTriggerAlert) { + // 需要触发告警的情况 + boolean shouldCreateNewAlert = previousValue == null || !StrUtil.equals(previousValue, metric.getMetricValue()); + + if (shouldCreateNewAlert) { // 新建告警消息,并且关闭之前的告警消息 LcPowerEnvAlertMessageEntity newMessage = new LcPowerEnvAlertMessageEntity() .setDeviceUid(metric.getDeviceUid()) @@ -69,15 +77,21 @@ public class LcPowerEnvMetricEventListener { .setAlertStatus(1L); alertService.save(newMessage); // 关闭之前的告警消息 + closePreviousAlert(alertMessage); + } else { + // 更新告警消息,延长告警时间 Optional.ofNullable(alertMessage).ifPresent(alert -> { - alert.setAlertStatus(0L); + alert.setEndTime(metric.getUpdateTime()); alertService.updateById(alert); }); } } else { - // 判断三分钟内是否有告警,如果有,则关闭告警消息,将告警消息的状态置为0,如果没有,则不用处理 - if (CollUtil.isNotEmpty(recentData) - && StrUtil.equals(CollUtil.getFirst(recentData).getMetricValue(), "true")) { + // 不需要触发告警的情况,如果之前有告警则关闭它 + boolean shouldCloseAlert = previousValue != null && + ((isSwitchProperty && StrUtil.equals(previousValue, "false")) || + (!isSwitchProperty && StrUtil.equals(previousValue, "true"))); + + if (shouldCloseAlert) { // 关闭最新的告警消息 Optional.ofNullable(alertMessage).ifPresent(alert -> { alert.setAlertStatus(0L); @@ -87,6 +101,18 @@ public class LcPowerEnvMetricEventListener { } } + /** + * 关闭之前的告警消息 + * + * @param alertMessage 告警消息 + */ + private void closePreviousAlert(LcPowerEnvAlertMessageEntity alertMessage) { + Optional.ofNullable(alertMessage).ifPresent(alert -> { + alert.setAlertStatus(0L); + alertService.updateById(alert); + }); + } + /** * 处理指标更新事件 *