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