1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
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
package com.netsdk.demo.customize.healthCodeEx.module;
 
import static com.netsdk.lib.Utils.getOsPrefix;
 
import java.io.UnsupportedEncodingException;
 
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.enumeration.ENUMERROR;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
 
/**
 * 通用高安全登录接口
 *
 * @author 47040
 * @since Created in 2021/5/25 11:05
 */
public class LoginModule {
 
    static NetSDKLib NetSdk = NetSDKLib.NETSDK_INSTANCE;
    
    // 编码格式
     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 NetSDKLib.LLong AutoRegisterLoginWithHighSecurity(String sn,String ipAddress, Integer port, String userName, String password,
            NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo) {
        
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam =
        new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();   // 输入结构体参数
        System.arraycopy(ipAddress.getBytes(), 0, pstInParam.szIP, 0, ipAddress.length());
        pstInParam.nPort = port;
        System.arraycopy(userName.getBytes(), 0, pstInParam.szUserName, 0, userName.length());
        System.arraycopy(password.getBytes(), 0, pstInParam.szPassword, 0, password.length());        
        //登录模式 参考EM_LOGIN_SPAC_CAP_TYPE   0 - TCP登陆, 默认方式      2 -主动注册的登入
        pstInParam.emSpecCap = 2; 
        //获取设备序列号指针对象
        Pointer deviceId = GetStringToPointer(sn);
        pstInParam.pCapParam = deviceId;    
        
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam =
        new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();  // 输出结构体参数
        pstOutParam.stuDeviceInfo = m_stDeviceInfo;             // 设备信息 登陆成功后会刷新这个实例
        
        NetSDKLib.LLong m_hLoginHandle = NetSdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        if (m_hLoginHandle.longValue() == 0) {
        System.err.printf("Login Device[%s] Port[%d]Failed. Last Error[%s]\n", ipAddress, port, ENUMERROR.getErrorMessage());
        } else {
        System.out.println("Login Success [ " + ipAddress + " ]");
        }
        return m_hLoginHandle;
   }
    
    
    // 登录 高安全 TCP
    public static NetSDKLib.LLong TcpLoginWithHighSecurity(String ipAddress, Integer port, String userName, String password,
                                                           NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo) {
        System.out.println("设备地址:" + ipAddress + "\n端口号:" + port + "\n用户名:" + userName + "\n密码:" + password);
 
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam =
                new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();   // 输入结构体参数
        System.arraycopy(ipAddress.getBytes(), 0, pstInParam.szIP, 0, ipAddress.length());
        pstInParam.nPort = port;
        System.arraycopy(userName.getBytes(), 0, pstInParam.szUserName, 0, userName.length());
        System.arraycopy(password.getBytes(), 0, pstInParam.szPassword, 0, password.length());
        pstInParam.emSpecCap = NetSDKLib.EM_LOGIN_SPAC_CAP_TYPE.EM_LOGIN_SPEC_CAP_TCP;
 
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam =
                new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();  // 输出结构体参数
        pstOutParam.stuDeviceInfo = m_stDeviceInfo;                     // 设备信息 登陆成功后会刷新这个实例
 
        NetSDKLib.LLong m_hLoginHandle = NetSdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        if (m_hLoginHandle.longValue() == 0) {
            System.err.printf("Login Device[%s] Port[%d]Failed. Last Error[%s]\n", ipAddress, port, ENUMERROR.getErrorMessage());
        } else {
            System.out.println("Login Success [ " + ipAddress + " ]");
        }
        return m_hLoginHandle;
    }
       
    public static Pointer GetStringToPointer(String src) {    
        Pointer pointer = null;
        try {
            byte[] b = src.getBytes(encode);
            pointer = new Memory(b.length+1);
            pointer.clear(b.length+1);
            
            pointer.write(0, b, 0, b.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pointer;
    }
 
    // 登出
    public static boolean logout(NetSDKLib.LLong m_hLoginHandle) {
        if (m_hLoginHandle.longValue() == 0) {
            System.err.println("LoginHandle invalid");
            return false;
        }
        if (!NetSdk.CLIENT_Logout(m_hLoginHandle)) {
            System.err.println("Logout Failed:" + ENUMERROR.getErrorCode());
            return false;
        } else {
            System.out.println("Logout Succeed.");
            m_hLoginHandle.setValue(0);
            return true;
        }
    }
}