liusuyi
2024-10-25 91f6e60919b92b7f99a41f2ddeb42df17cd0c487
ard-work/src/main/java/com/ard/tianyu/netty/command/ControlAngleCommand.java
@@ -11,37 +11,29 @@
 */
@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) (2 + 15 + 1 + 1 + 1); // 帧头 + 数据内容
    }
    public void encode(ByteBuf out) {
        out.writeShort(getDataHeader());    // 写入帧头
        out.writeShort(getDataHeader());    // 写入数据头
        out.writeShort(getDataLength());    // 写入数据长度
        out.writeByte(getCmdCode());        // 写入指令编号
        out.writeBytes(boxSn); // 写入云盒编号
        out.writeByte(encryptFlag); // 写入加密标志
        out.writeByte(actionId); // 写入动作编号
        out.writeByte(rotationAngle); // 写入转动角度
        out.writeBytes(getBoxSn());         // 写入盒子SN
        out.writeByte(getCmdCode());        // 写入命令码
        out.writeByte(encryptFlag);         // 写入加密标志
        out.writeByte(actionCode);          // 写入动作编号
        out.writeByte(rotationAngle);       // 写入转动角度
    }
}