package co.dc.aptiv.mina;


import javax.annotation.Resource;

import co.dc.aptiv.AptivApp;
import co.dc.commons.utils.DateTimeUtil;
import co.dc.aptiv.pojo.MaterielPojo;
import co.dc.aptiv.service.MaterielService;
import co.dc.aptiv.service.QrCodeService;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.serial.SerialAddress;
import org.apache.mina.transport.serial.SerialConnector;
import org.springframework.stereotype.Service;

import co.dc.aptiv.ScanFrame;


@Service("testMachineCollectorMina")
public class TestMachineCollectorMina extends MinaAbstract {

    private static final Log log = LogFactory.getLog(TestMachineCollectorMina.class);
    @Resource
    private QueryRunner queryRunner;
    @Resource
    private Configuration configuration;

    @Override
    public void init(boolean init) {
        // 创建连接客户端
        connector = new SerialConnector();
        connector.getFilterChain().addLast("log", new LoggingFilter());
        // 设置连接超时检查时间
        connector.setConnectTimeoutCheckInterval(30);
        // 添加处理器
        connector.setHandler(new TestMachineCollectorMinaHandler(this, queryRunner));

        setIp(configuration.getString("testMachine.com"));
        setPort(configuration.getInt("testMachine.bot"));
        connector.setDefaultRemoteAddress(new SerialAddress(getIp(), getPort(), SerialAddress.DataBits.DATABITS_8, SerialAddress.StopBits.BITS_1, SerialAddress.Parity.NONE, SerialAddress.FlowControl.NONE));
        if (init == true) {
            if (createConn()) {

            }
        }
    }


    @Override
    public void send(String message) {
        super.send(message);
    }

}
