package org.well.well.scheduler;

import org.well.well.manager.AlarmRecordManager;
import org.well.well.manager.DeviceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by admin on 2015/1/15.
 */


@Component
@Transactional
public class DeviceCheckingJob {
    private static Logger logger = LoggerFactory.getLogger(DeviceCheckingJob.class);

    @Resource
    public AlarmRecordManager alarmRecordManager;

    @Resource
    public DeviceManager deviceManager;
    private boolean enabled = true;

    // every 10 seconds
    @Scheduled(cron = "0 0 5 * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
//    @Scheduled(cron = "0 0/2 * * * ?")
    public void execute() {
        if (!enabled) {
            return;
        }
        try {//更新设备是否离线（7天无数据）
            deviceManager.updateDeviceStatus();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

//    @Scheduled(cron = "0 0/59 * * * ?")
//    public void executeConcen() {
//        if (!enabled) {
//            return;
//        }
//        try {
//            deviceConcentratorManager.insertNoDataConcenRecords();
//        } catch (Exception ex) {
//            logger.error(ex.getMessage(), ex);
//        }
//    }


    @Value("${deviceChecking.enabled}")
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}