package co.dc.aptiv.mina;

import co.dc.aptiv.AptivApp;
import co.dc.aptiv.ScanFrame;
import co.dc.aptiv.service.BillService;
import co.dc.commons.utils.DateTimeUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 一道采集逻辑处理类
 *
 * @author mx
 */
public class PLCMinaHandler extends MinaAbstractHandler {

    private static final Log log = LogFactory.getLog(PLCMinaHandler.class);

    public PLCMinaHandler(CollectorMina collectorMina, QueryRunner queryRunner) {
        super(collectorMina, queryRunner);
    }

    private BillService billService;

    public PLCMinaHandler(MinaAbstract mina, QueryRunner queryRunner, BillService billService) {
        super(mina, queryRunner);
        this.billService = billService;
    }

    /**
     * 处理收到的信息
     */
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String data = (String) message;
        log.info("collector received [" + data + "]");
        if (data != null) {
            String str = replaceBlank(data).trim();
            //确认PLC联机信号
            if ("LOK".equals(str)) {
                log.info("收到确认PLC联机信号");
                AptivApp.plcMina.getScanFrame().onLine = true;
                AptivApp.configuration.setProperty("onlineStatus", true);
                //保存修改的配置文件
                AptivApp.plcMina.getScanFrame().saveOlineStatus();
                AptivApp.testMachineCollectorMina.startConnection();
                //设置指示灯状态
                AptivApp.plcMina.getScanFrame().changeLight(AptivApp.plcMina.getScanFrame().label_scan3, AptivApp.plcMina.getScanFrame().onLine);
                AptivApp.plcMina.getScanFrame().comboBox_1.setEnabled(!AptivApp.plcMina.getScanFrame().onLine);
                AptivApp.plcMina.getScanFrame().comboBox_1.setSelectedIndex(0);
                AptivApp.plcMina.getScanFrame().comboBox_1.updateUI();
                AptivApp.nowMaterielPojo = null;
            }
            //确认PLC独立信号
            if ("DOK".equals(str)) {
                log.info("收到确认PLC独立信号");
                AptivApp.plcMina.getScanFrame().onLine = false;
                AptivApp.configuration.setProperty("onlineStatus", false);
                //保存修改的配置文件
                AptivApp.plcMina.getScanFrame().saveOlineStatus();
                //设置指示灯状态
                AptivApp.plcMina.getScanFrame().changeLight(AptivApp.plcMina.getScanFrame().label_scan3, AptivApp.plcMina.getScanFrame().onLine);
                AptivApp.plcMina.getScanFrame().comboBox_1.setEnabled(!AptivApp.plcMina.getScanFrame().onLine);
                AptivApp.plcMina.getScanFrame().comboBox_1.setSelectedIndex(0);
                AptivApp.plcMina.getScanFrame().comboBox_1.updateUI();
            }
            //确认PLC激光开启信号
            if ("SOK".equals(str)) {
                log.info("收到确认PLC激光开启信号");
            }
            //确认PLC激光关闭信号
            if ("EOK".equals(str)) {
               log.info("收到确认PLC激光关闭信号");
            }
        }
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\t|\r|\n");
            /* Pattern p = Pattern.compile("\\s*|\t|\r|\n");*/
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }


}
