2026-08-05 11:10:19 +08:00
|
|
|
|
package com.cmvr.inspection.service;
|
|
|
|
|
|
|
|
|
|
|
|
import com.cmvr.common.exception.GlobalException;
|
|
|
|
|
|
import com.cmvr.common.robot.RobotEndpoint;
|
|
|
|
|
|
import com.cmvr.common.robot.RobotEndpointResolver;
|
|
|
|
|
|
import com.cmvr.inspection.domain.InspectionRobot;
|
2026-08-21 11:48:52 +08:00
|
|
|
|
import com.cmvr.inspection.config.RobotStateProperties;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
import com.cmvr.inspection.mapper.InspectionRobotMapper;
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2026-08-21 11:48:52 +08:00
|
|
|
|
import java.util.Date;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
public class InspectionRobotEndpointResolver implements RobotEndpointResolver {
|
|
|
|
|
|
|
|
|
|
|
|
private final InspectionRobotMapper robotMapper;
|
2026-08-21 11:48:52 +08:00
|
|
|
|
private final RobotStateProperties robotStateProperties;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public RobotEndpoint resolve(String robotId) {
|
|
|
|
|
|
String normalized = StringUtils.trimToNull(robotId);
|
|
|
|
|
|
if (normalized == null) {
|
|
|
|
|
|
throw new GlobalException("机器人ID不能为空");
|
|
|
|
|
|
}
|
2026-08-21 11:48:52 +08:00
|
|
|
|
if (robotStateProperties.isVirtualRobot(normalized)) {
|
|
|
|
|
|
throw new GlobalException("虚拟机器人不能调用边缘设备服务:" + normalized);
|
|
|
|
|
|
}
|
2026-08-05 11:10:19 +08:00
|
|
|
|
List<InspectionRobot> robots = robotMapper.selectByRobotId(normalized);
|
|
|
|
|
|
if (robots.isEmpty()) {
|
|
|
|
|
|
throw new GlobalException("机器人不存在:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (robots.size() > 1) {
|
|
|
|
|
|
throw new GlobalException("机器人ID存在重复数据:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
InspectionRobot robot = robots.get(0);
|
|
|
|
|
|
if ("1".equals(robot.getArchivedStatus())) {
|
|
|
|
|
|
throw new GlobalException("机器人已归档:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!"1".equals(robot.getConnectStatus())) {
|
|
|
|
|
|
throw new GlobalException("机器人当前离线:" + normalized);
|
|
|
|
|
|
}
|
2026-08-21 11:48:52 +08:00
|
|
|
|
Date heartbeatCutoff = robotStateProperties.heartbeatCutoff(new Date());
|
|
|
|
|
|
if (robot.getLastHeartbeatTime() == null || robot.getLastHeartbeatTime().before(heartbeatCutoff)) {
|
|
|
|
|
|
throw new GlobalException("机器人QUIC心跳已超时:" + normalized);
|
|
|
|
|
|
}
|
2026-08-05 11:10:19 +08:00
|
|
|
|
if (StringUtils.isBlank(robot.getIpAddress()) || robot.getPort() == null) {
|
|
|
|
|
|
throw new GlobalException("机器人尚未上报gRPC地址:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
return new RobotEndpoint(robot.getIpAddress(), robot.getPort().intValue());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|