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 QrCodeService qrCodeService;
    @Resource
    private MaterielService materielService;
    @Resource
    private Configuration configuration;

    private ScanFrame scanFrame;

    @Override
    public void init(boolean init) {
        // 创建连接客户端
        connector = new SerialConnector();
        connector.getFilterChain().addLast("log", new LoggingFilter());
        // 设置连接超时检查时间
        connector.setConnectTimeoutCheckInterval(30);
        // 添加处理器
        connector.setHandler(new TestMachineCollectorMinaHandler(this, queryRunner) {
            // 接收到信息后触发的方法
            @Override
            public void messageReceived(IoSession session, Object message) throws Exception {
                IoBuffer in = (IoBuffer) message;
                byte[] bytes = new byte[in.remaining()];
                in.get(bytes, 0, in.remaining());
                String s = Integer.toHexString(bytes[0]);
                if (s != null) {
                    String dpn = AptivApp.ccu.HexString2String(bytes);
                    if (getScanFrame().onLine) {
                        MaterielPojo materielPojo = materielService.findByDPN(dpn);
                        //判断物料是否存在
                        if (materielPojo != null) {
                            //判断当前是否可打印模式
                            if (AptivApp.printStatus) {
                                AptivApp.nowMaterielPojo = materielPojo;
                                getScanFrame().setComboBox_1();
                                qrCodeService.sendQrCode();
                            }
                        } else {
                            //弹出提示信息
                            getScanFrame().alertMsg(1, "测试机发来的物料不存在，请检查测试机或物料！");
                        }
                        //给测试机回复收到物料号
                        Thread.sleep(100);
                        AptivApp.testMachineCollectorMina.sendByte(AptivApp.ccu.String2ASCII("ok"));
                    }
                }
            }

            @Override
            public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
            }
        });

        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()) {
                scanFrame = new ScanFrame();

            }
        }
    }


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

    public ScanFrame getScanFrame() {
        return scanFrame;
    }

    public void setScanFrame(ScanFrame scanFrame) {
        this.scanFrame = scanFrame;
    }
}
