Compare commits

..

5 Commits

Author SHA1 Message Date
wangsiyuan 31f4d13a7a 更新 MonitorService.java 2023-12-09 11:24:18 +08:00
wangsiyuan ff590d8c05 更新 HeartBeatService.java 2023-12-09 11:24:16 +08:00
wangsiyuan 645c2ddd90 更新 DeviceInfoDAO.java 2023-12-09 11:24:13 +08:00
wangsiyuan 5478362706 更新 DeviceInfo.java 2023-12-09 11:23:58 +08:00
wangsiyuan 62c056ef8c 更新 qywecahtpush.sql 2023-12-09 11:23:55 +08:00
5 changed files with 51 additions and 34 deletions

View File

@ -38,22 +38,16 @@ VALUES (1,'18281561650','gKGCDSgWV82XbU0H','textcard','@all',1000002,1,1800);
CREATE TABLE device_info ( CREATE TABLE device_info (
id INT PRIMARY KEY AUTO_INCREMENT, id INT PRIMARY KEY AUTO_INCREMENT,
is_Monitored INT,
time_out_period BIGINT, time_out_period BIGINT,
android_id VARCHAR(255), device_brand VARCHAR(255),
serial_number VARCHAR(255), android_version VARCHAR(255),
sn VARCHAR(255),
device_model VARCHAR(255), device_model VARCHAR(255),
last_online_time BIGINT, last_online_time BIGINT,
status INT status INT
); );
INSERT INTO device_info (time_out_period,android_id,serial_number,device_model,last_online_time,status) VALUES INSERT INTO device_info (is_monitored, time_out_period, device_brand, android_version, sn, device_model, last_online_time, status) VALUES
(3600, 'android_id_1', 'SN1234567890', 'Model A', UNIX_TIMESTAMP(), 1), (1, 600, 'google', 'Android 10', UNIX_TIMESTAMP(), 1),
(3600, 'android_id_2', 'SN1234567891', 'Model B', UNIX_TIMESTAMP(), 1),
(3600, 'android_id_3', 'SN1234567892', 'Model C', UNIX_TIMESTAMP(), 0),
(3600, 'android_id_4', 'SN1234567893', 'Model D', UNIX_TIMESTAMP(), 1),
(3600, 'android_id_5', 'SN1234567894', 'Model E', UNIX_TIMESTAMP(), 0),
(3600, 'android_id_6', 'SN1234567895', 'Model F', UNIX_TIMESTAMP(), 1),
(3600, 'android_id_7', 'SN1234567896', 'Model G', UNIX_TIMESTAMP(), 0),
(3600, 'android_id_8', 'SN1234567897', 'Model H', UNIX_TIMESTAMP(), 1),
(3600, 'android_id_9', 'SN1234567898', 'Model I', UNIX_TIMESTAMP(), 0),
(3600, 'android_id_10', 'SN1234567899', 'Model J', UNIX_TIMESTAMP(), 1);

View File

@ -4,11 +4,12 @@ import lombok.Data;
@Data @Data
public class DeviceInfo { public class DeviceInfo {
private String androidId; private String androidVersion;
private String serialNumber; private String deviceBrand;
private String SN;
private String deviceModel; private String deviceModel;
public boolean hasInvalidFields() { public boolean hasInvalidFields() {
return isNullOrInvalid(androidId) || isNullOrInvalid(serialNumber) || isNullOrInvalid(deviceModel); return isNullOrInvalid(androidVersion) || isNullOrInvalid(SN) || isNullOrInvalid(deviceModel) || isNullOrInvalid(deviceBrand);
} }
/** /**

View File

@ -1,6 +1,7 @@
package com.kimgo.wepush.model; package com.kimgo.wepush.model;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
@ -10,9 +11,12 @@ import lombok.Data;
public class DeviceInfoDAO { public class DeviceInfoDAO {
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Integer id; private Integer id;
private int isMonitored;
private long timeOutPeriod; private long timeOutPeriod;
private String androidId; private String deviceBrand;
private String serialNumber; private String androidVersion;
@TableField("sn")
private String SN;
private String deviceModel; private String deviceModel;
private long lastOnlineTime; private long lastOnlineTime;
private int status; private int status;

View File

@ -25,16 +25,21 @@ public class HeartBeatService {
if (!correctAccessToken.equals(accessToken)){ if (!correctAccessToken.equals(accessToken)){
return ServerResponseEntity.fail("Invalid accessToken"); return ServerResponseEntity.fail("Invalid accessToken");
} }
DeviceInfoDAO deviceInfoDAO = getDeviceInfoByAndroidId(deviceInfo.getAndroidId()); DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
if (deviceInfoDAO == null) { if (deviceInfoDAO == null) {
// 设备信息不存在,可能是无效的设备 // 设备信息不存在,可能是无效的设备
addDeviceInfo(deviceInfo); boolean isSuccess = addDeviceInfo(deviceInfo);
return ServerResponseEntity.success(); if (isSuccess){
return ServerResponseEntity.success();
} else {
logger.info("add to mysql error");
}
} }
try { try {
updateClientStatus(deviceInfo); updateClientStatus(deviceInfo);
return ServerResponseEntity.success(); return ServerResponseEntity.success();
} catch (Exception e) { } catch (Exception e) {
logger.error("update client status error.");
// 处理更新状态时的异常 // 处理更新状态时的异常
return ServerResponseEntity.fail("Error updating client status"); return ServerResponseEntity.fail("Error updating client status");
} }
@ -46,7 +51,7 @@ public class HeartBeatService {
// 更新该客户端的最后活跃时间 // 更新该客户端的最后活跃时间
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("android_id", deviceInfo.getAndroidId()); updateWrapper.eq("sn", deviceInfo.getSN());
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
int result = deviceInfoDAOMapper.update(null, updateWrapper); int result = deviceInfoDAOMapper.update(null, updateWrapper);
@ -56,6 +61,7 @@ public class HeartBeatService {
logger.warn("Update failed: No rows affected"); logger.warn("Update failed: No rows affected");
} }
} }
public boolean addDeviceInfo(DeviceInfo deviceInfo) { public boolean addDeviceInfo(DeviceInfo deviceInfo) {
try { try {
DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo); DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo);
@ -71,12 +77,12 @@ public class HeartBeatService {
/** /**
* androidId DeviceInfo * androidId DeviceInfo
* *
* @param androidId Android ID * @param SN SN
* @return androidId DeviceInfoDAO null * @return androidId DeviceInfoDAO null
*/ */
public DeviceInfoDAO getDeviceInfoByAndroidId(String androidId) { public DeviceInfoDAO getDeviceInfoBySN(String SN) {
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>(); QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("android_id", androidId); queryWrapper.eq("sn", SN);
try { try {
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper); DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
if (deviceInfoDAO != null) { if (deviceInfoDAO != null) {
@ -100,9 +106,11 @@ public class HeartBeatService {
// 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑 // 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑
// 示例代码(根据实际字段调整): // 示例代码(根据实际字段调整):
DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO(); DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO();
deviceInfoDAO.setAndroidId(deviceInfo.getAndroidId()); deviceInfoDAO.setIsMonitored(1);
deviceInfoDAO.setSerialNumber(deviceInfo.getSerialNumber()); deviceInfoDAO.setDeviceBrand(deviceInfo.getDeviceBrand());
deviceInfoDAO.setSN(deviceInfo.getSN());
deviceInfoDAO.setDeviceModel(deviceInfo.getDeviceModel()); deviceInfoDAO.setDeviceModel(deviceInfo.getDeviceModel());
deviceInfoDAO.setAndroidVersion(deviceInfo.getAndroidVersion());
deviceInfoDAO.setTimeOutPeriod(TIME_OUT_PERIOD); deviceInfoDAO.setTimeOutPeriod(TIME_OUT_PERIOD);
deviceInfoDAO.setLastOnlineTime(System.currentTimeMillis()); deviceInfoDAO.setLastOnlineTime(System.currentTimeMillis());
deviceInfoDAO.setStatus(1); deviceInfoDAO.setStatus(1);

View File

@ -33,8 +33,16 @@ public class MonitorService {
@Scheduled(fixedDelay = 480000) @Scheduled(fixedDelay = 480000)
public void monitorOnlineDevices() { public void monitorOnlineDevices() {
List<DeviceInfoDAO> devices = getAllDevices(); List<DeviceInfoDAO> devices = getAllDevices();
if (devices.isEmpty()) {
// 没有找到数据的处理逻辑
logger.info("no data was queried.");
}
for (DeviceInfoDAO device : devices) { for (DeviceInfoDAO device : devices) {
try { try {
if (device.getIsMonitored() != 1){
logger.info("device is not monitored ,device SN: {}",device.getSN());
continue;
}
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) { if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) {
updateStatus(device, 0); // 设备离线 updateStatus(device, 0); // 设备离线
@ -43,7 +51,7 @@ public class MonitorService {
updateStatus(device, 1); // 设备在线 updateStatus(device, 1); // 设备在线
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Error processing device with ID " + device.getAndroidId(), e); logger.error("Error processing device with SN " + device.getSN(), e);
} }
} }
} }
@ -90,9 +98,11 @@ public class MonitorService {
textCard.setTitle("设备掉线通知"); textCard.setTitle("设备掉线通知");
String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
textCard.setDescription("<div class='gray'>设备信息</div>" + textCard.setDescription("<div class='gray'>设备信息</div>" +
"<div class='highlight'>设备id: " + deviceInfoDAO.getAndroidId() + "</div>" + "<div class='highlight'>品牌: " + deviceInfoDAO.getDeviceBrand() + "</div>" +
"<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" + "<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" +
"<div class='highlight'>检测到掉线时间: " + formattedCurrentTime + "</div>"+ "<div class='highlight'>安卓版本: " + deviceInfoDAO.getAndroidVersion() + "</div>" +
"<div class='highlight'>SN: " + deviceInfoDAO.getSN() + "</div>" +
"<div class='highlight'>监测到掉线时间: " + formattedCurrentTime + "</div>" +
"检测到设备已掉线,请及时查看设备状态."); "检测到设备已掉线,请及时查看设备状态.");
textCard.setUrl("https://kimgo.cn"); textCard.setUrl("https://kimgo.cn");
// 将TextCard对象设置到TextCardMessage中 // 将TextCard对象设置到TextCardMessage中
@ -105,17 +115,17 @@ public class MonitorService {
private void updateStatus(DeviceInfoDAO device, int status) { private void updateStatus(DeviceInfoDAO device, int status) {
try { try {
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("android_id", device.getAndroidId()); updateWrapper.eq("SN", device.getSN());
updateWrapper.set("status", status); updateWrapper.set("status", status);
int result = deviceInfoDAOMapper.update(null, updateWrapper); int result = deviceInfoDAOMapper.update(null, updateWrapper);
if (result > 0) { if (result > 0) {
logger.debug("Update successful for device ID: " + device.getAndroidId()); logger.debug("Update successful for SN: " + device.getSN());
} else { } else {
logger.warn("Update failed: No rows affected for device ID: " + device.getAndroidId()); logger.warn("Update failed: No rows affected for SN: " + device.getSN());
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Error updating status for device ID: " + device.getAndroidId(), e); logger.error("Error updating status for SN: " + device.getSN(), e);
} }
} }
} }