package com.nms.server.thread;

import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import com.nms.server.common.Common;
import com.nms.server.common.Constants;
import com.nms.server.dao.CommonDao;
import com.nms.server.service.CommonService;
import com.nms.server.thread.dataCollect.DataCollectManagerThread;
import com.nms.server.util.BoneCPPool;

public class DetecDataCollectThread implements Runnable{
	private  Logger logger = Logger.getLogger(DetecDataCollectThread.class);
	
	public DetecDataCollectThread(){
		
	}
	
	@Override
	public void run() {
//		Thread.currentThread().setName("DC 数据收集管理线程 监测线程");
		Thread.currentThread().setName("DC Data Collection And Management Thread Monitoring Thread");
		
		Object rlt = null;
		boolean isDone = false;
		boolean isCancel = false;
		Future<?> future = null;
		try {
			
			Thread.sleep(5000);
			future = Common.getFutureMap().get(Constants.DATA_COLLECT_MANAGER);
			if(future!=null) {
				isDone = future.isDone();
				isCancel = future.isCancelled();
				logger.info("DC已注册数据收集管理线程，isDone="+isDone+",isCancel="+isCancel);
				rlt = future.get();
				logger.info("DC数据收集管理线程执行完毕，结果："+rlt);
			}else {
				logger.info("DC 未注册 数据收集管理 线程,开始注册。。。");
				Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_ERROR,Constants.ERROR_DESC_DATA_COLLECT_NO_RUN);
				
				//- DC端主动收集数据（监测数据、任务结果、回传文件）管理线程
				if(Constants.FLAG_DATA_COLLECT==1) {
					Thread.sleep(60000);
					//注销线程
					Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER);
					ScheduledFuture<?> fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS);
					//注册 管理线程
					Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture);
					logger.info("DC 数据收集管理 线程 注册成功");
					Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_RIGHT_AUTO,Constants.ERROR_DESC_DATA_COLLECT_NO_RUN);
					
					//单独起一个线程，监督数据收集线程是否运行
					Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME);//取消其实是不起作用的？
					Future<?> detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread());
					Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture);
					
				}
			}
		} catch (Exception e) {
			logger.error("DC data collection management thread monitoring thread exceptions, result："+rlt,e);
			
			try {
				Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_ERROR,Constants.ERROR_DESC_DATA_COLLECT);
				
				//- DC端主动收集数据（监测数据、任务结果、回传文件）管理线程
				if(future!=null) {
					isDone = future.isDone();
					isCancel = future.isCancelled();
					logger.info("DC 数据收集管理线程 运行状态：isDone="+isDone+",isCancel="+isCancel);
				}
				if(Constants.FLAG_DATA_COLLECT==1 && future!=null && future.isDone()) {//已执行完毕
					Thread.sleep(60000);
					//注销线程
					logger.info("DC 数据收集管理 线程 已执行完毕（异常导致）,再次注册。。。");
					Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER);
					ScheduledFuture<?> fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS);
					//注册 管理线程
					Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture);
					logger.info("DC 数据收集管理 线程 注册成功");
					Common.addErrorInfoForMySelf(Constants.ERROR_CODE_DATA_COLLECT,Constants.ERROR_INFO_SATAE_RIGHT_AUTO,Constants.ERROR_DESC_DATA_COLLECT);
					
					
					//单独起一个线程，监督数据收集线程是否运行
					Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME);
					Future<?> detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread());
					Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture);
				}
			}catch(Exception ee) {
				logger.error("DC data collection management thread is abnormal when registered again",ee);
			}
			
		}finally{
//			try{
//				//单独起一个线程，监督数据收集线程是否运行
//				Common.cancelRunnableAtOnce(Constants.DETECTI_DATA_COLLECT_THREAD_NAME);
//				Future<?> detecDataCollectFuture = Common.service.submit(new DetecDataCollectThread());
//				Common.registRunnable(Constants.DETECTI_DATA_COLLECT_THREAD_NAME, detecDataCollectFuture);
//			}catch (Exception e) {
//				logger.error("DC 数据收集管理线程 监测线程 注销 异常", e);
//			}
			logger.debug("DC 数据收集管理线程 监测线程 结束");
		}
	}
	
	public static void main (String []args){
		try {
			
			System.out.println(Long.MAX_VALUE/1000/60/60/24);
			
			BoneCPPool.initPool();
			CommonDao dao = new CommonDao();
			CommonService service = new CommonService(dao);
			
			// 初始化自身信息(失败情况下会由Web唤醒)
			if(!service.initServerParams()){
				return;
			}
			
			
			
//			//- DC端主动收集数据（监测数据、任务结果、回传文件）管理线程
			if(Constants.FLAG_DATA_COLLECT==1) {
				//注销线程
				Common.cancelRunnableAtOnce(Constants.DATA_COLLECT_MANAGER);
				ScheduledFuture<?> fcFuture = Common.scheduled.scheduleWithFixedDelay(new DataCollectManagerThread(),0, Constants.DATA_COLLECT_PERIOD, TimeUnit.SECONDS);
				//注册 管理线程
				Common.registRunnable(Constants.DATA_COLLECT_MANAGER, fcFuture);
			}
//			
//			new Thread(new DetecDataCollectThread()).start();
//			
//			//- 错误信息（DC程序运行异常捕获报警）解析管理线程
//			if(Constants.FLAG_ERROR_INFO==1){
//				//注销线程
//				Common.cancelRunnableAtOnce(Constants.ERROR_INFO_RESOVE_MANAGER);
//				ScheduledFuture<?> fcFuture = Common.scheduled.scheduleWithFixedDelay(new ErrorInfoResoveManagerThread(), 0, 30, TimeUnit.SECONDS);
//				//注册 管理线程
//				Common.registRunnable(Constants.ERROR_INFO_RESOVE_MANAGER, fcFuture);
//			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}