fix: 设备在离线,溶解氧和温度报警逻辑修改。
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package com.intc.iot.task;
|
||||
|
||||
import com.intc.common.redis.utils.RedisUtils;
|
||||
import com.intc.iot.config.AliyunIotProperties;
|
||||
import com.intc.iot.handler.DeviceDataHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 设备离线告警延迟触发定时任务
|
||||
* 对应 C# UpdateDataSecondlyJob.CheckWarnAndNotice 中对 DeviceOffline WarnWait 的处理:
|
||||
* 离线超过配置的 offline-warn-delay-minutes 分钟才真正触发告警。
|
||||
*
|
||||
* @author intc-iot
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnBean(DeviceDataHandler.class)
|
||||
public class DeviceOfflineWarnTask {
|
||||
|
||||
private static final String OFFLINE_WAIT_KEY_PREFIX = "device:offline:wait:";
|
||||
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
private final DeviceDataHandler deviceDataHandler;
|
||||
|
||||
private final AliyunIotProperties aliyunIotProperties;
|
||||
|
||||
/**
|
||||
* 每分钟扫描一次离线等待标记
|
||||
* 对超过配置的 offline-warn-delay-minutes 分钟的设备触发告警
|
||||
*/
|
||||
@Scheduled(fixedDelay = 60000)
|
||||
public void checkOfflineWaitAndTrigger() {
|
||||
try {
|
||||
Collection<String> keys = RedisUtils.keys(OFFLINE_WAIT_KEY_PREFIX + "*");
|
||||
if (keys == null || keys.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
for (String key : keys) {
|
||||
try {
|
||||
String offlineTimeStr = RedisUtils.getCacheObject(key);
|
||||
if (offlineTimeStr == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
LocalDateTime offlineTime = LocalDateTime.parse(offlineTimeStr, FORMATTER);
|
||||
long elapsedMinutes = java.time.Duration.between(offlineTime, now).toMinutes();
|
||||
|
||||
if (elapsedMinutes >= aliyunIotProperties.getOfflineWarnDelayMinutes()) {
|
||||
// 提取 deviceName(去掉前缀)
|
||||
String deviceName = key.substring(OFFLINE_WAIT_KEY_PREFIX.length());
|
||||
|
||||
// 触发告警前先删除标记,防止重复触发
|
||||
RedisUtils.deleteObject(key);
|
||||
|
||||
log.info("[离线告警] 设备离线已超过 {} 分钟,触发告警: {}", elapsedMinutes, deviceName);
|
||||
deviceDataHandler.triggerOfflineAlarm(deviceName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[离线告警] 处理单条离线标记异常,key={}: {}", key, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[离线告警] 扫描离线标记异常: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user