小爱同学调用本地jar -巴法云
  6jopcSuFv4vg 2023年11月01日 64 0

本文通过巴发云注册mqtt服务并在小爱同学注册实现

为什么要用巴法云的 自然是因为他免费啦

1. 准备

1.1注册巴法云

https://cloud.bemfa.com/

1.2 在巴法云上创建mqtt设备云

image

image

image
注意

  • 主题名称 一开始最好是006结尾 不是数字小爱是不会识别的
    image
  • 点击昵称就可以直接修改并且昵称和你呼叫小爱是有关系的
    比如我的是电脑百度 我就告诉小爱 "打开电脑百度" 这时我写的jar就会获得一条消息 消息内容是"on"
    相反"关闭电脑百度" 消息内容就是是"off"

1.3 在小米手机上关联巴法云

  • 打开米家app -> 我的 -> 连接其他平台 -> 添加自己的巴法云

1.4 验证

  • 呼叫小爱同学 "打开"+你的昵称 我的就是"打开电脑百度"

2 构建springboot项目

主要是监听和发送巴法云的mqtt消息并分析所携带的值做对应的操作
同理使用mqtt的测试工具 或者 其他语言实现也可以 在此只演示Java

2.1 pom文件

        <!-- MQTT -->
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
            <version>5.3.1.RELEASE</version>
        </dependency>

2.2 增加一个文件就行

下面这一段只是监听用的也是我从网上摘下来的一段 链接的话.... 忘了

package com.c.bafa.config;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 java.io.IOException;

@Configuration
public class MqttConfig {

    // 消费消息

    /**
     * 创建MqttPahoClientFactory,设置MQTT Broker连接属性,如果使用SSL验证,也在这里设置。
     * @return factory
     */
    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();

        // 设置代理端的URL地址,可以是多个
        options.setServerURIs(new String[]{"tcp://bemfa.com:9501"});

        factory.setConnectionOptions(options);
        return factory;
    }

    /**
     * 入站通道
     */
    @Bean
    public MessageChannel mqttInputChannel() {
        return new DirectChannel();
    }

    /**
     * 入站
     */
    @Bean
    public MessageProducer inbound() {
        // Paho客户端消息驱动通道适配器,主要用来订阅主题
        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(巴法云控制台左上角 私钥,
                mqttClientFactory(), 你的主题名称 我的是"xiaoc006");
        adapter.setCompletionTimeout(5000);

        // Paho消息转换器
        DefaultPahoMessageConverter defaultPahoMessageConverter = new DefaultPahoMessageConverter();
        // 按字节接收消息
//        defaultPahoMessageConverter.setPayloadAsBytes(true);
        adapter.setConverter(defaultPahoMessageConverter);
        adapter.setQos(1); // 设置QoS
        adapter.setOutputChannel(mqttInputChannel());
        return adapter;
    }

    @Bean
    // ServiceActivator注解表明:当前方法用于处理MQTT消息,inputChannel参数指定了用于消费消息的channel。
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public MessageHandler handler() {
        return message -> {
            String payload = message.getPayload().toString();

            // byte[] bytes = (byte[]) message.getPayload(); // 收到的消息是字节格式
            String topic = message.getHeaders().get("mqtt_receivedTopic").toString();

            // 根据主题分别进行消息处理。
            if (topic.matches(".+/sensor")) { // 匹配:1/sensor
                String sensorSn = topic.split("/")[0];
                System.out.println("传感器" + sensorSn + ": 的消息: " + payload);
            } else if (topic.equals("collector")) {
                System.out.println("采集器的消息:" + payload);
            } else if (topic.equals("xiaoc006")) {
                System.out.println("通知我的消息:主题[" + topic  + "],负载:" + payload);
            } else {
                System.out.println("丢弃消息:主题[" + topic  + "],负载:" + payload);
            }

            Runtime rt = Runtime.getRuntime();
            String url = "https://www.baidu.com/";
            try {
                rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } catch (IOException e) {
                e.printStackTrace();
            }

        };
    }

    // 发送消息

    /**
     * 出站通道
     */
    @Bean
    public MessageChannel mqttOutboundChannel() {
        return new DirectChannel();
    }

    /**
     * 出站
     */
    @Bean
    @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public MessageHandler outbound() {

        // 发送消息和消费消息Channel可以使用相同MqttPahoClientFactory
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("publishClient", mqttClientFactory());
        messageHandler.setAsync(true); // 如果设置成true,即异步,发送消息时将不会阻塞。
        messageHandler.setDefaultTopic("command");
        messageHandler.setDefaultQos(1); // 设置默认QoS

        // Paho消息转换器
        DefaultPahoMessageConverter defaultPahoMessageConverter = new DefaultPahoMessageConverter();

        // defaultPahoMessageConverter.setPayloadAsBytes(true); // 发送默认按字节类型发送消息
        messageHandler.setConverter(defaultPahoMessageConverter);
        return messageHandler;
    }

}

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   114   0   0 Java
  8s1LUHPryisj   2024年05月17日   49   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
6jopcSuFv4vg