package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.collect;

import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.springframework.util.StringUtils;

import java.util.concurrent.ExecutorService;

/**
 * 采集工位节拍监控处理器
 */
public class SxCellTaktMonitorHandler {

    private volatile Boolean isOpen = true;

    private volatile String plcCode2Flag;

    private volatile String plcCode2Data;

    private String org;

    private ExecutorService executorService;

    private ListenableFuture<Object> listenFuture;

    public SxCellTaktMonitorHandler(String org) {
        this.org = org;
        executorService = SpringContextsUtil.getBean(ExecutorService.class);
        ListeningExecutorService listenExecutor = MoreExecutors.listeningDecorator(executorService);
        listenFuture = listenExecutor.submit(() -> {
            execMonitor();
            return null;
        });
    }

    public SxCellTaktMonitorHandler plcCodeArr(String[] plcCodeArr) {
        this.plcCode2Flag = plcCodeArr[0];
        this.plcCode2Data = plcCodeArr[1];
        return this;
    }

    public void cancel() {
        if (null == this.listenFuture) return;
        isOpen = false;
        listenFuture.cancel(true);
    }

    private void execMonitor() {

        while (isOpen) {

            if (StringUtils.isEmpty(plcCode2Flag) || StringUtils.isEmpty(plcCode2Data)) continue;

            String plcFlag = plcCode2Flag;

            String plcData = plcCode2Data;




        }
    }

}
