/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/23
 */
package org.springblade.realData;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springblade.etl.TestWarnTask;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.*;

/***
 * Date:2022/5/22
 * Title: 环境辐射与气象监测系统(KRS) 模块
 * Description：MODBUS TCP 协议传输 ,任务管理
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@Component
@EnableScheduling
public class ScheduledTaskManager {

	/**
	 * 公共线程池
	 **/
	private static ThreadFactory publicThreadFactory = new ThreadFactoryBuilder().setNameFormat("public-thread-pool-%d").build();
	private static ExecutorService publicThreadPool = new ThreadPoolExecutor(1, 1,
		0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), publicThreadFactory, new ThreadPoolExecutor.AbortPolicy());

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private IRtuBaseInfoService rtuBaseInfoService;
	@Autowired
	private IRtuWarningService rtuWarningService;

	@Autowired
	private SimpMessagingTemplate messagingTemplate;


	@Scheduled(cron = "0 0/1 *  * * * ")
	public void countTask() {
		try {
			HomeInfoCountTask task = new HomeInfoCountTask(redisTemplate,rtuBaseInfoService,rtuWarningService,messagingTemplate);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			publicThreadPool.execute(futureTask);

		} catch (Exception e) {
			log.error("{}",e.getMessage());
		}
	}

//	@PostConstruct
//	public void init(){
//		this.scheduleExecutor.scheduleWithFixedDelay(() -> {
//			try {
//				Calendar calendar = Calendar.getInstance();
//				int second =calendar.get(Calendar.SECOND);
//				int currTimePoint=second < TIME_SECOND_ROUND ? 0:(second/TIME_SECOND_ROUND);
//				if (timePoint != currTimePoint){
//					calendar.set(Calendar.SECOND,currTimePoint*TIME_SECOND_ROUND);
//					Date tm = calendar.getTime();
//					String timeText = DateUtil.format(tm, "yyyy年MM月dd日 HH时mm分ss秒");
//					log.info("KRS数据读取任务创建，时间：{}", timeText);
//					HomeInfoCountTask task = new HomeInfoCountTask(redisTemplate,rtuBaseInfoService,rtuWarningService,messagingTemplate);
//					FutureTask<Integer> futureTask = new FutureTask<>(task);
//					publicThreadPool.execute(futureTask);
//					//更新时间点
//					timePoint = currTimePoint;
//				}
//			}catch (Exception e){
//				log.error("KRS数据读取任务创建失败 {}",e.getMessage());
//			}
//		}, 0, 2, TimeUnit.SECONDS);
//	}
}
