文档首页/ 设备接入 IoTDA/ API参考/ 应用侧API参考/ 应用示例/ 示例二:给指定设备下发消息
更新时间:2023-02-06 GMT+08:00

示例二:给指定设备下发消息

场景描述

本章节指导用户通过API给指定设备下发消息。API的调用方法参见如何调用API

涉及接口

操作步骤

  1. 确定待下发消息的设备。

    1. 查询设备列表
      • 接口信息

        URL:GET /v5/iot/{project_id}/devices

        详情参见查询设备列表

      • 请求示例
        GET https://{Endpoint}/v5/iot/{project_id}/devices?product_id={product_id}&gateway_id={gateway_id}&is_cascade_query={is_cascade_query}&node_id={node_id}&device_name={device_name}&limit={limit}&marker={marker}&offset={offset}&start_time={start_time}&end_time={end_time}&app_id={app_id}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • 响应示例

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "devices" : [ {
            "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
            "description" : "watermeter device",
            "product_name" : "Thermometer",
            "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
            "sw_version" : "1.1.0",
            "tags" : [ {
              "tag_value" : "testTagValue",
              "tag_key" : "testTagName"
            } ],
            "app_name" : "testAPP01",
            "device_name" : "dianadevice",
            "node_type" : "ENDPOINT",
            "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
            "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
            "fw_version" : "1.1.0",
            "node_id" : "ABC123456789",
            "status" : "INACTIVE"
          } ],
          "page" : {
            "marker" : "5c8f3d2d3df1f10d803adbda",
            "count" : 100
          }
        }
    2. 根据实际需要选择设备,并记录设备的“device_id”

  2. 给指定设备下发消息。

    1. 下发设备消息
      • 接口信息

        URL:POST /v5/iot/{project_id}/devices/{device_id}/messages

        详情参见下发设备消息

      • 请求示例
        POST https://{Endpoint}/v5/iot/{project_id}/devices/{device_id}/messages
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "message_id" : "99b32da9-cd17-4cdf-a286-f6e849cbc364",
          "name" : "messageName",
          "message" : "HelloWorld",
          "topic" : "messageDown"
        }
      • 响应示例

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "message_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
          "result" : {
            "status" : "PENDING",
            "created_time" : "20151212T121212Z",
            "finished_time" : "20151212T121213Z"
          }
        }
    2. 记录响应中的消息id,即“message_id”

  3. 确认消息下发结果。

    1. 查询指定消息id(即message_id)的消息。
      • 接口信息

        URL:GET /v5/iot/{project_id}/devices/{device_id}/messages/{message_id}

        详情参见查询指定消息id的消息

      • 请求示例
        GET https://{Endpoint}/v5/iot/{project_id}/devices/{device_id}/messages/{message_id}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • 响应示例

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "message_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
          "name" : "message_name",
          "message" : "string",
          "topic" : "string",
          "status" : "PENDING",
          "created_time" : "20151212T121212Z",
          "finished_time" : "20151212T121212Z"
        }
    2. 根据查询结果中的status字段内容确认消息下发结果。