소스 검색

执法记录仪告警

bihuisong 1 일 전
부모
커밋
25ea222722

+ 30 - 0
src/main/java/com/sooka/sponest/monitor/camera/domain/CentermonitorTRecorderAlarm.java

@@ -22,6 +22,12 @@ public class CentermonitorTRecorderAlarm extends BaseEntity {
      */
     private String cameraCode;
 
+
+    /**
+     * 摄像头名称
+     */
+    private String cameraName;
+
     /**
      * 告警名称
      */
@@ -36,6 +42,12 @@ public class CentermonitorTRecorderAlarm extends BaseEntity {
     private Date alarmTime;
 
 
+    /**
+     * 工单id
+     */
+    private String recordId;
+
+
     public void setCameraCode(String cameraCode) {
         this.cameraCode = cameraCode;
     }
@@ -44,6 +56,14 @@ public class CentermonitorTRecorderAlarm extends BaseEntity {
         return cameraCode;
     }
 
+    public String getCameraName() {
+        return cameraName;
+    }
+
+    public void setCameraName(String cameraName) {
+        this.cameraName = cameraName;
+    }
+
     public String getAlarmName() {
         return alarmName;
     }
@@ -60,12 +80,22 @@ public class CentermonitorTRecorderAlarm extends BaseEntity {
         this.alarmTime = alarmTime;
     }
 
+    public String getRecordId() {
+        return recordId;
+    }
+
+    public void setRecordId(String recordId) {
+        this.recordId = recordId;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                 .append("cameraCode", getCameraCode())
+                .append("cameraName", getCameraName())
                 .append("alarmName", getAlarmName())
                 .append("alarmTime", getAlarmTime())
+                .append("recordId", getRecordId())
                 .toString();
     }
 }

+ 2 - 1
src/main/java/com/sooka/sponest/monitor/camera/mapper/CentermonitorTCamerachannelMapper.java

@@ -3,6 +3,7 @@ package com.sooka.sponest.monitor.camera.mapper;
 
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 摄像头通道Mapper接口
@@ -12,5 +13,5 @@ import java.util.List;
  */
 public interface CentermonitorTCamerachannelMapper {
 
-    String selectChannelCodeByDeviceCode(String deviceCode);
+    Map<String, Object> selectChannelCodeByDeviceCode(String deviceCode);
 }

+ 22 - 13
src/main/java/com/sooka/sponest/monitor/dahua/controller/EquipmentStatusTaskController.java

@@ -7,6 +7,7 @@ import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.web.domain.AjaxResult;
 import com.ruoyi.common.log.annotation.Log;
 import com.ruoyi.common.log.enums.BusinessType;
+import com.ruoyi.common.redis.service.RedisService;
 import com.sooka.sponest.monitor.camera.domain.CentermonitorTRecorder;
 import com.sooka.sponest.monitor.camera.mapper.CentermonitorTCameraMapper;
 import com.sooka.sponest.monitor.camera.mapper.CentermonitorTRecorderMapper;
@@ -58,6 +59,8 @@ public class EquipmentStatusTaskController {
     private CentermonitorTRecorderMapper centermonitorTRecorderMapper;
     @Autowired
     private DeviceMonitorManager deviceMonitorManager;
+    @Autowired
+    private RedisService redisService;
     @Value("${sooka.dahua_interface_server.loginIp}")
     private String loginIp;
     @Value("${sooka.dahua_interface_server.loginPort}")
@@ -82,11 +85,13 @@ public class EquipmentStatusTaskController {
     @Log(title = "对接大华根据组织获取执法记录仪、单兵设备在线状态定时任务", businessType = BusinessType.OTHER)
     @ApiOperation(value = "对接大华根据组织获取执法记录仪、单兵设备在线状态定时任务", notes = "对接大华根据组织获取执法记录仪、单兵设备在线状态定时任务")
     @RequestMapping(value = "/getLongPollingPositionMsg", method = GET)
-    public void getLongPollingPositionMsg() throws Exception {
+    public void getLongPollingPositionMsg() {
         log.info("开始执行获取记录仪在离线数据定时任务");
-        R<?> result = HttpTestUtils.getToken(loginIp, Integer.parseInt(loginPort), userName, userPwd);
-        if (result.getCode() != 200) {
-            log.info("获取token失败,请检查配置信息:", JSONObject.parseObject(result.getMsg()));
+        R<?> result = null;
+        try {
+            result = HttpTestUtils.getToken(loginIp, Integer.parseInt(loginPort), userName, userPwd);
+        } catch (Exception e) {
+            log.info("获取token失败,请检查配置信息:{}", JSONObject.parseObject(result.getMsg()));
         }
         String token = result.getData().toString();
         getMessage(loginIp, Integer.parseInt(loginPort), token);
@@ -177,10 +182,10 @@ public class EquipmentStatusTaskController {
             map.put("cameraCode", deviceCode);
             //设备状态,”0” : 不在线 “1” : 在线,保存入库和执法仪状态数值同步
             map.put("status", var.get("status").equals("1") ? "0" : "1");
-            if (deviceCode.equals("11151730914298053")) {
-                System.out.println("设备是否在线:" + (var.get("status").equals("1") ? "在线" : "离线"));
-                System.out.println("设备在离线时间:" + deviceMonitorManager.formatStatusTime(var));
-            }
+//            if (deviceCode.equals("11151730914298053")) {
+//                System.out.println("设备是否在线:" + (var.get("status").equals("1") ? "在线" : "离线"));
+//                System.out.println("设备在离线时间:" + deviceMonitorManager.formatStatusTime(var));
+//            }
             if (var.get("status").equals("0")) {
                 map.put("onlineTime", null);
                 map.put("offlineTime", deviceMonitorManager.formatStatusTime(var));
@@ -189,7 +194,7 @@ public class EquipmentStatusTaskController {
                 map.put("offlineTime", null);
 
                 // 设备上线时,如果还没有监控任务,则启动新任务
-                if (!deviceMonitorManager.hasMonitoringTask(deviceCode)) {
+                if (!deviceMonitorManager.hasMonitoringTask(deviceCode) && !redisService.hasKey("deviceCode_" + deviceCode)) {
                     try {
                         deviceMonitorManager.startDeviceMonitoringIfAbsent(deviceCode, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(deviceMonitorManager.formatStatusTime(var)));
                     } catch (ParseException e) {
@@ -218,12 +223,15 @@ public class EquipmentStatusTaskController {
      */
     @RequestMapping(value = "/equipStartup/{cameraCode}", method = GET)
     public void equipStartup(@PathVariable("cameraCode") String cameraCode) {
+//        log.info("执法记录仪设备开机:{}", cameraCode);
         JSONObject json = deviceMonitorManager.getJsonObject(cameraCode, "1");
         if (ObjectUtils.isEmpty(json)) {
             log.info("获取设备信息失败,请检查设备编码:{}", cameraCode);
             return;
         }
-        deviceMonitorManager.stopDeviceMonitoring(json.get("code").toString());
+        String deviceCode =json.get("code").toString();
+        deviceMonitorManager.stopDeviceMonitoring(deviceCode);
+        redisService.setCacheObject("deviceCode_" + deviceCode, deviceCode);
     }
 
 
@@ -233,8 +241,9 @@ public class EquipmentStatusTaskController {
      * @param cameraCode 摄像头编码
      * @throws Exception
      */
-    @RequestMapping(value = "/equipShutdown/{cameraCode}", method = GET)
-    public void equipShutdown(@PathVariable("cameraCode") String cameraCode) {
+    @RequestMapping(value = "/equipShutdown/{cameraCode}/{recordId}", method = GET)
+    public void equipShutdown(@PathVariable("cameraCode") String cameraCode, @PathVariable("recordId") String recordId) {
+//        log.info("执法记录仪设备关机:{}", cameraCode);
         //根据通道编码(摄像头编码)获取大华设备info信息
         JSONObject json = deviceMonitorManager.getJsonObject(cameraCode, "1");
 
@@ -242,7 +251,7 @@ public class EquipmentStatusTaskController {
         //设备状态,”0” : 不在线 “1” : 在线
         if (!json.get("status").equals("0")) {
             //传递关机信号,状态为0,则添加告警定时任务
-            deviceMonitorManager.startShutdownMonitoring(json.get("code").toString(), "0");
+            deviceMonitorManager.startShutdownMonitoring(json.get("code").toString(), "0", recordId);
         }
 
     }

+ 31 - 32
src/main/java/com/sooka/sponest/monitor/dahua/utils/DeviceMonitorManager.java

@@ -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<>();

+ 3 - 2
src/main/resources/mapper/monitor/camera/CentermonitorTCamerachannelMapper.xml

@@ -4,9 +4,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.sooka.sponest.monitor.camera.mapper.CentermonitorTCamerachannelMapper">
 
-    <select id="selectChannelCodeByDeviceCode" resultType="String">
+    <select id="selectChannelCodeByDeviceCode" resultType="java.util.Map">
         SELECT
-            channel_code
+            channel_name channelName,
+            channel_code channelCode
         FROM
             centermonitor_t_camerachannel
         WHERE

+ 3 - 3
src/main/resources/mapper/monitor/camera/CentermonitorTRecorderAlarmMapper.xml

@@ -10,9 +10,9 @@
         <result property="alarmTime" column="alarm_time"/>
     </resultMap>
 
-    <insert id="insertCentermonitorTRecorderAlarm">
-        INSERT INTO centermonitor_t_recorder_alarm (camera_code, alarm_name, alarm_time)
-        VALUES (#{cameraCode}, #{alarmName}, #{alarmTime})
+    <insert id="insertCentermonitorTRecorderAlarm" parameterType="CentermonitorTRecorderAlarm">
+        INSERT INTO centermonitor_t_recorder_alarm (camera_code, camera_name, alarm_name, alarm_time, record_id)
+        VALUES (#{cameraCode}, #{cameraName}, #{alarmName}, #{alarmTime}, #{recordId})
     </insert>
 
 </mapper>