Merge branch 'dev'
commit
8aea9de61c
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -19,6 +19,5 @@ class HeartBeatServiceTest {
|
|||
@Test
|
||||
void addDeviceInfo() {
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
deviceInfo.setDeviceModel("Model C");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue