Update MonitorService.java

This commit is contained in:
2024-04-19 16:46:57 +08:00
parent 2f424e837d
commit 3d01fffebe

View File

@@ -33,50 +33,66 @@ public class MonitorService {
this.deviceInfoDAOMapper = deviceInfoDAOMapper; this.deviceInfoDAOMapper = deviceInfoDAOMapper;
} }
/**
* 定时监控在线设备的状态。
* 该方法每隔固定的时间间隔480000毫秒执行一次用于检查数据库中的设备是否在线。
* 对于每个设备,如果其最后上线时间与当前时间的差值超过了设备的超时周期,则认为该设备已离线,并更新设备状态及发送通知。
*
* @Scheduled 注解指定了方法的定时执行策略fixedDelay属性表示执行完一次后隔多长时间再执行下一次。
*/
@Scheduled(fixedDelay = 480000) @Scheduled(fixedDelay = 480000)
public void monitorOnlineDevices() { public void monitorOnlineDevices() {
List<DeviceInfoDAO> devices; List<DeviceInfoDAO> devices;
try { try {
// 尝试从数据库获取所有设备信息
devices = getAllDevices(); devices = getAllDevices();
} catch (DatabaseQueryException e) { } catch (DatabaseQueryException e) {
// 查询设备信息出错时的处理逻辑
logger.error("monitor online devices error."); logger.error("monitor online devices error.");
return; return;
} }
if (devices.isEmpty()) { if (devices.isEmpty()) {
// 没有找到数据的处理逻辑 // 如果查询到的设备列表为空,记录日志并返回
logger.info("no data was queried."); logger.info("no data was queried.");
return; return;
} }
for (DeviceInfoDAO device : devices) { for (DeviceInfoDAO device : devices) {
try { try {
// 如果设备不被监控,则跳过该设备的处理
if (device.getIsMonitored() != 1){ if (device.getIsMonitored() != 1){
logger.info("device is not monitored ,device SN: {}",device.getSN()); logger.info("device is not monitored ,device SN: {}",device.getSN());
continue; continue;
} }
// 计算设备最后上线时间与当前时间的差值
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
long timeDifferenceMillis = currentTimeMillis - device.getLastOnlineTime(); long timeDifferenceMillis = currentTimeMillis - device.getLastOnlineTime();
long timeDifferenceSeconds = timeDifferenceMillis / 1000; long timeDifferenceSeconds = timeDifferenceMillis / 1000;
// 如果设备超出设定的超时周期未上线,则认为设备离线,并进行相应处理
if (timeDifferenceSeconds > device.getTimeOutPeriod()) { if (timeDifferenceSeconds > device.getTimeOutPeriod()) {
logger.info("Device offline. SN: {}, LastOnlineTime: {}, TimeDifference: {}, TimeoutPeriod: {}", logger.info("Device offline. SN: {}, LastOnlineTime: {}, TimeDifference: {}, TimeoutPeriod: {}",
device.getSN(), device.getLastOnlineTime(), timeDifferenceSeconds, device.getTimeOutPeriod()); device.getSN(), device.getLastOnlineTime(), timeDifferenceSeconds, device.getTimeOutPeriod());
updateStatus(device, 0); // 设备离线 updateStatus(device, 0); // 更新设备状态为离线
logger.info("Update the device status to offline."); logger.info("Update the device status to offline.");
sendNotification(device); sendNotification(device); // 发送设备离线通知
} else { } else {
updateStatus(device, 1); // 设备在线 // 如果设备仍在设定的超时周期内上线,则认为设备在线,并更新设备状态
updateStatus(device, 1); // 更新设备状态为在线
logger.info("update the device status to online."); logger.info("update the device status to online.");
} }
} catch (DatabaseUpdateException dbe){ } catch (DatabaseUpdateException dbe){
// 更新数据库出错时的处理逻辑
logger.error("update to mysql error."); logger.error("update to mysql error.");
return; return;
} }
catch (Exception e) { catch (Exception e) {
// 处理设备信息处理过程中出现的其他异常
logger.error("Error processing device with SN " + device.getSN(), e); logger.error("Error processing device with SN " + device.getSN(), e);
} }
} }
} }
/** /**
* 获取数据库中所有DeviceInfoDAO对象的id。 * 获取数据库中所有DeviceInfoDAO对象的id。
* *