2025-10-10 15:46:25 +08:00
|
|
|
|
package com.fuyuanshen.global.queue;// ScheduledTasks.java
|
|
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
|
|
import com.fuyuanshen.common.core.utils.StringUtils;
|
|
|
|
|
|
import com.fuyuanshen.common.redis.utils.RedisUtils;
|
|
|
|
|
|
import com.fuyuanshen.equipment.domain.Device;
|
2025-10-25 08:35:25 +08:00
|
|
|
|
import com.fuyuanshen.equipment.domain.vo.OnlineStatusVo;
|
2025-10-10 15:46:25 +08:00
|
|
|
|
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
|
|
|
|
|
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_KEY;
|
|
|
|
|
|
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_KEY_PREFIX;
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
|
public class OnlineStatusTask {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private DeviceMapper deviceMapper;
|
|
|
|
|
|
// 使用cron表达式,每分钟的第0秒执行
|
|
|
|
|
|
@Scheduled(cron = "0 */3 * * * ?")
|
|
|
|
|
|
public void cronTask() {
|
2025-10-25 08:35:25 +08:00
|
|
|
|
List<OnlineStatusVo> onlineStatusVos = deviceMapper.queryOnlineStatusList();
|
|
|
|
|
|
onlineStatusVos.forEach(item -> {
|
2025-10-10 15:46:25 +08:00
|
|
|
|
String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
|
|
|
|
|
|
String status = RedisUtils.getCacheObject(onlineStatusKey);
|
2025-10-25 08:35:25 +08:00
|
|
|
|
if(StringUtils.isBlank(status) || "0".equals(status)){
|
|
|
|
|
|
UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
|
|
|
|
|
|
updateWrapper.eq("id", item.getId());
|
|
|
|
|
|
updateWrapper.set("online_status", 0);
|
|
|
|
|
|
deviceMapper.update(updateWrapper);
|
2025-10-10 15:46:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|