package com.zd.laboratory.mqtt.service.impl;

import com.alibaba.fastjson.JSON;
import com.zd.laboratory.mqtt.MqttProducer;
import com.zd.laboratory.mqtt.entiy.MessageBody;
import com.zd.laboratory.mqtt.enums.SendMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 公用发送类
 * @Author: zhoupan
 * @Date: 2021/10/08/10:42
 * @Description:
 */
@Service
public class CommonSend {

    @Autowired
    MqttProducer mqttProducer;


    /**
     * 发送消息：去重模式
     * @param topic
     * @param messageBody
     */
    public  void  send(String topic, MessageBody messageBody)
    {
        mqttProducer.send(topic, SendMode.DISTINCT.getCode(), JSON.toJSONString(messageBody));
    }

    /**
     * 发送消息
     * @param topic
     * @param messageBody
     * @param sendMode
     */
    public  void  send(String topic,MessageBody messageBody,SendMode sendMode)
    {
        mqttProducer.send(topic, sendMode.getCode(), JSON.toJSONString(messageBody));
    }


    /**
     * 发送消息：去重模式
     * @param topic
     * @param messageBody
     */
    public  void  send(String topic, String messageBody)
    {
        mqttProducer.send(topic, SendMode.ONCE.getCode(), messageBody);
    }

    /**
     * 发送消息：去重模式
     * @param topic
     * @param messageBody
     */
    public  void  send(String topic, byte[] messageBody)
    {
        mqttProducer.send(topic, SendMode.ONCE.getCode(), messageBody);
    }

}
