Compare commits
5 Commits
31f4d13a7a
...
d95a725934
| Author | SHA1 | Date | |
|---|---|---|---|
| d95a725934 | |||
| 8772a041f3 | |||
| ae889a2447 | |||
| ceaf3ac24d | |||
| fb1022ad98 |
@@ -5,6 +5,8 @@ import com.kimgo.wepush.model.DeviceInfo;
|
|||||||
import com.kimgo.wepush.response.ServerResponseEntity;
|
import com.kimgo.wepush.response.ServerResponseEntity;
|
||||||
import com.kimgo.wepush.service.HeartBeatService;
|
import com.kimgo.wepush.service.HeartBeatService;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
@@ -15,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class HeartbeatController {
|
public class HeartbeatController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private HeartBeatService heartBeatService;
|
private HeartBeatService heartBeatService;
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(CallerController.class);
|
||||||
|
|
||||||
@PostMapping("/heartbeat")
|
@PostMapping("/heartbeat")
|
||||||
public ServerResponseEntity receiveHeartbeat(@RequestHeader("accessToken") String accessToken,
|
public ServerResponseEntity receiveHeartbeat(@RequestHeader("accessToken") String accessToken,
|
||||||
@@ -25,6 +28,7 @@ public class HeartbeatController {
|
|||||||
return ServerResponseEntity.fail("accessToken cannot be empty.");
|
return ServerResponseEntity.fail("accessToken cannot be empty.");
|
||||||
}
|
}
|
||||||
if (deviceInfo == null || deviceInfo.hasInvalidFields()){
|
if (deviceInfo == null || deviceInfo.hasInvalidFields()){
|
||||||
|
logger.info("DeviceInfo: {}",deviceInfo.toString());
|
||||||
return ServerResponseEntity.fail("json body value error.");
|
return ServerResponseEntity.fail("json body value error.");
|
||||||
}
|
}
|
||||||
return heartBeatService.handleHeartbeatSignal(accessToken,deviceInfo);
|
return heartBeatService.handleHeartbeatSignal(accessToken,deviceInfo);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ public class DeviceInfoDAO {
|
|||||||
@TableId(type = IdType.AUTO)
|
@TableId(type = IdType.AUTO)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private int isMonitored;
|
private int isMonitored;
|
||||||
|
//设备超时时间,单位秒
|
||||||
private long timeOutPeriod;
|
private long timeOutPeriod;
|
||||||
private String deviceBrand;
|
private String deviceBrand;
|
||||||
private String androidVersion;
|
private String androidVersion;
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public class HeartBeatService {
|
|||||||
|
|
||||||
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
||||||
updateWrapper.eq("sn", deviceInfo.getSN());
|
updateWrapper.eq("sn", deviceInfo.getSN());
|
||||||
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
|
updateWrapper.set("last_online_time", currentTimeMillis);
|
||||||
|
|
||||||
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
||||||
if (result > 0) {
|
if (result > 0) {
|
||||||
@@ -86,11 +86,11 @@ public class HeartBeatService {
|
|||||||
try {
|
try {
|
||||||
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
|
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
|
||||||
if (deviceInfoDAO != null) {
|
if (deviceInfoDAO != null) {
|
||||||
logger.info("Data is queried in the database based on the device id");
|
logger.info("Data is queried in the database based on the SN");
|
||||||
return deviceInfoDAO;
|
return deviceInfoDAO;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error querying device info by android ID", e);
|
logger.error("Error querying device info by SN", e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,11 +44,17 @@ public class MonitorService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
long currentTimeMillis = System.currentTimeMillis();
|
||||||
if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) {
|
long timeDifferenceMillis = currentTimeMillis - device.getLastOnlineTime();
|
||||||
|
long timeDifferenceSeconds = timeDifferenceMillis / 1000;
|
||||||
|
if (timeDifferenceSeconds > device.getTimeOutPeriod()) {
|
||||||
|
logger.info("currentTimeMillis: {},device LastOnlineTime: {},时间差: {},设备超时时间: {}",currentTimeMillis,
|
||||||
|
device.getLastOnlineTime(),timeDifferenceSeconds,device.getTimeOutPeriod());
|
||||||
updateStatus(device, 0); // 设备离线
|
updateStatus(device, 0); // 设备离线
|
||||||
|
logger.info("Update the device status to offline.");
|
||||||
sendNotification(device);
|
sendNotification(device);
|
||||||
} else {
|
} else {
|
||||||
updateStatus(device, 1); // 设备在线
|
updateStatus(device, 1); // 设备在线
|
||||||
|
logger.info("update the device status to online.");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error processing device with SN " + device.getSN(), e);
|
logger.error("Error processing device with SN " + device.getSN(), e);
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ class HeartBeatServiceTest {
|
|||||||
void updateClientStatus() {
|
void updateClientStatus() {
|
||||||
DeviceInfo deviceInfo = new DeviceInfo();
|
DeviceInfo deviceInfo = new DeviceInfo();
|
||||||
deviceInfo.setDeviceModel("Model B");
|
deviceInfo.setDeviceModel("Model B");
|
||||||
deviceInfo.setSerialNumber("SN1234567891");
|
|
||||||
deviceInfo.setAndroidId("android_id_2");
|
|
||||||
heartBeatService.updateClientStatus(deviceInfo);
|
heartBeatService.updateClientStatus(deviceInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,8 +22,6 @@ class HeartBeatServiceTest {
|
|||||||
void addDeviceInfo() {
|
void addDeviceInfo() {
|
||||||
DeviceInfo deviceInfo = new DeviceInfo();
|
DeviceInfo deviceInfo = new DeviceInfo();
|
||||||
deviceInfo.setDeviceModel("Model C");
|
deviceInfo.setDeviceModel("Model C");
|
||||||
deviceInfo.setSerialNumber("SN1234567899");
|
|
||||||
deviceInfo.setAndroidId("dadwdwfcev");
|
|
||||||
heartBeatService.addDeviceInfo(deviceInfo);
|
heartBeatService.addDeviceInfo(deviceInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user