Updated on 2022-08-27 GMT+08:00

Creating a Batch Task

Function

This API is used by an application to create a batch task to perform batch operations on multiple devices. You can upgrade software and firmware, create, delete, freeze, and unfreeze devices, and create command tasks in batches.

Debugging

You can debug this API in API Explorer.

URI

Request Method

POST

URI

/v5/iot/{project_id}/batchtasks

Transport Protocol

HTTPS

Request Parameters

Parameter

Mandatory

Type

Location

Description

X-Auth-Token

Yes

String

Header

User token. You can obtain the token by calling the IAM API Obtaining a User Token Through Password Authentication. X-Subject-Token in the response header returned by the API is the desired user token. For details about how to obtain the token, see Token Authentication.

Instance-Id

No

String

Header

Instance ID. This parameter is required only when the API is called from the management plane in the physical multi-tenant scenario.

project_id

Yes

String

Path

Project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

app_id

No

String

Body

Resource space ID. This parameter is optional. If you have multiple resource spaces, you can use this parameter to specify the resource space that the batch task to create belongs to. If this parameter is not specified, the batch task to be created will belong to the default resource space.

The value is a string of no more than 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

task_name

Yes

String

Body

Batch task name.

The value is a string of no more than 128 characters. Only letters, digits, and underscores (_) are allowed.

task_type

Yes

String

Body

Batch task type.

Options:

  • softwareUpgrade: software upgrade task
  • firmwareUpgrade: firmware upgrade task
  • createDevices: batch device creation task
  • deleteDevices: batch device deletion task
  • freezeDevices: batch device freezing task
  • unfreezeDevices: batch device unfreezing task
  • createCommands: task for creating synchronous commands in batches
  • createAsyncCommands: task for creating asynchronous commands in batches
  • createMessages: batch message creation task
  • updateDeviceShadows: task for configuring device shadows in batch

targets

No

List<String>

Body

Batch task targets. Set this parameter to a device ID list, which can include up to 30,000 device IDs. This parameter is supported when task_type is set to firmwareUpgrade, softwareUpgrade, deleteDevices, freezeDevices, unfreezeDevices, createCommands, createAsyncCommands, updateDeviceShadows, or createMessages. If the targets, targets_filter, and document_source parameters are all specified, only one parameter takes effect. The platform uses targets, targets_filter, and document_source in descending order of priority.

Device ID list. The value is a string of no more than 128 characters. Only letters, digits, and underscores (_) are allowed.

targets_filter

No

Object

Body

Parameters for filtering task targets. The value is in JSON format (key-value pairs). Supported filter key is group_ids and its value is a list of group IDs, for example, ["e495cf17-ff79-4294-8f64-4d367919d665"]. Devices matching the filter will become the task targets. This parameter is supported when task_type is set to firmwareUpgrade, softwareUpgrade, createDevices, deleteDevices, freezeDevices, unfreezeDevices, createCommands, createAsyncCommands, updateDeviceShadows, or createMessages. If the targets, targets_filter, and document_source parameters are all specified, only one parameter takes effect. The platform uses targets, targets_filter, and document_source in descending order of priority.

document

No

Object

Body

Task execution data file, in JSON format (key-value pairs). This parameter is supported when task_type is set to firmwareUpgrade, softwareUpgrade, createCommands, createAsyncCommands, createMessages, or updateDeviceShadows.

  • When task_type is set to firmwareUpgrade or softwareUpgrade: Set the key to package_id and its value to IDs of the software or firmware packages uploaded to the platform. IDs can be obtained from the software library on the platform. Example: {"package_id": "32822e5744a45ede319d2c50"}.
  • When task_type is set to createCommands: Set synchronous command parameters, for example, {"service_id":"water","command_name":"ON_OFF","paras":{"value":"ON"}}. For details, see Synchronous Device Command APIs.
  • When task_type is set to createAsyncCommands: Set asynchronous command parameters, for example, {"service_id":"water","command_name":"ON_OFF","paras":{"value":"ON"},"expire_time":0,"send_strategy":"immediately"}. For details, see Asynchronous Device Command APIs.
  • When task_type is set to createMessages: Set message delivery parameters, for example, {"message_id":"99b32da9-cd17-4cdf-a286-f6e849cbc364","name":"messageName","message":"HelloWorld","encoding":"none","payload_format":"standard","topic":"messageDown","topic_full_name":"/device/message/down"}. For details, see Delivering a Device Message.
  • When task_type is set to updateDeviceShadows: Set device shadow parameters, for example, {"shadow": [{"service_id":"WaterMeter","desired": {"temperature": "60"}}]}. For details, see Configuring Desired Device Shadow Data. Restrictions: 1. The service_id and desired parameters are mandatory. 2. Up to five service IDs can be configured and each service ID must be unique. 3. The configuration cannot exceed 10 KB.

task_policy

No

TaskPolicy Object

Body

Task policy request details.

document_source

No

String

Body

ID of the uploaded batch task file. This parameter is supported when task_type is set to createDevices, deleteDevices, freezeDevices, or unfreezeDevices. Before using this parameter, call the API Uploading a Batch Task File to upload a file to obtain the file ID. For details about the file examples, see Template for Batch Registering Devices, Template for Batch Deleting Devices, Template for Batch Freezing Devices, and Template for Batch Unfreezing Devices. If the targets, targets_filter, and document_source parameters are all specified, only one parameter takes effect. The platform uses targets, targets_filter, and document_source in descending order of priority.

Table 1 TaskPolicy

Parameter

Mandatory

Type

Description

schedule_time

No

String

Time when the task is executed.

The task is executed within 7 days. If this parameter is not specified, the task is executed immediately. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

retry_count

No

Integer

Number of automatic retries upon failure for subtasks.

This parameter is mandatory if retry_interval is specified. Up to five retries are supported.

retry_interval

No

Integer

Interval for automatic retries upon failure for subtasks, in minutes.

The maximum value is 1440 (24 hours). If this parameter is not passed, subtasks will not be retried. This parameter is mandatory if retry_count is specified.

Response Parameters

Parameter

Type

Description

task_id

String

Batch task ID. It is allocated by the platform during batch task creation.

task_name

String

Batch task name.

task_type

String

Batch task type. Options:

  • softwareUpgrade: software upgrade task
  • firmwareUpgrade: firmware upgrade task
  • createDevices: batch device creation task
  • deleteDevices: batch device deletion task
  • freezeDevices: batch device freezing task
  • unfreezeDevices: batch device unfreezing task
  • createCommands: task for creating synchronous commands in batches
  • createAsyncCommands: task for creating asynchronous commands in batches
  • createMessages: batch message creation task
  • updateDeviceShadows: task for configuring device shadows in batch

targets

List<String>

Batch task targets. When task_type is set to firmwareUpgrade, softwareUpgrade, deleteDevices, freezeDevices, unfreezeDevices, createCommands, createAsyncCommands, createMessages, or updateDeviceShadows, set this parameter to a device ID list.

targets_filter

Object

Parameters for filtering task targets. The value is in JSON format (key-value pairs). Supported filter key is group_ids and its value is a list of group IDs, for example, ["e495cf17-ff79-4294-8f64-4d367919d665"]. Devices matching the filter will become the task targets.

document

Object

Task execution data file in JSON format. When task_type is set to softwareUpgrade or firmwareUpgrade, the JSON file contains the key-value pair. Set key to package_id and value to IDs of the software or firmware packages uploaded on the platform. IDs can be obtained from the software library on the platform. When task_type is set to createCommands, the JSON file contains command-related parameters, for example, {"service_id":"water","command_name":"ON_OFF","paras":{"value":"ON"}}. For details, see Synchronous Device Command APIs. When task_type is set to createAsyncCommands, the JSON file contains command-related parameters, for example, {"service_id":"water","command_name":"ON_OFF","paras":{"value":"ON"},"expire_time":0,"send_strategy":"immediately"}. For details, see Asynchronous Device Command APIs. When task_type is set to updateDeviceShadows, the JSON file contains command-related parameters, for example, {"shadow": [{"service_id": "WaterMeter","desired": {"temperature": "60"}}]}. For details, see Configuring Desired Device Shadow Data.

task_policy

TaskPolicy Object

Policies for executing the task.

status

String

(Optional) Batch task status. Options:

  • Initializing: The batch task is being initialized.
  • Waitting: The batch task is waiting to be executed.
  • Processing: The batch task is being executed.
  • Success: The batch task is executed.
  • Fail: The batch task fails to be executed.
  • PartialSuccess: Only some subtasks in the batch task are executed.
  • Stopped: The batch task is stopped.

status_desc

String

Description of the batch task status (including the error information of the parent task)

task_progress

TaskProgress Object

Subtask execution statistics.

create_time

String

Time when the batch task was created. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

Table 2 TaskPolicy

Parameter

Type

Description

schedule_time

String

Time when the task is executed.

The task is executed within 7 days. If this parameter is not specified, the task is executed immediately. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

retry_count

Integer

Number of automatic retries upon failure for subtasks.

This parameter is mandatory if retry_interval is specified. Up to five retries are supported.

retry_interval

Integer

Interval for automatic retries upon failure for subtasks, in minutes.

The maximum value is 1440 (24 hours). If this parameter is not passed, subtasks will not be retried. This parameter is mandatory if retry_count is specified.

Table 3 TaskProgress

Parameter

Type

Description

total

Integer

Total number of subtasks.

processing

Integer

Number of subtasks that are being executed.

success

Integer

Number of subtasks that are executed.

fail

Integer

Number of subtasks that fail.

waitting

Integer

Number of subtasks to execute.

fail_wait_retry

Integer

Number of subtasks waiting for retry.

stopped

Integer

Number of stopped subtasks.

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

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"
}

Error Code

HTTP Status Code

Error Code

Error Message

Description

Solution

400

IOTDA.014201

Invalid input. The batch task name already exists.

This task name already exists.

Change the task name and try again.

IOTDA.014203

Invalid input. The document params is invalid, errorMsg : %s

Invalid parameter in the document.

Check whether the request parameters meet the requirements in the HUAWEI CLOUD documentation.

IOTDA.014204

Invalid input. The targets and targets_filter can not all be empty

The targets and targets_filter parameters cannot both be empty.

Specify any one of the parameters.

IOTDA.014205

Invalid input. The targets_filter's key only support %s

The key of the targets_filter parameter can only be the specified type.

Check whether the request parameters meet the requirements in the HUAWEI CLOUD documentation.

IOTDA.014207

Invalid input. The scheduleTime must be greater than the current time, up to %s days later

The start time cannot be earlier than the current time, and the latest start time cannot exceed the specified number of days.

Check whether the request parameters meet the requirements in the HUAWEI CLOUD documentation.

IOTDA.014208

Invalid input. Parameter retry_count and retry_interval depend on each other and must be assigned at the same time

Both retry_count and retry_interval must be specified.

Specify the retry_count and retry_interval parameters.

403

IOTDA.014202

Operation not allowed. The amount of unfinished task has reached the limit %s.

The number of unfinished tasks reaches the upper limit.

Try again after the task is complete.

IOTDA.014206

Operation not allowed. The amount of targets has reached the limit %s.

The number of targets exceeds the upper limit.

Check whether the request parameters meet the requirements in the HUAWEI CLOUD documentation.