更新 MonitorService.java

pull/1/head
wangsiyuan 2023-12-15 10:33:14 +08:00
parent f74a1cf019
commit 4e4084e15d
1 changed files with 21 additions and 6 deletions

View File

@ -2,6 +2,8 @@ package com.kimgo.wepush.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kimgo.wepush.exceptions.DatabaseQueryException;
import com.kimgo.wepush.exceptions.DatabaseUpdateException;
import com.kimgo.wepush.mapper.DeviceInfoDAOMapper;
import com.kimgo.wepush.model.DeviceInfoDAO;
import com.kimgo.wepush.model.TextCardMessage;
@ -14,7 +16,6 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
@Service
@ -34,10 +35,18 @@ public class MonitorService {
@Scheduled(fixedDelay = 480000)
public void monitorOnlineDevices() {
List<DeviceInfoDAO> devices = getAllDevices();
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 {
@ -58,7 +67,11 @@ public class MonitorService {
updateStatus(device, 1); // 设备在线
logger.info("update the device status to online.");
}
} catch (Exception e) {
} catch (DatabaseUpdateException dbe){
logger.error("update to mysql error.");
return;
}
catch (Exception e) {
logger.error("Error processing device with SN " + device.getSN(), e);
}
}
@ -69,7 +82,7 @@ public class MonitorService {
*
* @return id
*/
private List<DeviceInfoDAO> getAllDevices() {
private List<DeviceInfoDAO> getAllDevices() throws DatabaseQueryException {
try {
// 查询所有DeviceInfoDAO对象
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
@ -77,8 +90,8 @@ public class MonitorService {
return deviceInfoDAOs;
} catch (Exception e) {
logger.error("Error querying all device info IDs", e);
throw new DatabaseQueryException("get all devices error.",e);
}
return Collections.emptyList();
}
@ -121,7 +134,7 @@ public class MonitorService {
return textCardMessage;
}
private void updateStatus(DeviceInfoDAO device, int status) {
private void updateStatus(DeviceInfoDAO device, int status) throws DatabaseUpdateException {
try {
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("SN", device.getSN());
@ -135,6 +148,8 @@ public class MonitorService {
}
} catch (Exception e) {
logger.error("Error updating status for SN: " + device.getSN(), e);
throw new DatabaseUpdateException("update to mysql error.",e);
}
}
}