|
@@ -8,13 +8,14 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -26,7 +27,7 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public class OfflineNotify {
|
|
public class OfflineNotify {
|
|
|
|
|
|
|
|
- private final RedisTemplate redisTemplate;
|
|
|
|
|
|
|
+ private final StringRedisTemplate redisTemplate;
|
|
|
private final KymCache kymCache;
|
|
private final KymCache kymCache;
|
|
|
|
|
|
|
|
@Value("${kym.notify-email}")
|
|
@Value("${kym.notify-email}")
|
|
@@ -41,9 +42,9 @@ public class OfflineNotify {
|
|
|
@Scheduled(cron = "0 0/5 * * * ? ")
|
|
@Scheduled(cron = "0 0/5 * * * ? ")
|
|
|
public void execute() {
|
|
public void execute() {
|
|
|
// 查询redis离线设备消息队列,存在且超过5min的消息则发送邮件提醒
|
|
// 查询redis离线设备消息队列,存在且超过5min的消息则发送邮件提醒
|
|
|
- var offlineList = redisTemplate.opsForZSet().rangeByScore(RedisKeys.OFFLINE, System.currentTimeMillis() + 5 * 60 * 1000, System.currentTimeMillis() + 10 * 60 * 1000);
|
|
|
|
|
|
|
+ Set<String> offlineList = redisTemplate.opsForZSet().rangeByScore(RedisKeys.OFFLINE, System.currentTimeMillis() + 5 * 60 * 1000, System.currentTimeMillis() + 10 * 60 * 1000);
|
|
|
if (!CommUtil.isEmptyOrNull(offlineList)) {
|
|
if (!CommUtil.isEmptyOrNull(offlineList)) {
|
|
|
- /**
|
|
|
|
|
|
|
+ /*
|
|
|
* 格式:
|
|
* 格式:
|
|
|
* 【设备离线】
|
|
* 【设备离线】
|
|
|
* 【阳光科创中心离】:001001,001002,001003
|
|
* 【阳光科创中心离】:001001,001002,001003
|
|
@@ -54,10 +55,12 @@ public class OfflineNotify {
|
|
|
// 以站点名称分组
|
|
// 以站点名称分组
|
|
|
var list = map.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getValue));
|
|
var list = map.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getValue));
|
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
|
- list.forEach((k, v) -> sb.append(temp.formatted(k, StringUtils.join(v.stream().map(Map.Entry::getKey).toArray(), ","))));
|
|
|
|
|
|
|
+ list.forEach((k, v) -> sb.append(temp.formatted(k, StringUtils.join(v.stream().map(Map.Entry::getKey).sorted(Comparator.comparingInt(Integer::parseInt)).toList(), ","))));
|
|
|
log.warn(sb.toString());
|
|
log.warn(sb.toString());
|
|
|
// 格式化信息,发送邮件
|
|
// 格式化信息,发送邮件
|
|
|
MailUtil.send(notifyEmail, "【设备离线通知】", sb.toString(), false);
|
|
MailUtil.send(notifyEmail, "【设备离线通知】", sb.toString(), false);
|
|
|
|
|
+ redisTemplate.opsForZSet().remove(RedisKeys.OFFLINE, offlineList.toArray(Object[]::new));
|
|
|
|
|
+ redisTemplate.opsForSet().add(RedisKeys.OFFLINE_EXPIRED, offlineList.toArray(String[]::new));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|