Compare commits

..

No commits in common. "d95a725934d13e75b5e73d71c70482b13ae1f16a" and "31f4d13a7ac3998d0888e5531d177228f4e4d5c7" have entirely different histories.

5 changed files with 8 additions and 15 deletions

View File

@ -5,8 +5,6 @@ import com.kimgo.wepush.model.DeviceInfo;
import com.kimgo.wepush.response.ServerResponseEntity;
import com.kimgo.wepush.service.HeartBeatService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -17,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
public class HeartbeatController {
@Autowired
private HeartBeatService heartBeatService;
private final Logger logger = LoggerFactory.getLogger(CallerController.class);
@PostMapping("/heartbeat")
public ServerResponseEntity receiveHeartbeat(@RequestHeader("accessToken") String accessToken,
@ -28,7 +25,6 @@ public class HeartbeatController {
return ServerResponseEntity.fail("accessToken cannot be empty.");
}
if (deviceInfo == null || deviceInfo.hasInvalidFields()){
logger.info("DeviceInfo: {}",deviceInfo.toString());
return ServerResponseEntity.fail("json body value error.");
}
return heartBeatService.handleHeartbeatSignal(accessToken,deviceInfo);

View File

@ -12,7 +12,6 @@ public class DeviceInfoDAO {
@TableId(type = IdType.AUTO)
private Integer id;
private int isMonitored;
//设备超时时间,单位秒
private long timeOutPeriod;
private String deviceBrand;
private String androidVersion;

View File

@ -52,7 +52,7 @@ public class HeartBeatService {
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("sn", deviceInfo.getSN());
updateWrapper.set("last_online_time", currentTimeMillis);
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
int result = deviceInfoDAOMapper.update(null, updateWrapper);
if (result > 0) {
@ -86,11 +86,11 @@ public class HeartBeatService {
try {
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
if (deviceInfoDAO != null) {
logger.info("Data is queried in the database based on the SN");
logger.info("Data is queried in the database based on the device id");
return deviceInfoDAO;
}
} catch (Exception e) {
logger.error("Error querying device info by SN", e);
logger.error("Error querying device info by android ID", e);
}
return null;
}

View File

@ -44,17 +44,11 @@ public class MonitorService {
continue;
}
long currentTimeMillis = System.currentTimeMillis();
long timeDifferenceMillis = currentTimeMillis - device.getLastOnlineTime();
long timeDifferenceSeconds = timeDifferenceMillis / 1000;
if (timeDifferenceSeconds > device.getTimeOutPeriod()) {
logger.info("currentTimeMillis: {},device LastOnlineTime: {},时间差: {},设备超时时间: {}",currentTimeMillis,
device.getLastOnlineTime(),timeDifferenceSeconds,device.getTimeOutPeriod());
if (currentTimeMillis - device.getLastOnlineTime() > device.getTimeOutPeriod()) {
updateStatus(device, 0); // 设备离线
logger.info("Update the device status to offline.");
sendNotification(device);
} else {
updateStatus(device, 1); // 设备在线
logger.info("update the device status to online.");
}
} catch (Exception e) {
logger.error("Error processing device with SN " + device.getSN(), e);

View File

@ -15,6 +15,8 @@ class HeartBeatServiceTest {
void updateClientStatus() {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setDeviceModel("Model B");
deviceInfo.setSerialNumber("SN1234567891");
deviceInfo.setAndroidId("android_id_2");
heartBeatService.updateClientStatus(deviceInfo);
}
@ -22,6 +24,8 @@ class HeartBeatServiceTest {
void addDeviceInfo() {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setDeviceModel("Model C");
deviceInfo.setSerialNumber("SN1234567899");
deviceInfo.setAndroidId("dadwdwfcev");
heartBeatService.addDeviceInfo(deviceInfo);
}
}