CMVR-IOT/cmvr-iot-inspection/src/main/java/com/cmvr/inspection/service/InspectionRobotEndpointResolver.java

56 lines
2.3 KiB
Java
Raw Normal View History

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;
import com.cmvr.inspection.config.RobotStateProperties;
import com.cmvr.inspection.mapper.InspectionRobotMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Date;
@Service
@RequiredArgsConstructor
public class InspectionRobotEndpointResolver implements RobotEndpointResolver {
private final InspectionRobotMapper robotMapper;
private final RobotStateProperties robotStateProperties;
@Override
public RobotEndpoint resolve(String robotId) {
String normalized = StringUtils.trimToNull(robotId);
if (normalized == null) {
throw new GlobalException("机器人ID不能为空");
}
if (robotStateProperties.isVirtualRobot(normalized)) {
throw new GlobalException("虚拟机器人不能调用边缘设备服务:" + normalized);
}
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);
}
Date heartbeatCutoff = robotStateProperties.heartbeatCutoff(new Date());
if (robot.getLastHeartbeatTime() == null || robot.getLastHeartbeatTime().before(heartbeatCutoff)) {
throw new GlobalException("机器人QUIC心跳已超时:" + normalized);
}
if (StringUtils.isBlank(robot.getIpAddress()) || robot.getPort() == null) {
throw new GlobalException("机器人尚未上报gRPC地址:" + normalized);
}
return new RobotEndpoint(robot.getIpAddress(), robot.getPort().intValue());
}
}