您可以在物联网平台上自定义Topic类,设备将消息发送到自定义Topic中,服务端通过调用AFU SDK中的方法向设备发布指令。自定义Topic通信不使用物模型,消息的数据结构由您自定义。

使用自定义Topic进行通信

准备开发环境

本示例中,服务端使用Java版的AFU 安服优SDK,需先准备Java开发环境。在项目中添加以下Maven依赖,导入安服优SDK相关依赖。
通过在pom.xml文件中添加Maven依赖安装AFU SDK。

  1. <!--AFU SDK核心包,AFU SDK的使用基础 -->
  2. <dependency>
  3. <groupId>io.gitee.afucloud</groupId>
  4. <artifactId>afu-base-common-sdk</artifactId>
  5. <version>1.0.0</version>
  6. </dependency>

为了AFU SDK完整功能使用,还需要将以下依赖项添加到pom.xml文件中,否则将出现异常。

  1. <!--其他相关依赖 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.76</version>
  6. </dependency>
  7. <!-- MQTT-jar -->
  8. <dependency>
  9. <groupId>org.eclipse.paho</groupId>
  10. <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
  11. <version>1.2.0</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.fusesource.mqtt-client</groupId>
  15. <artifactId>mqtt-client</artifactId>
  16. <version>1.14</version>
  17. </dependency>
  18. <!-- http请求-jar -->
  19. <dependency>
  20. <groupId>org.apache.httpcomponents</groupId>
  21. <artifactId>httpmime</artifactId>
  22. <version>4.5.13</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.apache.httpcomponents</groupId>
  26. <artifactId>httpclient-cache</artifactId>
  27. <version>4.5.6</version>
  28. </dependency>
  29. <!-- kafka-jar -->
  30. <dependency>
  31. <groupId>org.apache.kafka</groupId>
  32. <artifactId>kafka-clients</artifactId>
  33. <version>2.8.0</version>
  34. </dependency>

1.服务器发送消息给设备

以下是服务器通过SDK发送消息给设备示例
使用时请将如下示例中的userAccessKey、userSecret、ProductKey、DeviceName、DeviceSecret分别替换成您的accessKey secret、productKey、deviceName、DeviceSecret。

  1. import com.afu.common.sdk.config.AfuConfig;
  2. import com.afu.common.sdk.devicelink.AfuDeviceLinkMqtt;
  3. import com.afu.common.sdk.devicelink.AfuMqttMethod;
  4. public class TestMqttPublish {
  5. public static void main(String[] args) throws Exception {
  6. //连接安服优物联网云平台
  7. AfuConfig afuConfig = new AfuConfig();
  8. afuConfig.setMqttUrl("MqttUrl");//"tcp://xxxxx"连接安服优物联网云平台MQTT服务器
  9. afuConfig.setMqttClientId("MqttClientId"); //"DeviceName/ProductKey"用于区分MQTT客户端,每个设备请设置唯一的ClientId
  10. afuConfig.setProductKey("ProductKey"); //创建产品后,系统生成,在产品详情页查看
  11. afuConfig.setDeviceName("DeviceName"); //创建设备设置的名称
  12. afuConfig.setDeviceSecret("DeviceSecret"); //创建设备后,系统生成,在设备详情页查看
  13. afuConfig.setUserAccessKey("UserAccessKey"); //请登录安服优物联网云平台-->个人中心查看
  14. afuConfig.setUserSecret("UserSecret"); //请登录安服优物联网云平台-->个人中心查看
  15. AfuDeviceLinkMqtt linkMqtt = new AfuDeviceLinkMqtt(afuConfig);
  16. String topic="/DeviceName/ProductKey/user/get";
  17. //实例化AfuMqttMethod对象,其中方法能直接进行相应操作
  18. AfuMqttMethod afuMqttMethod = new AfuMqttMethod(linkMqtt);
  19. //执行发送消息
  20. String publish = afuMqttMethod.publish(topic, "this is test");
  21. System.out.println("返回值是:"+publish);
  22. }
  23. }

发布方法:publish

请求参数:

名称 类型 是否必选 示例值 描述
topic String “/7sgHs*/Pfp5*/user/get” 发布Topic
message String “mqttTest” 发布消息

返回数据实体示例

  1. {
  2. "status":200,
  3. "message":"success"
  4. }

返回数据实体介绍:

名称 类型 示例值 描述
message String success 返回信息
model Object success 返回结果实体
status String 200 状态码,请参见 错误码文档