Compare commits

..

No commits in common. "a53cfbab00a8fe8101140101c8168699fed15ecc" and "d8720df1887e1adb95bced28f1c76d1a21d618bd" have entirely different histories.

2 changed files with 21 additions and 40 deletions

View File

@ -3,10 +3,8 @@ package com.kimgo.wepush;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling
@MapperScan("com.kimgo.wepush.mapper") @MapperScan("com.kimgo.wepush.mapper")
public class WePushApplication { public class WePushApplication {

View File

@ -1,7 +1,6 @@
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;
@ -10,7 +9,6 @@ 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;
@ -29,31 +27,21 @@ 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(){
@Scheduled(fixedDelay = 480000) List<DeviceInfoDAO> devices = getAllDeviceIds();
public void monitorOnlineDevices() { for (int i = 0; i < devices.size(); i++) {
List<DeviceInfoDAO> devices = getAllDevices(); long currentTimeMillis = System.currentTimeMillis();
for (DeviceInfoDAO device : devices) { if (currentTimeMillis - devices.get(i).getLastOnlineTime() > devices.get(i).getTimeOutPeriod()){
try { sendNotification(devices.get(i));
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);
} }
} }
} }
/** /**
* DeviceInfoDAOid * DeviceInfoDAOid
* *
* @return id * @return id
*/ */
private List<DeviceInfoDAO> getAllDevices() { private List<DeviceInfoDAO> getAllDeviceIds() {
try { try {
// 查询所有DeviceInfoDAO对象 // 查询所有DeviceInfoDAO对象
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>()); List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
@ -65,7 +53,6 @@ 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();
@ -74,10 +61,22 @@ public class MonitorService {
if (qyWeChatSendMessageApiResponse ==null){ if (qyWeChatSendMessageApiResponse ==null){
logger.info("Send Notification Fail"); logger.info("Send Notification Fail");
} }
if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) { if (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();
@ -98,24 +97,8 @@ public class MonitorService {
// 将TextCard对象设置到TextCardMessage中 // 将TextCard对象设置到TextCardMessage中
textCardMessage.setTextcard(textCard); textCardMessage.setTextcard(textCard);
logger.info("TextCardMessage: {}", textCardMessage); logger.info("TextCardMessage: {}", textCardMessage.toString());
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);
}
}
} }