package com.cm.smartgate;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;

import netmanager.lib.PlatUtils;
import netmanager.lib.netmanager.NetLib;
import java.util.Scanner;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: TestDemo
 * @Description:
 * @Author: WangGeng
 * @Date: 2020/8/28 18:36
 * @Version: 1.0
 **/
public class TestDemo {

    public static String ry_net_library_name = "netmanager";

    public void execute() {
        NetLib instance = (NetLib) Native.loadLibrary(PlatUtils.sdk_plat_getLoadLibrary("D:\\develop\\java_libs\\net_manager_libs\\", ry_net_library_name), NetLib.class);
        instance.Net_Init(null, null);

        Scanner sc = new Scanner(System.in);
        System.out.println("=== 登陆 ===");
        System.out.println("Ip: ");
        String strIp = sc.next();

        System.out.println("Port: ");
        int nPort = sc.nextInt();

        System.out.println("Username: ");
        String strUsername = sc.next();

        System.out.println("Password: ");
        String strPassword = sc.next();

        IntByReference error = null;

        System.out.print(String.format("Start login - [ip:%s, port:%d]......\n", strIp, nPort));

        NativeLong loginID = instance.Net_LoginDevice(strIp, nPort, strUsername, strPassword, null, error);

        System.out.print(String.format("Login ID = %d.\n", loginID.longValue()));

        if (0 == loginID.longValue()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return;
        }

        System.out.printf("Start logout - [ip:%s, port:%d]......\n", strIp, nPort);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        instance.Net_LogoutDevice(loginID);

        System.out.printf("Logout OK.\n");

        instance.Net_Exit();
    }

    public static void main(String[] args) {
        TestDemo testDemo = new TestDemo();
        testDemo.execute();
    }

}
