对接海康门禁设备-初始化SDK和登录设备

news/2024/11/19 20:34:14/

对接海康门禁设备,初始化SDK


/*** @author czm* @date 2023/2/15*/
public interface HCNetSDK extends Library {public static HCNetSDK getInstance(String sdkPath) {
//      sdkPath sdk 目录路径System.out.println("sdk_path " + sdkPath);HCNetSDK INSTANCE = null;if (osSelect.isWindows()) {INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);}if (osSelect.isLinux()) {INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);}return INSTANCE;}// 初始化SDK,调用其他SDK函数的前提boolean NET_DVR_Init();boolean NET_DVR_SetSDKInitCfg(int enumType, Pointer lpInBuff);//启用日志文件写入接口boolean NET_DVR_SetLogToFile(int bLogEnable, String strLogDir, boolean bAutoDel);int NET_DVR_GetLastError();int NET_DVR_Login_V40(NET_DVR_USER_LOGIN_INFO pLoginInfo, NET_DVR_DEVICEINFO_V40 lpDeviceInfo);boolean NET_DVR_Logout(int lUserID);int NET_DVR_StartRemoteConfig(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferLen, fRemoteConfigCallback cbStateCallback, Pointer pUserData);//释放资源boolean NET_DVR_Cleanup();//逐个下发int NET_DVR_SendWithRecvRemoteConfig(int lHandle, Pointer lpInBuff, int dwInBuffSize, Pointer lpOutBuff, int dwOutBuffSize, IntByReference dwOutDataLen);//关闭长连接 释放资源boolean NET_DVR_StopRemoteConfig(int lHandle);boolean NET_DVR_STDXMLConfig(int lUserID, NET_DVR_XML_CONFIG_INPUT lpInputParam, NET_DVR_XML_CONFIG_OUTPUT lpOutputParam);int NET_DVR_GetNextRemoteConfig(int lHandle, Pointer lpOutBuff, int dwOutBuffSize);boolean NET_DVR_GetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpOutBuffer, int dwOutBufferSize);boolean NET_DVR_SetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpInParamBuffer, int dwInParamBufferSize);//判断是否在线boolean NET_DVR_RemoteControl(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferSize);int NET_DVR_SetupAlarmChan_V41(int lUserID, NET_DVR_SETUPALARM_PARAM lpSetupParam);boolean NET_DVR_CloseAlarmChan(int lAlarmHandle);//撤销布防boolean NET_DVR_CloseAlarmChan_V30(int lAlarmHandle);boolean NET_DVR_StopListen_V30(int lListenHandle);boolean NET_DVR_SetSDKLocalCfg(int enumType, Pointer lpInBuff);int NET_DVR_StartListen_V30(String sLocalIP, short wLocalPort, FMSGCallBack_V31 DataCallBack, Pointer pUserData);//注册回调函数,接收设备报警消息等。boolean NET_DVR_SetDVRMessageCallBack_V31(FMSGCallBack_V31 fMessageCallBack, Pointer pUser);int NET_DVR_SetupAlarmChan_V50(int iUserID, NET_DVR_SETUPALARM_PARAM_V50 lpSetupParam, Pointer pSub, int dwSubSize);public interface FMSGCallBack_V31 extends Callback {public boolean invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);}public interface MSGCallBack_V31 extends Callback {void invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);}public interface FLoginResultCallBack extends Callback {public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);}public interface fRemoteConfigCallback extends Callback {public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);}}

初始化

public class IotHikvisionDeviceApiService implements IotPanelApiService {// 接口的实例,通过接口实例调用外部dll/so的函数private static HCNetSDK hCNetSDK = null;private static HikvisionAlarmCallBack fMSFCallBack_V31 = null;//系统配置变量 获取SDK 文件目录private static String HK_SDK_PATH = "HK_SDK_PATH";static {try {// String sdkPath = "D:\\sdk\\winHikvision\\";String sdkFilePath = null;if (osSelect.isWindows()) {sdkFilePath = HK_SDK_PATH + File.separator + "HCNetSDK.dll";}if (osSelect.isLinux()) {sdkFilePath = HK_SDK_PATH + File.separator + "libhcnetsdk.so";}//判断文件是否存在if (FileUtil.exist(sdkFilePath)) {hCNetSDK = HCNetSDK.getInstance(sdkFilePath);if(ObjectUtil.isNull(hCNetSDK)){log.warn("海康SDK初始化失败");}if (osSelect.isLinux()) {loadSdk(sdkPath);}/******************************** 初始化 ********************************/log.info("start NET_DVR_Init");boolean initSuc = hCNetSDK.NET_DVR_Init();if (!initSuc) {log.error("HikvisionVideoService NET_DVR_Init fail");}/**加载日志*/hCNetSDK.NET_DVR_SetLogToFile(3, FileUtil.getTmpDirPath() + File.separator + "sdklog", false);} else {log.warn("海康SDK文件目录不存在");}} catch (Exception ex) {log.error("Error:{} ", ex.getMessage());}}

加载文件

 public static void loadSdk(String sdkPath) {BYTE_ARRAY ptrByteArray1 = new BYTE_ARRAY(256);BYTE_ARRAY ptrByteArray2 = new BYTE_ARRAY(256);//这里是库的绝对路径,请根据实际情况修改,注意改路径必须有访问权限String strPath1 = sdkPath + "libcrypto.so.1.1";String strPath2 = sdkPath + "libssl.so.1.1";System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());ptrByteArray1.write();hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());ptrByteArray2.write();hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());String strPathCom = sdkPath;NET_DVR_LOCAL_SDK_PATH struComPath = new NET_DVR_LOCAL_SDK_PATH();System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());struComPath.write();hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());}

建立长连接

    /*** 建立长连接*/private int getLongConnt(String strInBuffer, int HCode, int lUserID) {//创建长连接BYTE_ARRAY ptrByteArray = new BYTE_ARRAY(1024);    //数组
//        strInBuffer = "PUT /ISAPI/Intelligent/FDLib/FDSetUp?format=json";System.arraycopy(strInBuffer.getBytes(), 0, ptrByteArray.byValue, 0, strInBuffer.length());//字符串拷贝到数组中ptrByteArray.write();return hCNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCode, ptrByteArray.getPointer(), strInBuffer.length(), null, null);}

登录

    /*** 登录** @param m_sDeviceIP 设备ip地址* @param wPort       端口号,设备网络SDK登录默认端口8000* @param m_sUsername 用户名* @param m_sPassword 密码*/public int login_V40(String m_sDeviceIP, short wPort, String m_sUsername, String m_sPassword) {NET_DVR_USER_LOGIN_INFO pLoginInfo = new NET_DVR_USER_LOGIN_INFO();pLoginInfo.sDeviceAddress = new byte[IotHikNetConstant.NET_DVR_DEV_ADDRESS_MAX_LEN];System.arraycopy(m_sDeviceIP.getBytes(), 0, pLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());pLoginInfo.wPort = wPort;pLoginInfo.sUserName = new byte[IotHikNetConstant.NET_DVR_LOGIN_USERNAME_MAX_LEN];System.arraycopy(m_sUsername.getBytes(), 0, pLoginInfo.sUserName, 0, m_sUsername.length());pLoginInfo.sPassword = new byte[IotHikNetConstant.NET_DVR_LOGIN_PASSWD_MAX_LEN];System.arraycopy(m_sPassword.getBytes(), 0, pLoginInfo.sPassword, 0, m_sPassword.length());// 是否异步登录:false- 否,true- 是pLoginInfo.bUseAsynLogin = false;// write()调用后数据才写入到内存中pLoginInfo.write();NET_DVR_DEVICEINFO_V40 lpDeviceInfo = new NET_DVR_DEVICEINFO_V40();
//        lpDeviceInfo.byLoginMode = 0;int lUserID = -1;//用户句柄 实现对设备登录try {lUserID = hCNetSDK.NET_DVR_Login_V40(pLoginInfo, lpDeviceInfo);if (lUserID == -1) {log.warn("登录失败,错误码为 : {}", hCNetSDK.NET_DVR_GetLastError());} else {log.info("登录成功!ip: {}", m_sDeviceIP);// read()后,结构体中才有对应的数据lpDeviceInfo.read();}} catch (Exception e) {log.error("海康SDK 登录异常,请检查网络和SDK路径是否正确", e);} finally {pLoginInfo.clear();lpDeviceInfo.clear();}return lUserID;}

设备注销 SDK释放

    public void logout(int lUserID) {if (lUserID >= 0) {if (!hCNetSDK.NET_DVR_Logout(lUserID)) {log.warn("注销失败,错误码为: {}", hCNetSDK.NET_DVR_GetLastError());}log.info("注销成功");//hCNetSDK.NET_DVR_Cleanup(); //释放SDKreturn;} else {log.warn("设备未登录");hCNetSDK.NET_DVR_Cleanup();}
//        if (hCNetSDK.NET_DVR_CloseAlarmChan(lAlarmHandle)) {
//            System.out.println("撤防成功");
//        }}

http://www.ppmy.cn/news/1039185.html

相关文章

shell连接ubuntu

当使用aws的私钥连接时,老是弹出输入私钥密码,但是根本没有设置过密码,随便输入后,又提示该私钥无密码... 很早就使用过aws的ubuntu,这个问题也很早就遇到过,但是每次遇到都要各种找找找...索性这次记下来算了 此处用FinalShell连接为例 首先现在Putty连接工具: 点击官方下载 …

嵌入式 - 知识随记

嵌入式 - 知识随记 IO口/寄存器/CPU IO口/寄存器/CPU IO口电平和寄存器之间的交互可以通过以下步骤进行:输出数据到IO口:当CPU需要向外部设备输出数据时,它会将数据写入指定的寄存器。这个寄存器通常称为输出寄存器或数据寄存器。CPU将要输出…

C++ auto关键字

文章目录 一、auto的推导规则二、auto的限制 C11 中有:auto 类型推导 auto x 5; // OK: x 是 int 类型 auto pi new auto(1); // OK: pi 被推导为 int* const auto *v &x, u 6; // OK: v是const int*类型,u是const int类型 static auto y 0.0;…

Mathematica 与 Matlab 常见复杂指令集汇编

Mathematica 常见指令汇编 Mathematica 常见指令 NDSolve 求解结果的保存 sol NDSolve[{y[x] x^2, y[0] 0, g[x] -y[x]^2, g[0] 1}, {y, g}, {x, 0, 1}]; numericSoly sol[[1, 1, 2]]; numericSolg sol[[1, 2, 2]]; data Table[{x, numericSoly[x], numericSolg[x]},…

vue启动配置npm run serve,动态环境变量,根据不同环境访问不同域名

首先创建不同环境的配置文件,比如域名和一些常量,创建一个env文件,先看看文件目录 env.dev就是dev环境的域名,.test就是test环境域名,其他同理,然后配置package.json文件 {"name": "require-admin&qu…

炸裂全场!百度如流超级助理、Comate X两大黑科技全面开挂,工作生产力狂飙

2023年的WAVE SUMMIT深度学习开发者大会来得比往年稍晚了些,却有着不同寻常的意义。 过去半年多时间里,大模型可以说是驻留在浪尖的一个名词。几乎所有行业都在讨论大模型的话题,涌现了哪些超出预期的能力,将对产业产生什么样的影…

生成主机文件

生成主机文件 将一个初始模板文件从 http://rhgls.area12.example.com/materials/hosts.j2 下载到 /home/curtis/ansible 完成该模板,以便用它生成以下文件:针对每个清单主机包含一行内容,其格式与 /etc/hosts 相同 创建名为 /home/curtis/an…

UglifyJS 和JShaman相比有什么不同?都可以进行js混淆加密吗?

UglifyJS 和JShaman相比有什么不同? UglifyJS主要功能是压缩JS代码,减小代码体积;JShaman是专门用于对JS代码混淆加密,目的是让JS代码变的不可读、混淆功能逻辑、加密代码中的隐秘数据或字符,是用于代码保护的。 因此…