MeetingDeviceWebSocketService.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.usky.meeting.service;
  2. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  3. import com.usky.common.core.exception.BusinessException;
  4. import com.usky.meeting.domain.MeetingDevice;
  5. import com.usky.meeting.domain.MeetingDeviceHeartbeat;
  6. import com.usky.meeting.mapper.MeetingDeviceHeartbeatMapper;
  7. import com.usky.meeting.mapper.MeetingDeviceMapper;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.time.LocalDateTime;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @Slf4j
  16. @Service
  17. @RequiredArgsConstructor
  18. public class MeetingDeviceWebSocketService {
  19. private static final int HEARTBEAT_TIMEOUT_MINUTES = 5;
  20. @Autowired
  21. private MeetingDeviceMapper meetingDeviceMapper;
  22. @Autowired
  23. private MeetingDeviceHeartbeatMapper heartbeatMapper;
  24. public Map<String, Object> validateDevice(String deviceCode) {
  25. Map<String, Object> result = new HashMap<>();
  26. MeetingDevice device = validateDeviceExists(deviceCode);
  27. boolean isOnline = validateDeviceOnline(deviceCode);
  28. result.put("deviceCode", deviceCode);
  29. result.put("deviceName", device.getDeviceName());
  30. result.put("deviceStatus", isOnline ? 1 : 0);
  31. result.put("isOnline", isOnline);
  32. result.put("validateSuccess", true);
  33. log.info("设备验证成功, deviceCode: {}, isOnline: {}", deviceCode, isOnline);
  34. return result;
  35. }
  36. private MeetingDevice validateDeviceExists(String deviceCode) {
  37. MeetingDevice device = meetingDeviceMapper.selectOne(
  38. Wrappers.<MeetingDevice>lambdaQuery().eq(MeetingDevice::getDeviceCode, deviceCode)
  39. );
  40. if (device == null) {
  41. log.warn("设备不存在, deviceCode: {}", deviceCode);
  42. throw new BusinessException("设备不存在: " + deviceCode);
  43. }
  44. return device;
  45. }
  46. private boolean validateDeviceOnline(String deviceCode) {
  47. MeetingDeviceHeartbeat heartbeat = heartbeatMapper.selectOne(
  48. Wrappers.<MeetingDeviceHeartbeat>lambdaQuery()
  49. .eq(MeetingDeviceHeartbeat::getDeviceCode, deviceCode)
  50. .orderByDesc(MeetingDeviceHeartbeat::getCreateTime)
  51. .last("LIMIT 1")
  52. );
  53. if (heartbeat == null) {
  54. log.warn("设备心跳记录不存在, deviceCode: {}", deviceCode);
  55. throw new BusinessException("设备离线: " + deviceCode + ", 未找到心跳记录");
  56. }
  57. LocalDateTime heartbeatTime = heartbeat.getCreateTime();
  58. LocalDateTime now = LocalDateTime.now();
  59. if (heartbeatTime == null || now.isAfter(heartbeatTime.plusMinutes(HEARTBEAT_TIMEOUT_MINUTES))) {
  60. log.warn("设备心跳超时, deviceCode: {}, heartbeatTime: {}", deviceCode, heartbeatTime);
  61. throw new BusinessException("设备离线: " + deviceCode + ", 心跳超时");
  62. }
  63. return true;
  64. }
  65. }