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

import com.alibaba.fastjson2.JSON;
import com.ruoyi.device.mqtt.annotation.ProducerHandler;
import com.ruoyi.device.mqtt.domain.BaseJsonBody;
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 java.nio.charset.StandardCharsets;

/**
 * 登录应答：JSON body + AbstractEncoder 组帧、CRC、经 {@link com.ruoyi.device.mqtt.client.EmqClient} 下发。
 */
@ProducerHandler(msgType = MsgTypeEnum.JSON_BODY)
public class JsonBodyEncoder extends AbstractEncoder<BaseJsonBody>
{
    @Override
    protected void encode(BaseJsonBody t, ByteBuf body)
    {
        body.writeBytes(JSON.toJSONString(t).getBytes(StandardCharsets.UTF_8));
    }

    @Override
    protected String topic(BaseJsonBody t)
    {
        return generateTopic(t);
    }

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

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

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