package com.cisdi.data.emserver.gateway;

import com.cisdi.data.emserver.gateway.impl.AliveCheckTask;
import com.cisdi.data.emserver.gateway.impl.EmiaProChannelInitializer;
import com.cisdi.data.emserver.gateway.impl.EmiaProSessionFactory;
import com.cisdi.data.sdk.enums.ByteOrder;
import com.cisdi.data.sdk.gateway.netty.SessionFactory;
import com.cisdi.data.sdk.gateway.netty.TcpIoService;
import com.cisdi.data.sdk.gateway.netty.impl.DefaultTcpIoService;
import com.cisdi.data.sdk.vo.GatewayVo;
import io.netty.handler.ssl.OpenSsl;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.atomic.AtomicBoolean;

public class EMServerTest {
    private static TcpIoService ioService = null;
    @SuppressWarnings("unused")
    private static SessionFactory sessionFactory = null;


    public static void main(String[] args) throws UnsupportedEncodingException {
        String testmsg=(char)0x02+",T1234,S0123456789ABCDEFGHIJ,012.123456 ,N 0.987654 ,H 0.987654 ,W 0.987654 ,0  36,N  45,H  45,M3,H12:00,D01M12Y10,"+(char)0x0a+(char)0x0b+",A001196302303,N   "+(char)0x0a+(char)0x0b+(char)0x03;
        boolean available = OpenSsl.isAvailable();

        DefaultTcpIoService defaultIoService = new DefaultTcpIoService();
        SessionFactory factory = new EmiaProSessionFactory("10000034");

        factory.init(null, null);

        EmiaProChannelInitializer channelInitializer =
                new EmiaProChannelInitializer(factory, ByteOrder.BIGENDIAN, null);

        GatewayVo instanceVo = new GatewayVo();
        instanceVo.setByteOrder(ByteOrder.BIGENDIAN);
        instanceVo.setParameter("{\"listenIp\":\"10.76.48.66\",\"listenPort\":5002,\"threadSize\":50,\"timeout\":10}");

        defaultIoService.init(instanceVo, factory, channelInitializer);

        ioService = defaultIoService;
        sessionFactory = factory;

        boolean open = ioService.open();

        if(open == true) {

            AtomicBoolean shouldRun = new AtomicBoolean(true);
            AliveCheckTask task = new AliveCheckTask(10, shouldRun, factory);

            Thread thread = new Thread(task, "plc-alive-check-thread");
            thread.start();

            System.out.println("plc网关启动成功");
        }else {
            System.out.println("plc网关启动失败");
        }

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