package com.gyee.alarm.task.thread;

import com.gyee.alarm.model.auto.ProBasicPowerstationPoint;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class ReadWsDataThread implements Runnable {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Executor executor;

    private IEdosUtil edosUtil;

    private Integer readRows;

    private List<ProBasicPowerstationPoint> wpPointList;
    private StringBuilder str;

    private Integer interval;

    private boolean serverStarted=true;

    public ReadWsDataThread(Executor executor, IEdosUtil edosUtil, Integer readRows, List<ProBasicPowerstationPoint> wpPointList, Integer interval, StringBuilder str) {
        this.executor = executor;
        this.edosUtil = edosUtil;
        this.readRows = readRows;
        this.wpPointList = wpPointList;
        this.interval = interval;
        this.str = str;
    }


    @Override
    public synchronized void run() {


        String task = "task@ " + String.valueOf(str);
        logger.info("创建任务并提交到线程池中：" + task);
        FutureTask<String> futureTask = new FutureTask<>(new ReadWsDataThreadPoolPool(task,
				edosUtil, readRows,  wpPointList,interval,str));

        executor.execute(futureTask);


        // 在这里可以做别的任何事情
        String result = null;
        try {
            // 取得结果，同时设置超时执行时间为1秒。同样可以用future.get()，不设置执行超时时间取得结果
            result = futureTask.get();
        } catch (InterruptedException e) {
            futureTask.cancel(true);
        } catch (ExecutionException e) {
            futureTask.cancel(true);
        } catch (Exception e) {
            futureTask.cancel(true);
            // 超时后，进行相应处理
        } finally {
            logger.info("完成任务并提交到线程池中：" + task);
        }
    }
}