From 35f1a08b31531d971169403978cddf3328accb26 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Thu, 20 Feb 2025 16:01:06 +0800
Subject: [PATCH] 添加无人机控制权申请、处理、释放

---
 ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java |  237 +++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 142 insertions(+), 95 deletions(-)

diff --git a/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java b/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
index db9c7d1..a9cea50 100644
--- a/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
+++ b/ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
@@ -1,10 +1,18 @@
 package com.ard.tianyu.netty.handler;// MessageProtocolDecoder.java
 
-import com.ard.tianyu.netty.command.*;
+import com.ard.common.utils.ByteUtils;
+import com.ard.tianyu.model.dto.converter.realdata.UavSignalInfoConverter;
+import com.ard.tianyu.model.dto.converter.realdata.UavStateConverter;
+import com.ard.tianyu.model.dto.converter.realdata.UavTelemetryDataConverter;
+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.netty.command.BaseCmd;
 import com.ard.tianyu.netty.command.camera.CameraModeReplyCmd;
 import com.ard.tianyu.netty.command.camera.CapturePicReplyCmd;
 import com.ard.tianyu.netty.command.camera.RecordStartReplyCmd;
 import com.ard.tianyu.netty.command.camera.RecordStopReplyCmd;
+import com.ard.tianyu.netty.command.manual.*;
 import com.ard.tianyu.netty.command.notice.BoxPowerStatusReplyCmd;
 import com.ard.tianyu.netty.command.notice.FlyWayEndReplyCmd;
 import com.ard.tianyu.netty.command.notice.UavReadyReplyCmd;
@@ -14,12 +22,16 @@
 import com.ard.tianyu.netty.command.register.HeartbeatReplyCmd;
 import com.ard.tianyu.netty.command.register.RegisterReplyCmd;
 import com.ard.tianyu.netty.command.register.UavInitReplyCmd;
+import com.ard.tianyu.netty.command.settings.ReturnHeightSettingReplyCmd;
+import com.ard.tianyu.netty.command.settings.ReturnPointSettingReplyCmd;
+import com.ard.tianyu.netty.command.settings.VideoStreamSettingReplyCmd;
+import com.ard.tianyu.netty.command.wayline.*;
 import com.ard.tianyu.netty.protobuf.TelemetryDataPB;
+import com.google.protobuf.InvalidProtocolBufferException;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.ByteToMessageDecoder;
 import lombok.extern.slf4j.Slf4j;
-
 import java.util.List;
 
 @Slf4j
@@ -44,6 +56,7 @@
         }
         byte cmdCode = in.readByte();
         BaseCmd baseCmd = null;
+        byte actionCode;
         switch (cmdCode) {
             case 0x01:
                 byte success = in.readByte(); // 读取是否成功
@@ -55,125 +68,159 @@
                 baseCmd = new HeartbeatReplyCmd(timestamp);
                 log.debug("心跳回复: 时间戳 {}", timestamp);
                 break;
-            case 0x03:
-            case 0x04:
-                break;
             case 0x0A:
                 byte status = in.readByte();
                 baseCmd = new BoxPowerStatusReplyCmd(status);
-                log.debug("云盒开机/关机通知回复: 状态 {}", String.format("0x%02X", status & 0xFF));
+                log.debug("云盒开机/关机通知回复: 状态 {}", ByteUtils.byteToHex(status));
                 break;
             case (byte) 0xA8:
                 byte[] uavStatus = new byte[dataLength - 1];
                 in.readBytes(uavStatus);  // 读取 ProtoBuf 数据
-                TelemetryDataPB.UavState uavState = TelemetryDataPB.UavState.parseFrom(uavStatus);
+                TelemetryDataPB.UavState uavStatePB = TelemetryDataPB.UavState.parseFrom(uavStatus);
+                UavState uavState = UavStateConverter.convertProtoToEntity(uavStatePB);
                 baseCmd = new UavStateCmd(uavState);
                 log.debug("无人机状态数据: {}", uavState.getBoxSn());
                 break;
             case (byte) 0xA9:
-                byte[] uavData = new byte[dataLength - 1];
-                in.readBytes(uavData);
-                TelemetryDataPB.TelemetryData telemetryData = TelemetryDataPB.TelemetryData.parseFrom(uavData);
+                byte[] telemetryDataBytes = new byte[dataLength - 1];
+                in.readBytes(telemetryDataBytes);
+                TelemetryDataPB.TelemetryData telemetryDataPB = TelemetryDataPB.TelemetryData.parseFrom(telemetryDataBytes);
+                TelemetryData telemetryData = UavTelemetryDataConverter.convertProtoToEntity(telemetryDataPB);
                 baseCmd = new UavTelemetryDataCmd(telemetryData);
                 log.debug("遥测数据: {}", telemetryData.getBoxSn());
                 break;
             case (byte) 0x0C:
                 int delayTime = in.readInt();
-                uavData = new byte[15];
-                in.readBytes(uavData);
-                //  String boxSn12 = new String(uavData);
-                uavData = new byte[dataLength - 20];
-                in.readBytes(uavData);
-                TelemetryDataPB.SignalInfo signalInfo = TelemetryDataPB.SignalInfo.parseFrom(uavData);
+                byte[] signalInfoBytes = new byte[15];
+                in.readBytes(signalInfoBytes);
+                signalInfoBytes = new byte[dataLength - 20];
+                in.readBytes(signalInfoBytes);
+                TelemetryDataPB.SignalInfo signalInfoPB = TelemetryDataPB.SignalInfo.parseFrom(signalInfoBytes);
+                SignalInfo signalInfo =UavSignalInfoConverter.convertProtoToEntity(signalInfoPB);
                 baseCmd = new SignalInfoCmd(signalInfo);
                 log.debug("信道质量数据: {}", delayTime);
                 break;
             case (byte) 0xD1:
-                byte encryptionFlag = in.readByte();
-                byte actionCode = in.readByte();
-                switch (actionCode) {
-                    case 0x22:
-                        byte result = in.readByte();
-                        int errorCode = in.readInt();
-                        uavData = new byte[15];
-                        in.readBytes(uavData);
-                        String boxSn = new String(uavData);
-                        baseCmd = new CameraModeReplyCmd(boxSn, result, errorCode);
-                        log.debug("设置相机模式回复: {}", boxSn);
-                        break;
-                    case 0x23:
-                        result = in.readByte();
-                        errorCode = in.readInt();
-                        uavData = new byte[15];
-                        in.readBytes(uavData);
-                        boxSn = new String(uavData);
-                        baseCmd = new CapturePicReplyCmd(boxSn, result, errorCode);
-                        log.debug("拍照回复: {}", boxSn);
-                        break;
-                    case 0x24:
-                        result = in.readByte();
-                        errorCode = in.readInt();
-                        uavData = new byte[15];
-                        in.readBytes(uavData);
-                        boxSn = new String(uavData);
-                        baseCmd = new RecordStartReplyCmd(boxSn, result, errorCode);
-                        log.debug("开始录像回复: {}", boxSn);
-                        break;
-                    case 0x25:
-                        result = in.readByte();
-                        errorCode = in.readInt();
-                        uavData = new byte[15];
-                        in.readBytes(uavData);
-                        boxSn = new String(uavData);
-                        baseCmd = new RecordStopReplyCmd(boxSn, result, errorCode);
-                        log.debug("停止录像回复: {}", boxSn);
-                        break;
-                    case 0x2E:
-                        result = in.readByte();
-                        errorCode = in.readInt();
-                        uavData = new byte[15];
-                        in.readBytes(uavData);
-                        boxSn = new String(uavData);
-                        baseCmd = new FlyWayEndReplyCmd(boxSn, result, errorCode);
-                        log.debug("航线结束通知回复: {}", boxSn);
-                        break;
-                    case 0x3D:
-                        byte batteryPower = in.readByte();
-                        double longitude = in.readDouble();
-                        double latitude = in.readDouble();
-                        int altitude = in.readInt();
-                        uavData = new byte[dataLength - 24];
-                        in.readBytes(uavData);
-                        boxSn = new String(uavData);
-                        baseCmd = new UavReadyReplyCmd(boxSn, batteryPower, longitude, latitude, altitude);
-                        log.debug("无人机准备完成通知回复: 电池电量 {}", String.format("0x%02X", batteryPower & 0xFF));
-                        break;
-                    case 0x42:
-                        byte[] uavData1 = new byte[dataLength - 18];
-                        in.readBytes(uavData1);
-                        byte[] uavData2 = new byte[15];
-                        in.readBytes(uavData2);
-                        boxSn = new String(uavData2);
-                        TelemetryDataPB.UavInitInfo uavInitInfo = TelemetryDataPB.UavInitInfo.parseFrom(uavData1);
-                        baseCmd = new UavInitReplyCmd(boxSn, uavInitInfo);
-                        log.debug("无人机初始化数据: {}", boxSn);
-                        break;
-                    default:
-                        log.warn("无法识别的命令码动作编号:{}", String.format("0x%02X", actionCode & 0xFF));
-                        break;
-                }
+                in.readByte();//加密标志
+                actionCode = in.readByte();//动作编号
+                baseCmd = processD1ReplyCmd(in, actionCode);
+                break;
+            case (byte) 0xD2:
+                in.readByte();//加密标志
+                actionCode = in.readByte();//动作编号
+                baseCmd = processD2ReplyCmd(in, actionCode);
                 break;
             default:
-                encryptionFlag = in.readByte();
+                in.readByte();//加密标志
                 actionCode = in.readByte();
-                log.warn("无法识别的命令码: {},加密标志:{},动作编号:{}",
-                        String.format("0x%02X", cmdCode & 0xFF),
-                        String.format("0x%02X", encryptionFlag & 0xFF),
-                        String.format("0x%02X", actionCode & 0xFF));//String.format("0x%02X", cmdCode & 0xFF)
-
+                log.warn("无法识别的命令码: {},动作编号:{}",
+                        ByteUtils.byteToHex(cmdCode),
+                        ByteUtils.byteToHex(actionCode)
+                );
         }
         if (baseCmd != null) {
             out.add(baseCmd);
         }
     }
+
+    //处理D1指令回复
+    private BaseCmd processD1ReplyCmd(ByteBuf in, byte actionCode) {
+        byte result = in.readByte();
+        int errorCode = in.readInt();
+        byte[] uavData = new byte[15];
+        in.readBytes(uavData);
+        String boxSn = new String(uavData);
+        switch (actionCode) {
+            case 0x10:
+                return new WayLineUploadReplyCmd(boxSn, result, errorCode);
+            case 0x11:
+                return new TakeOffReplyCmd(boxSn, result, errorCode);
+            case 0x12:
+                return new ReturnHomeReplyCmd(boxSn, result, errorCode);
+            case 0x13:
+                return new ReturnHomeCancelReplyCmd(boxSn, result, errorCode);
+            case 0x14:
+                return new LandReplyCmd(boxSn, result, errorCode);
+            case 0x15:
+                return new LandCancelReplyCmd(boxSn, result, errorCode);
+            case 0x17:
+                return new WayLineBeginReplyCmd(boxSn, result, errorCode);
+            case 0x18:
+                return new WayLinePauseReplyCmd(boxSn, result, errorCode);
+            case 0x19:
+                return new WayLineResumeReplyCmd(boxSn, result, errorCode);
+            case 0x20:
+                return new WayLineEndReplyCmd(boxSn, result, errorCode);
+            case 0x21:
+                return new ReturnHeightSettingReplyCmd(boxSn, result, errorCode);
+            case 0x29:
+                return new LandForcedReplyCmd(boxSn, result, errorCode);
+            case 0x31:
+                return new ReturnPointSettingReplyCmd(boxSn, result, errorCode);
+            case 0x32:
+                return new EmergencyBrakeReplyCmd(boxSn, result, errorCode);
+            case 0x39:
+                return new PointControlReachedReplyCmd(boxSn, result, errorCode);
+            case 0x22:
+                return new CameraModeReplyCmd(boxSn, result, errorCode);
+            case 0x23:
+                return new CapturePicReplyCmd(boxSn, result, errorCode);
+            case 0x24:
+                return new RecordStartReplyCmd(boxSn, result, errorCode);
+            case 0x25:
+                return new RecordStopReplyCmd(boxSn, result, errorCode);
+            case 0x2E:
+                return new FlyWayEndReplyCmd(boxSn, result, errorCode);
+            // Special cases
+            case 0x3D:
+            case 0x42:
+                return processSpecialD1ReplyCmd(in, actionCode, boxSn);
+            // Add other cases...
+            default:
+                log.warn("无法识别D1的动作编号:{}", ByteUtils.byteToHex(actionCode));
+        }
+        return null;
+    }
+    //处理D2指令回复
+    private BaseCmd processD2ReplyCmd(ByteBuf in, byte actionCode) {
+        byte result = in.readByte();
+        int errorCode = in.readInt();
+        byte[] uavData = new byte[15];
+        in.readBytes(uavData);
+        String boxSn = new String(uavData);
+        switch (actionCode) {
+            case 0x05:
+                return new VideoStreamSettingReplyCmd(boxSn, result, errorCode);
+            // Add other cases...
+            default:
+                log.warn("无法识别D2的动作编号:{}", ByteUtils.byteToHex(actionCode));
+        }
+        return null;
+    }
+    //处理特殊D1指令回复
+    private BaseCmd processSpecialD1ReplyCmd(ByteBuf in, byte actionCode, String boxSn) {
+        switch (actionCode) {
+            //无人机准备完成通知回复
+            case 0x3D:
+                byte batteryPower = in.readByte();
+                double longitude = in.readDouble();
+                double latitude = in.readDouble();
+                int altitude = in.readInt();
+                return new UavReadyReplyCmd(boxSn, batteryPower, longitude, latitude, altitude);
+            //无人机初始化信息回复
+            case 0x42:
+//                byte[] uavData1 = new byte[in.readableBytes() - 15];
+//                in.readBytes(uavData1);
+//                TelemetryDataPB.UavInitInfo uavInitInfo;
+//                try {
+//                    uavInitInfo = TelemetryDataPB.UavInitInfo.parseFrom(uavData1);
+//                } catch (InvalidProtocolBufferException e) {
+//                    throw new RuntimeException(e);
+//                }
+//                return new UavInitReplyCmd(boxSn, uavInitInfo);
+            default:
+                return null;
+        }
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3