package com.cisdi.data.Spectrograph8860.test;

import com.cisdi.data.Spectrograph8860.SGSocketGateway;
import com.cisdi.data.Spectrograph8860.impl.SGSessionFactory;
import com.cisdi.data.Spectrograph8860.impl.SGChannelInitializer;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StartPlcTest {
    private static Logger logger = LoggerFactory.getLogger(SGSocketGateway.class);

    private static TcpIoService ioService = null;
    @SuppressWarnings("unused")
    private static SessionFactory sessionFactory = null;

    public static void main(String[] args) {
        boolean available = OpenSsl.isAvailable();
        logger.info("openssl available={}", available);

        DefaultTcpIoService defaultIoService = new DefaultTcpIoService();
        SessionFactory factory = new SGSessionFactory(null);

        ServiceProviderTest testServiceProvider = new ServiceProviderTest();
        //String passthroughAddress = "192.168.235.1:9098";

        factory.init(testServiceProvider, null);

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

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

        defaultIoService.init(instanceVo, factory, channelInitializer);

        ioService = defaultIoService;
        sessionFactory = factory;

        boolean open = ioService.open();

        if (open == true) {
            logger.info("plc网关启动成功");
        } else {
            logger.info("plc网关启动失败");
        }

        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
