小爱同学调用本地jar -巴法云

打印 上一主题 下一主题

主题 888|帖子 888|积分 2664

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

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

1.1注册巴法云
  1. https://cloud.bemfa.com/
复制代码
1.2 在巴法云上创建mqtt设备云




注意

  • 主题名称 一开始最好是006结尾 不是数字小爱是不会识别的

  • 点击昵称就可以直接修改并且昵称和你呼叫小爱是有关系的
    比如我的是电脑百度 我就告诉小爱 "打开电脑百度" 这时我写的jar就会获得一条消息 消息内容是"on"
    相反"关闭电脑百度" 消息内容就是是"off"
1.3 在小米手机上关联巴法云


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


  • 呼叫小爱同学 "打开"+你的昵称 我的就是"打开电脑百度"
2 构建springboot项目

主要是监听和发送巴法云的mqtt消息并分析所携带的值做对应的操作
同理使用mqtt的测试工具 或者 其他语言实现也可以 在此只演示Java
2.1 pom文件
  1.         
  2.         <dependency>
  3.             <groupId>org.springframework.integration</groupId>
  4.             <artifactId>spring-integration-mqtt</artifactId>
  5.             <version>5.3.1.RELEASE</version>
  6.         </dependency>
复制代码
2.2 增加一个文件就行

下面这一段只是监听用的也是我从网上摘下来的一段 链接的话.... 忘了
  1. package com.c.bafa.config;
  2. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.integration.annotation.ServiceActivator;
  6. import org.springframework.integration.channel.DirectChannel;
  7. import org.springframework.integration.core.MessageProducer;
  8. import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
  9. import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
  10. import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
  11. import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
  12. import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
  13. import org.springframework.messaging.MessageChannel;
  14. import org.springframework.messaging.MessageHandler;
  15. import java.io.IOException;
  16. @Configuration
  17. public class MqttConfig {
  18.     // 消费消息
  19.     /**
  20.      * 创建MqttPahoClientFactory,设置MQTT Broker连接属性,如果使用SSL验证,也在这里设置。
  21.      * @return factory
  22.      */
  23.     @Bean
  24.     public MqttPahoClientFactory mqttClientFactory() {
  25.         DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
  26.         MqttConnectOptions options = new MqttConnectOptions();
  27.         // 设置代理端的URL地址,可以是多个
  28.         options.setServerURIs(new String[]{"tcp://bemfa.com:9501"});
  29.         factory.setConnectionOptions(options);
  30.         return factory;
  31.     }
  32.     /**
  33.      * 入站通道
  34.      */
  35.     @Bean
  36.     public MessageChannel mqttInputChannel() {
  37.         return new DirectChannel();
  38.     }
  39.     /**
  40.      * 入站
  41.      */
  42.     @Bean
  43.     public MessageProducer inbound() {
  44.         // Paho客户端消息驱动通道适配器,主要用来订阅主题
  45.         MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(巴法云控制台左上角 私钥,
  46.                 mqttClientFactory(), 你的主题名称 我的是"xiaoc006");
  47.         adapter.setCompletionTimeout(5000);
  48.         // Paho消息转换器
  49.         DefaultPahoMessageConverter defaultPahoMessageConverter = new DefaultPahoMessageConverter();
  50.         // 按字节接收消息
  51. //        defaultPahoMessageConverter.setPayloadAsBytes(true);
  52.         adapter.setConverter(defaultPahoMessageConverter);
  53.         adapter.setQos(1); // 设置QoS
  54.         adapter.setOutputChannel(mqttInputChannel());
  55.         return adapter;
  56.     }
  57.     @Bean
  58.     // ServiceActivator注解表明:当前方法用于处理MQTT消息,inputChannel参数指定了用于消费消息的channel。
  59.     @ServiceActivator(inputChannel = "mqttInputChannel")
  60.     public MessageHandler handler() {
  61.         return message -> {
  62.             String payload = message.getPayload().toString();
  63.             // byte[] bytes = (byte[]) message.getPayload(); // 收到的消息是字节格式
  64.             String topic = message.getHeaders().get("mqtt_receivedTopic").toString();
  65.             // 根据主题分别进行消息处理。
  66.             if (topic.matches(".+/sensor")) { // 匹配:1/sensor
  67.                 String sensorSn = topic.split("/")[0];
  68.                 System.out.println("传感器" + sensorSn + ": 的消息: " + payload);
  69.             } else if (topic.equals("collector")) {
  70.                 System.out.println("采集器的消息:" + payload);
  71.             } else if (topic.equals("xiaoc006")) {
  72.                 System.out.println("通知我的消息:主题[" + topic  + "],负载:" + payload);
  73.             } else {
  74.                 System.out.println("丢弃消息:主题[" + topic  + "],负载:" + payload);
  75.             }
  76.             Runtime rt = Runtime.getRuntime();
  77.             String url = "https://www.baidu.com/";
  78.             try {
  79.                 rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
  80.             } catch (IOException e) {
  81.                 e.printStackTrace();
  82.             }
  83.         };
  84.     }
  85.     // 发送消息
  86.     /**
  87.      * 出站通道
  88.      */
  89.     @Bean
  90.     public MessageChannel mqttOutboundChannel() {
  91.         return new DirectChannel();
  92.     }
  93.     /**
  94.      * 出站
  95.      */
  96.     @Bean
  97.     @ServiceActivator(inputChannel = "mqttOutboundChannel")
  98.     public MessageHandler outbound() {
  99.         // 发送消息和消费消息Channel可以使用相同MqttPahoClientFactory
  100.         MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("publishClient", mqttClientFactory());
  101.         messageHandler.setAsync(true); // 如果设置成true,即异步,发送消息时将不会阻塞。
  102.         messageHandler.setDefaultTopic("command");
  103.         messageHandler.setDefaultQos(1); // 设置默认QoS
  104.         // Paho消息转换器
  105.         DefaultPahoMessageConverter defaultPahoMessageConverter = new DefaultPahoMessageConverter();
  106.         // defaultPahoMessageConverter.setPayloadAsBytes(true); // 发送默认按字节类型发送消息
  107.         messageHandler.setConverter(defaultPahoMessageConverter);
  108.         return messageHandler;
  109.     }
  110. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表