package com.multictrl.modules.business.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;

/**
 * mqtt发送工具类
 *
 * @author Sdy
 * @since 1.0.0 2026/4/22
 */
@Slf4j
@Component
public class MqttPushService {
    @Resource(name = "mqttOutboundChannel1")
    private MessageChannel mqttOutboundHandler1;

    @Resource(name = "mqttOutboundChannel2")
    private MessageChannel mqttOutboundHandler2;

    /**
     * 客户端1推送消息
     *
     * @param topic 主题
     * @param msg   消息
     */
    public void pushMessageByClient1(String topic, String msg) {
        log.debug("client1 down-> topic:{},msg:{}", topic, msg);
        mqttOutboundHandler1.send(MessageBuilder.withPayload(msg).
                setHeader("mqtt_topic", topic).build());
    }

    /**
     * 客户端2推送消息
     *
     * @param topic 主题
     * @param msg   消息
     */
    public void pushMessageByClient2(String topic, String msg) {
        log.debug("client2 down-> topic:{},msg:{}", topic, msg);
        mqttOutboundHandler2.send(MessageBuilder.withPayload(msg).
                setHeader("mqtt_topic", topic).build());
    }
}
