‘liusuyi’
2024-04-02 6a26b1d539576a556d0f8682af7ae4317de78c7a
ard-work/src/main/java/com/ruoyi/utils/sdk/hiksdk/service/impl/HikvisionSDK.java
@@ -73,116 +73,7 @@
     * @修改人和其它信息
     */
    @Override
    public boolean login(ArdCameras camera) {
        try {
            // 初始化
            if (!hCNetSDK.NET_DVR_Init()) {
                log.error("SDK初始化失败");
            }
            //打印海康sdk日志
            if (Platform.isWindows()) {
                String WIN_PATH = System.getProperty("user.dir") + File.separator + "ardLog" + File.separator + "logs" + File.separator;
                hCNetSDK.NET_DVR_SetLogToFile(3, WIN_PATH, true);
            } else {
                hCNetSDK.NET_DVR_SetLogToFile(3, "/home/ardLog/hiklog", true);
            }
            String m_sDeviceIP = camera.getIp();
            String m_sUsername = camera.getUsername();
            String m_sPassword = camera.getPassword();
            short m_sPort = camera.getPort().shortValue();
            //设置连接时间与重连时间
            hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
            hCNetSDK.NET_DVR_SetReconnect(5000, true);
            //设备信息, 输出参数
            HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
            HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
            // 注册设备-登录参数,包括设备地址、登录用户、密码等
            m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
            System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
            m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
            System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
            m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
            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;
            //是否异步登录:0- 否,1- 是  windowsSDK里是true和false
            m_strLoginInfo.bUseAsynLogin = false;
            m_strLoginInfo.write();
            //同步登录
            int lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
            if (lUserID < 0) {
                log.debug("Login Failed [ " + camera.getIp() + ":" + camera.getPort() + " ],错误码:" + hCNetSDK.NET_DVR_GetLastError());
                camera.setChanNum(0);
                camera.setLoginId(-1);
                camera.setState("0");
                //删除管理通道
                ardChannelService.deleteArdChannelByDeviceId(camera.getId());
                ardCamerasService.updateArdCameras(camera);
                return false;
            }
            log.debug("Login Success [ " + camera.getIp() + ":" + camera.getPort() + " ]");
            if (fExceptionCallBack == null) {
                fExceptionCallBack = new ExceptionCallBack();//异常回调
                //设置异常回调函数(可在回调函数中获取设备上下线状态等)
                if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, null)) {
                    log.debug("Set fExceptionCallBack function fail");
                    return false;
                } else {
                    log.debug("Set fExceptionCallBack function successfully!");
                }
            }
            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
                GlobalVariable.loginMap.remove(camera.getId());
            }
            //删除管理通道
            ardChannelService.deleteArdChannelByDeviceId(camera.getId());
            GlobalVariable.loginMap.put(camera.getId(), lUserID);
            GlobalVariable.loginCameraMap.put(lUserID, camera);
            camera.setLoginId(lUserID);
            camera.setState("1");
            camera.setChanNum((int) m_strDeviceInfo.struDeviceV30.byChanNum);
            camera.setStartDChan((int) m_strDeviceInfo.struDeviceV30.byStartDChan);
            //获取最新通道
            List<ArdChannel> cameraChannelList = getIPChannelInfo(camera);
            if (cameraChannelList.size() > 0) {
                camera.setChannelList(cameraChannelList);
                for (ArdChannel channel : cameraChannelList) {
                    channel.setId(IdUtils.simpleUUID());
                    ardChannelService.insertArdChannel(channel);
                }
                //添加到流媒体
                addVtdu(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);
            }
            ardCamerasService.updateArdCameras(camera);
        } catch (Exception ex) {
            log.error("注册设备异常", ex);
        }
        return true;
    }
    /**
     * @描述 注册登录 集成于NET_DVR_Login_V30,支持同步和异步登录
     * @参数 [dvrLogin]
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:12
     * @修改人和其它信息
     */
    @Override
    @Async("loginExecutor")
    public void asyncLogin(ArdCameras camera) {
    public AjaxResult login(ArdCameras camera) {
        try {
            // 初始化
            if (!hCNetSDK.NET_DVR_Init()) {
@@ -223,18 +114,126 @@
            int lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
            if (lUserID < 0) {
                int errorCode = hCNetSDK.NET_DVR_GetLastError();
                log.debug("Login Failed 【 " + camera.getIp() + ":" + camera.getPort() + " 】," +
                        "Error Code:" + errorCode + " " +
                        "Error Info:" + SdkErrorCodeEnum.getDescByCode(errorCode));
                camera.setChanNum(0);
                camera.setLoginId(-1);
                camera.setState("0");
                //删除管理通道
                ardChannelService.deleteArdChannelByDeviceId(camera.getId());
                ardCamerasService.updateArdCameras(camera);
                return;
                log.error("设备[" + camera.getIp() + ":" + camera.getPort() + "]登录失败: errorCode:" + errorCode + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(errorCode));
                return AjaxResult.error("登录失败: errorCode:" + errorCode + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(errorCode));
            }
            log.debug("Login Success [ " + camera.getIp() + ":" + camera.getPort() + " ]");
            if (fExceptionCallBack == null) {
                fExceptionCallBack = new ExceptionCallBack();//异常回调
                //设置异常回调函数(可在回调函数中获取设备上下线状态等)
                if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, null)) {
                    log.debug("Set fExceptionCallBack function fail");
                } else {
                    log.debug("Set fExceptionCallBack function successfully!");
                }
            }
            if (GlobalVariable.loginMap.containsKey(camera.getId())) {
                GlobalVariable.loginMap.remove(camera.getId());
            }
            //删除管理通道
            ardChannelService.deleteArdChannelByDeviceId(camera.getId());
            GlobalVariable.loginMap.put(camera.getId(), lUserID);
            GlobalVariable.loginCameraMap.put(lUserID, camera);
            camera.setLoginId(lUserID);
            camera.setState("1");
            camera.setChanNum((int) m_strDeviceInfo.struDeviceV30.byChanNum);
            camera.setStartDChan((int) m_strDeviceInfo.struDeviceV30.byStartDChan);
            //获取最新通道
            List<ArdChannel> cameraChannelList = getIPChannelInfo(camera);
            if (cameraChannelList.size() > 0) {
                camera.setChannelList(cameraChannelList);
                for (ArdChannel channel : cameraChannelList) {
                    channel.setId(IdUtils.simpleUUID());
                    ardChannelService.insertArdChannel(channel);
                }
                //添加到流媒体
                addVtdu(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);
            }
            ardCamerasService.updateArdCameras(camera);
            return AjaxResult.success("设备登录成功");
        } catch (Exception ex) {
            log.error("注册设备异常", ex);
            return AjaxResult.error("注册设备异常" + ex.getMessage());
        }
    }
    /**
     * @描述 注册登录 集成于NET_DVR_Login_V30,支持同步和异步登录
     * @参数 [dvrLogin]
     * @返回值 java.lang.Integer
     * @创建人 刘苏义
     * @创建时间 2023/1/17 16:12
     * @修改人和其它信息
     */
    @Override
    @Async("loginExecutor")
    public AjaxResult asyncLogin(ArdCameras camera) {
        try {
            // 初始化
            if (!hCNetSDK.NET_DVR_Init()) {
                log.error("SDK初始化失败");
                return AjaxResult.error("SDK初始化失败");
            }
            //打印海康sdk日志
            if (Platform.isWindows()) {
                String WIN_PATH = System.getProperty("user.dir") + File.separator + "ardLog" + File.separator + "logs" + File.separator;
                hCNetSDK.NET_DVR_SetLogToFile(3, WIN_PATH, true);
            } else {
                hCNetSDK.NET_DVR_SetLogToFile(3, "/home/ardLog/hiklog", true);
            }
            String m_sDeviceIP = camera.getIp();
            String m_sUsername = camera.getUsername();
            String m_sPassword = camera.getPassword();
            short m_sPort = camera.getPort().shortValue();
            //设置连接时间与重连时间
            hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
            hCNetSDK.NET_DVR_SetReconnect(5000, true);
            //设备信息, 输出参数
            HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
            HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
            // 注册设备-登录参数,包括设备地址、登录用户、密码等
            m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
            System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
            m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
            System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
            m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
            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;
            //是否异步登录:0- 否,1- 是  windowsSDK里是true和false
            m_strLoginInfo.bUseAsynLogin = false;
            m_strLoginInfo.write();
            //同步登录
            int lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
            if (lUserID < 0) {
                int errorCode = hCNetSDK.NET_DVR_GetLastError();
                camera.setChanNum(0);
                camera.setLoginId(-1);
                camera.setState("0");
                //删除管理通道
                ardChannelService.deleteArdChannelByDeviceId(camera.getId());
                ardCamerasService.updateArdCameras(camera);
                log.error("设备[" + camera.getIp() + ":" + camera.getPort() + "]登录失败: errorCode:" + errorCode + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(errorCode));
                return AjaxResult.error("设备登录失败: errorCode:" + errorCode + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(errorCode));
            }
            log.debug("Login Success 【 " + camera.getIp() + ":" + camera.getPort() + " 】");
            synchronized (_lock) {
                if (fExceptionCallBack == null) {
@@ -242,7 +241,6 @@
                    //设置异常回调函数(可在回调函数中获取设备上下线状态等)
                    if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, null)) {
                        log.debug("Set fExceptionCallBack function fail");
                        return;
                    } else {
                        log.debug("Set fExceptionCallBack function successfully!");
                    }
@@ -277,8 +275,10 @@
            addVtdu(camera);
            //创建引导队列
            createGuideQueue(camera);
            return AjaxResult.success("设备登录成功");
        } catch (Exception ex) {
            log.error("注册设备异常", ex);
            return AjaxResult.error("注册设备异常" + ex.getMessage());
        }
    }
@@ -1310,8 +1310,8 @@
        boolean b_GetCameraParam = hCNetSDK.NET_DVR_GetDVRConfig(userId, NET_DVR_GET_CCDPARAMCFG, chanNo, point, struDayNigh.size(), ibrBytesReturned);
        if (!b_GetCameraParam) {
            int code = hCNetSDK.NET_DVR_GetLastError();
            log.error("获取前端参数失败: errorCode" + code + " errorInfo" + SdkErrorCodeEnum.getDescByCode(code));
            return AjaxResult.error("获取前端参数失败: errorCode" + code + " errorInfo" + SdkErrorCodeEnum.getDescByCode(code));
            log.error("获取前端参数失败: errorCode:" + code + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(code));
            return AjaxResult.error("获取前端参数失败: errorCode:" + code + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(code));
        }
        struDayNigh.read();
        String current = struDayNigh.struDayNight.byDayNightFilterType == 1 ? "开启" : "关闭";
@@ -1917,14 +1917,14 @@
            log.error("获取GIS信息数据失败: errorCode:" + code + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(code));
            return AjaxResult.error("获取GIS信息数据失败: errorCode:" + code + " errorInfo:" + SdkErrorCodeEnum.getDescByCode(code));
        }
            struGisInfo.read();
            Map<String, Object> map = new HashMap<>();
            map.put("p", struGisInfo.struPtzPos.fPanPos);
            map.put("t", struGisInfo.struPtzPos.fTiltPos < 0 ? struGisInfo.struPtzPos.fTiltPos + 360 : struGisInfo.struPtzPos.fTiltPos);
            map.put("z", struGisInfo.struPtzPos.fZoomPos);
            map.put("fHorFieldAngle", struGisInfo.fHorizontalValue);// 水平视场角
            map.put("fVerFieldAngle", struGisInfo.fVerticalValue);// 垂直视场角
            return AjaxResult.success(map);
        struGisInfo.read();
        Map<String, Object> map = new HashMap<>();
        map.put("p", struGisInfo.struPtzPos.fPanPos);
        map.put("t", struGisInfo.struPtzPos.fTiltPos < 0 ? struGisInfo.struPtzPos.fTiltPos + 360 : struGisInfo.struPtzPos.fTiltPos);
        map.put("z", struGisInfo.struPtzPos.fZoomPos);
        map.put("fHorFieldAngle", struGisInfo.fHorizontalValue);// 水平视场角
        map.put("fVerFieldAngle", struGisInfo.fVerticalValue);// 垂直视场角
        return AjaxResult.success(map);
    }
}