package com.gyee.alarm.task.thread;

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

import java.util.Date;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;

public class EquipmentInfo5Thread implements Runnable {

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

	private ProBasicEquipment wt;
	private   Map<String, ProEconEquipmentInfoDay5> rates;

	private Map<Double, Double> stateDescription1;
	private Map<Double, Double> stateDescription2;
	private Date recordDate;
	private Date endDate;

	private Date beginDate;

	private IEdosUtil edosUtil;

	private CountDownLatch latch;

	public EquipmentInfo5Thread(Executor executor, ProBasicEquipment wt, Map<String, ProEconEquipmentInfoDay5> rates,
								Date endDate, Date beginDate,
								IEdosUtil edosUtil, Map<Double, Double> stateDescription1,
								Map<Double, Double> stateDescription2,
                                CountDownLatch latch	) {

		this.executor = executor;
		this.wt = wt;
		this.rates = rates;
		this.endDate = endDate;
		this.beginDate = beginDate;
		this.edosUtil = edosUtil;
		this.stateDescription1 = stateDescription1;
		this.stateDescription2 = stateDescription2;
		this.latch = latch;
	}

	@Override
	public synchronized void run() {

		String task = "task@ " + wt.getId();
		logger.info("设备5计算服务创建任务并提交到线程池中：" + wt.getId());
		FutureTask<String> futureTask = new FutureTask<String>(new EquipmentInfo5ThreadPool(task,
				 wt,  rates,
				   endDate,  beginDate,
				 edosUtil, stateDescription1,
				 stateDescription2));

		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 {
			latch.countDown();
		}
	}
}