package com.netsdk.demo.customize; import com.netsdk.demo.util.CaseMenu; import com.netsdk.lib.NetSDKLib; import com.netsdk.lib.ToolKits; import com.netsdk.lib.callback.impl.DefaultHaveReconnectCallBack; import com.netsdk.lib.structure.*; import com.sun.jna.Memory; import com.sun.jna.Pointer; import java.io.File; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; import static com.netsdk.lib.Utils.getOsPrefix; public class MultiTalkDevDemo { // SDk对象初始化 public static final NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE; public static final NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE; // 判断是否初始化 private static boolean bInit = false; // 判断log是否打开 private static boolean bLogOpen = false; // 设备信息 private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex(); // 登录句柄 private NetSDKLib.LLong loginHandle = new NetSDKLib.LLong(0); //实现断线重连静态单例 public static class DefaultDisconnectCallback implements NetSDKLib.fDisConnect { private static volatile DefaultDisconnectCallback INSTANCE; private DefaultDisconnectCallback() {} public static DefaultDisconnectCallback getINSTANCE() { if (INSTANCE == null) { synchronized (DefaultDisconnectCallback.class){ if (INSTANCE == null){ INSTANCE = new DefaultDisconnectCallback(); } } } return INSTANCE; } @Override public void invoke(NetSDKLib.LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser) { System.out.printf("Device[%s] Port[%d] DisConnected!\n", pchDVRIP, nDVRPort); } } // 回调函数需要是静态的,防止被系统回收 // 断线回调 private static NetSDKLib.fDisConnect disConnectCB = DefaultDisconnectCallback.getINSTANCE(); // 重连回调 private static NetSDKLib.fHaveReConnect haveReConnectCB = DefaultHaveReconnectCallBack.getINSTANCE(); //订阅句柄 NetSDKLib.LLong lAttachHandle = new NetSDKLib.LLong(0); // 编码格式 public static String encode; static { String osPrefix = getOsPrefix(); if (osPrefix.toLowerCase().startsWith("win32-amd64")) { encode = "GBK"; } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) { encode = "UTF-8"; } } /** * 按照指定格式,获取当前时间 */ public static String getDate() { SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return simpleDate.format(new Date()).replaceAll("[^0-9]", "-"); } /** * 初始化SDK库 */ public static boolean initSdk() { bInit = netsdk.CLIENT_Init(disConnectCB, null);// 进程启动时,初始化一次 if (!bInit) { System.out.println("Initialize SDK failed"); return false; } // 配置日志 enableLog(); // 设置断线重连回调接口, 此操作为可选操作,但建议用户进行设置 netsdk.CLIENT_SetAutoReconnect(haveReConnectCB, null); // 设置登录超时时间和尝试次数,可选 // 登录请求响应超时时间设置为3S int waitTime = 3000; //单位为ms // 登录时尝试建立链接 1 次 int tryTimes = 1; // 设置连接设备超时时间和尝试次数 netsdk.CLIENT_SetConnectTime(waitTime, tryTimes); // 设置更多网络参数, NET_PARAM 的nWaittime , nConnectTryNum 成员与 CLIENT_SetConnectTime // 接口设置的登录设备超时时间和尝试次数意义相同,可选 NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM(); // 登录时尝试建立链接的超时时间 netParam.nConnectTime = 10000; // 设置子连接的超时时间 netParam.nGetConnInfoTime = 3000; //设置登陆网络环境 netsdk.CLIENT_SetNetworkParam(netParam); return true; } /** * 打开 sdk log */ private static void enableLog() { // SDK全局日志打印信息 NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO(); //设置日志路径 File path = new File("sdklog/"); //判断日志路径是否存在,若不存在则创建 if (!path.exists()){ path.mkdir(); } // 这里的log保存地址依据实际情况自己调整 String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + "sdklog" + getDate() + ".log"; //日志输出策略,0:输出到文件(默认); 1:输出到窗口, setLog.nPrintStrategy = 0; //是否重设日志路径, 取值0否 ,1是 setLog.bSetFilePath = 1; //日志路径(默认"./sdk_log/sdk_log.log") byte[] szLogFilePath = setLog.szLogFilePath; //自定义log保存地址,将数据logPath数据copy到LOG_SET_PRINT_INFO-->szLogFilePath变量中 System.arraycopy(logPath.getBytes(), 0, szLogFilePath, 0, logPath.getBytes().length); //是否重设日志打印输出策略 取值0否 ,1是 setLog.bSetPrintStrategy = 1; // 打开日志功能 bLogOpen = netsdk.CLIENT_LogOpen(setLog); if (!bLogOpen){ System.err.println("Failed to open NetSDK log "+ ToolKits.getErrorCode()); }else { System.out.println("Success to open NetSDK log "); } } /** * 高安全登录 */ public void loginWithHighLevel() { // 输入结构体参数 NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() { { // IP szIP = m_strIpAddr.getBytes(); // 端口 nPort = m_nPort; // 用户名 szUserName = m_strUser.getBytes(); // 密码 szPassword = m_strPassword.getBytes(); } }; // 登录的输出结构体参数 NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY(); // 高安全级别登陆 loginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam); if (loginHandle.longValue() == 0) { //登陆失败 System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIpAddr, m_nPort, ToolKits.getErrorCode()); } else { //登陆成功 // 获取设备信息 deviceInfo = pstOutParam.stuDeviceInfo; System.out.printf("Login Success Device Address[%s] 设备包含[%d]个通道 \n",m_strIpAddr,deviceInfo.byChanNum); } } /** * 退出 */ public void logOut() { //判断是否已登录 if (loginHandle.longValue() != 0) { netsdk.CLIENT_Logout(loginHandle); System.out.println("LogOut Success"); } } /** * 清理sdk环境并退出 */ public static void cleanAndExit() { //判断log是否打开 if (bLogOpen) { // 关闭sdk日志打印 netsdk.CLIENT_LogClose(); } // 进程关闭时,调用一次 netsdk.CLIENT_Cleanup(); System.exit(0); } /** * 初始化测试 */ public void initTest() { initSdk(); this.loginWithHighLevel(); } /** * 结束测试 */ public void endTest() { System.out.println("End Test"); this.logOut(); // 登出设备 System.out.println("See You..."); cleanAndExit(); // 清理资源并退出 } public void runTest() { System.out.println("Run Test"); CaseMenu menu = new CaseMenu(); menu.addItem(new CaseMenu.Item(this, "新增设备信息", "insertMultiTalkDev")); menu.addItem(new CaseMenu.Item(this, "修改设备信息", "updateMultiTalkDev")); menu.run(); } public boolean insertMultiTalkDev() throws UnsupportedEncodingException { NET_IN_INSERT_MULTI_TALK_DEV stIn = new NET_IN_INSERT_MULTI_TALK_DEV(); NET_OUT_INSERT_MULTI_TALK_DEV stOut = new NET_OUT_INSERT_MULTI_TALK_DEV(); stIn.nInsertInfoNum = 1;//新增的个数 stIn.stuInsertInfo[0].nDevType = 1;//设备类型前端设备, 0:未知, 1:前端 – VTAVTO, 2:后端 other String node = "1001001"; System.arraycopy(node.getBytes(encode), 0, stIn.stuInsertInfo[0].szNode, 0, node.getBytes(encode).length); //设备通讯录信息 NET_VT_CONTACTINFO_INFO //名字 String firstName = "yyyy"; System.arraycopy(firstName.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szFirstName, 0, firstName.getBytes(encode).length); //姓 String familyName = "xxxx"; System.arraycopy(familyName.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szFamilyName, 0, familyName.getBytes(encode).length); //可视对讲短号 String VTShortNumber = "8066"; System.arraycopy(VTShortNumber.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szVTShortNumber, 0, VTShortNumber.getBytes(encode).length); //可视对讲中号 String VTMiddleNumber = "66668066"; System.arraycopy(VTMiddleNumber.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szVTMiddleNumber, 0, VTMiddleNumber.getBytes(encode).length); //可视对讲长号, 或VTH模拟室内机的序列号 String VTLongNumber = "66668066"; System.arraycopy(VTLongNumber.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szVTLongNumber, 0, VTLongNumber.getBytes(encode).length); //可视对讲网络地址 String VTNetAddress = "172.6.102.204"; System.arraycopy(VTNetAddress.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szVTNetAddress, 0, VTNetAddress.getBytes(encode).length); //室内机关联的门口号码 String VTOPosition = ""; System.arraycopy(VTOPosition.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szVTOPosition, 0, VTOPosition.getBytes(encode).length); //昵称 // String NickName = "这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个"; // System.arraycopy(NickName.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szNickName, 0, NickName.getBytes(encode).length); //备注 String Notes = "Notes"; System.arraycopy(Notes.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szNotes, 0, Notes.getBytes(encode).length); //可视对讲所属设备系列, 与设备类型大类不完全对等, 取决于对讲所属的应用场景, "VTH": 室内机, "VTO": 门口机, "VTS": 管理机, "VTA": 紧急求助终端 String Type = "VTO"; System.arraycopy(Type.getBytes(encode), 0, stIn.stuInsertInfo[0].stuContactInfo.szType, 0, Type.getBytes(encode).length); //设置通讯录格式扩展 NET_VT_CONTACTINFO_INFO_EX infoEX = new NET_VT_CONTACTINFO_INFO_EX(); infoEX.bUseNickNameEx = 1; String szNickNameEx = "这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个名字比较长这个"; System.arraycopy(szNickNameEx.getBytes(encode), 0, infoEX.szNickNameEx, 0, szNickNameEx.getBytes(encode).length); stIn.stuInsertInfo[0].stuContactInfo.pContactInfoEx = new Memory(infoEX.size()); stIn.stuInsertInfo[0].stuContactInfo.pContactInfoEx.clear(infoEX.size()); ToolKits.SetStructDataToPointer(infoEX, stIn.stuInsertInfo[0].stuContactInfo.pContactInfoEx, 0); //前端设备详细信息 NET_TALK_DEV_FRONT_DEV_INFO //是否使能 stIn.stuInsertInfo[0].stuFrontDevInfo.bEnable = 1; //Ip地址 String Address = "172.6.102.204"; System.arraycopy(Address.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szAddress, 0, Address.getBytes(encode).length); //对讲协议端口 stIn.stuInsertInfo[0].stuFrontDevInfo.nPort = 5000; //前端设备类型类型, "Vto": 单元门口机, "Wall": 围墙机, "Visitor": 访客机 String Type2 = "Vto"; System.arraycopy(Type2.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szType, 0, Type2.getBytes(encode).length); String MiddleNumber = "66668066"; System.arraycopy(MiddleNumber.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szMiddleNumber, 0, MiddleNumber.getBytes(encode).length); String Username = "admin"; System.arraycopy(Username.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szUsername, 0, Username.getBytes(encode).length); String Password = "admin123"; System.arraycopy(Password.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szPassword, 0, Password.getBytes(encode).length); String SN = "ASDASDASDASD"; System.arraycopy(SN.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szSN, 0, SN.getBytes(encode).length); //设备所处位置 // String szMachineAddress = "这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个"; // System.arraycopy(szMachineAddress.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szMachineAddress, 0, szMachineAddress.getBytes(encode).length); //设备所处位置扩展 stIn.stuInsertInfo[0].stuFrontDevInfo.bMachineAddressEx = 1; String szMachineAddressEx = "这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长这个"; System.arraycopy(szMachineAddressEx.getBytes(encode), 0, stIn.stuInsertInfo[0].stuFrontDevInfo.szMachineAddressEx, 0, szMachineAddressEx.getBytes(encode).length); stIn.write(); stOut.write(); boolean bRet = netsdk.CLIENT_InsertMultiTalkDev(loginHandle,stIn.getPointer(),stOut.getPointer(),3000); stIn.read(); stOut.read(); if (!bRet) { System.err.printf("insertMultiTalkDev Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError()); return false; }else { System.out.println("CLIENT_InsertMultiTalkDev succeed"); return true; } } public boolean updateMultiTalkDev() throws UnsupportedEncodingException { NET_IN_UPDATE_MULTI_TALK_DEV stIn = new NET_IN_UPDATE_MULTI_TALK_DEV(); NET_OUT_UPDATE_MULTI_TALK_DEV stOut = new NET_OUT_UPDATE_MULTI_TALK_DEV(); stIn.nUpdataInfoNum = 1;//更新的个数 stIn.stuUpdateInfo[0].nRecNo = 1;//设备编号 stIn.stuUpdateInfo[0].stuInfo.nDevType = 1;//设备类型前端设备, 0:未知, 1:前端 – VTAVTO, 2:后端 other //设备通讯录信息 NET_VT_CONTACTINFO_INFO //名字 String firstName = "dawei"; System.arraycopy(firstName.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szFirstName, 0, firstName.getBytes(encode).length); //姓 String familyName = "li"; System.arraycopy(familyName.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szFamilyName, 0, familyName.getBytes(encode).length); //可视对讲短号 String VTShortNumber = "8066"; System.arraycopy(VTShortNumber.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szVTShortNumber, 0, VTShortNumber.getBytes(encode).length); //可视对讲中号 String VTMiddleNumber = "66668066"; System.arraycopy(VTMiddleNumber.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szVTMiddleNumber, 0, VTMiddleNumber.getBytes(encode).length); //可视对讲长号, 或VTH模拟室内机的序列号 String VTLongNumber = "66668066"; System.arraycopy(VTLongNumber.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szVTLongNumber, 0, VTLongNumber.getBytes(encode).length); //可视对讲网络地址 String VTNetAddress = "172.6.102.204"; System.arraycopy(VTNetAddress.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szVTNetAddress, 0, VTNetAddress.getBytes(encode).length); //室内机关联的门口号码 String VTOPosition = ""; System.arraycopy(VTOPosition.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szVTOPosition, 0, VTOPosition.getBytes(encode).length); //昵称 // String NickName = "NickName"; // System.arraycopy(NickName.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szNickName, 0, NickName.getBytes(encode).length); //设置通讯录格式扩展 NET_VT_CONTACTINFO_INFO_EX infoEX = new NET_VT_CONTACTINFO_INFO_EX(); infoEX.bUseNickNameEx = 1; String szNickNameEx = "这个名字比较长这个名字比较长这个名字"; System.arraycopy(szNickNameEx.getBytes(encode), 0, infoEX.szNickNameEx, 0, szNickNameEx.getBytes(encode).length); stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.pContactInfoEx = new Memory(infoEX.size()); stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.pContactInfoEx.clear(infoEX.size()); ToolKits.SetStructDataToPointer(infoEX, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.pContactInfoEx, 0); //备注 String Notes = "Notes"; System.arraycopy(Notes.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szNotes, 0, Notes.getBytes(encode).length); //可视对讲所属设备系列, 与设备类型大类不完全对等, 取决于对讲所属的应用场景, "VTH": 室内机, "VTO": 门口机, "VTS": 管理机, "VTA": 紧急求助终端 String Type = "VTO"; System.arraycopy(Type.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuContactInfo.szType, 0, Type.getBytes(encode).length); //前端设备详细信息 NET_TALK_DEV_FRONT_DEV_INFO //是否使能 stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.bEnable = 1; //Ip地址 String Address = "172.6.102.204"; System.arraycopy(Address.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szAddress, 0, Address.getBytes(encode).length); //对讲协议端口 stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.nPort = 5000; //前端设备类型类型, "Vto": 单元门口机, "Wall": 围墙机, "Visitor": 访客机 String Type2 = "Vto"; System.arraycopy(Type2.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szType, 0, Type2.getBytes(encode).length); String MiddleNumber = "66668066"; System.arraycopy(MiddleNumber.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szMiddleNumber, 0, MiddleNumber.getBytes(encode).length); String Username = "admin"; System.arraycopy(Username.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szUsername, 0, Username.getBytes(encode).length); String Password = "admin123"; System.arraycopy(Password.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szPassword, 0, Password.getBytes(encode).length); String SN = "ASDASDASDASD"; System.arraycopy(SN.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szSN, 0, SN.getBytes(encode).length); stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.bMachineAddressEx = 1; String szMachineAddressEx = "这个地址需要比较长这个地址需要比较长这个地址需要比较长这个地址需要比较长"; System.arraycopy(szMachineAddressEx.getBytes(encode), 0, stIn.stuUpdateInfo[0].stuInfo.stuFrontDevInfo.szMachineAddressEx, 0, szMachineAddressEx.getBytes(encode).length); stIn.write(); stOut.write(); stIn.write(); stOut.write(); boolean bRet = netsdk.CLIENT_UpdateMultiTalkDev(loginHandle,stIn.getPointer(),stOut.getPointer(),3000); stIn.read(); stOut.read(); if (!bRet) { System.err.printf("updateMultiTalkDev Fail.Last Error[0x%x]\n", netsdk.CLIENT_GetLastError()); return false; }else { System.out.println("CLIENT_UpdateMultiTalkDev succeed"); return true; } } // 配置登陆地址,端口,用户名,密码 private String m_strIpAddr = "172.6.88.185"; private int m_nPort = 37777; private String m_strUser = "admin"; private String m_strPassword = "admin123"; public static void main(String[] args) { MultiTalkDevDemo demo=new MultiTalkDevDemo(); demo.initTest(); demo.runTest(); demo.endTest(); } }