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

import com.ruoyi.device.mqtt.annotation.ProducerHandler;
import com.ruoyi.device.mqtt.domain.encoder.upgrade.SendApkListRequest;
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;

/**
 * 固件版本列表下行编码 GET_APK_DOWN
 */
@Component
@ProducerHandler(msgType = MsgTypeEnum.GET_APK_DOWN)
public class SendApkListEncoder extends AbstractEncoder<SendApkListRequest>
{
    @Override
    protected void encode(SendApkListRequest 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.getVersionList().size());
        for (String version : message.getVersionList())
        {
            body.writeByte(version.getBytes().length);
            body.writeBytes(version.getBytes());
        }
    }

    @Override
    protected String topic(SendApkListRequest sendApkListRequest)
    {
        return generateTopic(sendApkListRequest);
    }

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

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

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