78 lines
2.7 KiB
Java
78 lines
2.7 KiB
Java
|
|
package com.fastbee.scheduled;
|
||
|
|
|
||
|
|
import com.fastbee.common.NettyCacheKey;
|
||
|
|
import com.fastbee.common.core.redis.RedisCache;
|
||
|
|
import com.fastbee.iot.domain.DevicePlan;
|
||
|
|
import com.fastbee.iot.domain.DeviceStatusRecordDTO;
|
||
|
|
import com.fastbee.iot.mapper.DevicePlanMapper;
|
||
|
|
import com.fastbee.iot.mapper.DeviceStatusHistoryMapper;
|
||
|
|
import com.fastbee.memory.GlobalMemory;
|
||
|
|
import org.apache.commons.collections.CollectionUtils;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
|
||
|
|
import java.text.SimpleDateFormat;
|
||
|
|
import java.util.Collection;
|
||
|
|
import java.util.Date;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@Component
|
||
|
|
public class ScheduledTask {
|
||
|
|
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private RedisCache redisCache;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private DeviceStatusHistoryMapper deviceStatusHistoryMapper;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private GlobalMemory globalMemory;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
private DevicePlanMapper devicePlanMapper;
|
||
|
|
|
||
|
|
|
||
|
|
//整点更新设备状态
|
||
|
|
@Scheduled(cron = "0 0 * * * ?")
|
||
|
|
// @Scheduled(cron = "0 * * * * ?")
|
||
|
|
public void executeHourlyTask() {
|
||
|
|
System.out.println("整点定时任务执行时间: " + dateFormat.format(new Date()));
|
||
|
|
String key = NettyCacheKey.deviceRunningStatusKey;
|
||
|
|
// Collection<String> keys = redisCache.keys(key);
|
||
|
|
Collection<String> keys = redisCache.getListKeyByPrefix(key);
|
||
|
|
if (!CollectionUtils.isEmpty(keys)) {
|
||
|
|
keys.forEach(k -> {
|
||
|
|
DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(k);
|
||
|
|
if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) {
|
||
|
|
// recordDTO.getHistories().forEach(x -> {
|
||
|
|
// deviceStatusHistoryMapper.insert(x);
|
||
|
|
// });
|
||
|
|
deviceStatusHistoryMapper.insertBatch(recordDTO.getHistories());
|
||
|
|
}
|
||
|
|
redisCache.deleteObject(k);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
System.out.println("任务执行完成");
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//0点刷新任务创建状态
|
||
|
|
@Scheduled(cron = "0 0 0 * * ?")
|
||
|
|
public void refreshDevicePlanTask() {
|
||
|
|
System.out.println("0点定时刷新状态");
|
||
|
|
Map<String, List<DevicePlan>> map = GlobalMemory.devicePlanMap;
|
||
|
|
if (!CollectionUtils.isEmpty(map.keySet())) {
|
||
|
|
map.values().forEach(x -> {
|
||
|
|
if (!CollectionUtils.isEmpty(x)) {
|
||
|
|
x.forEach(y -> {
|
||
|
|
y.setDayTriggerFlag(false);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
System.out.println("0点定时刷新状态执行完成");
|
||
|
|
}
|
||
|
|
}
|