| ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterReplyCommand.java | ●●●●● patch | view | raw | blame | history | |
| ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java | ●●●●● patch | view | raw | blame | history | |
| ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java | ●●●●● patch | view | raw | blame | history |
ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterReplyCommand.java
New file @@ -0,0 +1,30 @@ package com.ard.tianyu.netty.command;// RegisterMessage.java import io.netty.buffer.ByteBuf; import lombok.Data; /** * 注册命令 * * @author 刘苏义 * @date 2024/10/22 16:36 */ @Data public class RegisterReplyCommand extends BaseCommand { private byte success; public RegisterReplyCommand(byte success) { setDataHeader((short) 0x6A77); // 设置帧头 setDataLength((short) (6)); // 数据长度 setCmdCode((byte) 0x01); // 设置命令编号 this.success = success; } @Override public void encode(ByteBuf out) { out.writeShort(getDataHeader()); // 写入帧头 out.writeShort(getDataLength()); // 写入数据长度 out.writeByte(getCmdCode()); // 写入指令编号 out.writeByte(success); // 写入用户编号 } } ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
@@ -1,9 +1,6 @@ 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.tianyu.netty.command.UavDataCommand; import com.ard.tianyu.netty.command.UavStateCommand; import com.ard.tianyu.netty.command.*; import com.ard.tianyu.netty.protobuf.TelemetryDataPB; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; @@ -38,11 +35,12 @@ case 0x01: byte success = in.readByte(); // 读取是否成功 log.warn("注册回复: {}", success == 0x01 ? "成功" : "失败"); baseCommand = new RegisterReplyCommand(success); break; case 0x02: long timestamp = in.readLong(); // 读取时间戳 baseCommand = new HeartbeatReplyCommand(timestamp); log.warn("心跳回复: 时间戳 {}", timestamp); log.debug("心跳回复: 时间戳 {}", timestamp); break; case (byte) 0xA8: byte[] uavStatus = new byte[dataLength - 1]; ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
@@ -133,7 +133,7 @@ ByteBuf buffer = Unpooled.buffer(); msg.encode(buffer); // 确保对消息进行编码 channel.writeAndFlush(buffer); // 发送编码后的数据 log.info("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF)); log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF)); } else { log.error("与天宇服务器链接断开,无法发送数据!"); } @@ -154,7 +154,13 @@ switch (cmdCode) { case 0x01: // 处理心跳消息 log.info("收到心跳消息-->处理心跳消息"); RegisterReplyCommand registerReplyCommand = (RegisterReplyCommand) tyCommand; log.info("收到注册消息-->处理注册消息{}", registerReplyCommand.getSuccess()); if (registerReplyCommand.getSuccess() != 1) { // 注册失败,重新注册 log.info("注册失败,重新注册"); registerMessage(); } break; case 0x02: // 处理注册消息