|
@@ -4,11 +4,13 @@ package com.sooka.sponest.monitor.dahua.utils;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
|
|
import com.ruoyi.common.core.domain.R;
|
|
|
+import com.ruoyi.common.redis.service.RedisService;
|
|
|
import com.sooka.sponest.monitor.camera.domain.CentermonitorTRecorderAlarm;
|
|
|
import com.sooka.sponest.monitor.camera.mapper.CentermonitorTCamerachannelMapper;
|
|
|
import com.sooka.sponest.monitor.camera.mapper.CentermonitorTRecorderAlarmMapper;
|
|
|
import com.sooka.sponest.monitor.camera.mapper.CentermonitorTRecorderMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
@@ -34,6 +36,8 @@ public class DeviceMonitorManager {
|
|
|
private CentermonitorTRecorderAlarmMapper centermonitorTRecorderAlarmMapper;
|
|
|
@Resource
|
|
|
private CentermonitorTCamerachannelMapper centermonitorTCamerachannelMapper;
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
@Value("${sooka.dahua_interface_server.loginIp}")
|
|
|
private String loginIp;
|
|
|
@Value("${sooka.dahua_interface_server.loginPort}")
|
|
@@ -62,12 +66,11 @@ public class DeviceMonitorManager {
|
|
|
*
|
|
|
* @param deviceCode 设备编码
|
|
|
* @param onlineTime 上线时间
|
|
|
- * @return true表示成功启动任务,false表示任务已存在
|
|
|
*/
|
|
|
- public boolean startDeviceMonitoringIfAbsent(String deviceCode, Date onlineTime) {
|
|
|
- // 如果已有监控任务,直接返回false
|
|
|
+ public void startDeviceMonitoringIfAbsent(String deviceCode, Date onlineTime) {
|
|
|
+ // 如果已有监控任务,直接返回
|
|
|
if (deviceTasks.containsKey(deviceCode)) {
|
|
|
- return false;
|
|
|
+ return;
|
|
|
}
|
|
|
// 创建监控任务
|
|
|
Runnable monitorTask = () -> {
|
|
@@ -81,12 +84,10 @@ public class DeviceMonitorManager {
|
|
|
if (existing != null) {
|
|
|
// 如果已经有其他线程创建了任务,取消当前创建的任务
|
|
|
future.cancel(false);
|
|
|
- return false;
|
|
|
}
|
|
|
-
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 检查设备是否有监控任务
|
|
|
*
|
|
@@ -107,6 +108,7 @@ public class DeviceMonitorManager {
|
|
|
if (future != null) {
|
|
|
future.cancel(false);
|
|
|
}
|
|
|
+ redisService.deleteObject("deviceCode_" + deviceCode);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -117,8 +119,8 @@ public class DeviceMonitorManager {
|
|
|
Date now = new Date();
|
|
|
long offlineMinutes = (now.getTime() - onlineTime.getTime()) / (60 * 1000);
|
|
|
if (offlineMinutes >= 5) {
|
|
|
- System.out.println("设备 " + deviceCode + " 已在线超过5分钟,触发报警!");
|
|
|
- triggerAlarm(deviceCode, onlineTime,"设备开机,未执法告警");
|
|
|
+// System.out.println("设备 " + deviceCode + " 已在线超过5分钟,触发报警!");
|
|
|
+ triggerAlarm(deviceCode, onlineTime, "设备开机,未执法告警", null);
|
|
|
stopDeviceMonitoring(deviceCode);
|
|
|
}
|
|
|
}
|
|
@@ -126,14 +128,17 @@ public class DeviceMonitorManager {
|
|
|
/**
|
|
|
* 触发报警
|
|
|
*/
|
|
|
- private void triggerAlarm(String deviceCode, Date onlineTime,String alarmName) {
|
|
|
- String cameraCode = centermonitorTCamerachannelMapper.selectChannelCodeByDeviceCode(deviceCode);
|
|
|
+ private void triggerAlarm(String deviceCode, Date onlineTime, String alarmName, String recordId) {
|
|
|
+ Map<String, Object> map = centermonitorTCamerachannelMapper.selectChannelCodeByDeviceCode(deviceCode);
|
|
|
CentermonitorTRecorderAlarm saveVo = new CentermonitorTRecorderAlarm();
|
|
|
- saveVo.setCameraCode(cameraCode);
|
|
|
+ saveVo.setCameraCode(map.get("channelCode").toString());
|
|
|
+ saveVo.setCameraName(map.get("channelName").toString());
|
|
|
saveVo.setAlarmName(alarmName);
|
|
|
saveVo.setAlarmTime(onlineTime);
|
|
|
+ saveVo.setRecordId(recordId);
|
|
|
+// log.info("设备编码:" + deviceCode + "摄像头编码:" + map.get("channelCode").toString() + "时间:" + onlineTime + "执行报警操作.");
|
|
|
centermonitorTRecorderAlarmMapper.insertCentermonitorTRecorderAlarm(saveVo);
|
|
|
- System.out.println("设备编码:" + deviceCode + "摄像头编码:" + cameraCode + "执行报警操作.");
|
|
|
+// System.out.println("设备编码:" + deviceCode + "摄像头编码:" + map.get("channelCode").toString() + "执行报警操作.");
|
|
|
}
|
|
|
|
|
|
|
|
@@ -142,19 +147,16 @@ public class DeviceMonitorManager {
|
|
|
*
|
|
|
* @param deviceCode 摄像头编码
|
|
|
* @param currentStatus 当前设备状态
|
|
|
- * @return true表示成功启动任务,false表示任务已存在
|
|
|
*/
|
|
|
- public boolean startShutdownMonitoring(String deviceCode, String currentStatus) {
|
|
|
- if (deviceTasks.containsKey(deviceCode)) {
|
|
|
- return false;
|
|
|
+ public void startShutdownMonitoring(String deviceCode, String currentStatus, String recordId) {
|
|
|
+ if (!deviceTasks.containsKey(deviceCode)) {
|
|
|
+ // 记录初始状态
|
|
|
+ deviceInitialStatus.put(deviceCode, currentStatus);
|
|
|
+ // 记录开始关机时间
|
|
|
+ monitoringStartTimes.put(deviceCode, System.currentTimeMillis());
|
|
|
}
|
|
|
- // 记录初始状态
|
|
|
- deviceInitialStatus.put(deviceCode, currentStatus);
|
|
|
- // 记录开始关机时间
|
|
|
- monitoringStartTimes.put(deviceCode, System.currentTimeMillis());
|
|
|
-
|
|
|
Runnable shutdownMonitorTask = () -> {
|
|
|
- checkDeviceStatusChange(deviceCode);
|
|
|
+ checkDeviceStatusChange(deviceCode, recordId);
|
|
|
};
|
|
|
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(
|
|
|
shutdownMonitorTask, 0, 1, TimeUnit.MINUTES);
|
|
@@ -162,20 +164,18 @@ public class DeviceMonitorManager {
|
|
|
ScheduledFuture<?> existing = deviceTasks.putIfAbsent(deviceCode, future);
|
|
|
if (existing != null) {
|
|
|
future.cancel(false);
|
|
|
- return false;
|
|
|
}
|
|
|
- return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查设备状态变化
|
|
|
*/
|
|
|
- private void checkDeviceStatusChange(String deviceCode) {
|
|
|
+ private void checkDeviceStatusChange(String deviceCode, String recordId) {
|
|
|
// 1. 状态变化检查
|
|
|
JSONObject jsonObject = queryDeviceStatus(deviceCode);
|
|
|
String initialStatus = deviceInitialStatus.get(deviceCode);
|
|
|
if (jsonObject.get("status").equals(initialStatus)) {
|
|
|
- System.out.println("设备已经关机,停止当前监控任务");
|
|
|
+// System.out.println("设备已经关机,停止当前监控任务");
|
|
|
stopDeviceMonitoring(deviceCode);
|
|
|
}
|
|
|
// 2. 强制检查时间阈值(即使状态已变化也记录时长)
|
|
@@ -183,11 +183,10 @@ public class DeviceMonitorManager {
|
|
|
if (startTime == null) return;
|
|
|
|
|
|
long elapsedMinutes = (System.currentTimeMillis() - startTime) / (60 * 1000);
|
|
|
- if (elapsedMinutes >= 5) {
|
|
|
- triggerAlarm(deviceCode, new Date(),"设备执法完成未关机,告警");
|
|
|
- System.out.println("设备 " + deviceCode + " 触发关机按钮,超过5分钟内状态未变化,触发报警");
|
|
|
+ if (elapsedMinutes >= 10) {
|
|
|
+ triggerAlarm(deviceCode, new Date(), "设备执法完成未关机,告警", recordId);
|
|
|
+// System.out.println("设备 " + deviceCode + " 触发关机按钮,超过5分钟内状态未变化,触发报警");
|
|
|
stopDeviceMonitoring(deviceCode);
|
|
|
- return;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -217,7 +216,7 @@ public class DeviceMonitorManager {
|
|
|
try {
|
|
|
result = HttpTestUtils.getToken(loginIp, Integer.parseInt(loginPort), userName, userPwd);
|
|
|
} catch (Exception e) {
|
|
|
- log.info("获取token失败,请检查配置信息:", JSONObject.parseObject(result.getMsg()));
|
|
|
+ log.info("获取token失败,请检查配置信息:{}", JSONObject.parseObject(result.getMsg()));
|
|
|
}
|
|
|
String token = result.getData().toString();
|
|
|
Map<String, String> content = new HashMap<>();
|