package com.gyee.alarm;


import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.service.AlarmScannerAsyncService;
import com.gyee.alarm.service.AlarmScannerService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;

@Slf4j
@Component
public class CalculateServer {

    @Resource
    AlarmScannerService alarmScannerService;

//    @Resource
//    AlarmScannerAsyncService alarmScannerAsyncService;
    @Value("${interval}")
    private Integer interval;
    @Value("${task-count}")
    private Integer taskCount;
    private boolean serverStarted = false;
    private boolean runStarted = false;
    public boolean start() throws InterruptedException {

        if (serverStarted) {
            return true;
        }

        try {
            //GlobalVar.config = configProperties;
            log.warn("警计算服务启动...... ");
            log.info("开始加载配置及测点数据......");
            alarmScannerService.init();

            runStarted=true;
            alarmScannerService.taskJobs(runStarted);
            serverStarted = true;
        } catch (Exception ex) {
            serverStarted = false;
            log.error(ex.getMessage());
            return false;
        }

        try {
            Thread.sleep((interval * 1000));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    public void stop() {
        serverStarted=false;
        runStarted=false;
        try {
            Thread.sleep(5000);

        } catch (Exception ex) {
            log.info(ex.getMessage());
        }
    }

}
