package com.yfd.platform.component.iec104.server.slave.handler;

import com.yfd.platform.component.iec104.core.ControlManageUtil;
import com.yfd.platform.component.iec104.core.Iec104ThreadLocal;
import com.yfd.platform.component.utils.OptimizedThreadPool;
import com.yfd.platform.component.iec104.message.MessageDetail;
import com.yfd.platform.component.iec104.server.handler.ChannelHandlerImpl;
import com.yfd.platform.component.iec104.server.handler.DataHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
* @ClassName: Iec104TcpSlaveHandler
* @Description: 从机处理类
* @author YDL
* @date 2020年5月13日
 */
public class Iec104TcpSlaveHandler extends SimpleChannelInboundHandler<MessageDetail> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChannelInboundHandlerAdapter.class);

    private DataHandler dataHandler;


	public Iec104TcpSlaveHandler(DataHandler dataHandler) {
		this.dataHandler = dataHandler;
	}

    /**
     * 新连接
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Iec104ThreadLocal.setControlPool(new ControlManageUtil(ctx).setFrameAmountMax(Iec104ThreadLocal.getIec104Conig().getFrameAmountMax()));
        /**
         * 启动
         */
        Iec104ThreadLocal.getControlPool().startSendFrameTask();
//        if (dataHandler != null) {
//            OptimizedThreadPool threadPool = OptimizedThreadPool.getInstance();
//            threadPool.execute(() -> {
//                try {
//                    dataHandler.handlerAdded(new ChannelHandlerImpl(ctx));
//                } catch (Exception e) {
//                    LOGGER.error("Exception caught", e);
//                }
//            });
//            threadPool.shutdown();
//        }
    }

    /**
     * 断开连接
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * 收到消息
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageDetail ruleDetail104) throws Exception {
//    	if (dataHandler != null) {
//
//            OptimizedThreadPool threadPool = OptimizedThreadPool.getInstance();
//            threadPool.execute(() -> {
//                try {
//                    dataHandler.channelRead(new ChannelHandlerImpl(ctx), ruleDetail104);
//                } catch (Exception e) {
//                    LOGGER.error("Exception caught", e);
//                }
//            });
//            threadPool.shutdown();
//        }
    }

    /**
     * 连接
     *
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        LOGGER.error("SimpleChatClient:" + incoming.remoteAddress() + "在线");
    }

    /**
     * 关闭
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        LOGGER.error("SimpleChatClient:" + incoming.remoteAddress() + "掉线");
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        Channel incoming = ctx.channel();
        LOGGER.error("SimpleChatClient:" + incoming.remoteAddress() + "异常");
        // 当出现异常就关闭连接
        //cause.printStackTrace();
        LOGGER.error("Exception caught", cause);
        ctx.close();
    }
}
