package com.ruoyi.device.mqtt.handler.encoder.upgrade;

import com.ruoyi.device.mqtt.annotation.ProducerHandler;
import com.ruoyi.device.mqtt.domain.encoder.upgrade.SendApkUrlRequest;
import com.ruoyi.device.mqtt.enums.MsgTypeEnum;
import com.ruoyi.device.mqtt.enums.QosEnum;
import com.ruoyi.device.mqtt.handler.encoder.AbstractEncoder;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

/**
 * 固件 URL 下行编码 GET_APK_URL_DOWN
 */
@Component
@ProducerHandler(msgType = MsgTypeEnum.GET_APK_URL_DOWN)
public class SendApkUrlEncoder extends AbstractEncoder<SendApkUrlRequest>
{
    @Override
    protected void encode(SendApkUrlRequest message, ByteBuf body)
    {
        setTargetType(message.getDeviceType(), body);
        body.writeIntLE(message.getDeviceSn().intValue());
        setTargetType(message.getTargetDeviceType(), body);
        body.writeByte(message.getFirmwareType());
        body.writeIntLE(0);

        body.writeByte(message.getDownloadUrl().getBytes().length);
        body.writeBytes(message.getDownloadUrl().getBytes());
    }

    @Override
    protected String topic(SendApkUrlRequest sendApkUrlRequest)
    {
        return generateTopic(sendApkUrlRequest);
    }

    @Override
    protected MsgTypeEnum msgType()
    {
        return MsgTypeEnum.GET_APK_URL_DOWN;
    }

    @Override
    protected QosEnum qos()
    {
        return QosEnum.QoS2;
    }

    @Override
    protected boolean retain()
    {
        return false;
    }
}
