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.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);

View File

@ -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;

View File

@ -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;
} }

View File

@ -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);

View File

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