文档首页/ 设备接入 IoTDA/ API参考/ 应用侧API参考/ 应用示例/ 示例一:使用模板文件批量创建设备
更新时间:2024-07-02 GMT+08:00
分享

示例一:使用模板文件批量创建设备

场景描述

本章节指导用户通过API批量创建设备。API的调用方法参见如何调用API

物联网平台支持通过请求参数和模板文件两种方式批量创建设备。本节以模板文件的方式为例,介绍如何批量创建设备。

涉及接口

操作步骤

  1. 确定待创建设备所属的产品。

    1. 查询产品列表
      • 接口信息

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

        详情参见查询产品列表

      • 请求示例
        GET https://{Endpoint}/v5/iot/{project_id}/products?limit={limit}&marker={marker}&app_id={app_id}&offset={offset}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • 响应示例

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "products" : [ {
            "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
            "app_name" : "testAPP01",
            "product_id" : "5ba24f5ebbe8f56f5a14f605",
            "name" : "Thermometer",
            "device_type" : "Thermometer",
            "protocol_type" : "CoAP",
            "data_format" : "binary",
            "manufacturer_name" : "ABC",
            "industry" : "smartCity",
            "description" : "this is a thermometer produced by Huawei",
            "create_time" : "20190303T081011Z"
          } ],
          "page" : {
            "count" : 10,
            "marker" : "5c90fa7d3c4e4405e8525079"
          }
        }
    2. 根据实际需要选择产品,并记录产品的product_id。

  2. 下载批量任务模板文件。

    访问这里,下载模板文件。

  3. 填写模板文件中待创建设备的参数。

    参考创建设备,编辑2中下载的模板文件,填写各项参数。

    样例如下:

  4. 上传批量任务文件。

    1. 上传批量任务文件
      • 接口信息

        URL:POST /v5/iot/{project_id}/batchtask-files

        详情参见上传批量任务文件

      • 请求示例
        POST https://{Endpoint}/v5/iot/{project_id}/batchtask-files 
        Content-Type: multipart/form-data 
        X-Auth-Token: ******** 
        Instance-Id: ********
      • 响应示例

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "file_id" : "0c3c77dd-42a2-4309-9e10-da2e8bf64ac3",
          "file_name" : "BatchCreateDevices_test01.xlsx",
          "upload_time" : "20200617T081608Z"
        }
    2. 记录批量任务文件“file_id”

  5. 创建批量任务

    1. 创建批量任务
      • 接口信息

        URL:POST /v5/iot/{project_id}/batchtasks

        详情参见创建批量任务

      • 请求示例
        POST https://{Endpoint}/v5/iot/{project_id}/batchtasks
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
        
        {
          "app_id" : "Ev8FVvCfOdQDzrFrxSOemiw_aMca",
          "task_name" : "BatchCommandTask",
          "task_type" : "softwareUpgrade",
          "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
          "targets_filter" : {
            "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
          },
          "document" : {
            "package_id" : "32822e5744a45ede319d2c50"
          },
          "task_policy" : {
            "schedule_time" : "20151212T121212Z",
            "retry_count" : 5,
            "retry_interval" : 60
          },
          "document_source" : "jeQDJQZltU8iKgFFoW060F5SGZka"
        }
      • 响应示例

        Status Code: 201 Created

        Content-Type: application/json
        
        {
          "task_id" : "5c8ba99030344005c02316ad",
          "task_name" : "testname",
          "task_type" : "softwareUpgrade",
          "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
          "targets_filter" : {
            "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
          },
          "document" : {
            "package_id" : "32822e5744a45ede319d2c50"
          },
          "task_policy" : {
            "schedule_time" : "20151212T121212Z",
            "retry_count" : 5,
            "retry_interval" : 60
          },
          "status" : "Success",
          "status_desc" : "string",
          "task_progress" : {
            "total" : 0,
            "processing" : 0,
            "success" : 0,
            "fail" : 0,
            "waitting" : 0,
            "fail_wait_retry" : 0,
            "stopped" : 0
          },
          "create_time" : "20151212T121212Z"
        }
    2. 记录批量任务“task_id”

  6. 查询批量任务

    1. 查询批量任务
      • 接口信息

        URL:GET /v5/iot/{project_id}/batchtasks/{task_id}

        详情参见查询批量任务

      • 请求示例
        GET https://{Endpoint}/v5/iot/{project_id}/batchtasks/{task_id}?limit={limit}&marker={marker}&offset={offset}
        Content-Type: application/json
        X-Auth-Token: ********
        Instance-Id: ********
      • 响应示例

        Status Code: 200 OK

        Content-Type: application/json
        
        {
          "batchtasks" : {
            "task_id" : "5c8ba99030344005c02316ad",
            "task_name" : "testname",
            "task_type" : "softwareUpgrade",
            "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
            "targets_filter" : {
              "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
            },
            "document" : {
              "package_id" : "32822e5744a45ede319d2c50"
            },
            "task_policy" : {
              "schedule_time" : "20151212T121212Z",
              "retry_count" : 5,
              "retry_interval" : 60
            },
            "status" : "Success",
            "status_desc" : "string",
            "task_progress" : {
              "total" : 0,
              "processing" : 0,
              "success" : 0,
              "fail" : 0,
              "waitting" : 0,
              "fail_wait_retry" : 0,
              "stopped" : 0
            },
            "create_time" : "20151212T121212Z"
          },
          "task_details" : [ {
            "target" : "e495cf17-ff79-4294-8f64-4d367919d665",
            "status" : "Success",
            "output" : "success",
            "error" : {
              "error_code" : "IOTDA.000002",
              "error_msg" : "The request is unauthorized."
            }
          } ],
          "page" : {
            "count" : 10,
            "marker" : "5c90fa7d3c4e4405e8525079"
          }
        }
    2. 确认任务执行结果。

      根据查询结果,确认批量创建设备任务完成情况。

相关文档