设备告警

This commit is contained in:
2025-10-10 15:46:25 +08:00
parent 2c3effa683
commit 5b4fa38dbd
3 changed files with 48 additions and 4 deletions

View File

@ -0,0 +1,43 @@
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;
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() {
QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
List<Device> devices = deviceMapper.selectList(queryWrapper);
devices.forEach(item -> {
String onlineStatusKey = GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + item.getDeviceImei() + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX;
String status = RedisUtils.getCacheObject(onlineStatusKey);
String onlineStatus = item.getOnlineStatus()==null?"0" : item.getOnlineStatus().toString();
if("1".equals(onlineStatus) || "2".equals(onlineStatus)){
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);
}
}
});
}
}