zhaowenyu
2025-10-15 a9780c49f8cf587af26cc83d439320ad02b8668f
ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java
@@ -3,8 +3,6 @@
import com.ard.common.utils.ByteUtils;
import com.ard.work.aspect.annotation.CheckTakeoffAltitude;
import com.ard.work.aspect.annotation.CheckControlRight;
import com.ard.work.component.netty.TcpClientSession;
import com.ard.work.component.netty.TyTcpClientManager;
import com.ard.work.component.netty.command.BaseCmd;
import com.ard.work.component.netty.command.camera.*;
import com.ard.work.component.netty.command.manual.*;
@@ -47,18 +45,18 @@
import com.ard.work.component.netty.command.real.UavTelemetryDataCmd;
import com.ard.work.component.netty.handler.ErrorCodeEnum;
import com.ard.work.component.netty.protobuf.TelemetryDataPB;
import com.ard.work.service.IArdDroneService;
import com.ard.work.service.IArdServerService;
import com.ard.work.service.IArdDroneInfoService;
import com.ard.work.service.IArdServerInfoService;
import com.ard.work.service.ITyService;
import com.ard.work.domain.message.UavControlType;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -77,9 +75,9 @@
@Service
public class TyServiceImpl implements ITyService {
    @Resource
    private IArdServerService ardServerService;
    private IArdServerInfoService ardServerService;
    @Resource
    private IArdDroneService ardDroneService;
    private IArdDroneInfoService ardDroneService;
    @Resource
    private TyClient tyClient;
@@ -100,7 +98,7 @@
     */
    @Override
    public List<BoxInfoReceiver> getBoxList() {
        List<ArdServerInfo> ardServerInfos = ardServerService.selectArdServerManagementList(new ArdServerInfo());
        List<ArdServerInfo> ardServerInfos = ardServerService.selectArdServerInfoList(new ArdServerInfo());
        return getBoxList(ardServerInfos);
    }
@@ -112,8 +110,7 @@
    @Override
    public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSn);
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(baseUrl,boxSn);
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(ardDroneInfo.getServerInfoId(),boxSn);
        return boxLiveUrl.getContent();
    }
@@ -125,8 +122,8 @@
    @Override
    public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxHistoryDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(baseUrl,boxHistoryDTO);
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(ardDroneInfo.getServerInfoId(),
                boxHistoryDTO);
        return boxHistory.getContent();
    }
@@ -138,8 +135,7 @@
    @Override
    public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxPhotosDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(baseUrl,boxPhotosDTO);
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(ardDroneInfo.getServerInfoId(),boxPhotosDTO);
        return boxPhotos.getContent();
    }
@@ -149,8 +145,7 @@
    @Override
    public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSettingsDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(baseUrl,boxSettingsDTO);
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(ardDroneInfo.getServerInfoId(),boxSettingsDTO);
        return result.getState() == 1;
    }
@@ -843,22 +838,26 @@
     * @return List<BoxInfoReceiver>
     */
    @Override
    public List<BoxInfoReceiver> getBoxSnToList(String boxSn) {
        ArdServerInfo serverInfo = new ArdServerInfo();
        serverInfo.setHost(boxSn);
        List<ArdServerInfo> ardServerInfos = ardServerService.selectArdServerManagementList(serverInfo);
        return getBoxList(ardServerInfos);
    public List<BoxInfoReceiver> getBoxSnToList(String id) {
        List<BoxInfoReceiver> lisbox = new ArrayList<>();
        try {
            lisbox = tyClient.getBoxList(id).getContent();
            return lisbox;
        } catch (Exception e) {
            log.error("获取云盒列表失败: {}", id, e);
            return lisbox;
        }
    }
    private List<BoxInfoReceiver> getBoxList(List<ArdServerInfo> ls) {
        return ls.stream()
                .flatMap(ardServerInfo -> {
                    String baseUrl = "http://" + ardServerInfo.getHost() + ":" + ardServerInfo.getHttpPort();
                    try {
                        List<BoxInfoReceiver> boxes = tyClient.getBoxList(baseUrl).getContent();
                        List<BoxInfoReceiver> boxes = tyClient.getBoxList(ardServerInfo.getId()).getContent();
                        return Optional.ofNullable(boxes).orElse(Collections.emptyList()).stream();
                    } catch (Exception e) {
                        log.error("获取云盒列表失败: {}", baseUrl, e);
                        log.error("获取云盒列表失败: {}", ardServerInfo.getId(), e);
                        return Stream.empty();
                    }
                })
@@ -880,27 +879,6 @@
    }
    /**
     * 定时向所有已连接的六合服务器发送心跳
     */
    @Scheduled(fixedRate = 5000) // 每5秒执行一次
    public void sendHeartBeat() {
        long timestamp = System.currentTimeMillis();
        for (TcpClientSession session : channels.values()) {
            ChannelFuture future = session.getChannelFuture();
            if (future == null || !future.channel().isActive()) {
                log.warn("边缘服务器 [{}] 连接未激活,跳过心跳发送", session.getServerId());
                continue;
            }
            HeartbeatCmd heartbeat = new HeartbeatCmd(timestamp);
            future.channel().writeAndFlush(heartbeat);
            log.info("→ 向边缘服务器 [{}] ({}:{}) 发送心跳,时间戳={}",
                    session.getServerId(), session.getHost(), session.getPort(), timestamp);
        }
    }
    /**
     * 发送消息
     */