Example 1: Creating Devices in Batches Using a Template
Scenarios
This topic describes how to create devices in batches using APIs. For details about how to call APIs, see Calling APIs.
The platform allows you to create devices in batches using request parameters or a template. This topic describes how to create devices in batches using a template.
Involved APIs
- Query the Product List: Determine the product to which devices to create belong.
- Upload a Batch Task File: Fill in a batch task file and upload the file to determine the devices to create in batches.
- Create a Batch Task: Create devices in batches by using a template.
- Query a Batch Task: Confirm the batch device creation result.
Procedure
- Determine the product to which devices to create belong.
- Query the product list
- API
URL: GET /v5/iot/{project_id}/products
For details, see Query the Product List.
- Example request
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: ********
- Example response
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" } }
- API
- Select a product based on project requirements and record the value of product_id.
- Query the product list
- Download the batch task template.
Click here to download the template.
- Set parameters of devices to create in the template.
Edit the template downloaded in 2 by referring to Create a Device.
Example:
- Update a batch task file.
- Update a batch task file.
- API
URL: POST /v5/iot/{project_id}/batchtask-files
For details, see Upload a Batch Task File.
- Example request
POST https://{Endpoint}/v5/iot/{project_id}/batchtask-files Content-Type: multipart/form-data X-Auth-Token: ******** Instance-Id: ********
- Example response
Content-Type: application/json { "file_id" : "0c3c77dd-42a2-4309-9e10-da2e8bf64ac3", "file_name" : "BatchCreateDevices_test01.xlsx", "upload_time" : "20200617T081608Z" }
- API
- Record the value of file_id.
- Update a batch task file.
- Create a batch task.
- Create a batch task.
- API
URL: POST /v5/iot/{project_id}/batchtasks
For details, see Create a Batch Task.
- Example request
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" }
- Example response
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" }
- API
- Record the value of task_id.
- Create a batch task.
- Query the batch task.
- Query the batch task.
- API
URL: GET /v5/iot/{project_id}/batchtasks/{task_id}
For details, see Query a Batch Task.
- Example request
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: ********
- Example response
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" } }
- API
- Check the task execution result.
Check whether the batch device creation task is complete based on the response.
- Query the batch task.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot