liusuyi
2024-10-23 37aeda6f5a45a220955eafd55bf5154c71c2d705
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.ard.tianyu.netty.command;
 
import io.netty.buffer.ByteBuf;
import lombok.Data;
 
/**
 * 云台转动(基于角度)命令
 *
 * @author 刘苏义
 * @date 2024/10/22 16:36
 */
 
@Data
public class ControlAngleCommand extends BaseCommand{
    private final byte[] boxSn; // 云盒编号
    private final byte encryptFlag; // 加密标志
    private final byte actionId; // 动作编号
    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;
        this.rotationAngle = rotationAngle;
    }
 
    public short getDataLength() {
        // 计算数据长度: 云盒编号 + 指令编号 + 加密标志 + 动作编号 + 转动角度
        return (short) (2 + 15 + 1 + 1 + 1); // 帧头 + 数据内容
    }
 
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeBytes(boxSn); // 写入云盒编号
        out.writeByte(encryptFlag); // 写入加密标志
        out.writeByte(actionId); // 写入动作编号
        out.writeByte(rotationAngle); // 写入转动角度
    }
}