package com.github.brandonbai.smartmonitor.config;

import com.github.brandonbai.smartmonitor.mqtt.MqttMessageConsumer;
import com.github.brandonbai.smartmonitor.service.SensorService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;

import javax.annotation.RegEx;
import javax.annotation.Resource;

/**
 * mqtt配置
 * @author jifeihu
 * @since 2018年6月13日
 */
@Configuration
public class MqttConfig {

    @Value("mqtt.url")
    private String url;

    @Value("mqtt.username")
    private String username;

    @Value("mqtt.password")
    private String password;

    @Resource
    private SensorService sensorService;

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setServerURIs(url);
        factory.setUserName(username);
        factory.setPassword(password);
        return factory;
    }


    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }

    @Bean
    public MessageProducer inbound() {
        MqttPahoMessageDrivenChannelAdapter adapter =
                new MqttPahoMessageDrivenChannelAdapter(url, "dataClient",
                        "sm/data");
        adapter.setCompletionTimeout(5000);
        adapter.setConverter(new DefaultPahoMessageConverter());
        adapter.setQos(0);
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public MessageHandler handler() {
        return new MqttMessageConsumer(sensorService);
    }

    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler mqttOutbound() {
        MqttPahoMessageHandler messageHandler =
                new MqttPahoMessageHandler("msgClient", mqttClientFactory());
        messageHandler.setAsync(true);
        messageHandler.setDefaultTopic("sm/msg");
        return messageHandler;
    }

    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }

    @MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
    public interface DefaultMqttMessageSender {

        void sendToMqtt(String data);

    }


}

@@ -2407,28 +2407,33 @@
  public 
+Mqtt
 Message
-Handl
+Consum
 er handl

update comsumer