BjkjZoneScheduler.java 1.95 KB
package com.iot.scheduler.zone;

import com.iot.scheduler.service.BjkjDevicePullService;
import com.iot.scheduler.task.AbstractZoneScheduler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class BjkjZoneScheduler extends AbstractZoneScheduler {

    @Resource
    private BjkjDevicePullService bjkjDevicePullService;

    @Override
    protected String getZoneName() {
        return "BJKJ (博晶科技)";
    }

    @Scheduled(cron = "${scheduler.bjkj.pull:0 0/10 * * * ?}")
    public void pullDevicesFromThirdParty() {
        String taskName = "Pull Devices (3rd Party -> IoT)";
        logStart(taskName);
        try {
            log.info("[{}] Simulating pulling devices...", getZoneName());

            // 使用原子变量或计数器来判断是真实数据还是假数据
            boolean useRealData = shouldUseRealData();

//            if (useRealData) {
                log.info("本次执行:同步真实设备数据");
                bjkjDevicePullService.pullDeviceAndPushToIot(true); // 从远程接口获取真实数据
//            } else {
//                log.info("本次执行:同步模拟设备数据(本地设备列表)");
//                bjkjDevicePullService.pullDeviceAndPushToIot(false); // 使用本地设备列表生成模拟数据
//            }

            Thread.sleep(1000);
        } catch (Exception e) {
            logError(taskName, e);
        } finally {
            logEnd(taskName);
        }
    }

    // 方案一:使用原子整数计数器
    private final java.util.concurrent.atomic.AtomicInteger executeCount = new java.util.concurrent.atomic.AtomicInteger(0);

    private boolean shouldUseRealData() {
        int count = executeCount.incrementAndGet();
        // 奇数执行真实数据,偶数执行假数据
        return count % 2 == 1;
    }

}