BjkjZoneScheduler.java
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
}
}