|
@@ -12,6 +12,7 @@ import vip.xiaonuo.coldchain.core.alarm.config.ColdChainAlarmMessageProperties;
|
|
|
import vip.xiaonuo.coldchain.core.alarm.enums.SensorAlarmType;
|
|
|
import vip.xiaonuo.coldchain.core.event.SensorAlarmEvent;
|
|
|
import vip.xiaonuo.coldchain.core.renke.config.JfcloudColdChainServerProperties;
|
|
|
+import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.enums.MonitorStatusEnum;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortarget.service.MonitorTargetService;
|
|
|
import vip.xiaonuo.coldchain.modular.monitortargetregion.entity.MonitorTargetRegion;
|
|
@@ -42,6 +43,7 @@ public class DeviceOfflineDetectionService {
|
|
|
private final ApplicationEventPublisher applicationEventPublisher;
|
|
|
private final MonitorTargetRegionService monitorTargetRegionService;
|
|
|
private final MonitorTargetService monitorTargetService;
|
|
|
+ private final MonitorDeviceService monitorDeviceService;
|
|
|
private final ColdChainAlarmMessageProperties alarmMessageProperties;
|
|
|
private final JfcloudColdChainServerProperties jfcloudColdChainServerProperties;
|
|
|
private static final String KEY_SPILT = "-";
|
|
@@ -63,71 +65,38 @@ public class DeviceOfflineDetectionService {
|
|
|
|
|
|
/**
|
|
|
* 定期检测设备状态是否离线
|
|
|
- * 每 5 分钟运行一次
|
|
|
+ * 每 3 分钟运行一次
|
|
|
*/
|
|
|
-// @Scheduled(fixedRate = 300000) // 每 5 分钟(以毫秒为单位)
|
|
|
-//// @Scheduled(fixedRateString = "${coldchain.device-offline-interval:300000}")
|
|
|
-// public void checkDeviceStatus() {
|
|
|
-// log.info("开始自动检测设备在线状态...");
|
|
|
-// // 获取 Redis 中所有设备的键
|
|
|
-// Set<String> deviceKeys = getAllDeviceCodes();
|
|
|
-// Map<String,Integer> deviceCodes = new LinkedHashMap<>();
|
|
|
-// if(deviceKeys!=null && !deviceKeys.isEmpty()){
|
|
|
-// // 遍历设备键,逐一检测状态
|
|
|
-// for (String key : deviceKeys) {
|
|
|
-// String[] split = key.split(KEY_SPILT);
|
|
|
-// if (split.length == 0) {
|
|
|
-// continue;
|
|
|
-// }
|
|
|
-// String deviceCode = split[0];
|
|
|
-// Integer route = null;
|
|
|
-// if (split.length == 2) {
|
|
|
-// route = Integer.valueOf(split[1]);
|
|
|
-// }
|
|
|
-// deviceCodes.put(deviceCode,route);
|
|
|
-// // 从 Redis 中获取设备的最后上报时间
|
|
|
-// String lastReportTimeStr = getLastDeviceTime(getKey(deviceCode, route));
|
|
|
-// if (lastReportTimeStr != null) {
|
|
|
-// // 将上报时间字符串解析为 LocalDateTime 对象
|
|
|
-// LocalDateTime lastReportTime = LocalDateTime.parse(lastReportTimeStr, DATE_TIME_FORMATTER);
|
|
|
-// // 获取当前时间
|
|
|
-// LocalDateTime now = LocalDateTime.now();
|
|
|
-// final long OFFLINE_THRESHOLD_MINUTES = jfcloudColdChainServerProperties.getDeviceOfflineInterval() / 60 / 1000;
|
|
|
-// // 如果最后上报时间超过离线阈值,判断设备为离线
|
|
|
-// if (lastReportTime.plusMinutes(OFFLINE_THRESHOLD_MINUTES).isBefore(now)) {
|
|
|
-// log.error("设备{}-{} 已离线,最后上报时间:{}。\n", deviceCode, route, lastReportTimeStr);
|
|
|
-// publishAlarm(deviceCode, route, lastReportTimeStr);
|
|
|
-// monitorTargetService.updateStatusByDeviceCode(deviceCode, route, MonitorStatusEnum.OFF);
|
|
|
-// } else {
|
|
|
-// log.info("设备 {}-{} 在线,最后上报时间:{}。\n", deviceCode, route, lastReportTimeStr);
|
|
|
-// monitorTargetService.updateStatusByDeviceCode(deviceCode, route, MonitorStatusEnum.ONLINE);
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// monitorTargetService.updateStatusByDeviceCode(deviceCode, route, MonitorStatusEnum.OFF);
|
|
|
-// log.info("设备 {}-{} 缺少数据,可能从未上报过。\n", deviceCode, route);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// log.info("查找出从来没上线的设备都统一都下线处理");
|
|
|
-// monitorTargetService.updateStatusbatch(deviceCodes);
|
|
|
-// }
|
|
|
-
|
|
|
- @Scheduled(fixedRate = 300000, initialDelay = 60000) // 每 5 分钟运行一次
|
|
|
+ @Scheduled(fixedRate = 180000, initialDelay = 60000) // 每 3 分钟运行一次
|
|
|
public void checkDeviceStatus() {
|
|
|
log.info("开始自动检测设备在线状态...");
|
|
|
Map<String, Integer> deviceCodes = new LinkedHashMap<>();
|
|
|
- Set<String> deviceKeys = getAllDeviceCodes();
|
|
|
- if(deviceKeys!=null && deviceKeys.size()>0){
|
|
|
+ // 获取 Redis 中的设备集合
|
|
|
+ Set<String> sensorCodes = new HashSet<>();
|
|
|
+ Set<String> redisDeviceKeys = getAllRedisDeviceCodes();
|
|
|
+ if (redisDeviceKeys != null && !redisDeviceKeys.isEmpty()) {
|
|
|
+ Set<String> sensorCodes1 = redisDeviceKeys.stream()
|
|
|
+ .map(key -> key.split(KEY_SPILT)[0]) // 提取出 sensorCode
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ sensorCodes.addAll(sensorCodes1);
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
final long OFFLINE_THRESHOLD_MINUTES = jfcloudColdChainServerProperties.getDeviceOfflineInterval() / 60 / 1000;
|
|
|
- for (String key : deviceKeys) {
|
|
|
+ // 处理每个设备
|
|
|
+ for (String key : redisDeviceKeys) {
|
|
|
processDevice(key, now, OFFLINE_THRESHOLD_MINUTES, deviceCodes);
|
|
|
}
|
|
|
}
|
|
|
+ // 获取所有设备代码
|
|
|
+ Set<String> allSensorCodes = monitorDeviceService.getAllDeviceCodes();
|
|
|
+ // 创建新的集合,避免修改原始集合
|
|
|
+ Set<String> offlineSensorCodes = new HashSet<>(allSensorCodes);
|
|
|
+ offlineSensorCodes.removeAll(sensorCodes); // 剩下的是未上线的设备
|
|
|
log.info("查找出从来没上线的设备,统一设置为离线...");
|
|
|
- monitorTargetService.updateStatusbatch(deviceCodes);
|
|
|
+ monitorTargetService.updateStatusbatch(offlineSensorCodes); // 更新离线设备状态
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 处理设备状态
|
|
|
* @param key
|
|
@@ -135,7 +104,7 @@ public class DeviceOfflineDetectionService {
|
|
|
* @param offlineThresholdMinutes
|
|
|
* @param deviceCodes
|
|
|
*/
|
|
|
- private void processDevice(String key, LocalDateTime now, long offlineThresholdMinutes, Map<String, Integer> deviceCodes) {
|
|
|
+ public void processDevice(String key, LocalDateTime now, long offlineThresholdMinutes, Map<String, Integer> deviceCodes) {
|
|
|
String[] split = key.split(KEY_SPILT);
|
|
|
if (split.length == 0) {
|
|
|
return;
|
|
@@ -259,7 +228,7 @@ public class DeviceOfflineDetectionService {
|
|
|
return commonCacheOperator.get(key) != null;
|
|
|
}
|
|
|
|
|
|
- private Set<String> getAllDeviceCodes() {
|
|
|
+ private Set<String> getAllRedisDeviceCodes() {
|
|
|
return commonCacheOperator.getAllKeys().stream().filter(key -> key.startsWith(CACHE_KEY_PREFIX)) // 过滤指定前缀的键
|
|
|
.map(key -> key.replace(CACHE_KEY_PREFIX, "")) // 去掉前缀
|
|
|
.collect(Collectors.toSet());
|