更新 MonitorService.java
parent
d8720df188
commit
058649bcaa
|
|
@ -1,6 +1,7 @@
|
||||||
package com.kimgo.wepush.service;
|
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.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;
|
||||||
|
|
@ -9,6 +10,7 @@ import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
@ -27,21 +29,31 @@ public class MonitorService {
|
||||||
private final Logger logger = LoggerFactory.getLogger(MonitorService.class);
|
private final Logger logger = LoggerFactory.getLogger(MonitorService.class);
|
||||||
@Autowired
|
@Autowired
|
||||||
private DeviceInfoDAOMapper deviceInfoDAOMapper;
|
private DeviceInfoDAOMapper deviceInfoDAOMapper;
|
||||||
public void monitorOnlineDevices(){
|
|
||||||
List<DeviceInfoDAO> devices = getAllDeviceIds();
|
@Scheduled(fixedDelay = 480000)
|
||||||
for (int i = 0; i < devices.size(); i++) {
|
public void monitorOnlineDevices() {
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
List<DeviceInfoDAO> devices = getAllDevices();
|
||||||
if (currentTimeMillis - devices.get(i).getLastOnlineTime() > devices.get(i).getTimeOutPeriod()){
|
for (DeviceInfoDAO device : devices) {
|
||||||
sendNotification(devices.get(i));
|
try {
|
||||||
|
long currentTimeMillis = System.currentTimeMillis();
|
||||||
|
if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) {
|
||||||
|
updateStatus(device, 0); // 设备离线
|
||||||
|
sendNotification(device);
|
||||||
|
} else {
|
||||||
|
updateStatus(device, 1); // 设备在线
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error processing device with ID " + device.getAndroidId(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据库中所有DeviceInfoDAO对象的id。
|
* 获取数据库中所有DeviceInfoDAO对象的id。
|
||||||
*
|
*
|
||||||
* @return 包含所有id的列表。
|
* @return 包含所有id的列表。
|
||||||
*/
|
*/
|
||||||
private List<DeviceInfoDAO> getAllDeviceIds() {
|
private List<DeviceInfoDAO> getAllDevices() {
|
||||||
try {
|
try {
|
||||||
// 查询所有DeviceInfoDAO对象
|
// 查询所有DeviceInfoDAO对象
|
||||||
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
|
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
|
||||||
|
|
@ -53,6 +65,7 @@ public class MonitorService {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void sendNotification(DeviceInfoDAO deviceInfoDAO){
|
private void sendNotification(DeviceInfoDAO deviceInfoDAO){
|
||||||
Request request = new Request();
|
Request request = new Request();
|
||||||
String accessToken = tokenService.getAccessToken();
|
String accessToken = tokenService.getAccessToken();
|
||||||
|
|
@ -61,22 +74,10 @@ public class MonitorService {
|
||||||
if (qyWeChatSendMessageApiResponse ==null){
|
if (qyWeChatSendMessageApiResponse ==null){
|
||||||
logger.info("Send Notification Fail");
|
logger.info("Send Notification Fail");
|
||||||
}
|
}
|
||||||
if (qyWeChatSendMessageApiResponse.getErrcode() == 0){
|
if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) {
|
||||||
logger.info("Send Notification Success");
|
logger.info("Send Notification Success");
|
||||||
} else if (qyWeChatSendMessageApiResponse.getErrcode() == 42001 || qyWeChatSendMessageApiResponse.getErrcode() == 40014){
|
|
||||||
tokenService.setAccessToken();
|
|
||||||
accessToken = tokenService.getAccessToken();
|
|
||||||
QyWeChatSendMessageApiResponse qyApiResponse = request.okhttpRequest(url,accessToken,deviceInfoDAO);
|
|
||||||
if (qyApiResponse.getErrcode() == 0){
|
|
||||||
logger.info("Send Notification Success");
|
|
||||||
} else {
|
|
||||||
logger.error("Send Notification Fail");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logger.error("Send Notification Fail");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) {
|
public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) {
|
||||||
TextCardMessage textCardMessage = new TextCardMessage();
|
TextCardMessage textCardMessage = new TextCardMessage();
|
||||||
|
|
||||||
|
|
@ -97,8 +98,24 @@ public class MonitorService {
|
||||||
// 将TextCard对象设置到TextCardMessage中
|
// 将TextCard对象设置到TextCardMessage中
|
||||||
textCardMessage.setTextcard(textCard);
|
textCardMessage.setTextcard(textCard);
|
||||||
|
|
||||||
logger.info("TextCardMessage: {}", textCardMessage.toString());
|
logger.info("TextCardMessage: {}", textCardMessage);
|
||||||
|
|
||||||
return textCardMessage;
|
return textCardMessage;
|
||||||
}
|
}
|
||||||
|
private void updateStatus(DeviceInfoDAO device, int status) {
|
||||||
|
try {
|
||||||
|
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
||||||
|
updateWrapper.eq("android_id", device.getAndroidId());
|
||||||
|
updateWrapper.set("status", status);
|
||||||
|
|
||||||
|
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
||||||
|
if (result > 0) {
|
||||||
|
logger.debug("Update successful for device ID: " + device.getAndroidId());
|
||||||
|
} else {
|
||||||
|
logger.warn("Update failed: No rows affected for device ID: " + device.getAndroidId());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error updating status for device ID: " + device.getAndroidId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue