fuliqi
2025-02-14 c6976365d5bfb39a32db8b541b1fe3ceb30c7826
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.ycl.api.DH.module;
 
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
import com.ycl.api.DH.lib.NetSDKLib;
import com.ycl.api.DH.lib.NetSDKLib.LLong;
import com.ycl.api.DH.lib.NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY;
import com.ycl.api.DH.lib.NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY;
import com.ycl.api.DH.lib.ToolKits;
import com.ycl.api.DH.lib.structure.*;
import com.ycl.platform.domain.result.OSDResult;
import com.ycl.utils.StringUtils;
import constant.ApiConstants;
import enumeration.DeviceType;
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import static com.ycl.api.DH.lib.NetSDKLib.CFG_CMD_VIDEOWIDGET;
import static com.ycl.api.DH.lib.NetSDKLib.NET_DEV_VIDEO_OSD_CFG;
import static com.ycl.api.DH.lib.NetSDKLib.NET_EM_OSD_BLEND_TYPE.NET_EM_OSD_BLEND_TYPE_MAIN;
import static com.ycl.api.DH.lib.enumeration.NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_CUSTOMTITLE;
import static com.ycl.api.DH.lib.enumeration.NET_EM_CFG_OPERATE_TYPE.NET_EM_CFG_GBMODE;
 
/**
 * 登陆接口实现
 * 主要有 :初始化、登陆、登出功能
 */
@Slf4j
public class LoginModule {
 
    public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
    public static NetSDKLib configsdk = NetSDKLib.CONFIG_INSTANCE;
 
    private static boolean bInit = false;
    private static boolean bLogopen = false;
 
    /**
     * \if ENGLISH_LANG
     * Init
     * \else
     * 初始化
     * \endif
     */
    public static boolean init(NetSDKLib.fDisConnect disConnect, NetSDKLib.fHaveReConnect haveReConnect) {
        bInit = netsdk.CLIENT_Init(disConnect, null);
        if (!bInit) {
            System.out.println("Initialize SDK failed");
            return false;
        }
 
        //打开日志,可选
        NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
        File path = new File("./sdklog/");
        if (!path.exists()) {
            path.mkdir();
        }
        String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + ToolKits.getDate() + ".log";
        setLog.nPrintStrategy = 0;
        setLog.bSetFilePath = 1;
        System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
        System.out.println(logPath);
        setLog.bSetPrintStrategy = 1;
        bLogopen = netsdk.CLIENT_LogOpen(setLog);
        if (!bLogopen) {
            System.err.println("Failed to open NetSDK log");
        }
 
        // 设置断线重连回调接口,设置过断线重连成功回调函数后,当设备出现断线情况,SDK内部会自动进行重连操作
        // 此操作为可选操作,但建议用户进行设置
        netsdk.CLIENT_SetAutoReconnect(haveReConnect, null);
 
        //设置登录超时时间和尝试次数,可选
        int waitTime = 3000; //登录请求响应超时时间设置为5S
        int tryTimes = 1;    //登录时尝试建立链接1次
        netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
 
 
        // 设置更多网络参数,NET_PARAM的nWaittime,nConnectTryNum成员与CLIENT_SetConnectTime
        // 接口设置的登录设备超时时间和尝试次数意义相同,可选
        NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
        netParam.nConnectTime = 3000;      // 登录时尝试建立链接的超时时间
        netParam.nGetConnInfoTime = 3000;   // 设置子连接的超时时间
        netParam.nGetDevInfoTime = 3000;//获取设备信息超时时间,为0默认1000ms
        netsdk.CLIENT_SetNetworkParam(netParam);
 
        return true;
    }
 
    /**
     * \if ENGLISH_LANG
     * CleanUp
     * \else
     * 清除环境
     * \endif
     */
    public static void cleanup() {
        if (bLogopen) {
            netsdk.CLIENT_LogClose();
        }
 
        if (bInit) {
            netsdk.CLIENT_Cleanup();
        }
    }
 
    /**
     * \if ENGLISH_LANG
     * Login Device
     * \else
     * 登录设备
     * \endif
     */
    public static LLong login(String m_strIp, int m_nPort, String m_strUser, String m_strPassword) {
        //IntByReference nError = new IntByReference(0);
        //入参
        NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
        pstInParam.nPort = m_nPort;
        pstInParam.szIP = m_strIp.getBytes();
        pstInParam.szPassword = m_strPassword.getBytes();
        pstInParam.szUserName = m_strUser.getBytes();
        //出参
        //设备信息
        NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
        NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
        pstOutParam.stuDeviceInfo = m_stDeviceInfo;
        // 登陆句柄
        LLong m_hLoginHandle = new LLong(0);
        m_hLoginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        if (m_hLoginHandle.longValue() == 0) {
//            System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIp, m_nPort, ToolKits.getErrorCodePrint());
        } else {
//            System.out.println("Login Success [ " + m_strIp + " ]");
//            log.info("登录成功");
        }
        return m_hLoginHandle;
    }
 
    /**
     * \if ENGLISH_LANG
     * Logout Device
     * \else
     * 登出设备
     * \endif
     */
    public static boolean logout(LLong loginId) {
        boolean bRet = netsdk.CLIENT_Logout(loginId);
        return bRet;
    }
 
    public static OSDResult getOsd(String serialNumber, LLong loginId) {
        OSDResult osdResult = new OSDResult();
        NetSDKLib.NET_TIME deviceTime = new NetSDKLib.NET_TIME();
        if (!netsdk.CLIENT_QueryDeviceTime(loginId, deviceTime, 3000)) {
//            log.error("CLIENT_QueryDeviceTime Failed!" + ToolKits.getErrorCodePrint());
            return null;
        }
        Date checkTime = new Date();
        osdResult.setCheckTime(checkTime);
        String date = deviceTime.toStringTime();
        date = date.replace("/", "-");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date time = simpleDateFormat.parse(date);
            osdResult.setOsdTime(time);
        } catch (ParseException e) {
//            log.error("时间解析失败");
        }
        NET_CFG_GBMODE_INFO reserve = new NET_CFG_GBMODE_INFO();
        NET_CFG_GBMODE_INFO osdInfo = new NET_CFG_GBMODE_INFO();
        osdInfo.write();
        boolean b = false;
        /**
         * 通过国标倒数第七位判断通道 0/全景 1/细节
         * 对于海康来说: 国标倒7: 0/全景/通道2 1/细节/通道1
         * 对于大华来说: 国标倒7: 0/全景/通道1 1/细节/通道2
         * 宇视单通道不考虑
         */
        if (serialNumber.charAt(ApiConstants.SerialNumber_Channel) == ApiConstants.SerialNumber_All) {
            b = netsdk.CLIENT_GetConfig(loginId, NET_EM_CFG_GBMODE, 0, osdInfo.getPointer(), osdInfo.size(), 3000, reserve.getPointer());
        } else if (serialNumber.charAt(ApiConstants.SerialNumber_Channel) == ApiConstants.SerialNumber_Detail) {
            b = netsdk.CLIENT_GetConfig(loginId, NET_EM_CFG_GBMODE, 1, osdInfo.getPointer(), osdInfo.size(), 3000, reserve.getPointer());
        }
 
        if (!b) {
//            log.error("CLIENT_GetConfig Failed!" + ToolKits.getErrorCodePrint());
            return null;
        } else {
            osdInfo.read();
            NET_GBMODE_CUSTOMTITLE_INFO[] stuCustomTitle = osdInfo.stuCustomTitle;
            for (NET_GBMODE_CUSTOMTITLE_INFO title : stuCustomTitle) {
                String position = new String(title.szPositon, StandardCharsets.UTF_8).trim();
                if ("LeftDown".equals(position)) {
                    NET_TITLE_OSD_INFO[] stuOSD = title.stuOSD;
                    for (NET_TITLE_OSD_INFO osd : stuOSD) {
                        String osdStr = null;
                        osdStr = new String(osd.szText, StandardCharsets.UTF_8).trim();
//                        log.info("TEXT:"+osdStr);
                        if (!StringUtils.isEmpty(osdStr)) {
                            osdResult.setOSD4(osdStr);
                        }
                    }
                } else if ("RightDown".equals(position)) {
                    NET_TITLE_OSD_INFO[] stuOSD = title.stuOSD;
                    int num = 0;
                    for (NET_TITLE_OSD_INFO osd : stuOSD) {
                        String osdStr = null;
                        osdStr = new String(osd.szText, StandardCharsets.UTF_8).trim();
//                        log.info("TEXT:"+osdStr);
                        if (!StringUtils.isEmpty(osdStr)) {
                            if (num == 0) {
                                osdResult.setOSD1(osdStr);
                            } else if (num == 1) {
                                osdResult.setOSD2(osdStr);
                            } else if (num == 2) {
                                osdResult.setOSD3(osdStr);
                            } else if (num == 3) {
                                osdResult.setName(osdStr);
                            }
                        }
                        num++;
                    }
                }
            }
        }
        osdResult.setDeviceBrand(DeviceType.DH.getType());
        return osdResult;
    }
 
    /**
     * 针对利旧设备获取OSD的方法
     * @param serialNumber
     * @param loginId
     * @return
     */
    public static OSDResult getOsdOld(String serialNumber, LLong loginId) {
        OSDResult result = new OSDResult();
        NetSDKLib.NET_OSD_CUSTOM_TITLE osdInfo4 = new NetSDKLib.NET_OSD_CUSTOM_TITLE();
        osdInfo4.emOsdBlendType = NetSDKLib.NET_EM_OSD_BLEND_TYPE.NET_EM_OSD_BLEND_TYPE_MAIN;
        NetSDKLib.NET_OSD_CUSTOM_TITLE reserve4 = new NetSDKLib.NET_OSD_CUSTOM_TITLE();
        osdInfo4.write();
        boolean e = false;
        e = netsdk.CLIENT_GetConfig(loginId, NET_EM_CFG_CUSTOMTITLE, 0, osdInfo4.getPointer(), osdInfo4.size(), 3000, reserve4.getPointer());
        if (!e) {
            log.error("e为空,尝试另一个通道");
            e = netsdk.CLIENT_GetConfig(loginId, NET_EM_CFG_CUSTOMTITLE, 1, osdInfo4.getPointer(), osdInfo4.size(), 3000, reserve4.getPointer());
        }
        if(e) {
            osdInfo4.read();
            NetSDKLib.NET_CUSTOM_TITLE_INFO[] stuCustomTitle = osdInfo4.stuCustomTitle;
            for (NetSDKLib.NET_CUSTOM_TITLE_INFO net_custom_title_info : stuCustomTitle) {
                String name = new String(net_custom_title_info.szText, StandardCharsets.UTF_8).trim();
                log.info("name:{}",name);
                if(StringUtils.isNotEmpty(name) && name.contains("|")){
                    String[] osdNames = name.split("\\|");
                    //剔除掉空串
//                    List<String> nonEmptyList = new ArrayList<>();
//                    for (String osd : osdNames) {
//                        if (StringUtils.isNotEmpty(osd)) {
//                            nonEmptyList.add(osd);
//                        }
//                    }
                    result.setOSD1(osdNames[0]);
                    result.setOSD2(osdNames[1]);
                    result.setOSD3(osdNames[2]);
                    result.setName(osdNames[3]);
                }
            }
        }else {
            log.error(serialNumber,"利旧osd为空");
        }
        return result;
    }
 
    /**
     * 获取单个配置
     * @param hLoginHandle 登陆句柄
     * @param nChn 通道号,-1 表示全通道
     * @param strCmd 配置名称
     * @param cmdObject 配置对应的结构体对象
     * @return 成功返回 true
     */
    public static boolean GetDevConfig(LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) {
        boolean result = false;
        IntByReference error = new IntByReference(0);
        int nBufferLen = 2*1024*1024;
        byte[] strBuffer = new byte[nBufferLen];
 
        if(netsdk.CLIENT_GetNewDevConfig( hLoginHandle, strCmd , nChn, strBuffer, nBufferLen,error,3000,null)) {
            cmdObject.write();
            if (netsdk.CLIENT_ParseData(strCmd, strBuffer, cmdObject.getPointer(),
                    cmdObject.size(), null)) {
                cmdObject.read();
                result = true;
            } else {
                System.err.println("Parse " + strCmd + " Config Failed!" + ToolKits.getErrorCodePrint());
                result = false;
            }
        } else {
            System.err.println("调用失败");
            result = false;
        }
 
        return result;
    }
 
}