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
package com.netsdk.demo.customize;
 
import java.io.File;
 
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
 
/**
 * The class demonstrates the basic usage of the SDK.
 * @author 29779
 *
 */
public class SDKBasicProcess {    
    /** Singleton is recommended */
    private static class DisconnectCallback implements NetSDKLib.fDisConnect {
        private static DisconnectCallback instance = new DisconnectCallback();
        private DisconnectCallback() {}
        public static DisconnectCallback getInstance() { 
            return instance;
        }
        
        public void invoke(LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser){
            System.out.printf("Device[%s:%d] Disconnect!\n" , pchDVRIP , nDVRPort);
        }
    }
    
    /** Singleton is recommended */
    private static class HaveReconnectCallback implements NetSDKLib.fHaveReConnect {
        private static HaveReconnectCallback instance = new HaveReconnectCallback();
        private HaveReconnectCallback() {}
        public static HaveReconnectCallback getInstance() { 
            return instance;
        }
        
        public void invoke(LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser){
            System.out.printf("Device[%s:%d] HaveReconnected!\n" , pchDVRIP , nDVRPort);
        }
    }    
    
    public static void main(String[] args) {
        NetSDKLib netsdkApi = NetSDKLib.NETSDK_INSTANCE;
        
        /**
         * Initialize SDK and add disconnect listener.
         * The callback will be triggered when the network is disconnected.
         * The API only need to be called once.
         */
        netsdkApi.CLIENT_Init(DisconnectCallback.getInstance(), null);
        
        /**
         * Add reconnection listener, the callback will be triggered 
         * when the network is restored.
         * The API only need to be called once.
         */
        netsdkApi.CLIENT_SetAutoReconnect(HaveReconnectCallback.getInstance(), null);
        
        
        /**
         * Log is optional. The SDK will generate log file in the current directory. 
         */
        NetSDKLib.LOG_SET_PRINT_INFO logInfo = new NetSDKLib.LOG_SET_PRINT_INFO();    
        File currentPath = new File(".");        
        String logPath = currentPath.getAbsoluteFile().getParent() + "netsdk.log";
        
        logInfo.bSetFilePath = 1; // enable file path
        System.arraycopy(logPath.getBytes(), 0, logInfo.szLogFilePath, 0, logPath.getBytes().length);
        
        logInfo.bSetPrintStrategy = 1; // enable print strategy
        logInfo.nPrintStrategy = 0;
        boolean logOpened = netsdkApi.CLIENT_LogOpen(logInfo);
        if (!logOpened) {
            System.err.println("Failed to open NetSDK log !!!");
        }
        
        /**
         * At this step, you may fail to login.
         * Please refer to the Last-Error in NetSDKLib.java
         */
        final int nSpecCap = 0; /// login device by TCP
        final IntByReference error = new IntByReference();
        final String address = "172.23.1.224";
        final int port = 37777; // default port
        final String usrname = "admin";
        final String password = "admin";
        final NetSDKLib.NET_DEVICEINFO deviceInfo = new NetSDKLib.NET_DEVICEINFO();
        
        LLong loginHandle = netsdkApi.CLIENT_LoginEx(address, (short)port, usrname, 
                password, nSpecCap, null, deviceInfo, error);
        if(loginHandle.longValue() == 0) {
            System.err.printf("Login Device [%s:%d] Failed ! Last Error[%x]\n", address, port, netsdkApi.CLIENT_GetLastError() );
            return;
        }
 
        System.out.printf("Login Device [%s:%d] Success. \n", address, port);    
        
        // TODO Use the loginHandle, do something you are interested in.
        
        System.out.println("Do something here.");
        
        /**
         * After completing the business, logout the device.
         */
        netsdkApi.CLIENT_Logout(loginHandle);
        
        /**
         * Finally, clean up the resources used in SDK.
         * The API only need to be called once.
         */
        netsdkApi.CLIENT_Cleanup();
        
        System.out.println("Done.");
    }
}