liusuyi
2024-10-25 75902fb9d5b27a440d14308ccca3c3cff32167bb
优化
14 files modified
2 files deleted
1 files renamed
193 ■■■■■ changed files
ard-work/src/main/java/com/ard/tianyu/controller/UavController.java 7 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/BaseCommand.java 3 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/ControlAngleCommand.java 34 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/DroneReadyNotificationCommand.java 4 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/HeartbeatCommand.java 3 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/HeartbeatReplyCommand.java 5 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/PointFlyCommand.java 4 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterCommand.java 3 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterReplyCommand.java 3 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/SubscribeCommand.java 26 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/SubscribeReplyCommand.java 26 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/command/UavInitInfoCommand.java 18 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java 7 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java 25 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/netty/protobuf/TelemetryDataBuf.proto patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/service/ITyService.java 5 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java 20 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/tianyu/controller/UavController.java
@@ -74,13 +74,6 @@
        return AjaxResult.success(state);
    }
    @ApiOperation("无人机控制订阅")
    @GetMapping("/uavControlSubscribe")
    public AjaxResult uavControlSubscribe(String boxSn) {
        tyService.uavControlSubscribe(boxSn);
        return AjaxResult.success();
    }
    @ApiOperation("云台转动(基于角度)")
    @GetMapping("/controlAngle")
    public AjaxResult controlAngle(String boxSn,Integer actionId,Integer rotationAngle) {
ard-work/src/main/java/com/ard/tianyu/netty/command/BaseCommand.java
@@ -7,7 +7,8 @@
public abstract class BaseCommand {
    private short dataHeader; // 帧头
    private short dataLength; // 数据长度
    private byte cmdCode; // 指令编号
    private byte[] boxSn;     // 云盒编号
    private byte cmdCode;     // 指令编号
    // 编码方法,子类需要实现
    public abstract void encode(ByteBuf out);
ard-work/src/main/java/com/ard/tianyu/netty/command/ControlAngleCommand.java
@@ -11,37 +11,25 @@
 */
@Data
public class ControlAngleCommand extends BaseCommand{
    private final byte[] boxSn; // 云盒编号
public class ControlAngleCommand extends BaseCommand {
    private final byte encryptFlag; // 加密标志
    private final byte actionId; // 动作编号
    private final byte actionCode; // 动作编号
    private final byte rotationAngle; // 转动角度
    public ControlAngleCommand(byte[] boxSn, byte encryptFlag, byte actionId, byte rotationAngle) {
        if (boxSn.length != 15) {
            throw new IllegalArgumentException("云盒编号长度必须为15字节");
        }
        setDataHeader((short)0x7479);
        setDataLength(getDataLength());
        setCmdCode((byte)0xD1);
        this.boxSn = boxSn;
        this.encryptFlag = encryptFlag;
        this.actionId = actionId;
    public ControlAngleCommand(String boxSn, byte actionCode, byte rotationAngle) {
        setDataHeader((short) 0x7479);
        setDataLength((short)19);
        setBoxSn(boxSn.getBytes());
        setCmdCode((byte) 0xD1);
        this.encryptFlag = 0x00;
        this.actionCode = actionCode;
        this.rotationAngle = rotationAngle;
    }
    public short getDataLength() {
        // 计算数据长度: 云盒编号 + 指令编号 + 加密标志 + 动作编号 + 转动角度
        return (short) (1 + 15 + 1 + 1 + 1); // 帧头 + 数据内容
    }
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeBytes(boxSn);              // 写入云盒编号
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeByte(encryptFlag);         // 写入加密标志
        out.writeByte(actionId);            // 写入动作编号
        out.writeByte(actionCode);          // 写入动作编号
        out.writeByte(rotationAngle);       // 写入转动角度
    }
}
ard-work/src/main/java/com/ard/tianyu/netty/command/DroneReadyNotificationCommand.java
@@ -16,10 +16,12 @@
    private byte actionCode;
    private TelemetryDataPB.UavInitInfo uavInitInfo;
    public DroneReadyNotificationCommand(TelemetryDataPB.UavInitInfo uavInitInfo, byte actionCode) {
    public DroneReadyNotificationCommand(String boxSn,TelemetryDataPB.UavInitInfo uavInitInfo, byte actionCode) {
        setDataHeader((short) 0x6A77); // 设置帧头
        // setDataLength((short) (1 + uavState.toByteArray().length)); // 数据长度
        setBoxSn(boxSn.getBytes());
        setCmdCode((byte) 0xD1); // 设置命令编号
        this.uavInitInfo = uavInitInfo;
        this.actionCode = actionCode;
    }
ard-work/src/main/java/com/ard/tianyu/netty/command/HeartbeatCommand.java
@@ -19,9 +19,6 @@
        this.timestamp = timestamp;
    }
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());
        out.writeShort(getDataLength());
        out.writeByte(getCmdCode());
        out.writeLong(timestamp); // 写入时间戳
    }
}
ard-work/src/main/java/com/ard/tianyu/netty/command/HeartbeatReplyCommand.java
@@ -17,16 +17,13 @@
    private long timestamp; // 时间戳
    public HeartbeatReplyCommand(long timestamp) {
        this.timestamp = timestamp;
        setDataHeader((short) 0x6A77); // 设置帧头
        setCmdCode((byte) 0x02); // 设置指令编号
        setDataLength((short) (13)); // 数据长度 (指令编号 + 时间戳)
        this.timestamp = timestamp;
    }
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());
        out.writeShort(getDataLength());
        out.writeByte(getCmdCode());
        out.writeLong(timestamp); // 写入时间戳
    }
}
ard-work/src/main/java/com/ard/tianyu/netty/command/PointFlyCommand.java
@@ -19,9 +19,7 @@
    @Override
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeDouble(longitude);         // 写入经度
        out.writeDouble(latitude);          // 写入纬度
        out.writeFloat(height);             // 写入高度
ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterCommand.java
@@ -22,9 +22,6 @@
    @Override
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeInt(userId);               // 写入用户编号
        out.writeBytes(accessToken);        // 写入访问令牌
    }
ard-work/src/main/java/com/ard/tianyu/netty/command/RegisterReplyCommand.java
@@ -22,9 +22,6 @@
    @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/command/SubscribeCommand.java
File was deleted
ard-work/src/main/java/com/ard/tianyu/netty/command/SubscribeReplyCommand.java
File was deleted
ard-work/src/main/java/com/ard/tianyu/netty/command/UavInitInfoCommand.java
@@ -14,24 +14,22 @@
public class UavInitInfoCommand extends BaseCommand{
    private final byte encryptFlag; // 加密标志
    private final byte actionId; // 动作编号
    private final byte actionCode; // 动作编号
    private final byte result; // 执行结果
    public UavInitInfoCommand(byte encryptFlag, byte actionId, byte result) {
    public UavInitInfoCommand(String boxSn, byte actionCode) {
        setDataHeader((short)0x7479);
        setDataLength((short) 3);
        setDataLength((short) 18);
        setBoxSn(boxSn.getBytes());
        setCmdCode((byte)0xD1);
        this.encryptFlag = encryptFlag;
        this.actionId = actionId;
        this.result = result;
        this.encryptFlag = 0x00;
        this.actionCode = actionCode;
        this.result = 0x01;
    }
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeByte(encryptFlag);         // 写入加密标志
        out.writeByte(actionId);            // 写入动作编号
        out.writeByte(actionCode);          // 写入动作编号
        out.writeByte(result);              // 写入执行结果
    }
}
ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolDecoder.java
@@ -44,10 +44,6 @@
                break;
            case 0x03:
            case 0x04:
                byte[] boxSn = new byte[dataLength - 1];
                in.readBytes(boxSn);
                SubscribeReplyCommand subscribeReply = new SubscribeReplyCommand(new String(boxSn));
                log.debug("订阅回复: {}", subscribeReply.getBoxSn());
                break;
            case (byte) 0xA8:
                byte[] uavStatus = new byte[dataLength - 1];
@@ -83,8 +79,9 @@
                        in.readBytes(uavData1);
                        byte[] uavData2 = new byte[15];
                        in.readBytes(uavData2);
                        String boxSn = new String(uavData2);
                        TelemetryDataPB.UavInitInfo uavInitInfo = TelemetryDataPB.UavInitInfo.parseFrom(uavData1);
                        baseCommand = new DroneReadyNotificationCommand(uavInitInfo, actionCode);
                        baseCommand = new DroneReadyNotificationCommand(boxSn, uavInitInfo, actionCode);
                        log.info("无人机初始化数据: {}", uavInitInfo.getUavModel());
                        break;
                    default:
ard-work/src/main/java/com/ard/tianyu/netty/handler/MessageProtocolEncoder.java
@@ -4,13 +4,38 @@
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
@Slf4j
public class MessageProtocolEncoder extends MessageToByteEncoder<BaseCommand> {
    @Override
    protected void encode(ChannelHandlerContext ctx, BaseCommand command, ByteBuf out) throws Exception {
        out.writeShort(command.getDataHeader()); // 写入帧头
        out.writeShort(command.getDataLength()); // 写入数据长度
        if(command.getBoxSn() != null) {
            out.writeBytes(command.getBoxSn());
        }// 写入指令编号
        out.writeByte(command.getCmdCode()); // 写入指令编号
        command.encode(out); // 编码具体负载
        // 打印
        if(command.getCmdCode()!=0x02) {
            printHex(out);
        }
    }
    private void printHex(ByteBuf out)
    {
        // 打印编码后的字节数组
        byte[] encodedBytes = new byte[out.readableBytes()]; // 创建字节数组,大小为可读字节数
        out.getBytes(out.readerIndex(), encodedBytes); // 读取ByteBuf中的字节到字节数组中
        // log.warn("Encoded message bytes: {}", Arrays.toString(encodedBytes));
        // 将字节数组转换为十六进制格式
        StringBuilder hexString = new StringBuilder();
        for (byte b : encodedBytes) {
            hexString.append(String.format("%02X ", b)); // 将每个字节转换为两位十六进制并加上空格
        }
        log.warn("Encoded message in hex: {}", hexString.toString().trim());
    }
}
ard-work/src/main/java/com/ard/tianyu/netty/protobuf/TelemetryDataBuf.proto
ard-work/src/main/java/com/ard/tianyu/service/ITyService.java
@@ -63,11 +63,6 @@
    void registerMessage();
    /**
     * 无人机控制订阅
     */
    void uavControlSubscribe(String boxSn);
    /**
     * 云台转动(基于角度)
     */
    void controlAngle(String boxSn,Integer actionId,Integer rotationAngle) ;
ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
@@ -127,13 +127,6 @@
        this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
    }
    /**
     * 无人机控制订阅
     */
    public void uavControlSubscribe(String boxSn) {
        SubscribeCommand subscribeCommand = new SubscribeCommand(boxSn);
        this.sendMessage(tyConfiguration.getIpPort(), subscribeCommand);
    }
    /**
     * 发送消息
@@ -146,10 +139,7 @@
                InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
                String host = ipSocket.getHostString();
                int port = ipSocket.getPort();
                // 创建 ByteBuf 并编码消息
                ByteBuf buffer = Unpooled.buffer();
                msg.encode(buffer); // 确保对消息进行编码
                channel.writeAndFlush(buffer); // 发送编码后的数据
                channel.writeAndFlush(msg); // 发送编码后的数据
                log.debug("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
            } else {
                log.error("与天宇服务器链接断开,无法发送数据!");
@@ -164,7 +154,7 @@
     */
    @Override
    public void controlAngle(String boxSn, Integer actionId, Integer rotationAngle) {
        ControlAngleCommand controlAngleCommand = new ControlAngleCommand(boxSn.getBytes(), (byte) 0x00, actionId.byteValue(), rotationAngle.byteValue());
        ControlAngleCommand controlAngleCommand = new ControlAngleCommand(boxSn, actionId.byteValue(), rotationAngle.byteValue());
        this.sendMessage(tyConfiguration.getIpPort(), controlAngleCommand);
    }
@@ -213,13 +203,9 @@
                break;
            case 0x03:
                // 处理订阅消息
                SubscribeCommand subscribeCommand = (SubscribeCommand) tyCommand;
                log.info("收到订阅消息-->处理订阅消息{}", subscribeCommand.getBoxSn());
                break;
            case 0x04:
                // 处理取消订阅消息
                subscribeCommand = (SubscribeCommand) tyCommand;
                log.info("收到取消订阅消息-->处理取消订阅消息{}", subscribeCommand.getBoxSn());
                break;
            case (byte) 0xA8:
                // 处理无人机状态消息
@@ -246,7 +232,7 @@
                // 处理无人机
                DroneReadyNotificationCommand droneReadyNotificationCommand = (DroneReadyNotificationCommand) tyCommand;
                if (droneReadyNotificationCommand.getActionCode() == (byte) 0x42) {
                    UavInitInfoCommand uavInitCommand = new UavInitInfoCommand((byte) 0x00, (byte) 0x42, (byte) 0x01);
                    UavInitInfoCommand uavInitCommand = new UavInitInfoCommand(new String(droneReadyNotificationCommand.getBoxSn()), (byte) 0x42);
                    this.sendMessage(tyConfiguration.getIpPort(), uavInitCommand);
                    log.info("收到无人机初始化详情-回复防止再次发送");
                }