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

Deliver an Asynchronous Command

Function

A product model defines commands that the IoT platform can deliver to devices. An application can call this API to deliver asynchronous commands to a specific device to control the device. The platform delivers commands to the device and asynchronously returns the command execution result to the application. The command execution result supports data forwarding. After the application calls the API for creating a rule triggering condition (resource is set to device.command.status and event to update) and the API for creating a rule action and activates a rule, the platform pushes the result to the server specified by the rule when the command status changes. The server can be a user-defined HTTP server, AMQP server, or Huawei Cloud storage server. For details, see Pushing a Device Command Status Change Notification. Notes:

  • This API is used only to deliver commands asynchronously to NB-IoT devices.

  • This API supports asynchronous command delivery to a single device. For details about how to deliver commands to multiple devices asynchronously, see Creating a Batch Task.

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}/devices/{device_id}/async-commands

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.

device_id

Yes

String

Parameter description: ID of the device to which the command is delivered. The ID is unique and is allocated by the platform during device registration. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

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. In the returned response header, X-Subject-Token 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.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

service_id

No

String

Parameter description: ID of the device service that the device command belongs to, which is defined in the product model associated with the device. This parameter is mandatory if the device requires codecs to parse commands. Value: The value can contain a maximum of 64 characters.

Maximum: 64

command_name

No

String

Parameter description: command name, which is defined in the product model associated with the device. This parameter is mandatory if the device requires codecs to parse commands. Value: The value can contain a maximum of 128 characters.

Maximum: 128

paras

Yes

Object

Parameter description: command executed by the device. The command is in JSON format (key-value pairs). If service_id is specified, each key is a paraName in commands in the product model. If service_id is left empty, custom command format is used. {"value":"1"} is an example device command. The specific format depends on the application and device. The maximum size of the request object is 256 KB.

Maximum: 261909

expire_time

No

Integer

Parameter description: duration for the platform to cache commands. The unit is second. The platform can cache up to 20 messages (that is, up to 20 commands in the PENDING state). This parameter is valid only when send_strategy is set to delay. By default, commands are cached for 24 hours and can be cached for up to 2 days.

Minimum: 0

Maximum: 172800

send_strategy

Yes

String

Parameter description: delivery policy. By default, commands are cached and then delivered. Options:

  • immediately: The command is delivered immediately. In this case, expire_time is invalid.

  • delay: The command is cached and delivered after the device reports data or goes online. If expire_time is set to 0 or not specified, the command is cached for 24 hours by default.

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

device_id

String

Unique device ID, which is allocated by the platform during device registration.

command_id

String

Device command ID, which is unique and is allocated by the platform during command delivery.

service_id

String

ID of the device service that the device command belongs to, which is defined in the product model associated with the device.

command_name

String

Command name, which is defined in the product model associated with the device.

paras

Object

Command executed by the device. The command is in JSON format (key-value pairs). If service_id is specified, each key is a paraName in commands in the product model. If service_id is left empty, custom command format is used. {"value":"1"} is an example device command. The specific format depends on the application and device.

expire_time

Integer

Duration for the platform to cache commands, in seconds.

status

String

Device command status. If the command is cached, PENDING is returned. If the command is delivered to the device, SENT is returned.

created_time

String

UTC time when the command was created. The value is in the format of yyyyMMdd'T'HHmmss'Z'.

send_strategy

String

Delivery policy. immediately indicates that the command is delivered immediately. delay indicates that the command is cached and delivered when the device reports data or goes online.

Example Requests

Creating an asynchronous command. The command name is ON_OFF, and the command is ON.

POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/async-commands

{
  "service_id" : "reboot",
  "command_name" : "ON_OFF",
  "paras" : {
    "value" : "ON"
  },
  "expire_time" : 0,
  "send_strategy" : "immediately"
}

Example Responses

Status code: 200

OK

{
  "device_id" : "c1224afb-e9f0-4916-8220-b6bab568e888",
  "command_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
  "service_id" : "Switch",
  "command_name" : "ON_OFF",
  "send_strategy" : "immediately",
  "paras" : {
    "value" : "ON"
  },
  "expire_time" : 0,
  "status" : "SENT",
  "created_time" : "20151212T121212Z"
}

Status Codes

Status Code

Description

200

OK

400

Bad Request

401

Unauthorized

403

Forbidden

404

Not Found

500

Internal Server Error

Error Codes

See Error Codes.