From 0362c0c8502a81fc974afb8d2aff85d9efd7bbb1 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: Thu, 29 Jun 2023 17:28:22 +0800
Subject: [PATCH] 修改报警逻辑 增加优先级队列

---
 ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java |  283 ++++++++++++++++++++++++++++----------------------------
 1 files changed, 141 insertions(+), 142 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java b/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
index e2e2290..9a5e8ed 100644
--- a/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
+++ b/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
@@ -6,13 +6,13 @@
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUtils;
-import com.ruoyi.constant.sdkPriority;
+import com.ruoyi.common.utils.uuid.IdUtils;
+import com.ruoyi.constant.CamPriority;
 import com.ruoyi.device.camera.domain.CameraCmd;
 import com.ruoyi.device.camera.mapper.ArdCamerasMapper;
 import com.ruoyi.device.hiksdk.common.GlobalVariable;
 import com.ruoyi.device.camera.domain.ArdCameras;
 import com.ruoyi.device.hiksdk.config.MinioClientSingleton;
-import com.ruoyi.device.hiksdk.domain.recordInfo;
 import com.ruoyi.device.hiksdk.util.hikSdkUtil.GisUtil;
 import com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK;
 import com.ruoyi.device.hiksdk.service.IHikClientService;
@@ -24,7 +24,6 @@
 import com.sun.jna.Pointer;
 import com.sun.jna.ptr.IntByReference;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import sun.misc.BASE64Encoder;
 
@@ -184,7 +183,7 @@
         System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
         m_strLoginInfo.wPort = m_sPort;
         m_strLoginInfo.byVerifyMode = 0;
-        m_strLoginInfo.byLoginMode=0;
+        m_strLoginInfo.byLoginMode = 0;
         //是否异步登录:0- 否,1- 是  windowsSDK里是true和false
         m_strLoginInfo.bUseAsynLogin = true;
         //异步登录回调
@@ -211,6 +210,7 @@
             log.debug("加载lib完成!");
             List<ArdCameras> ardCameras = ardCamerasMapper.selectArdCamerasListNoDataScope(new ArdCameras());
             for (ArdCameras camera : ardCameras) {
+                Thread.sleep(500);
                 login(camera);
             }
         } catch (Exception ex) {
@@ -267,7 +267,7 @@
      */
     @Override
     @SdkOperate
-    public boolean PTZControlWithSpeed(CameraCmd cmd) {
+    public boolean pTZControlWithSpeed(CameraCmd cmd) {
         String cameraId = cmd.getCameraId();
         boolean enable = cmd.isEnable();
         Integer channelNum = cmd.getChannelNum();
@@ -596,7 +596,7 @@
             double p = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
             double t = c.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
             double z = d.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
-            log.debug("T垂直参数为: " + p + "P水平参数为: " + t + "Z变倍参数为: " + z);
+//            log.debug("T垂直参数为: " + p + "P水平参数为: " + t + "Z变倍参数为: " + z);
             Map<String, Object> ptzMap = new HashMap<>();
             ptzMap.put("p", p);
             ptzMap.put("t", t);
@@ -605,7 +605,7 @@
         } else {
             int code = hCNetSDK.NET_DVR_GetLastError();
             log.info("控制失败,请稍后重试" + code);
-            return null;
+            return new HashMap<>();
         }
 
     }
@@ -653,7 +653,7 @@
 
     @Override
     @SdkOperate
-    public boolean setTargetPosition(CameraCmd cmd) {
+    public boolean guideTargetPosition(CameraCmd cmd) {
         String cameraId = cmd.getCameraId();
         Integer channelNum = cmd.getChannelNum();
         if (!GlobalVariable.loginMap.containsKey(cameraId)) {
@@ -663,7 +663,8 @@
         NET_DVR_PTZPOS m_ptzPosCurrent = new NET_DVR_PTZPOS();
         m_ptzPosCurrent.wAction = 1;
         try {
-            double[] cameraPositon = cmd.getCamPosition();
+            ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId);
+            double[] cameraPositon = new double[]{cameras.getLongitude(), cameras.getLatitude(), cameras.getAltitude()};
             double[] targetPositions = cmd.getTargetPosition();
             double[] cameraPTZ = GisUtil.getCameraPTZ(cameraPositon, targetPositions, 20, 150);
             String p = String.valueOf((int) (cameraPTZ[0] * 10));
@@ -681,7 +682,7 @@
             }
             return bool;
         } catch (Exception ex) {
-            log.error(ex.getMessage());
+            log.error("引导异常:" + ex.getMessage());
             return false;
         }
     }
@@ -710,7 +711,7 @@
             ardCameras.setOperatorId(operator);
             //设置当前过期时间
             Date now = new Date();
-            now.setTime(now.getTime() + expired * 1000 * 60);
+            now.setTime(now.getTime() + expired * 1000);
             ardCameras.setOperatorExpired(now);
             ardCamerasMapper.updateArdCameras(ardCameras);
         } else {
@@ -719,24 +720,24 @@
             if (currentOperator.equals(operator)) {
                 //设置当前过期时间
                 Date now = new Date();
-                now.setTime(now.getTime() + expired * 1000 * 60);
+                now.setTime(now.getTime() + expired * 1000);
                 ardCameras.setOperatorExpired(now);
                 ardCamerasMapper.updateArdCameras(ardCameras);
             } else {
                 //如果非本人比较优先级
                 Integer currentLevel = 0;//当前操作者的优先级
-                if (sdkPriority.priorityMap.containsKey(currentOperator)) {
+                if (CamPriority.priorityMap.containsKey(currentOperator)) {
                     /*当前控制者为系统报警用户*/
-                    currentLevel = (Integer) sdkPriority.priorityMap.get(currentOperator);
+                    currentLevel = (Integer) CamPriority.priorityMap.get(currentOperator);
                 } else {
                     /*当前控制者为普通用户*/
                     SysUser sysUser = sysUserMapper.selectUserById(currentOperator);
                     currentLevel = sysUser.getCameraPriority();
                 }
                 Integer operatorLevel = 0;//获取申请者的优先级
-                if (sdkPriority.priorityMap.containsKey(operator)) {
+                if (CamPriority.priorityMap.containsKey(operator)) {
                     /*包含说明当前申请控制者为系统报警用户*/
-                    operatorLevel = (Integer) sdkPriority.priorityMap.get(operator);
+                    operatorLevel = (Integer) CamPriority.priorityMap.get(operator);
                 } else {
                     /*否则申请控制者为当前登录用户*/
                     LoginUser loginUser = SecurityUtils.getLoginUser();
@@ -746,7 +747,7 @@
                 //判断优先级
                 if (operatorLevel > currentLevel) {
                     Date now = new Date();
-                    now.setTime(now.getTime() + expired * 60 * 1000);
+                    now.setTime(now.getTime() + expired * 60);
                     ardCameras.setOperatorExpired(now);//设置当前过期时间
                     ardCameras.setOperatorId(operator);//设置当前用户
                     ardCamerasMapper.updateArdCameras(ardCameras);
@@ -1013,6 +1014,25 @@
         return bool;
     }
 
+    public String getFocusMode(CameraCmd cmd)
+    {
+        String cameraId = cmd.getCameraId();
+        Integer channelNum = cmd.getChannelNum();
+        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
+            return "";
+        }
+        Integer userId = GlobalVariable.loginMap.get(cameraId);
+        NET_DVR_FOCUSMODE_CFG struFocusMode = new NET_DVR_FOCUSMODE_CFG();
+        Pointer point = struFocusMode.getPointer();
+        IntByReference ibrBytesReturned = new IntByReference(0);
+        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_FOCUSMODECFG, channelNum, point, struFocusMode.size(), ibrBytesReturned);
+        if (!b_GetCameraParam) {
+            System.out.println("获取前端参数失败,错误码:" + hCNetSDK.NET_DVR_GetLastError());
+        }
+        struFocusMode.read();
+        log.info("当前聚焦模式:" + struFocusMode.byFocusMode);
+        return String.valueOf(struFocusMode.byFocusMode);
+    }
     /**
      * @描述 云台加热开关
      * @参数 [userId, channelNum, enable]
@@ -1232,109 +1252,14 @@
      * @修改人和其它信息
      */
     @Override
-    @SdkOperate
     public String record(CameraCmd cmd) {
-        String cameraId = cmd.getCameraId();
-        Integer channelNum = cmd.getChannelNum();
-        boolean enable = cmd.isEnable();
-        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
-            return "";
-        }
-        Integer userId = GlobalVariable.loginMap.get(cameraId);
-        String path = "";
-        //预览参数
-        NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
-        previewinfo.read();
-        previewinfo.lChannel = channelNum;
-        previewinfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推
-        previewinfo.dwLinkMode = 0;//连接方式:0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RTP/HTTP,6-HRUDP(可靠传输),7-RTSP/HTTPS,8-NPQ
-        previewinfo.hPlayWnd = null;//播放窗口的句柄,为NULL表示不解码显示。
-        previewinfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流
-        previewinfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式
-        previewinfo.write();
-        int lRealHandle;
-        if (enable) {
-            if (!GlobalVariable.user_real_Map.containsKey(userId)) {
-                lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
-                if (lRealHandle == -1) {
-                    int iErr = hCNetSDK.NET_DVR_GetLastError();
-                    log.error("取流失败" + iErr);
-                    return "";
-                }
-                log.info("取流成功");
-
-                File file = new File("D:/record/temp.mp4");
-                if (!file.exists()) {
-                    try {
-                        File fileParent = file.getParentFile();
-                        if (!fileParent.exists()) {
-                            fileParent.mkdirs();
-                        }
-                        file.createNewFile();
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                }
-                try {
-                    path = file.getCanonicalPath();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-                recordInfo info = new recordInfo();
-                info.setLRealHandle(lRealHandle);
-                info.setRecordPath(path);
-                GlobalVariable.user_real_Map.put(userId, info);
-            }
-            recordInfo info = GlobalVariable.user_real_Map.get(userId);
-            if (!hCNetSDK.NET_DVR_SaveRealData_V30(info.getLRealHandle(), 1, info.getRecordPath())) {
-                log.error("保存视频文件到临时文件夹失败 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
-                return "保存视频文件到临时文件夹失败 错误码为:" + hCNetSDK.NET_DVR_GetLastError();
-            }
-            log.info("录像开始");
-            //return info.getRecordPath();
-            return "";
-        } else {
-            recordInfo info = GlobalVariable.user_real_Map.get(userId);
-            if (StringUtils.isNull(info)) {
-                return "";
-            }
-            hCNetSDK.NET_DVR_StopRealPlay(info.getLRealHandle());
-            log.info("录像停止");
-            //存入minio
-            String BucketName = "record";
-            String uuid = UUID.randomUUID().toString().replace("-", "");
-            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
-            String ObjectName = cameraId + "/" + time + "/" + uuid + ".mp4";
-            String ContentType = "video/MP4";
-            FileInputStream stream = null;
-            try {
-                stream = new FileInputStream(info.getRecordPath());
-            } catch (FileNotFoundException e) {
-                e.printStackTrace();
-            }
-            String url = "";
-            try {
-                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
-                if (b) {
-                    url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
-                    log.info("上传文件成功!" + url);
-                }
-            } catch (IOException ex) {
-                log.error("上传文件异常:" + ex.getMessage());
-            }
-            GlobalVariable.user_real_Map.remove(userId);
-            return url;
-        }
-    }
-
-    @Override
-    public void recordToMinio(CameraCmd cmd) {
         try {
             String cameraId = cmd.getCameraId();
             Integer channelNum = cmd.getChannelNum();
+            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
             boolean enable = cmd.isEnable();
             if (!GlobalVariable.loginMap.containsKey(cameraId)) {
-                return;
+                return "";
             }
             Integer userId = GlobalVariable.loginMap.get(cameraId);
             //强制I帧结构体对象
@@ -1357,50 +1282,124 @@
             previewinfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流
             previewinfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式
             previewinfo.write();
-            int lRealHandle;
+            String url = "";
             if (enable) {
-                if (!GlobalVariable.user_real_Map.containsKey(userId)) {
-                    lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
+                if (!GlobalVariable.previewMap.containsKey(cameraId)) {
+                    int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
                     if (lRealHandle == -1) {
                         log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
-                        return;
+                        return "";
                     }
                     log.info("取流成功");
-
-                    String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
-                    recordInfo info = new recordInfo();
-                    info.setLRealHandle(lRealHandle);
-                    info.setRecordPath(path);
-                    GlobalVariable.user_real_Map.put(userId, info);
+                    GlobalVariable.previewMap.put(cameraId, lRealHandle);
                 }
-                recordInfo info = GlobalVariable.user_real_Map.get(userId);
-                if (!hCNetSDK.NET_DVR_SaveRealData_V30(info.getLRealHandle(), 2, info.getRecordPath())) {
+                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
                     log.error("保存视频文件到临时文件夹失败 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
-                    return;
+                    return "";
                 }
                 log.info("录像开始");
             } else {
-                recordInfo info = GlobalVariable.user_real_Map.get(userId);
-                if (StringUtils.isNull(info)) {
-                    return;
+                if (GlobalVariable.previewMap.containsKey(cameraId)) {
+                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
+                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
+                    GlobalVariable.previewMap.remove(cameraId);
                 }
-                hCNetSDK.NET_DVR_StopRealPlay(info.getLRealHandle());
                 log.info("录像停止");
-                if (cmd.isUploadMinio()) {
-                    //存入minio
-                    String BucketName = cmd.getRecordBucketName();
-                    String ObjectName = cmd.getRecordObjectName();
-                    String ContentType = "video/MP4";
-                    FileInputStream stream = new FileInputStream(info.getRecordPath());
-                    boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
-                    if (b) {
-                        log.info("上传文件成功!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
-                        GlobalVariable.user_real_Map.remove(userId);
-                    }
+                //存入minio
+                String BucketName = cmd.getRecordBucketName();
+                String ObjectName = cmd.getRecordObjectName();
+                String ContentType = "video/MP4";
+                FileInputStream stream = new FileInputStream(path);
+                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
+                if (b) {
+                    url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
+                    log.info("上传文件成功!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                 }
             }
+            return url;
         } catch (Exception ex) {
             log.error("录像异常" + ex.getMessage());
+            return "";
+        }
+    }
+
+    @Override
+    public String recordToMinio(CameraCmd cmd) {
+        try {
+            String cameraId = cmd.getCameraId();
+            Integer channelNum = cmd.getChannelNum();
+            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
+            boolean enable = cmd.isEnable();
+            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
+                return "";
+            }
+            Integer userId = GlobalVariable.loginMap.get(cameraId);
+            //强制I帧结构体对象
+            HCNetSDK.NET_DVR_I_FRAME netDvrIFrame = new HCNetSDK.NET_DVR_I_FRAME();   //新建结构体对象
+            netDvrIFrame.read();
+            netDvrIFrame.dwChannel = channelNum;//因为上文代码中设置了通道号,按照上文中的设置
+            netDvrIFrame.byStreamType = 0;
+            netDvrIFrame.dwSize = netDvrIFrame.size();
+            netDvrIFrame.write();
+            if (!hCNetSDK.NET_DVR_RemoteControl(userId, 3402, netDvrIFrame.getPointer(), netDvrIFrame.dwSize)) {
+                log.error("强制I帧 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
+            }
+            //预览参数
+            NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
+            previewinfo.read();
+            previewinfo.lChannel = channelNum;
+            previewinfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推
+            previewinfo.dwLinkMode = 0;//连接方式:0-TCP方式,1-UDP方式,2-多播方式,3-RTP方式,4-RTP/RTSP,5-RTP/HTTP,6-HRUDP(可靠传输),7-RTSP/HTTPS,8-NPQ
+            previewinfo.hPlayWnd = null;//播放窗口的句柄,为NULL表示不解码显示。
+            previewinfo.bBlocked = 0;//0- 非阻塞取流,1-阻塞取流
+            previewinfo.byNPQMode = 0;//NPQ模式:0-直连模式,1-过流媒体模式
+            previewinfo.write();
+            if (enable) {
+                if (GlobalVariable.previewMap.containsKey(cameraId)) {
+                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
+                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
+                    GlobalVariable.previewMap.remove(cameraId);
+                    log.debug("停止当前录像");
+                }
+                int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
+                if (lRealHandle == -1) {
+                    log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
+                    return "";
+                }
+                log.debug("取流成功");
+                GlobalVariable.threadMap.put(cameraId, Thread.currentThread().getName());
+                GlobalVariable.previewMap.put(cameraId, lRealHandle);
+                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
+                    log.error("保存视频文件到临时文件夹失败 错误码为:  " + hCNetSDK.NET_DVR_GetLastError());
+                    return "";
+                }
+                log.debug("录像开始");
+            } else {
+                if (GlobalVariable.previewMap.containsKey(cameraId)) {
+                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
+                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
+                    GlobalVariable.previewMap.remove(cameraId);
+                }
+                log.debug("录像停止");
+                //存入minio
+                String BucketName = cmd.getRecordBucketName();
+                String ObjectName = cmd.getRecordObjectName();
+                String ContentType = "video/MP4";
+                FileInputStream stream = new FileInputStream(path);
+                String simpleUUID = IdUtils.simpleUUID();
+                String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
+                String recordName = cameraId + "/" + time + "/" + ObjectName +"_"+ simpleUUID + ".mp4";
+                boolean b = MinioUtils.uploadObject(BucketName, recordName, stream, stream.available(), ContentType);
+                if (b) {
+                    String url = MinioClientSingleton.domainUrl + "/" + BucketName + "/" + recordName;
+                    log.debug("上传文件成功!" + url);
+                    return url;
+                }
+            }
+            return "";
+        } catch (Exception ex) {
+            log.error("录像异常" + ex.getMessage());
+            return "";
         }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3