MesSyncTask.java 1.5 KB
package com.iot.scheduler.task;

import com.iot.scheduler.service.MesSyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * MES设备状态同步定时任务
 * 每分钟执行一次,将设备状态同步到MES系统
 */
@Slf4j
@Component
@ConditionalOnProperty(name = "mes.sync.enabled", havingValue = "true", matchIfMissing = true)
public class MesSyncTask {

    @Autowired
    private MesSyncService mesSyncService;

    @Value("${mes.sync.cron:0 */1 * * * ?}")
    private String cronExpression;

    /**
     * 定时任务:每分钟同步设备状态到MES
     * 默认cron: (每分钟的第0秒执行)
     */
    @Scheduled(cron = "${mes.sync.cron:0 */1 * * * ?}")
    public void syncDeviceStates() {
        log.info("[MesSyncTask] 开始执行设备状态同步任务...");
        long startTime = System.currentTimeMillis();
        try {
            mesSyncService.syncDeviceStatesToMes();
        } catch (Exception e) {
            log.error("[MesSyncTask] 设备状态同步任务执行异常", e);
        } finally {
            long costTime = System.currentTimeMillis() - startTime;
            log.info("[MesSyncTask] 设备状态同步任务结束,耗时: {}ms", costTime);
        }
    }
}