Merge branch 'dev'

master
wangsiyuan 2023-12-09 14:15:31 +08:00
commit 8aea9de61c
5 changed files with 15 additions and 5 deletions

View File

@ -5,6 +5,8 @@ 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;
@ -15,6 +17,7 @@ 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,
@ -25,6 +28,7 @@ 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,6 +12,7 @@ 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); // 设置新的 accessToken
updateWrapper.set("last_online_time", currentTimeMillis);
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 device id");
logger.info("Data is queried in the database based on the SN");
return deviceInfoDAO;
}
} catch (Exception e) {
logger.error("Error querying device info by android ID", e);
logger.error("Error querying device info by SN", e);
}
return null;
}

View File

@ -44,11 +44,17 @@ public class MonitorService {
continue;
}
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); // 设备离线
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

@ -19,6 +19,5 @@ class HeartBeatServiceTest {
@Test
void addDeviceInfo() {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setDeviceModel("Model C");
}
}