package com.judong.chuanyiserver.util;

import com.alibaba.fastjson.JSONObject;
import com.judong.chuanyiserver.exception.CustomException;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.UaClient;
import org.eclipse.milo.opcua.stack.core.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Test {

    private static Logger logger = LoggerFactory.getLogger(Test.class);

    public static void main(String[] args) throws Exception {
        System.out.println(RSAUtil.encryptByPublic("jd123456", RSAUtil.PUBLIC_KEY));
//        OpcUaClient client = test();
//        if (client == null) {
//            throw new CustomException(500, "客户端配置实例化失败");
//        }
//        // 创建连接
//        client.connect().get();
//        List<JSONObject> jsonObjectList = OpcServerUaUtil.generOpcUaTree(client, null);
//        System.out.println(jsonObjectList.toString());
    }

    public static OpcUaClient test() throws Exception {
        OpcUaClient client = OpcServerUaUtil.createClient();
        OpcServerUaUtil.getFuture().whenCompleteAsync((c, ex) -> {
            if (ex != null) {
                throw new CustomException(500, ex.getMessage());
            }
            try {
                c.disconnect().get();
                Stack.releaseSharedResources();
            } catch (InterruptedException | ExecutionException e) {
                throw new CustomException(500, e.getMessage());
            }
        });
        return client;
    }


}
