Merge branch 'dev'
commit
8c5ef984e7
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.kimgo.wepush.exceptions;
|
||||||
|
|
||||||
|
public class DatabaseUpdateException extends Exception{
|
||||||
|
public DatabaseUpdateException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseUpdateException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,8 @@ package com.kimgo.wepush.service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
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.mapper.DeviceInfoDAOMapper;
|
||||||
import com.kimgo.wepush.model.DeviceInfoDAO;
|
import com.kimgo.wepush.model.DeviceInfoDAO;
|
||||||
import com.kimgo.wepush.model.TextCardMessage;
|
import com.kimgo.wepush.model.TextCardMessage;
|
||||||
|
|
@ -14,7 +16,6 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
|
@ -34,10 +35,18 @@ public class MonitorService {
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 480000)
|
@Scheduled(fixedDelay = 480000)
|
||||||
public void monitorOnlineDevices() {
|
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()) {
|
if (devices.isEmpty()) {
|
||||||
// 没有找到数据的处理逻辑
|
// 没有找到数据的处理逻辑
|
||||||
logger.info("no data was queried.");
|
logger.info("no data was queried.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
for (DeviceInfoDAO device : devices) {
|
for (DeviceInfoDAO device : devices) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -49,8 +58,8 @@ public class MonitorService {
|
||||||
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("currentTimeMillis: {},device LastOnlineTime: {},时间差: {},设备超时时间: {}",currentTimeMillis,
|
logger.info("Device offline. SN: {}, LastOnlineTime: {}, TimeDifference: {}, TimeoutPeriod: {}",
|
||||||
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);
|
||||||
|
|
@ -58,7 +67,11 @@ public class MonitorService {
|
||||||
updateStatus(device, 1); // 设备在线
|
updateStatus(device, 1); // 设备在线
|
||||||
logger.info("update the device status to online.");
|
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);
|
logger.error("Error processing device with SN " + device.getSN(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,7 +82,7 @@ public class MonitorService {
|
||||||
*
|
*
|
||||||
* @return 包含所有id的列表。
|
* @return 包含所有id的列表。
|
||||||
*/
|
*/
|
||||||
private List<DeviceInfoDAO> getAllDevices() {
|
private List<DeviceInfoDAO> getAllDevices() throws DatabaseQueryException {
|
||||||
try {
|
try {
|
||||||
// 查询所有DeviceInfoDAO对象
|
// 查询所有DeviceInfoDAO对象
|
||||||
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
|
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
|
||||||
|
|
@ -77,8 +90,8 @@ public class MonitorService {
|
||||||
return deviceInfoDAOs;
|
return deviceInfoDAOs;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error querying all device info IDs", 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;
|
return textCardMessage;
|
||||||
}
|
}
|
||||||
private void updateStatus(DeviceInfoDAO device, int status) {
|
private void updateStatus(DeviceInfoDAO device, int status) throws DatabaseUpdateException {
|
||||||
try {
|
try {
|
||||||
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
||||||
updateWrapper.eq("SN", device.getSN());
|
updateWrapper.eq("SN", device.getSN());
|
||||||
|
|
@ -135,6 +148,8 @@ public class MonitorService {
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error updating status for SN: " + device.getSN(), e);
|
logger.error("Error updating status for SN: " + device.getSN(), e);
|
||||||
|
throw new DatabaseUpdateException("update to mysql error.",e);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue