Compare commits
5 Commits
a53cfbab00
...
31f4d13a7a
| Author | SHA1 | Date |
|---|---|---|
|
|
31f4d13a7a | |
|
|
ff590d8c05 | |
|
|
645c2ddd90 | |
|
|
5478362706 | |
|
|
62c056ef8c |
|
|
@ -38,22 +38,16 @@ VALUES (1,'18281561650','gKGCDSgWV82XbU0H','textcard','@all',1000002,1,1800);
|
|||
|
||||
CREATE TABLE device_info (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
is_Monitored INT,
|
||||
time_out_period BIGINT,
|
||||
android_id VARCHAR(255),
|
||||
serial_number VARCHAR(255),
|
||||
device_brand VARCHAR(255),
|
||||
android_version VARCHAR(255),
|
||||
sn VARCHAR(255),
|
||||
device_model VARCHAR(255),
|
||||
last_online_time BIGINT,
|
||||
status INT
|
||||
);
|
||||
|
||||
INSERT INTO device_info (time_out_period,android_id,serial_number,device_model,last_online_time,status) VALUES
|
||||
(3600, 'android_id_1', 'SN1234567890', 'Model A', 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);
|
||||
INSERT INTO device_info (is_monitored, time_out_period, device_brand, android_version, sn, device_model, last_online_time, status) VALUES
|
||||
(1, 600, 'google', 'Android 10', UNIX_TIMESTAMP(), 1),
|
||||
|
||||
|
|
|
|||
|
|
@ -4,11 +4,12 @@ import lombok.Data;
|
|||
|
||||
@Data
|
||||
public class DeviceInfo {
|
||||
private String androidId;
|
||||
private String serialNumber;
|
||||
private String androidVersion;
|
||||
private String deviceBrand;
|
||||
private String SN;
|
||||
private String deviceModel;
|
||||
public boolean hasInvalidFields() {
|
||||
return isNullOrInvalid(androidId) || isNullOrInvalid(serialNumber) || isNullOrInvalid(deviceModel);
|
||||
return isNullOrInvalid(androidVersion) || isNullOrInvalid(SN) || isNullOrInvalid(deviceModel) || isNullOrInvalid(deviceBrand);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.kimgo.wepush.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
|
@ -10,9 +11,12 @@ import lombok.Data;
|
|||
public class DeviceInfoDAO {
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
private int isMonitored;
|
||||
private long timeOutPeriod;
|
||||
private String androidId;
|
||||
private String serialNumber;
|
||||
private String deviceBrand;
|
||||
private String androidVersion;
|
||||
@TableField("sn")
|
||||
private String SN;
|
||||
private String deviceModel;
|
||||
private long lastOnlineTime;
|
||||
private int status;
|
||||
|
|
|
|||
|
|
@ -25,16 +25,21 @@ public class HeartBeatService {
|
|||
if (!correctAccessToken.equals(accessToken)){
|
||||
return ServerResponseEntity.fail("Invalid accessToken");
|
||||
}
|
||||
DeviceInfoDAO deviceInfoDAO = getDeviceInfoByAndroidId(deviceInfo.getAndroidId());
|
||||
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
|
||||
if (deviceInfoDAO == null) {
|
||||
// 设备信息不存在,可能是无效的设备
|
||||
addDeviceInfo(deviceInfo);
|
||||
return ServerResponseEntity.success();
|
||||
boolean isSuccess = addDeviceInfo(deviceInfo);
|
||||
if (isSuccess){
|
||||
return ServerResponseEntity.success();
|
||||
} else {
|
||||
logger.info("add to mysql error");
|
||||
}
|
||||
}
|
||||
try {
|
||||
updateClientStatus(deviceInfo);
|
||||
return ServerResponseEntity.success();
|
||||
} catch (Exception e) {
|
||||
logger.error("update client status error.");
|
||||
// 处理更新状态时的异常
|
||||
return ServerResponseEntity.fail("Error updating client status");
|
||||
}
|
||||
|
|
@ -46,7 +51,7 @@ public class HeartBeatService {
|
|||
// 更新该客户端的最后活跃时间
|
||||
|
||||
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.eq("android_id", deviceInfo.getAndroidId());
|
||||
updateWrapper.eq("sn", deviceInfo.getSN());
|
||||
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
|
||||
|
||||
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
||||
|
|
@ -56,6 +61,7 @@ public class HeartBeatService {
|
|||
logger.warn("Update failed: No rows affected");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addDeviceInfo(DeviceInfo deviceInfo) {
|
||||
try {
|
||||
DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo);
|
||||
|
|
@ -71,12 +77,12 @@ public class HeartBeatService {
|
|||
/**
|
||||
* 根据 androidId 查询 DeviceInfo。
|
||||
*
|
||||
* @param androidId 要查询的 Android ID。
|
||||
* @param SN 要查询的 SN。
|
||||
* @return 与 androidId 对应的 DeviceInfoDAO 对象,如果没有找到,则返回 null。
|
||||
*/
|
||||
public DeviceInfoDAO getDeviceInfoByAndroidId(String androidId) {
|
||||
public DeviceInfoDAO getDeviceInfoBySN(String SN) {
|
||||
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("android_id", androidId);
|
||||
queryWrapper.eq("sn", SN);
|
||||
try {
|
||||
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
|
||||
if (deviceInfoDAO != null) {
|
||||
|
|
@ -100,9 +106,11 @@ public class HeartBeatService {
|
|||
// 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑
|
||||
// 示例代码(根据实际字段调整):
|
||||
DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO();
|
||||
deviceInfoDAO.setAndroidId(deviceInfo.getAndroidId());
|
||||
deviceInfoDAO.setSerialNumber(deviceInfo.getSerialNumber());
|
||||
deviceInfoDAO.setIsMonitored(1);
|
||||
deviceInfoDAO.setDeviceBrand(deviceInfo.getDeviceBrand());
|
||||
deviceInfoDAO.setSN(deviceInfo.getSN());
|
||||
deviceInfoDAO.setDeviceModel(deviceInfo.getDeviceModel());
|
||||
deviceInfoDAO.setAndroidVersion(deviceInfo.getAndroidVersion());
|
||||
deviceInfoDAO.setTimeOutPeriod(TIME_OUT_PERIOD);
|
||||
deviceInfoDAO.setLastOnlineTime(System.currentTimeMillis());
|
||||
deviceInfoDAO.setStatus(1);
|
||||
|
|
|
|||
|
|
@ -33,8 +33,16 @@ public class MonitorService {
|
|||
@Scheduled(fixedDelay = 480000)
|
||||
public void monitorOnlineDevices() {
|
||||
List<DeviceInfoDAO> devices = getAllDevices();
|
||||
if (devices.isEmpty()) {
|
||||
// 没有找到数据的处理逻辑
|
||||
logger.info("no data was queried.");
|
||||
}
|
||||
for (DeviceInfoDAO device : devices) {
|
||||
try {
|
||||
if (device.getIsMonitored() != 1){
|
||||
logger.info("device is not monitored ,device SN: {}",device.getSN());
|
||||
continue;
|
||||
}
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) {
|
||||
updateStatus(device, 0); // 设备离线
|
||||
|
|
@ -43,7 +51,7 @@ public class MonitorService {
|
|||
updateStatus(device, 1); // 设备在线
|
||||
}
|
||||
} 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("设备掉线通知");
|
||||
String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
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'>检测到掉线时间: " + 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对象设置到TextCardMessage中
|
||||
|
|
@ -105,17 +115,17 @@ public class MonitorService {
|
|||
private void updateStatus(DeviceInfoDAO device, int status) {
|
||||
try {
|
||||
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.eq("android_id", device.getAndroidId());
|
||||
updateWrapper.eq("SN", device.getSN());
|
||||
updateWrapper.set("status", status);
|
||||
|
||||
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
||||
if (result > 0) {
|
||||
logger.debug("Update successful for device ID: " + device.getAndroidId());
|
||||
logger.debug("Update successful for SN: " + device.getSN());
|
||||
} 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) {
|
||||
logger.error("Error updating status for device ID: " + device.getAndroidId(), e);
|
||||
logger.error("Error updating status for SN: " + device.getSN(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue