提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.demo.module; |
H |
2 |
|
|
3 |
import java.io.File; |
|
4 |
|
|
5 |
import com.iailab.netsdk.lib.NetSDKLib; |
|
6 |
import com.iailab.netsdk.lib.NetSDKLib.LLong; |
|
7 |
import com.iailab.netsdk.lib.NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY; |
|
8 |
import com.iailab.netsdk.lib.NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY; |
|
9 |
import com.iailab.netsdk.lib.ToolKits; |
|
10 |
|
|
11 |
/** |
|
12 |
* 登陆接口实现 |
|
13 |
* 主要有 :初始化、登陆、登出功能 |
|
14 |
*/ |
|
15 |
public class LoginModule { |
|
16 |
|
|
17 |
public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE; |
|
18 |
public static NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE; |
|
19 |
|
|
20 |
// 设备信息 |
|
21 |
public static NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex(); |
|
22 |
|
|
23 |
// 登陆句柄 |
|
24 |
public static LLong m_hLoginHandle = new LLong(0); |
|
25 |
|
|
26 |
private static boolean bInit = false; |
|
27 |
private static boolean bLogopen = false; |
|
28 |
|
|
29 |
/** |
|
30 |
* \if ENGLISH_LANG |
|
31 |
* Init |
|
32 |
* \else |
|
33 |
* 初始化 |
|
34 |
* \endif |
|
35 |
*/ |
|
36 |
public static boolean init(NetSDKLib.fDisConnect disConnect, NetSDKLib.fHaveReConnect haveReConnect) { |
|
37 |
bInit = netsdk.CLIENT_Init(disConnect, null); |
|
38 |
if(!bInit) { |
|
39 |
System.out.println("Initialize SDK failed"); |
|
40 |
return false; |
|
41 |
} |
|
42 |
|
|
43 |
//打开日志,可选 |
|
44 |
NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO(); |
|
45 |
File path = new File("./sdklog/"); |
|
46 |
if (!path.exists()) { |
|
47 |
path.mkdir(); |
|
48 |
} |
|
49 |
String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + ToolKits.getDate() + ".log"; |
|
50 |
setLog.nPrintStrategy = 0; |
|
51 |
setLog.bSetFilePath = 1; |
|
52 |
System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length); |
|
53 |
System.out.println(logPath); |
|
54 |
setLog.bSetPrintStrategy = 1; |
|
55 |
bLogopen = netsdk.CLIENT_LogOpen(setLog); |
|
56 |
if(!bLogopen ) { |
|
57 |
System.err.println("Failed to open NetSDK log"); |
|
58 |
} |
|
59 |
|
|
60 |
// 设置断线重连回调接口,设置过断线重连成功回调函数后,当设备出现断线情况,SDK内部会自动进行重连操作 |
|
61 |
// 此操作为可选操作,但建议用户进行设置 |
|
62 |
netsdk.CLIENT_SetAutoReconnect(haveReConnect, null); |
|
63 |
|
|
64 |
//设置登录超时时间和尝试次数,可选 |
|
65 |
int waitTime = 5000; //登录请求响应超时时间设置为5S |
|
66 |
int tryTimes = 1; //登录时尝试建立链接1次 |
|
67 |
netsdk.CLIENT_SetConnectTime(waitTime, tryTimes); |
|
68 |
|
|
69 |
|
|
70 |
// 设置更多网络参数,NET_PARAM的nWaittime,nConnectTryNum成员与CLIENT_SetConnectTime |
|
71 |
// 接口设置的登录设备超时时间和尝试次数意义相同,可选 |
|
72 |
NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM(); |
|
73 |
netParam.nConnectTime = 10000; // 登录时尝试建立链接的超时时间 |
|
74 |
netParam.nGetConnInfoTime = 3000; // 设置子连接的超时时间 |
|
75 |
netParam.nGetDevInfoTime = 3000;//获取设备信息超时时间,为0默认1000ms |
|
76 |
netsdk.CLIENT_SetNetworkParam(netParam); |
|
77 |
|
|
78 |
return true; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* \if ENGLISH_LANG |
|
83 |
* CleanUp |
|
84 |
* \else |
|
85 |
* 清除环境 |
|
86 |
* \endif |
|
87 |
*/ |
|
88 |
public static void cleanup() { |
|
89 |
if(bLogopen) { |
|
90 |
netsdk.CLIENT_LogClose(); |
|
91 |
} |
|
92 |
|
|
93 |
if(bInit) { |
|
94 |
netsdk.CLIENT_Cleanup(); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* \if ENGLISH_LANG |
|
100 |
* Login Device |
|
101 |
* \else |
|
102 |
* 登录设备 |
|
103 |
* \endif |
|
104 |
*/ |
|
105 |
public static boolean login(String m_strIp, int m_nPort, String m_strUser, String m_strPassword) { |
|
106 |
//IntByReference nError = new IntByReference(0); |
|
107 |
//入参 |
|
108 |
NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam=new NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY(); |
|
109 |
pstInParam.nPort=m_nPort; |
|
110 |
pstInParam.szIP=m_strIp.getBytes(); |
|
111 |
pstInParam.szPassword=m_strPassword.getBytes(); |
|
112 |
pstInParam.szUserName=m_strUser.getBytes(); |
|
113 |
//出参 |
|
114 |
NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam=new NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY(); |
|
115 |
pstOutParam.stuDeviceInfo=m_stDeviceInfo; |
|
116 |
//m_hLoginHandle = netsdk.CLIENT_LoginEx2(m_strIp, m_nPort, m_strUser, m_strPassword, 0, null, m_stDeviceInfo, nError); |
|
117 |
m_hLoginHandle=netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam); |
|
118 |
if(m_hLoginHandle.longValue() == 0) { |
|
119 |
System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIp, m_nPort, ToolKits.getErrorCodePrint()); |
|
120 |
} else { |
|
121 |
System.out.println("Login Success [ " + m_strIp + " ]"); |
|
122 |
} |
|
123 |
|
|
124 |
return m_hLoginHandle.longValue() == 0? false:true; |
|
125 |
} |
|
126 |
|
|
127 |
/** |
|
128 |
* \if ENGLISH_LANG |
|
129 |
* Logout Device |
|
130 |
* \else |
|
131 |
* 登出设备 |
|
132 |
* \endif |
|
133 |
*/ |
|
134 |
public static boolean logout() { |
|
135 |
if(m_hLoginHandle.longValue() == 0) { |
|
136 |
return false; |
|
137 |
} |
|
138 |
|
|
139 |
boolean bRet = netsdk.CLIENT_Logout(m_hLoginHandle); |
|
140 |
if(bRet) { |
|
141 |
m_hLoginHandle.setValue(0); |
|
142 |
} |
|
143 |
|
|
144 |
return bRet; |
|
145 |
} |
|
146 |
} |