liusuyi
2024-06-25 7556c19ef999f51d07d85cb5d13eba27d9efa41e
ard-work/src/main/java/com/ruoyi/utils/sdk/dhsdk/service/impl/DahuaSDK.java
@@ -4,7 +4,10 @@
import com.ruoyi.alarm.global.domain.GuideTask;
import com.ruoyi.alarm.global.service.impl.QueueHandler;
import com.ruoyi.common.annotation.SdkOperate;
import com.ruoyi.common.config.ARDConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
@@ -13,6 +16,7 @@
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.device.camera.domain.CameraCmd;
import com.ruoyi.device.camera.service.IArdCamerasService;
import com.ruoyi.device.camera.service.ICameraSdkService;
import com.ruoyi.device.channel.domain.ArdChannel;
import com.ruoyi.device.channel.service.IArdChannelService;
import com.ruoyi.media.domain.Vtdu;
@@ -71,6 +75,9 @@
    private IArdChannelService ardChannelService;
    @Resource
    private IVtduService vtduService;
    @Resource
    ICameraSdkService cameraSdkService;
    @Value("${minio.endpoint}")
    private String minioEndPoint;
    @Resource
@@ -105,76 +112,24 @@
                log.error("设备[" + camera.getIp() + ":" + camera.getPort() + "]登录失败:" + getErrorCodePrint());
                return AjaxResult.warn(ErrorCode.getErrorCode(LoginModule.netsdk.CLIENT_GetLastError()));
            }
            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
                GlobalVariable.loginMap.remove(camera.getId());
            }
            //删除管理通道
            ardChannelService.deleteArdChannelByDeviceId(camera.getId());
//            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
//                GlobalVariable.loginMap.remove(camera.getId());
//            }
            camera.setState("1");
            camera.setChanNum(m_stDeviceInfo.byChanNum);
            camera.setStartDChan(1);
            camera.setLoginId((int) loginId.longValue());
            ardCamerasService.updateArdCameras(camera);
            GlobalVariable.loginMap.put(camera.getId(), loginId);
            //获取最新通道
            for (int i = 1; i < m_stDeviceInfo.byChanNum + 1; i++) {
                ArdChannel channel = new ArdChannel();
                NetSDKLib.AV_CFG_ChannelName av_cfg_channelName = new NetSDKLib.AV_CFG_ChannelName();
                boolean b = ConfigModule.GetNewDevConfig(loginId, i - 1, CFG_CMD_CHANNELTITLE, av_cfg_channelName);
                if (b) {
                    String chanName = null;
                    try {
                        chanName = new String(av_cfg_channelName.szName, "GBK").trim();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    channel.setName(chanName);
                } else {
                    channel.setName("通道" + i);
                }
                channel.setDeviceId(camera.getId());
                channel.setChanNo(i);
                ardChannelService.insertArdChannel(channel);
            List<ArdChannel> ardChannelList = getChannels(camera);
            if (ardChannelList.size() > 0) {
                camera.setChanNum(ardChannelList.size());
                ardCamerasService.updateArdCameras(camera);
                //配置到流媒体
                addVtdu(camera);
            }
            //配置到流媒体
            for (int i = 1; i < m_stDeviceInfo.byChanNum + 1; i++) {
                String name = camera.getId() + "_" + i;
                String rtspSource = "rtsp://" + camera.getUsername() + ":" + camera.getPassword() + "@" + camera.getIp() + ":" + camera.getRtspPort() + "/cam/realmonitor?channel=" + i + "&subtype=0";
                Vtdu vtdu = vtduService.selectVtduByName(name);
                if (vtdu != null) {
                    vtduService.deleteVtduByName(name);
                }
                //添加到流媒体
                CameraCmd cmd = new CameraCmd(camera.getId(), i);
                Map<String, Object> videoCompressionCfg = getVideoCompressionCfg(cmd);
                vtdu = new Vtdu();
                if (videoCompressionCfg.get("videoEncType") != null) {
                    if (videoCompressionCfg.get("videoEncType").equals("标准h264")) {
                        vtdu.setIsCode("0");//默认不转码
                    } else {
                        vtdu.setIsCode("1");//默认转码
                    }
                } else {
                    vtdu.setIsCode("0");//默认不转码
                }
                vtdu.setRtspSource(rtspSource);
                vtdu.setName(camera.getId() + "_" + i);
                vtdu.setMode("1");//默认CPU软解码
                vtdu.setCameraId(camera.getId());
                vtduService.insertVtdu(vtdu);
            }
            //创建引导队列
            if (camera.getCamAlarmGuideEnable() != null && camera.getCamAlarmGuideEnable() == 1) {
                if (!GuidePriorityQueue.cameraQueueMap.containsKey(camera.getId())) {
                    Comparator<GuideTask> comparator = GuidePriorityQueue.getComparator();
                    PriorityBlockingQueue<GuideTask> priorityQueue = new PriorityBlockingQueue<>(1000, comparator);
                    GuidePriorityQueue.cameraQueueMap.put(camera.getId(), priorityQueue);
                }
                //启动队列处理器
                queueHandler.process(camera.getId());
            }
            createGuideQueue(camera);
            return AjaxResult.success("设备登录成功");
        } catch (Exception ex) {
            log.error("设备登录异常:" + ex.getMessage());
@@ -199,11 +154,9 @@
                log.error("设备[" + camera.getIp() + ":" + camera.getPort() + "]登录失败:" + getErrorCodePrint());
                return AjaxResult.warn(getErrorCodePrint());
            }
            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
                GlobalVariable.loginMap.remove(camera.getId());
            }
            //删除管理通道
            ardChannelService.deleteArdChannelByDeviceId(camera.getId());
//            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
//                GlobalVariable.loginMap.remove(camera.getId());
//            }
            camera.setState("1");
            camera.setChanNum(m_stDeviceInfo.byChanNum);
            camera.setStartDChan(1);
@@ -212,27 +165,13 @@
            GlobalVariable.loginMap.put(camera.getId(), loginId);
            //获取最新通道
            for (int i = 1; i < m_stDeviceInfo.byChanNum + 1; i++) {
                ArdChannel channel = new ArdChannel();
                NetSDKLib.AV_CFG_ChannelName av_cfg_channelName = new NetSDKLib.AV_CFG_ChannelName();
                boolean b = ConfigModule.GetNewDevConfig(loginId, i - 1, CFG_CMD_CHANNELTITLE, av_cfg_channelName);
                if (b) {
                    String chanName = null;
                    try {
                        chanName = new String(av_cfg_channelName.szName, "GBK").trim();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    channel.setName(chanName);
                } else {
                    channel.setName("通道" + i);
                }
                channel.setDeviceId(camera.getId());
                channel.setChanNo(i);
                ardChannelService.insertArdChannel(channel);
            List<ArdChannel> ardChannelList = getChannels(camera);
            if (ardChannelList.size() > 0) {
                camera.setChanNum(ardChannelList.size());
                ardCamerasService.updateArdCameras(camera);
                //配置到流媒体
                addVtdu(camera);
            }
            //配置到流媒体
            addVtdu(camera);
            //创建引导队列
            createGuideQueue(camera);
            return AjaxResult.success("登录成功");
@@ -274,13 +213,49 @@
    //创建引导队列
    private void createGuideQueue(ArdCameras camera) {
        if (!GuidePriorityQueue.cameraQueueMap.containsKey(camera.getId())) {
            Comparator<GuideTask> comparator = GuidePriorityQueue.getComparator();
            PriorityBlockingQueue<GuideTask> priorityQueue = new PriorityBlockingQueue<>(1000, comparator);
            GuidePriorityQueue.cameraQueueMap.put(camera.getId(), priorityQueue);
            //启动队列处理器
            queueHandler.process(camera.getId());
        if (camera.getGdtype().equals("1")) {
            if (!GuidePriorityQueue.cameraQueueMap.containsKey(camera.getId())) {
                Comparator<GuideTask> comparator = GuidePriorityQueue.getComparator();
                PriorityBlockingQueue<GuideTask> priorityQueue = new PriorityBlockingQueue<>(1000, comparator);
                GuidePriorityQueue.cameraQueueMap.put(camera.getId(), priorityQueue);
                //启动队列处理器
                queueHandler.process(camera.getId());
            }
        }
    }
    //获取通道
    public List<ArdChannel> getChannels(ArdCameras camera) {
        ardChannelService.deleteArdChannelByDeviceId(camera.getId());
        LLong loginId = new LLong(camera.getLoginId());
        List<ArdChannel> ardChannelList = new ArrayList<>();
        for (int i = 1; i < camera.getChanNum() + 1; i++) {
            ArdChannel channel = new ArdChannel();
            NetSDKLib.AV_CFG_ChannelName av_cfg_channelName = new NetSDKLib.AV_CFG_ChannelName();
            boolean b = ConfigModule.GetNewDevConfig(loginId, i - 1, CFG_CMD_CHANNELTITLE, av_cfg_channelName);
            if (!b) {
                log.error("获取配置失败,请稍后重试" + getErrorCodePrint());
                // return AjaxResult.warn(ErrorCode.getErrorCode(LoginModule.netsdk.CLIENT_GetLastError()));
            }
            String chanName = "";
            try {
                chanName = new String(av_cfg_channelName.szName, "GBK").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            channel.setName(chanName.equals("") ? "通道" + i : chanName);
            channel.setDeviceId(camera.getId());
            channel.setChanNo(i);
            if (camera.getGdtype().equals("2")) {
                //NVR过滤不在线的通道
                if (!ConfigModule.queryCameraState(loginId, camera.getChanNum(), i)) {
                    continue;
                }
            }
            ardChannelList.add(channel);
            ardChannelService.insertArdChannel(channel);
        }
        return ardChannelList;
    }
    /**
@@ -436,9 +411,9 @@
        float t = (float) dh_ptz_location_info.nPTZTilt / 10 * -1;
        String nPTZTilt = df.format(t < 0 ? t + 360 : t);
        String nPTZZoom = df.format((float) dh_ptz_location_info.nPTZZoom);
        ptzMap.put("p", nPTZPan);
        ptzMap.put("t", nPTZTilt);
        ptzMap.put("z", nPTZZoom);
        ptzMap.put("p" , nPTZPan);
        ptzMap.put("t" , nPTZTilt);
        ptzMap.put("z" , nPTZZoom);
        return AjaxResult.success(ptzMap);
    }
@@ -559,7 +534,9 @@
                return AjaxResult.warn("设备未登录");
            }
            LLong loginId = (LLong) GlobalVariable.loginMap.get(cameraId);
            String imagePath = FileUtils.createFile("D:/LocalCaptureTemp/" + cameraId + ".jpeg");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String imagePath = FileUtils.createFile(tempPath + "/" + cameraId + ".jpeg");
            fCaptureReceiveCB1 m_CaptureReceiveCB = new fCaptureReceiveCB1(imagePath);
            CapturePictureModule.setSnapRevCallBack(m_CaptureReceiveCB);
            boolean b = CapturePictureModule.remoteCapturePicture(loginId, chanNo - 1);
@@ -584,6 +561,7 @@
    @Override
    public AjaxResult localRecordStart(CameraCmd cmd) {
        try {
            String operator = cmd.getOperator();
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
@@ -597,7 +575,10 @@
            //    GlobalVariable.previewMap.remove(cameraId);
            //    log.debug("停止当前录像");
            //}
            String path = FileUtils.createFile("D:/LocalRecordTemp/" + cameraId + ".mp4");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String path = FileUtils.createFile(tempPath + "/" + operator + "/" + cameraId + ".mp4");
            LLong lRealHandle = RealPlayModule.startRealPlay(loginId, chanNo - 1, path);
            if (lRealHandle.longValue() <= 0) {
                log.error("取流失败" + getErrorCodePrint());
@@ -610,7 +591,7 @@
            //    return false;
            //}
            log.debug("本地录像开始");
            return AjaxResult.success("本地录像开始", lRealHandle);
            return AjaxResult.success("本地录像开始" , lRealHandle);
        } catch (Exception ex) {
            log.error("本地录像开始异常" + ex.getMessage());
            return AjaxResult.error("本地录像开始异常" + ex.getMessage());
@@ -621,33 +602,19 @@
    @Override
    public AjaxResult localRecordStop(CameraCmd cmd) {
        try {
            String operator = cmd.getOperator();
            String cameraId = cmd.getCameraId();
            LLong lRealHandle = new LLong(cmd.getRecordId());
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return AjaxResult.warn("设备未登录");
            }
            RealPlayModule.stopRealPlay(lRealHandle);
            log.debug("本地录像停止");
            String recordPath = FileUtils.createFile("D:/LocalRecordTemp/" + cameraId + ".mp4");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String recordPath = FileUtils.createFile(tempPath + "/" + operator + "/" + cameraId + ".mp4");
            byte[] imageBytes = Files.readAllBytes(Paths.get(recordPath));
            // OutputStream outputStream = response.getOutputStream();
            // outputStream.write(imageBytes);
            return AjaxResult.success(imageBytes);
            //if (GlobalVariable.previewMap.containsKey(cameraId)) {
            //    lRealHandle = new LLong(GlobalVariable.previewMap.get(cameraId));
            //    RealPlayModule.stopRealPlay(lRealHandle);
            //    GlobalVariable.previewMap.remove(cameraId);
            //    log.debug("本地录像停止");
            //    String recordPath = FileUtils.createFile("D:/LocalRecordTemp/" + cameraId + ".mp4");
            //    byte[] imageBytes = Files.readAllBytes(Paths.get(recordPath));
            //   // OutputStream outputStream = response.getOutputStream();
            //   // outputStream.write(imageBytes);
            //    return AjaxResult.success(imageBytes);
            //}
            //else {
            //    return AjaxResult.error("设备未开始录像");
            //}
        } catch (Exception ex) {
            log.error("本地录像停止异常" + ex.getMessage());
            return AjaxResult.error("本地录像停止异常" + ex.getMessage());
@@ -668,7 +635,9 @@
            String url = "";
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String path = FileUtils.createFile(tempPath + "/" + cameraId + ".mp4");
            boolean enable = cmd.isEnable();
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return "";
@@ -707,7 +676,9 @@
        try {
            String cameraId = cmd.getCameraId();
            Integer chanNo = cmd.getChanNo();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String path = FileUtils.createFile(tempPath + "/" + cameraId + ".mp4");
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return false;
            }
@@ -743,7 +714,9 @@
        String url = "";
        try {
            String cameraId = cmd.getCameraId();
            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
            // 本地临时录像地址
            String tempPath = ARDConfig.getProfile() + Constants.LOCAL_RECORD_TEMP_PREFIX;
            String path = FileUtils.createFile(tempPath + "/" + cameraId + ".mp4");
            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                return "";
            }
@@ -793,10 +766,6 @@
        return result;
    }
    @Override
    public List<ArdChannel> getIPChannelInfo(ArdCameras camera) {
        return null;
    }
    /**
     * 引导目标位置
@@ -816,15 +785,18 @@
            double[] cameraPositon = new double[]{cameras.getLongitude(), cameras.getLatitude(), cameras.getAltitude()};
            double[] targetPositions = cmd.getTargetPosition();
            double[] cameraPTZ = GisUtil.getCameraPTZ(cameraPositon, targetPositions, 20, 150);
            //修正俯仰
            double correctPitch = cameraSdkService.correctPitch(cmd);
            double newt = cameraPTZ[1] + correctPitch;
            int p = (int) (cameraPTZ[0] * 10);
            int t = (int) (cameraPTZ[1] * 10);
            int t = (int) (newt * 10);
            int z = (int) (cameraPTZ[2]);
            boolean bool = netsdk.CLIENT_DHPTZControlEx(loginId, chanNo - 1, NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_EXACTGOTO, p, t, z, 0);
            if (!bool) {
                log.error("设置ptz失败:" + getErrorCodePrint());
                return AjaxResult.warn("设置ptz失败:" + getErrorCodePrint());
            }
            return AjaxResult.success("引导目标位置成功");
            return AjaxResult.success("引导目标位置成功",correctPitch);
        } catch (Exception ex) {
            log.error("引导目标位置异常:" + ex.getMessage());
            return AjaxResult.error("引导目标位置异常:" + ex.getMessage());
@@ -1138,10 +1110,10 @@
                int nHeight = cfg_encode_info.stuMainStream[0].stuVideoFormat.nHeight;
                String resolution = nWidth + "*" + nHeight;
                float nFrameRate = cfg_encode_info.stuMainStream[0].stuVideoFormat.nFrameRate;
                map.put("resolution", resolution);//分辨率
                map.put("videoBitrate", String.valueOf(nBitRate));//比特率
                map.put("videoEncType", videoEncType);//编码
                map.put("nFrameRate", String.valueOf(nFrameRate));//帧率
                map.put("resolution" , resolution);//分辨率
                map.put("videoBitrate" , String.valueOf(nBitRate));//比特率
                map.put("videoEncType" , videoEncType);//编码
                map.put("nFrameRate" , String.valueOf(nFrameRate));//帧率
            }
        } catch (Exception ex) {
            log.error("取码流压缩参数异常:" + ex.getMessage());
@@ -1168,8 +1140,8 @@
            float nAngelH = (float) dh_out_ptz_view_range_status.nAngelH / 10;
            float nAngelV = (float) dh_out_ptz_view_range_status.nAngelV / 10;
            Map<String, Object> map = getPtz(cmd);//获取ptz
            map.put("fHorFieldAngle", nAngelH);// 水平视场角
            map.put("fVerFieldAngle", nAngelV);// 垂直视场角
            map.put("fHorFieldAngle" , nAngelH);// 水平视场角
            map.put("fVerFieldAngle" , nAngelV);// 垂直视场角
            return AjaxResult.success(map);
        } catch (Exception ex) {
            log.error("获取云台可视域异常" + ex.getMessage());