‘liusuyi’
2023-06-15 81dd45819944baa8032d942e2e28c5c859c0cc81
ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
@@ -36,6 +36,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import static com.ruoyi.device.hiksdk.common.GlobalVariable.threadMap;
import static com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK.*;
/**
@@ -184,7 +185,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 +212,7 @@
            log.debug("加载lib完成!");
            List<ArdCameras> ardCameras = ardCamerasMapper.selectArdCamerasListNoDataScope(new ArdCameras());
            for (ArdCameras camera : ardCameras) {
                Thread.sleep(100);
                login(camera);
            }
        } catch (Exception ex) {
@@ -267,7 +269,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();
@@ -663,7 +665,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 +684,7 @@
            }
            return bool;
        } catch (Exception ex) {
            log.error(ex.getMessage());
            log.error("引导异常:" + ex.getMessage());
            return false;
        }
    }
@@ -710,7 +713,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,7 +722,7 @@
            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 {
@@ -746,7 +749,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);
@@ -1232,98 +1235,74 @@
     * @修改人和其它信息
     */
    @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)) {
        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 "";
            }
            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();
            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();
            String url = "";
            try {
            if (enable) {
                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 "";
                    }
                    log.info("取流成功");
                    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.info("录像开始");
            } else {
                if (GlobalVariable.previewMap.containsKey(cameraId)) {
                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                    GlobalVariable.previewMap.remove(cameraId);
                }
                log.info("录像停止");
                //存入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("上传文件成功!" + url);
                    log.info("上传文件成功!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                }
            } catch (IOException ex) {
                log.error("上传文件异常:" + ex.getMessage());
            }
            GlobalVariable.user_real_Map.remove(userId);
            return url;
        } catch (Exception ex) {
            log.error("录像异常" + ex.getMessage());
            return "";
        }
    }
@@ -1332,6 +1311,7 @@
        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;
@@ -1357,46 +1337,41 @@
            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) {
                        log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
                        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);
                if (GlobalVariable.previewMap.containsKey(cameraId)) {
                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
                    GlobalVariable.previewMap.remove(cameraId);
                    log.info("录像停止");
                }
                recordInfo info = GlobalVariable.user_real_Map.get(userId);
                if (!hCNetSDK.NET_DVR_SaveRealData_V30(info.getLRealHandle(), 2, info.getRecordPath())) {
                int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
                if (lRealHandle == -1) {
                    log.error("取流失败" + hCNetSDK.NET_DVR_GetLastError());
                    return;
                }
                log.info("取流成功");
                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.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) {
                    log.info("上传文件成功!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                }
            }
        } catch (Exception ex) {