package com.muyu.parsing.consumer;

import com.muyu.cargateway.domain.properties.MqttProperties;
import com.muyu.common.rabbit.constants.RabbitConstants;
import com.muyu.parsing.mqtt.service.MqttClientService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class RabbitListenerComponent {

    @Autowired
    private MqttClientService mqttClientService;

    @RabbitListener(queuesToDeclare = @Queue(value = RabbitConstants.FORM_QUEUE, durable = "true"))
    public void downline(MqttProperties mqttProperties, Message message, Channel channel) {
        try {
            mqttClientService.connectAndSubscribeAsync(mqttProperties);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("处理RabbitMQ消息时发生错误", e);
        }
    }
}
