Updated on 2024-06-27 GMT+08:00

Create 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, modify, delete, freeze, and unfreeze devices, and create command and message tasks in batches.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

POST /v5/iot/{project_id}/batchtasks

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

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

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

Parameter description: 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

Parameter description: instance ID. Unique identifier of each instance in the physical multi-tenant scenario. Mandatory for professional editions and recommended in other cases. Log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID. For details, see Viewing Instance Details.

Minimum: 1

Maximum: 36

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

app_id

No

String

Parameter description: resource space ID. This parameter is optional. If you have multiple resource spaces, you can use this parameter to specify the resource space to which the batch task to create will belong. If this parameter is not specified, the batch task to create will belong to the default resource space. Value: The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

task_name

Yes

String

Parameter description: batch task name. Value: The value can contain a maximum of 128 characters. Only letters, digits, and underscores (_) are allowed.

Minimum: 1

Maximum: 128

task_type

Yes

String

Parameter description: 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

  • updateDevices: batch device update task

task_mode

No

String

Parameter description: batch task mode. Currently, only the gateway mode is supported. This parameter is supported when task_type is set to firmwareUpgrade or softwareUpgrade. In the software/firmware upgrade scenario, if the device to be upgraded is a child device of a gateway, the version information obtaining notification and upgrade notification delivered by the IoT platform carry task_id (ID of the software/firmware upgrade batch task) and sub_device_count (number of child devices to be upgraded in a batch task). Value: GATEWAY (gateway mode).

task_ext_info

No

Object

Parameter description: extended information of batch tasks This parameter is supported when task_type is set to firmwareUpgrade or softwareUpgrade. In the software/firmware upgrade scenario, this parameter is carried in the version information obtaining notification and upgrade notification delivered by the IoT platform. Value: a maximum of 512 characters.

Maximum: 512

targets

No

Array of strings

Parameter description: 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. Value: device ID list. The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

targets_filter

No

Map<String,Object>

Parameter description: 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, 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

Parameter description: 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: Enter 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 Message to a Device.

  • 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 Properties in a Device Shadow. 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 size of the configuration content cannot exceed 10 KB.

task_policy

No

TaskPolicy object

Parameter description: task policy request details.

document_source

No

String

Parameter description: 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 for uploading a batch task file to obtain the file ID. For details about file templates, see Template for batch device registration, Template for batch device update, Template for batch device deletion, Template for batch device freezing, and Template for batch device unfreezing. 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 4 TaskPolicy

Parameter

Mandatory

Type

Description

schedule_time

No

String

Parameter description: time when the task is executed. Value: 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

Parameter description: number of automatic retries upon failure for subtasks. Value: This parameter is mandatory if retry_interval is specified. Up to five retries are supported.

Minimum: 1

Maximum: 5

retry_interval

No

Integer

Parameter description: interval for automatic retries upon failure for subtasks, in minutes. Value: 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.

Minimum: 0

Maximum: 1440

Response Parameters

Status code: 201

Table 5 Response body 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

  • updateDevices: batch device update task

task_mode

String

Parameter description: batch task mode. Currently, only the gateway mode is supported. This parameter is supported when task_type is set to firmwareUpgrade or softwareUpgrade. In the software/firmware upgrade scenario, if the device to be upgraded is a child device of a gateway, the version information obtaining notification and upgrade notification delivered by the IoT platform carry task_id (ID of the software/firmware upgrade batch task) and sub_device_count (number of child devices to be upgraded in a batch task). Value: GATEWAY (gateway mode).

task_ext_info

Object

Parameter description: extended information of batch tasks This parameter is supported when task_type is set to firmwareUpgrade or softwareUpgrade. In the software/firmware upgrade scenario, this parameter is carried in the version information obtaining notification and upgrade notification delivered by the IoT platform. Value: a maximum of 512 characters.

Maximum: 512

targets

Array of strings

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

Map<String,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 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 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 parameters, for example, {"shadow": [{"service_id": "WaterMeter","desired": {"temperature": "60"}}]}. For details, see Configuring Desired Properties in a Device Shadow.

task_policy

TaskPolicy object

Policies for executing the task.

status

String

Indicates the status of a batch task. This parameter is optional. Options:

  • Initializing: The task is being initialized.

  • Waitting: The task is waiting to be executed.

  • Processing: The task is being executed.

  • Success: The task is executed.

  • Fail: The task execution failed.

  • PartialSuccess: Only some subtasks in the batch task are executed.

  • Stopped: The task is stopped.

  • Stopping: The task is being 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 6 TaskPolicy

Parameter

Type

Description

schedule_time

String

Parameter description: time when the task is executed. Value: 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

Parameter description: number of automatic retries upon failure for subtasks. Value: This parameter is mandatory if retry_interval is specified. Up to five retries are supported.

Minimum: 1

Maximum: 5

retry_interval

Integer

Parameter description: interval for automatic retries upon failure for subtasks, in minutes. Value: 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.

Minimum: 0

Maximum: 1440

Table 7 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.

removed

Integer

Indicates the number of removed subtasks.

Example Requests

  • Creates a batch software upgrade task for a specified device.

    POST https://{endpoint}/v5/iot/{project_id}/batchtasks
    
    {
      "app_id" : "Ev8FVvCfOdQDzrFrxSOemiw_aMca",
      "task_name" : "BatchSoftwareUpgradeTask",
      "task_type" : "softwareUpgrade",
      "targets" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ],
      "document" : {
        "package_id" : "32822e5744a45ede319d2c50"
      },
      "task_policy" : {
        "schedule_time" : "20151212T121212Z",
        "retry_count" : 5,
        "retry_interval" : 60
      }
    }
  • Creates a batch software upgrade task for a specified device.

    POST https://{endpoint}/v5/iot/{project_id}/batchtasks
    
    {
      "app_id" : "Ev8FVvCfOdQDzrFrxSOemiw_aMca",
      "task_name" : "BatchSoftwareUpgradeTask",
      "task_type" : "softwareUpgrade",
      "document" : {
        "package_id" : "32822e5744a45ede319d2c50"
      },
      "targets_filter" : {
        "group_ids" : [ "e495cf17-ff79-4294-8f64-4d367919d665" ]
      },
      "task_policy" : {
        "schedule_time" : "20151212T121212Z",
        "retry_count" : 5,
        "retry_interval" : 60
      }
    }
  • Creates a batch device registration task for a specified device.

    POST https://{endpoint}/v5/iot/{project_id}/batchtasks
    
    {
      "app_id" : "Ev8FVvCfOdQDzrFrxSOemiw_aMca",
      "task_name" : "BatchCreateDevicesTask",
      "task_type" : "createDevices",
      "task_policy" : {
        "schedule_time" : "20151212T121212Z",
        "retry_count" : 5,
        "retry_interval" : 60
      },
      "document_source" : "jeQDJQZltU8iKgFFoW060F5SGZka"
    }

Example Responses

Status code: 201

Created

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

Status Codes

Status Code

Description

201

Created

400

BAD REQUEST

401

Unauthorized

403

FORBIDDEN

500

Internal Server Error

Error Codes

See Error Codes.