package com.ard.work.component.netty.handler;// MessageProtocolDecoder.java import com.ard.common.utils.ByteUtils; import com.ard.work.component.netty.command.camera.*; import com.ard.work.component.netty.command.manual.*; import com.ard.work.component.netty.command.wayline.*; import com.ard.work.domain.dto.converter.realdata.UavSignalInfoConverter; import com.ard.work.domain.dto.converter.realdata.UavStateConverter; import com.ard.work.domain.dto.converter.realdata.UavTelemetryDataConverter; import com.ard.work.domain.dto.signal.SignalInfo; import com.ard.work.domain.dto.state.UavState; import com.ard.work.domain.dto.telemetry.TelemetryData; import com.ard.work.component.netty.command.BaseCmd; import com.ard.work.component.netty.command.notice.BoxPowerStatusReplyCmd; import com.ard.work.component.netty.command.notice.WayLineFinishedReplyCmd; import com.ard.work.component.netty.command.notice.UavReadyReplyCmd; import com.ard.work.component.netty.command.real.SignalInfoCmd; import com.ard.work.component.netty.command.real.UavStateCmd; import com.ard.work.component.netty.command.real.UavTelemetryDataCmd; import com.ard.work.component.netty.command.register.HeartbeatReplyCmd; import com.ard.work.component.netty.command.register.RegisterReplyCmd; import com.ard.work.component.netty.command.settings.ReturnHeightSettingReplyCmd; import com.ard.work.component.netty.command.settings.ReturnPointSettingReplyCmd; import com.ard.work.component.netty.command.settings.VideoStreamSettingReplyCmd; import com.ard.work.component.netty.protobuf.TelemetryDataPB; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; import java.util.List; @Slf4j public class MessageProtocolDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { // 检查可读字节数是否足够 if (in.readableBytes() < 4) { return; // 不足以读取头部 } in.markReaderIndex(); // 标记当前读取位置 short dataHead = in.readShort(); if (dataHead != 0x6A77) { return; } short dataLength = in.readShort(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); // 如果不足,重置读取位置 return; } byte cmdCode = in.readByte(); 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 0x09: in.readByte();//加密标志 double longitude = in.readDouble(); double latitude = in.readDouble(); timestamp = in.readLong(); // 读取时间戳 long picLength = in.readLong(); // 读取原图大小 byte[] boxSn = new byte[15]; in.readBytes(boxSn);// 读取boxSn byte[] pic = new byte[dataLength - 49]; in.readBytes(pic);// 读取原图地址 String picPath = new String(pic, StandardCharsets.UTF_8); baseCmd = new PicUploadCompletedReplyCmd(longitude, latitude, timestamp, picLength, boxSn, pic); log.warn("图片上传完成通知回复: 图片 {}", picPath); 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 uavStatePB = TelemetryDataPB.UavState.parseFrom(uavStatus); UavState uavState = UavStateConverter.convertProtoToEntity(uavStatePB); baseCmd = new UavStateCmd(uavState); log.debug("无人机状态数据: {}", uavState.getBoxSn()); break; case (byte) 0xA9: 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(); 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: in.readByte();//加密标志 actionCode = in.readByte();//动作编号 baseCmd = processD1ReplyCmd(in, actionCode); break; case (byte) 0xD2: in.readByte();//加密标志 actionCode = in.readByte();//动作编号 baseCmd = processD2ReplyCmd(in, actionCode); break; default: in.readByte();//加密标志 actionCode = in.readByte(); 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 0x3B: return new FlyRoundReplyCmd(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 WayLineFinishedReplyCmd(boxSn, result, errorCode); // Special cases case 0x3D: case 0x40: return new FormatSdCardReplyCmd(boxSn, result, errorCode); 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; } } }