2026-08-25 10:58:17 +08:00
|
|
|
|
package com.cmvr.resource.service;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
|
|
|
|
|
|
import com.cmvr.common.exception.GlobalException;
|
|
|
|
|
|
import com.cmvr.common.robot.RobotEndpoint;
|
|
|
|
|
|
import com.cmvr.common.robot.RobotEndpointResolver;
|
2026-08-25 10:58:17 +08:00
|
|
|
|
import com.cmvr.resource.config.ResourceRobotStateProperties;
|
|
|
|
|
|
import com.cmvr.resource.domain.ResourceRobot;
|
|
|
|
|
|
import com.cmvr.resource.mapper.ResourceRobotMapper;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
2026-08-21 11:48:52 +08:00
|
|
|
|
import java.util.Date;
|
2026-08-05 11:10:19 +08:00
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
@RequiredArgsConstructor
|
2026-08-25 10:58:17 +08:00
|
|
|
|
public class ResourceRobotEndpointResolver implements RobotEndpointResolver {
|
|
|
|
|
|
private final ResourceRobotMapper robotMapper;
|
|
|
|
|
|
private final ResourceRobotStateProperties stateProperties;
|
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-25 10:58:17 +08:00
|
|
|
|
if (stateProperties.isVirtualRobot(normalized)) {
|
2026-08-21 11:48:52 +08:00
|
|
|
|
throw new GlobalException("虚拟机器人不能调用边缘设备服务:" + normalized);
|
|
|
|
|
|
}
|
2026-08-25 10:58:17 +08:00
|
|
|
|
ResourceRobot robot = robotMapper.selectByRobotIdentity(normalized);
|
|
|
|
|
|
if (robot == null) {
|
2026-08-05 11:10:19 +08:00
|
|
|
|
throw new GlobalException("机器人不存在:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
if ("1".equals(robot.getArchivedStatus())) {
|
|
|
|
|
|
throw new GlobalException("机器人已归档:" + normalized);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!"1".equals(robot.getConnectStatus())) {
|
|
|
|
|
|
throw new GlobalException("机器人当前离线:" + normalized);
|
|
|
|
|
|
}
|
2026-08-25 10:58:17 +08:00
|
|
|
|
Date cutoff = stateProperties.heartbeatCutoff(new Date());
|
|
|
|
|
|
if (robot.getLastHeartbeatTime() == null || robot.getLastHeartbeatTime().before(cutoff)) {
|
2026-08-21 11:48:52 +08:00
|
|
|
|
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());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|