From c197c31a1a7bc8a0d7011e39193e3d94060b397c Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 11 Mar 2025 13:35:37 +0800
Subject: [PATCH] 优化websocket应答信息

---
 ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java |  553 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 467 insertions(+), 86 deletions(-)

diff --git a/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java b/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
index 1525c4d..e3e3e54 100644
--- a/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
+++ b/ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
@@ -1,27 +1,31 @@
 package com.ard.tianyu.service.impl;
 
+import com.ard.common.utils.ByteUtils;
+import com.ard.tianyu.aspect.annotation.RequireUavControl;
 import com.ard.tianyu.config.TyConfiguration;
-import com.ard.tianyu.http.TyClient;
-import com.ard.tianyu.model.cache.UavSignalInfoCache;
-import com.ard.tianyu.model.cache.UavStateCache;
-import com.ard.tianyu.model.cache.UavTelemetryDataCache;
-import com.ard.tianyu.model.dto.BoxHistoryDTO;
-import com.ard.tianyu.model.dto.BoxPhotosDTO;
-import com.ard.tianyu.model.dto.BoxSettingsDTO;
-import com.ard.tianyu.model.dto.camera.*;
-import com.ard.tianyu.model.dto.control.AbsAngleControlDTO;
-import com.ard.tianyu.model.dto.control.AngleControlDTO;
-import com.ard.tianyu.model.dto.control.SpeedControlDTO;
-import com.ard.tianyu.model.dto.converter.UavSignalInfoConverter;
-import com.ard.tianyu.model.dto.converter.UavStateConverter;
-import com.ard.tianyu.model.dto.converter.UavTelemetryDataConverter;
-import com.ard.tianyu.model.dto.manual.ReturnHomeDTO;
-import com.ard.tianyu.model.dto.manual.SwitchUavControlDTO;
-import com.ard.tianyu.model.dto.manual.TakeOffDTO;
-import com.ard.tianyu.model.dto.signal.SignalInfo;
-import com.ard.tianyu.model.dto.state.UavState;
-import com.ard.tianyu.model.dto.telemetry.TelemetryData;
-import com.ard.tianyu.model.receiver.*;
+import com.ard.tianyu.domain.dto.camera.*;
+import com.ard.tianyu.domain.dto.manual.*;
+import com.ard.tianyu.domain.dto.speaker.*;
+import com.ard.tianyu.domain.dto.wayline.*;
+import com.ard.tianyu.domain.receiver.*;
+import com.ard.tianyu.forest.TyClient;
+import com.ard.tianyu.domain.cache.TyTcpServerCache;
+import com.ard.tianyu.domain.cache.UavSignalInfoCache;
+import com.ard.tianyu.domain.cache.UavStateCache;
+import com.ard.tianyu.domain.cache.UavTelemetryDataCache;
+import com.ard.tianyu.domain.dto.BoxHistoryDTO;
+import com.ard.tianyu.domain.dto.BoxPhotosDTO;
+import com.ard.tianyu.domain.dto.BoxSettingsDTO;
+import com.ard.tianyu.domain.dto.control.AbsAngleControlDTO;
+import com.ard.tianyu.domain.dto.control.AngleControlDTO;
+import com.ard.tianyu.domain.dto.control.SpeedControlDTO;
+import com.ard.tianyu.domain.dto.converter.wayline.PlanLineDataConverter;
+import com.ard.tianyu.domain.dto.settings.ReturnHeightSettingDTO;
+import com.ard.tianyu.domain.dto.settings.ReturnPointSettingDTO;
+import com.ard.tianyu.domain.dto.settings.VideoStreamSettingDTO;
+import com.ard.tianyu.domain.dto.signal.SignalInfo;
+import com.ard.tianyu.domain.dto.state.UavState;
+import com.ard.tianyu.domain.dto.telemetry.TelemetryData;
 import com.ard.tianyu.netty.command.*;
 import com.ard.tianyu.netty.TyNettyTcpClient;
 import com.ard.tianyu.netty.command.camera.*;
@@ -36,7 +40,15 @@
 import com.ard.tianyu.netty.command.real.UavStateCmd;
 import com.ard.tianyu.netty.command.real.UavTelemetryDataCmd;
 import com.ard.tianyu.netty.command.register.*;
+import com.ard.tianyu.netty.command.settings.*;
+import com.ard.tianyu.netty.command.speaker.*;
+import com.ard.tianyu.netty.command.wayline.*;
+import com.ard.tianyu.netty.handler.ErrorCodeEnum;
+import com.ard.tianyu.netty.protobuf.TelemetryDataPB;
 import com.ard.tianyu.service.ITyService;
+import com.ard.tianyu.websocket.domain.UavControlType;
+import com.ard.tianyu.websocket.utils.WebSocketUtil;
+import com.ard.vtdu.service.IMediaService;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
 import lombok.extern.slf4j.Slf4j;
@@ -64,6 +76,9 @@
     @Resource
     private TyConfiguration tyConfiguration;
 
+    @Resource
+    private IMediaService mediaService;
+
     /**
      * 获取天宇token信息
      *
@@ -83,7 +98,7 @@
     public List<BoxInfoReceiver> getBoxList() {
         //请求云盒列表
         try {
-            TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList(tyConfiguration.getAccessToken().convertFromDTO());
+            TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList();
             return boxListReveiver.getContent();
         } catch (Exception e) {
             log.error("获取云盒列表失败:{}", e.getMessage());
@@ -99,7 +114,7 @@
     @Override
     public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
 
-        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn);
         return boxLiveUrl.getContent();
     }
 
@@ -110,7 +125,7 @@
      */
     @Override
     public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
-        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(boxHistoryDTO, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(boxHistoryDTO);
         return boxHistory.getContent();
     }
 
@@ -121,7 +136,7 @@
      */
     @Override
     public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
-        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(boxPhotosDTO, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(boxPhotosDTO);
         return boxPhotos.getContent();
     }
 
@@ -130,92 +145,114 @@
      */
     @Override
     public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
-        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(boxSettingsDTO, tyConfiguration.getAccessToken().convertFromDTO());
+        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(boxSettingsDTO);
         return result.getState() == 1;
+    }
+
+    /**
+     * 设置返航高度
+     */
+    @Override
+    public Boolean updateReturnHeight(ReturnHeightSettingDTO returnHeightSettingDTO) {
+        ReturnHeightSettingCmd returnHeightSettingCmd = new ReturnHeightSettingCmd(returnHeightSettingDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), returnHeightSettingCmd);
+        return true;
+    }
+
+    /**
+     * 设置返航点
+     */
+    @Override
+    public Boolean updateReturnPoint(ReturnPointSettingDTO returnPointSettingDTO) {
+        ReturnPointSettingCmd returnPointSettingCmd = new ReturnPointSettingCmd(returnPointSettingDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), returnPointSettingCmd);
+        return true;
+    }
+
+    /**
+     * 设置视频码流
+     */
+    @Override
+    public Boolean updateVideoStream(VideoStreamSettingDTO videoStreamSettingDTO) {
+        VideoStreamSettingCmd videoStreamSettingCmd = new VideoStreamSettingCmd(videoStreamSettingDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), videoStreamSettingCmd);
+        return true;
     }
 
     /**
      * 注册天宇平台
      */
     @Override
-    public void registerMessage() {
-        AccessTokenReceiver tyToken = tyConfiguration.getAccessToken();
-        RegisterCmd registerMessage = new RegisterCmd(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes());
-        this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
-    }
-
-    /**
-     * 发送消息
-     */
-    public void sendMessage(String key, BaseCmd msg) {
+    public Boolean registerMessage() {
         try {
-            ChannelFuture future = TyNettyTcpClient.channels.get(key).getChannelFuture();
-            Channel channel = future.channel();
-            if (channel.isActive()) {
-                InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
-                String host = ipSocket.getHostString();
-                int port = ipSocket.getPort();
-                channel.writeAndFlush(msg); // 发送编码后的数据
-                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
-            } else {
-                log.error("与天宇服务器链接断开,无法发送数据!");
-            }
+            AccessTokenReceiver tyToken = tyConfiguration.getAccessToken();
+            RegisterCmd registerMessage = new RegisterCmd(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes());
+            this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
         } catch (Exception e) {
-            log.error("发送消息异常:{}", e.getMessage());
+            log.error("注册天宇平台失败:{}", e.getMessage());
         }
+        return true;
     }
 
     /**
      * 云台控制基于角度
      */
+    @RequireUavControl(UavControlType.PAYLOAD)
     @Override
-    public void angleControl(AngleControlDTO angleControlDto) {
+    public Boolean angleControl(AngleControlDTO angleControlDto) {
         AngleControlCmd controlAngleCommand = new AngleControlCmd(angleControlDto);
         this.sendMessage(tyConfiguration.getIpPort(), controlAngleCommand);
+        return true;
     }
 
     /**
      * 云台绝对值控制(基于角度)
      */
+    @RequireUavControl(UavControlType.PAYLOAD)
     @Override
-    public void absAngleControl(AbsAngleControlDTO absAngleControlDto) {
+    public Boolean absAngleControl(AbsAngleControlDTO absAngleControlDto) {
         AbsAngleControlCmd absAngleControlCommand = new AbsAngleControlCmd(absAngleControlDto);
         this.sendMessage(tyConfiguration.getIpPort(), absAngleControlCommand);
+        return true;
     }
 
     /**
      * 云台转动(基于速度)
      */
+    @RequireUavControl(UavControlType.PAYLOAD)
     @Override
-    public void speedControl(SpeedControlDTO speedControlDto) {
+    public Boolean speedControl(SpeedControlDTO speedControlDto) {
         SpeedControlCmd speedControlCommand = new SpeedControlCmd(speedControlDto);
         this.sendMessage(tyConfiguration.getIpPort(), speedControlCommand);
+        return true;
     }
 
     /**
      * 设置相机模式
      */
     @Override
-    public void updateCameraMode(CameraModeDTO cameraModeDTO) {
+    public Boolean updateCameraMode(CameraModeDTO cameraModeDTO) {
         CameraModeCmd cameraModeCommand = new CameraModeCmd(cameraModeDTO);
         this.sendMessage(tyConfiguration.getIpPort(), cameraModeCommand);
+        return true;
     }
 
     /**
      * 拍照
      */
     @Override
-    public void capturePic(CapturePicDTO capturePicDto) {
+    public Boolean capturePic(CapturePicDTO capturePicDto) {
         updateCameraMode(CameraModeDTO.builder().boxSn(capturePicDto.getBoxSn()).actionParam((byte) 0x01).build());
         CapturePicCmd capturePicCommand = new CapturePicCmd(capturePicDto);
         this.sendMessage(tyConfiguration.getIpPort(), capturePicCommand);
+        return true;
     }
 
     /**
      * 录像
      */
     @Override
-    public void record(RecordDTO recordDto) {
+    public Boolean record(RecordDTO recordDto) {
         BaseCmd baseCmd;
         if (recordDto.getIsRecord()) {
             updateCameraMode(CameraModeDTO.builder().boxSn(recordDto.getBoxSn()).actionParam((byte) 0x02).build());
@@ -224,78 +261,168 @@
             baseCmd = new RecordStopCmd(recordDto);
         }
         this.sendMessage(tyConfiguration.getIpPort(), baseCmd);
+        return true;
     }
 
     /**
      * 变倍
      */
     @Override
-    public void zoom(ZoomDTO zoomDTO) {
+    public Boolean zoom(ZoomDTO zoomDTO) {
         ZoomCmd zoomCommand = new ZoomCmd(zoomDTO);
         this.sendMessage(tyConfiguration.getIpPort(), zoomCommand);
+        return true;
     }
 
     /**
      * 变倍到指定倍数
      */
     @Override
-    public void zoomToLevel(ZoomToLevelDTO zoomToLevelDTO) {
+    public Boolean zoomToLevel(ZoomToLevelDTO zoomToLevelDTO) {
         ZoomToLevelCmd zoomToLevelCommand = new ZoomToLevelCmd(zoomToLevelDTO);
         this.sendMessage(tyConfiguration.getIpPort(), zoomToLevelCommand);
+        return true;
     }
 
     /**
      * 切换摄像头
      */
     @Override
-    public void switchCamera(SwitchCameraDTO switchCameraDTO) {
+    public Boolean switchCamera(SwitchCameraDTO switchCameraDTO) {
         SwitchCameraCmd switchCameraCmd = new SwitchCameraCmd(switchCameraDTO);
         this.sendMessage(tyConfiguration.getIpPort(), switchCameraCmd);
+        return true;
     }
 
     /**
      * 切换视频源
      */
     @Override
-    public void switchSource(SwitchSourceDTO switchSourceDTO) {
+    public Boolean switchSource(SwitchSourceDTO switchSourceDTO) {
         SwitchSourceCmd switchSourceCmd = new SwitchSourceCmd(switchSourceDTO);
         this.sendMessage(tyConfiguration.getIpPort(), switchSourceCmd);
+        return true;
     }
 
     /**
      * 打开或关闭照片回传
      */
     @Override
-    public void photoReturnToggle(PhotoReturnToggleDTO photoReturnToggleDTO) {
+    public Boolean photoReturnToggle(PhotoReturnToggleDTO photoReturnToggleDTO) {
         PhotoReturnToggleCmd photoReturnToggleCmd = new PhotoReturnToggleCmd(photoReturnToggleDTO);
         this.sendMessage(tyConfiguration.getIpPort(), photoReturnToggleCmd);
+        return true;
     }
 
     /**
      * 切换无人机控制权
      */
     @Override
-    public void switchUavControl(SwitchUavControlDTO switchUavControlDTO) {
+    public Boolean switchUavControl(SwitchUavControlDTO switchUavControlDTO) {
         SwitchUavControlCmd switchUavControlCmd = new SwitchUavControlCmd(switchUavControlDTO);
         this.sendMessage(tyConfiguration.getIpPort(), switchUavControlCmd);
+        return true;
     }
 
     /**
      * 起飞
      */
+    @RequireUavControl(UavControlType.FLY)
     @Override
-    public void takeOff(TakeOffDTO takeOffDTO) {
+    public Boolean takeOff(TakeOffDTO takeOffDTO) {
         TakeOffCmd takeOffCmd = new TakeOffCmd(takeOffDTO);
         this.sendMessage(tyConfiguration.getIpPort(), takeOffCmd);
+        return true;
     }
 
     /**
      * 返航
      */
     @Override
-    public void returnHome(ReturnHomeDTO returnHomeDTO) {
-        ReturnHomeCmd returnHomeCmd=new ReturnHomeCmd(returnHomeDTO);
+    public Boolean returnHome(ReturnHomeDTO returnHomeDTO) {
+        ReturnHomeCmd returnHomeCmd = new ReturnHomeCmd(returnHomeDTO);
         this.sendMessage(tyConfiguration.getIpPort(), returnHomeCmd);
+        return true;
+    }
+
+    /**
+     * 取消返航
+     */
+    @Override
+    public Boolean returnHomeCancel(ReturnHomeCancelDTO returnHomeCancelDTO) {
+        ReturnHomeCancelCmd returnHomeCancelCmd = new ReturnHomeCancelCmd(returnHomeCancelDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), returnHomeCancelCmd);
+        return true;
+    }
+
+    /**
+     * 降落
+     */
+    @Override
+    public Boolean land(LandDTO landDTO) {
+        LandCmd landCmd = new LandCmd(landDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), landCmd);
+        return true;
+    }
+
+    /**
+     * 取消降落
+     */
+    @Override
+    public Boolean landCancel(LandCancelDTO landCancelDTO) {
+        LandCancelCmd landCancelCmd = new LandCancelCmd(landCancelDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), landCancelCmd);
+        return true;
+    }
+
+    /**
+     * 强制降落
+     */
+    @Override
+    public Boolean landForced(LandForcedDTO landForcedDTO) {
+        LandForcedCmd landForcedCmd = new LandForcedCmd(landForcedDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), landForcedCmd);
+        return true;
+    }
+
+    /**
+     * 方向控制
+     */
+    @Override
+    public Boolean flyControl(FlyControlDTO flyControlDTO) {
+        FlyControlCmd flyControlCmd = new FlyControlCmd(flyControlDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), flyControlCmd);
+        return true;
+    }
+
+    /**
+     * 指点飞行控制
+     */
+    @Override
+    public Boolean flyPointControl(FlyPointControlDTO flyPointControlDTO) {
+        FlyPointControlCmd flyPointControlCmd = new FlyPointControlCmd(flyPointControlDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), flyPointControlCmd);
+        return true;
+    }
+
+    /**
+     * 停止指点飞行控制
+     */
+    @Override
+    public Boolean flyPointControlCancel(FlyPointControlCancelDTO flyPointControlCancelDTO) {
+        FlyPointControlCancelCmd flyPointControlCancelCmd = new FlyPointControlCancelCmd(flyPointControlCancelDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), flyPointControlCancelCmd);
+        return true;
+    }
+
+    /**
+     * 紧急制动
+     */
+    @Override
+    public Boolean emeragencyBrake(EmeragencyBrakeDTO emeragencyBrakeDTO) {
+        EmergencyBrakeCmd emergencyBrakeCmd = new EmergencyBrakeCmd(emeragencyBrakeDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), emergencyBrakeCmd);
+        return true;
     }
 
     /**
@@ -323,16 +450,159 @@
     }
 
     /**
+     * 喊话模式切换
+     */
+    @Override
+    public void speakModeSwitch(SpeakModeSwitchDTO speakModeSwitchDTO) {
+        SpeakModeSwitchCmd speakModeSwitchCmd = new SpeakModeSwitchCmd(speakModeSwitchDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), speakModeSwitchCmd);
+    }
+
+    /**
+     * 文字喊话
+     */
+    @Override
+    public void textSpeak(TextSpeakDTO textSpeakDTO) {
+        TextSpeakCmd textSpeakCmd = new TextSpeakCmd(textSpeakDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), textSpeakCmd);
+    }
+
+    /**
+     * 实时喊话
+     */
+    @Override
+    public void realSpeak(RealSpeakDTO realSpeakDTO) {
+        RealSpeakCmd realSpeakCmd = new RealSpeakCmd(realSpeakDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), realSpeakCmd);
+    }
+
+    /**
+     * 录音喊话
+     */
+    @Override
+    public void recordSpeak(RecordSpeakDTO recordSpeakDTO) {
+        RecordSpeakCmd recordSpeakCmd = new RecordSpeakCmd(recordSpeakDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), recordSpeakCmd);
+    }
+
+    /**
+     * 设置音量
+     */
+    @Override
+    public void volumeSetting(VolumeSettingDTO volumeSettingDTO) {
+        VolumeSettingCmd volumeSettingCmd = new VolumeSettingCmd(volumeSettingDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), volumeSettingCmd);
+    }
+
+    /**
+     * 设置循环播放
+     */
+    @Override
+    public void loopSetting(LoopSettingDTO loopSettingDTO) {
+        LoopSettingCmd loopSettingCmd = new LoopSettingCmd(loopSettingDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), loopSettingCmd);
+    }
+
+    /**
+     * 停止喊话
+     */
+    @Override
+    public void stopSpeak(StopSpeakDTO stopSpeakDTO) {
+        StopSpeakCmd stopSpeakCmd = new StopSpeakCmd(stopSpeakDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), stopSpeakCmd);
+    }
+
+    /**
+     * 航线上传
+     */
+    @Override
+    public void wayLineUpload(WayLineUploadDTO wayLineUploadDTO) {
+        wayLineUploadDTO.setWayLineData(this.CreatePlanLineData());//设置航线数据
+        WayLineUploadCmd wayLineUploadCmd = new WayLineUploadCmd(wayLineUploadDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), wayLineUploadCmd);
+    }
+
+    //创建航线
+    private PlanLineData CreatePlanLineData() {
+        TelemetryDataPB.PointData pointData1 = TelemetryDataPB.PointData.newBuilder()
+                .setLng(124.12345).setLat(45.12345).setHeight(80).setSpeed(10)
+                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
+                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
+                .build();
+        TelemetryDataPB.PointData pointData2 = TelemetryDataPB.PointData.newBuilder()
+                .setLng(124.22222).setLat(45.22222).setHeight(80).setSpeed(10)
+                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
+                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
+                .build();
+        TelemetryDataPB.PointData pointData3 = TelemetryDataPB.PointData.newBuilder()
+                .setLng(124.33333).setLat(45.33333).setHeight(80).setSpeed(10)
+                .setFlightPathMode(1)//航线飞行模式:1=直线飞行,2=协调转弯,3=曲线飞行(停),4=曲线飞行(不停)
+                .setSafeGohomeFlag(0)//是否为安全返航点 0=不是,1=是
+                .build();
+
+        //创建航线
+        TelemetryDataPB.PlanLineData PlanLineDataPB = TelemetryDataPB.PlanLineData.newBuilder()
+                .setFinishedAction(2)//结束航线后动作:1=返航,2=原地悬停,3=原地降落,4=原地绕飞
+                .setMaxSpeed(10)//最大飞行速度
+                .setAutoSpeed(10)//自动飞行速度
+                .addPoints(pointData1)//航线点1
+                .addPoints(pointData2)//航线点2
+                .addPoints(pointData3)//航线点3
+                .setLoseAction(1)//网络失联后动作:0=返回HOME点,1=继续航线,默认0
+                .setHomeHeight(6)//返航高度:(单位:米,范围20~1500)
+                .setIsSaveEnergyMode(0)////是否开启节能模式:0=不是,1=是(说明:节能模式下,飞机沿最短直线距离由起飞点到航线第一个航点,非升高到第一个航点高度再飞往第一个航点)
+                .build();
+
+        return PlanLineDataConverter.convertProtoToEntity(PlanLineDataPB);
+    }
+
+    /**
+     * 航线开始
+     */
+    @Override
+    public void wayLineBegin(WayLineBeginDTO wayLineBeginDTO) {
+        WayLineBeginCmd wayLineBeginCmd = new WayLineBeginCmd(wayLineBeginDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), wayLineBeginCmd);
+    }
+
+    /**
+     * 航线暂停
+     */
+    @Override
+    public void wayLinePause(WayLinePauseDTO wayLinePauseDTO) {
+        WayLinePauseCmd wayLinePauseCmd = new WayLinePauseCmd(wayLinePauseDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), wayLinePauseCmd);
+    }
+
+    /**
+     * 航线恢复
+     */
+    @Override
+    public void wayLineResume(WayLineResumeDTO wayLineResumeDTO) {
+        WayLineResumeCmd wayLineResumeCmd = new WayLineResumeCmd(wayLineResumeDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), wayLineResumeCmd);
+    }
+
+    /**
+     * 航线结束
+     */
+    @Override
+    public void wayLineEnd(WayLineEndDTO wayLineEndDTO) {
+        WayLineEndCmd wayLineEndCmd = new WayLineEndCmd(wayLineEndDTO);
+        this.sendMessage(tyConfiguration.getIpPort(), wayLineEndCmd);
+    }
+
+    /**
      * 处理天宇服务器返回的消息
      */
     @Override
-    public void processMessage(String key, BaseCmd tyCommand) {
+    public void processReplyMessage(String key, BaseCmd tyCommand) {
         byte cmdCode = tyCommand.getCmdCode();
         switch (cmdCode) {
             case 0x01:
                 // 处理心跳消息
                 RegisterReplyCmd registerReplyCommand = (RegisterReplyCmd) tyCommand;
-                log.debug("收到注册消息-->处理注册消息:{}", registerReplyCommand.getSuccess());
+                logPrint("注册消息", registerReplyCommand.getSuccess());
                 if (registerReplyCommand.getSuccess() != 1) {
                     // 注册失败,重新注册
                     log.info("注册失败,重新注册");
@@ -342,7 +612,7 @@
             case 0x02:
                 // 处理注册消息
                 HeartbeatReplyCmd heartbeatReplyCmd = (HeartbeatReplyCmd) tyCommand;
-                log.debug("收到心跳消息-->处理心跳消息:{}", heartbeatReplyCmd.getTimestamp());
+                logPrint("心跳消息", heartbeatReplyCmd.getTimestamp());
                 break;
             case 0x03:
                 // 处理订阅消息
@@ -353,28 +623,28 @@
             case 0x0A:
                 // 处理云盒开关机通知消息
                 BoxPowerStatusReplyCmd boxPowerStatusReplyCmd = (BoxPowerStatusReplyCmd) tyCommand;
-                log.debug("收到云盒开关机通知消息-->状态:{}", boxPowerStatusReplyCmd.getStatus());
+                logPrint("云盒开关机通知消息", boxPowerStatusReplyCmd.getStatus());
                 break;
             case (byte) 0xA8:
                 // 处理无人机状态消息
-                log.debug("收到无人机状态消息-->无人机状态消息");
                 UavStateCmd uavStateCmd = (UavStateCmd) tyCommand;
-                UavState uavState = UavStateConverter.convertProtoToEntity(uavStateCmd.getUavState());
-                UavStateCache.putUavState(uavState);
+                UavStateCache.putUavState(uavStateCmd.getUavState());
+                logPrint("无人机状态", uavStateCmd.getUavState().getBoxSn());
+               // WebSocketUtil.sendMessageAll(uavStateCmd.getUavState());
                 break;
             case (byte) 0xA9:
                 // 处理无人机遥测消息
-                log.debug("收到无人机遥测消息-->无人机遥测消息");
                 UavTelemetryDataCmd uavTelemetryDataCmd = (UavTelemetryDataCmd) tyCommand;
-                TelemetryData telemetryData = UavTelemetryDataConverter.convertProtoToEntity(uavTelemetryDataCmd.getTelemetryData());
-                UavTelemetryDataCache.putUavTelemetryData(telemetryData);
+                UavTelemetryDataCache.putUavTelemetryData(uavTelemetryDataCmd.getTelemetryData());
+                logPrint("无人机遥测", uavTelemetryDataCmd.getTelemetryData().getBoxSn());
+                //WebSocketUtil.sendMessageAll(uavTelemetryDataCmd.getTelemetryData());
                 break;
+
             case (byte) 0x0C:
                 // 处理无人机信道质量数据
-                log.debug("收到无人机信道质量数据消息-->无人机信道质量数据消息");
                 SignalInfoCmd signalInfoCmd = (SignalInfoCmd) tyCommand;
-                SignalInfo signalInfo = UavSignalInfoConverter.convertProtoToEntity(signalInfoCmd.getSignalInfo());
-                UavSignalInfoCache.putUavSignalInfoCache(signalInfo);
+                UavSignalInfoCache.putUavSignalInfoCache(signalInfoCmd.getSignalInfo());
+                logPrint("无人机信道质量", signalInfoCmd.getSignalInfo().getBoxSn());
                 break;
             case (byte) 0xD1:
                 // 处理无人机
@@ -382,47 +652,133 @@
                     UavInitReplyCmd uavInitReplyCmd = (UavInitReplyCmd) tyCommand;
                     UavInitInfoCmd uavInitCommand = new UavInitInfoCmd(uavInitReplyCmd.getBoxSn());
                     this.sendMessage(tyConfiguration.getIpPort(), uavInitCommand);
-                    log.info("收到无人机初始化详情-回复防止再次发送");
+                    logPrint("无人机初始化详情", uavInitCommand.getBoxSn());
                 }
                 if (tyCommand instanceof UavReadyReplyCmd) {
                     UavReadyReplyCmd uavReadyReplyCmd = (UavReadyReplyCmd) tyCommand;
-                    log.info("收到无人机准备完成通知,电池电量{} 经度{} 纬度{} 海拔{}", uavReadyReplyCmd.getBatteryPower(), uavReadyReplyCmd.getLongitude(), uavReadyReplyCmd.getLatitude(), uavReadyReplyCmd.getAltitude());
+                    logPrint("无人机准备完成通知", uavReadyReplyCmd.getBatteryPower());
                 }
                 if (tyCommand instanceof CameraModeReplyCmd) {
                     CameraModeReplyCmd cameraModeReplyCommand = (CameraModeReplyCmd) tyCommand;
-                    log.info("收到无人机设置相机模式回复:{} 错误码:{}", String.format("0x%02X", cameraModeReplyCommand.getResult() & 0xFF), cameraModeReplyCommand.getErrorCode());
+                    logPrint("无人机设置相机模式", cameraModeReplyCommand.getResult(), cameraModeReplyCommand.getErrorCode());
                 }
                 if (tyCommand instanceof FlyWayEndReplyCmd) {
                     FlyWayEndReplyCmd flyWayEndReplyCmd = (FlyWayEndReplyCmd) tyCommand;
-                    log.info("收到无人机飞行结束通知:状态:{}", flyWayEndReplyCmd.getResult());
+                    logPrint("无人机飞行结束", flyWayEndReplyCmd.getResult(), flyWayEndReplyCmd.getErrorCode());
                 }
                 if (tyCommand instanceof CapturePicReplyCmd) {
                     CapturePicReplyCmd capturePicReplyCommand = (CapturePicReplyCmd) tyCommand;
-                    log.info("收到无人机拍照回复:{} 错误码:{}", String.format("0x%02X", capturePicReplyCommand.getResult() & 0xFF), capturePicReplyCommand.getErrorCode());
+                    logPrint("无人机拍照", capturePicReplyCommand.getResult(), capturePicReplyCommand.getErrorCode());
                 }
                 if (tyCommand instanceof RecordStartReplyCmd) {
                     RecordStartReplyCmd recordStartReplyCommand = (RecordStartReplyCmd) tyCommand;
-                    log.info("收到无人机开始录像回复:{} 错误码:{}", String.format("0x%02X", recordStartReplyCommand.getResult() & 0xFF), recordStartReplyCommand.getErrorCode());
+                    logPrint("无人机开始录像", recordStartReplyCommand.getResult(), recordStartReplyCommand.getErrorCode());
                 }
                 if (tyCommand instanceof RecordStopReplyCmd) {
                     RecordStopReplyCmd recordStopReplyCommand = (RecordStopReplyCmd) tyCommand;
-                    log.info("收到无人机停止录像回复:{} 错误码:{}", String.format("0x%02X", recordStopReplyCommand.getResult() & 0xFF), recordStopReplyCommand.getErrorCode());
+                    logPrint("无人机停止录像", recordStopReplyCommand.getResult(), recordStopReplyCommand.getErrorCode());
                 }
                 //起飞回复
                 if (tyCommand instanceof TakeOffReplyCmd) {
                     TakeOffReplyCmd takeOffReplyCommand = (TakeOffReplyCmd) tyCommand;
-                    log.info("收到无人机起飞回复:{} 错误码:{}", String.format("0x%02X", takeOffReplyCommand.getResult() & 0xFF), takeOffReplyCommand.getErrorCode());
+                    logPrint("无人机起飞", takeOffReplyCommand.getResult(), takeOffReplyCommand.getErrorCode());
                 }
                 //返航回复
                 if (tyCommand instanceof ReturnHomeReplyCmd) {
                     ReturnHomeReplyCmd returnHomeReplyCmd = (ReturnHomeReplyCmd) tyCommand;
-                    log.info("收到无人机返航回复:{} 错误码:{}", String.format("0x%02X", returnHomeReplyCmd.getResult() & 0xFF), returnHomeReplyCmd.getErrorCode());
+                    logPrint("无人机返航", returnHomeReplyCmd.getResult(), returnHomeReplyCmd.getErrorCode());
+                }
+                //取消返航回复
+                if (tyCommand instanceof ReturnHomeCancelReplyCmd) {
+                    ReturnHomeCancelReplyCmd returnHomeCancelReplyCmd = (ReturnHomeCancelReplyCmd) tyCommand;
+                    logPrint("取消无人机返航", returnHomeCancelReplyCmd.getResult(), returnHomeCancelReplyCmd.getErrorCode());
+                }
+                //降落回复
+                if (tyCommand instanceof LandReplyCmd) {
+                    LandReplyCmd landReplyCmd = (LandReplyCmd) tyCommand;
+                    logPrint("降落", landReplyCmd.getResult(), landReplyCmd.getErrorCode());
+                }
+                //取消降落回复
+                if (tyCommand instanceof LandCancelReplyCmd) {
+                    LandCancelReplyCmd landCancelReplyCmd = (LandCancelReplyCmd) tyCommand;
+                    logPrint("取消降落", landCancelReplyCmd.getResult(), landCancelReplyCmd.getErrorCode());
+                }
+                //强制降落回复
+                if (tyCommand instanceof LandForcedReplyCmd) {
+                    LandForcedReplyCmd landForcedReplyCmd = (LandForcedReplyCmd) tyCommand;
+                    logPrint("强制降落", landForcedReplyCmd.getResult(), landForcedReplyCmd.getErrorCode());
+                }
+                //到达指点飞行位置回复
+                if (tyCommand instanceof PointControlReachedReplyCmd) {
+                    PointControlReachedReplyCmd pointControlReachedReplyCmd = (PointControlReachedReplyCmd) tyCommand;
+                    logPrint("到达指点飞行位置", pointControlReachedReplyCmd.getResult(), pointControlReachedReplyCmd.getErrorCode());
+                }
+                //紧急制动回复
+                if (tyCommand instanceof EmergencyBrakeReplyCmd) {
+                    EmergencyBrakeReplyCmd emergencyBrakeReplyCmd = (EmergencyBrakeReplyCmd) tyCommand;
+                    logPrint("紧急制动", emergencyBrakeReplyCmd.getResult(), emergencyBrakeReplyCmd.getErrorCode());
+                }
+                //上传航线回复
+                if (tyCommand instanceof WayLineUploadReplyCmd) {
+                    WayLineUploadReplyCmd wayLineUploadReplyCmd = (WayLineUploadReplyCmd) tyCommand;
+                    logPrint("上传航线", wayLineUploadReplyCmd.getResult(), wayLineUploadReplyCmd.getErrorCode());
+                }
+                //开始航线回复
+                if (tyCommand instanceof WayLineBeginReplyCmd) {
+                    WayLineBeginReplyCmd wayLineBeginReplyCmd = (WayLineBeginReplyCmd) tyCommand;
+                    logPrint("开始航线", wayLineBeginReplyCmd.getResult(), wayLineBeginReplyCmd.getErrorCode());
+                }
+                //暂停航线回复
+                if (tyCommand instanceof WayLinePauseReplyCmd) {
+                    WayLinePauseReplyCmd wayLinePauseReplyCmd = (WayLinePauseReplyCmd) tyCommand;
+                    logPrint("暂停航线", wayLinePauseReplyCmd.getResult(), wayLinePauseReplyCmd.getErrorCode());
+                }
+                //恢复航线回复
+                if (tyCommand instanceof WayLineResumeReplyCmd) {
+                    WayLineResumeReplyCmd wayLineResumeReplyCmd = (WayLineResumeReplyCmd) tyCommand;
+                    logPrint("恢复航线", wayLineResumeReplyCmd.getResult(), wayLineResumeReplyCmd.getErrorCode());
+                }
+                //结束航线回复
+                if (tyCommand instanceof WayLineEndReplyCmd) {
+                    WayLineEndReplyCmd wayLineEndReplyCmd = (WayLineEndReplyCmd) tyCommand;
+                    logPrint("结束航线", wayLineEndReplyCmd.getResult(), wayLineEndReplyCmd.getErrorCode());
+                }
+                //设置返航高度回复
+                if (tyCommand instanceof ReturnHeightSettingReplyCmd) {
+                    ReturnHeightSettingReplyCmd returnHeightSettingReplyCmd = (ReturnHeightSettingReplyCmd) tyCommand;
+                    logPrint("设置返航高度", returnHeightSettingReplyCmd.getResult(), returnHeightSettingReplyCmd.getErrorCode());
+                }
+                //设置返航点回复
+                if (tyCommand instanceof ReturnPointSettingReplyCmd) {
+                    ReturnPointSettingReplyCmd returnPointSettingReplyCmd = (ReturnPointSettingReplyCmd) tyCommand;
+                    logPrint("设置返航点", returnPointSettingReplyCmd.getResult(), returnPointSettingReplyCmd.getErrorCode());
+                }
+                break;
+            case (byte) 0xD2:
+                //设置视频码流回复
+                if (tyCommand instanceof VideoStreamSettingReplyCmd) {
+                    VideoStreamSettingReplyCmd videoStreamSettingReplyCmd = (VideoStreamSettingReplyCmd) tyCommand;
+                    logPrint("设置视频码流", videoStreamSettingReplyCmd.getResult(), videoStreamSettingReplyCmd.getErrorCode());
                 }
                 break;
             default:
                 // 处理其他消息
-                log.debug("收到其他消息-->其他消息");
+                log.warn("收到其他消息-->{}", tyCommand.getCmdCode());
         }
+    }
+
+    //公共日志打印
+    private void logPrint(String message, Object obj) {
+        log.debug("收到{}回复{}", message, obj);
+    }
+
+    //公共日志打印
+    private void logPrint(String message, byte result, Integer errorCode) {
+        log.info("收到{}回复:{} 错误码:{}【{}】",
+                message,
+                ByteUtils.byteToHex(result),
+                errorCode,
+                ErrorCodeEnum.getDescByCode(errorCode));
     }
 
     /**
@@ -430,9 +786,34 @@
      */
     @Scheduled(fixedRate = 3000)
     public void sendHeartBeat() {
+        if (!TyTcpServerCache.getTcpServerCache(tyConfiguration.getTcpserver() + ":" + tyConfiguration.getTcpport())) {
+            return;
+        }
         long timestamp = System.currentTimeMillis();
         log.debug("定时向天宇发送⼼跳,时间戳{}", timestamp);
         HeartbeatCmd heartbeatMessage = new HeartbeatCmd(timestamp);
         this.sendMessage(tyConfiguration.getIpPort(), heartbeatMessage);
     }
+
+    /**
+     * 发送消息
+     */
+    private void sendMessage(String key, BaseCmd msg) {
+        try {
+            ChannelFuture future = TyNettyTcpClient.channels.get(key).getChannelFuture();
+            Channel channel = future.channel();
+            if (channel.isActive()) {
+                InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
+                String host = ipSocket.getHostString();
+                int port = ipSocket.getPort();
+                channel.writeAndFlush(msg); // 发送编码后的数据
+                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
+            } else {
+                log.error("与天宇服务器链接断开,无法发送数据!");
+            }
+        } catch (Exception e) {
+            log.error("发送消息异常:{}", e.getMessage());
+        }
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3