liusuyi
2024-11-01 dc4d96ff05b50617c685d90d625c6e2617c81dde
ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
@@ -1,12 +1,28 @@
package com.ard.tianyu.netty.handler;// MessageProtocolDecoder.java
import com.ard.tianyu.netty.command.BaseCommand;
import com.ard.tianyu.netty.command.HeartbeatReplyCommand;
import com.ard.common.utils.ByteUtils;
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;
import com.ard.tianyu.netty.command.real.SignalInfoCmd;
import com.ard.tianyu.netty.command.real.UavStateCmd;
import com.ard.tianyu.netty.command.real.UavTelemetryDataCmd;
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.wayline.WayLineUploadReplyCmd;
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
@@ -20,7 +36,7 @@
        in.markReaderIndex(); // 标记当前读取位置
        short dataHead = in.readShort();
        if(dataHead != 0x6A77){
        if (dataHead != 0x6A77) {
            return;
        }
        short dataLength = in.readShort();
@@ -30,22 +46,137 @@
            return;
        }
        byte cmdCode = in.readByte();
        BaseCommand command = null;
        // 根据 cmdCode 创建相应消息对象
        if (cmdCode == 0x01) { // 注册回复消息
            byte success = in.readByte(); // 读取是否成功
            log.info("注册回复: {}", success == 0x01 ? "成功" : "失败");
        BaseCmd baseCmd = null;
        byte actionCode;
        switch (cmdCode) {
            case 0x01:
                byte success = in.readByte(); // 读取是否成功
                log.warn("注册回复: {}", success == 0x01 ? "成功" : "失败");
                baseCmd = new RegisterReplyCmd(success);
                break;
            case 0x02:
                long timestamp = in.readLong(); // 读取时间戳
                baseCmd = new HeartbeatReplyCmd(timestamp);
                log.debug("心跳回复: 时间戳 {}", timestamp);
                break;
            case 0x0A:
                byte status = in.readByte();
                baseCmd = new BoxPowerStatusReplyCmd(status);
                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);
                baseCmd = new UavStateCmd(uavState);
                log.debug("无人机状态数据: {}", uavState.getBoxSn());
                break;
            case (byte) 0xA9:
                byte[] telemetryDataBytes = new byte[dataLength - 1];
                in.readBytes(telemetryDataBytes);
                TelemetryDataPB.TelemetryData telemetryData = TelemetryDataPB.TelemetryData.parseFrom(telemetryDataBytes);
                baseCmd = new UavTelemetryDataCmd(telemetryData);
                log.debug("遥测数据: {}", telemetryData.getBoxSn());
                break;
            case (byte) 0x0C:
                int delayTime = in.readInt();
                byte[] signalInfoBytes = new byte[15];
                in.readBytes(signalInfoBytes);
                signalInfoBytes = new byte[dataLength - 20];
                in.readBytes(signalInfoBytes);
                TelemetryDataPB.SignalInfo signalInfo = TelemetryDataPB.SignalInfo.parseFrom(signalInfoBytes);
                baseCmd = new SignalInfoCmd(signalInfo);
                log.debug("信道质量数据: {}", delayTime);
                break;
            case (byte) 0xD1:
                in.readByte();//加密标志
                actionCode = in.readByte();//动作编号
                baseCmd = processD1ReplyCmd(in, actionCode);
                break;
            default:
                in.readByte();//加密标志
                actionCode = in.readByte();
                log.warn("无法识别的命令码: {},动作编号:{}",
                        ByteUtils.byteToHex(cmdCode),
                        ByteUtils.byteToHex(actionCode)
                );
        }
        if (cmdCode == 0x02) { // 注册回复消息
            long timestamp = in.readLong(); // 读取时间戳
            command = new HeartbeatReplyCommand(timestamp); // 创建心跳回复对象
            log.info("心跳回复: 时间戳 {}", timestamp);
        }
        if (command != null) {
            out.add(command); // 添加解码的命令对象到输出列表
        } else {
          //  log.warn("无法识别的命令码: {}", cmdCode);
        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 0x29:
                return new LandForcedReplyCmd(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("无法识别的动作编号:{}", 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;
        }
    }
}