Updated on 2022-02-24 GMT+08:00

Creating Device Commands

Typical Scenario

The device profile file defines commands that the IoT platform can deliver to a device. When an NA needs to configure or modify the service attributes of a device, the NA can call this API to deliver commands to the device.

The IoT platform provides two command delivery modes:

  • Immediate delivery: The IoT platform delivers commands to devices immediately after receiving the commands. This ensures real-time performance but does not ensure serialization.
  • Cached command delivery: After receiving commands, the IoT platform caches the commands. When the devices are reachable, the IoT platform delivers the commands in sequence. Specifically, the IoT platform delivers the latter command only after receiving the response of the previous command (which is the ACK automatically replied by the module) to ensure serialization instead of real-time performance.

This API applies to devices that use LWM2M/CoAP, for example, NB-IoT devices.

API Function

This API is used by an NA to deliver a command to a device to control the device. Immediate delivery and cached command delivery are supported on the IoT platform.

API Prototype

Method

POST

URL

https://server:port/iocm/app/cmd/v1.4.0/deviceCommands?appId={appId}

Transport Protocol

HTTPS

Request Parameters

Parameter

Mandatory or Optional

Type

Location

Description

app_key

Mandatory

String

header

Identifies an application that can be accessed on the IoT platform. The value of this parameter is allocated by the IoT platform when the application is created on the platform.

Authorization

Mandatory

String

header

Indicates the authentication information for accessing the IoT platform. The value is Bearer {accessToken}, in which {accessToken} indicates the access token returned by the Authentication API.

appId

Optional

String(1-64)

query

Identifies an application that can be accessed on the IoT platform. The value of this parameter is allocated by the IoT platform when the application is created on the platform. Set this parameter to the value of appId of the authorized application.

deviceId

Mandatory

String(64)

body

Uniquely identifies a device to which a command is delivered. The value of this parameter is allocated by the IoT platform during device registration.

command

Mandatory

CommandDTO

body

Indicates the command information.

callbackUrl

Optional

String(1024)

body

Indicates the URL for receiving command status change notifications. When the command status changes, such as execution failure, execution success, timeout, sending, or sent, the NA is notified.

expireTime

Optional

Integer(>=0)

body

Indicates the command expiration time, in seconds. That is, the validity period of the created device command is expireTime seconds. The command will not be delivered after the specified time elapses. If this parameter is not specified, the default validity period is 48 hours (86400 seconds x 2).

If it is set to 0, the IoT platform will deliver the command to the specific device immediately no matter what command mode is set on the IoT platform (if the device is sleeping or the link has aged, the device cannot receive the command, the IoT platform cannot receive any response from the device, and the command times out in the end).

If expireTime is not 0, commands are cached on the IoT platform (in the PENDING state) within the period specified by expireTime. The commands are delivered only when the device goes online or reports data to the IoT platform.

maxRetransmit

Optional

Integer(0-3)

body

Indicates the maximum number of times the command is retransmitted.

CommandDTO structure

Parameter

Mandatory or Optional

Type

Location

Description

serviceId

Mandatory

String(1-64)

body

Identifies the service corresponding to the command. The value of this parameter must be the same as serviceId defined in the profile.

method

Mandatory

String(1-128)

body

Indicates the name of a specific command under the service. The value of this parameter must be the same as the command name defined in the profile file.

paras

Mandatory

ObjectNode

body

Indicates a command parameter in the jsonString format. The value consists of key-value pairs. Each key is the paraName parameter in commands in the profile file. The specific format depends on the application and device.

Response Parameters

Status Code: 201 Created

Parameter

Type

Description

commandId

String(1-64)

Uniquely identifies a device command. The value of this parameter is allocated by the IoT platform during command delivery.

appId

String(1-64)

Identifies the application to which the device belongs.

deviceId

String(1-64)

Uniquely identifies a device to which a command is delivered. The value of this parameter is allocated by the IoT platform during device registration.

command

CommandDTO

Indicates the command information.

callbackUrl

String(1024)

Indicates the URL for receiving command status change notifications. When the command status changes, such as execution failure, execution success, timeout, sending, or sent, the NA is notified.

expireTime

Integer(>=0)

Indicates the command expiration time, in seconds. That is, the validity period of the created device command is expireTime seconds. The command will not be delivered after the specified time elapses. If this parameter is not specified, the default validity period is 48 hours (86400 seconds x 2).

status

String

Indicates the status of the command.

  • PENDING: The command is cached and has not been delivered.
  • EXPIRED: The command has expired.
  • SUCCESSFUL: The command has been successfully executed.
  • FAILED: The command fails to be executed.
  • TIMEOUT: Command execution times out.
  • CANCELED: The command has been canceled.
  • DELIVERED: The command has been delivered.
  • SENT: The command is being delivered.

creationTime

String(20)

Indicates the time when the command is created.

executeTime

String(20)

Indicates the time when the command is executed.

platformIssuedTime

String(20)

Indicates the time when the IoT platform sends the command.

deliveredTime

String(20)

Indicates the time when the command is delivered to the device.

issuedTimes

Integer(>=0)

Indicates the number of times the IoT platform delivers a command.

maxRetransmit

Integer(0-3)

Indicates the maximum number of times the command is retransmitted.

Request Example

Method: POST
request: https://server:port/iocm/app/cmd/v1.4.0/deviceCommands
Header:
app_key: ******
Authorization: Bearer ******
Content-Type: application/json
Body:
{
  "deviceId": "********",
  "command": {
    "serviceId": "********",
    "method": "********",
    "paras": {
      "paraName1": "paraValue1",
      "paraName2": "paraValue2"
    }
  },
  "callbackUrl": "http://127.0.0.1:9999/cmd/callbackUrl",
  "maxRetransmit": ******

}

Response Example

Response:
Status Code: 201 Created
Content-Type: application/json
Body:
{
  "commandId": "********",
  "appId": "********",
  "deviceId": "********",
  "command": {
    "serviceId": "********",
    "method": "********",
    "paras": {
      "paraName1": "paraValue1",
      "paraName2": "paraValue2"
    }
  },
  "callbackUrl": "http://127.0.0.1:9999/cmd/callbackUrl",
  "expireTime": null,
  "status": "PENDDING",
 
  "creationTime": "20170222T164000Z",
  "executeTime": null,
  "platformIssuedTime": null,
  "deliveredTime": null,
  "issuedTimes": null,
  "maxRetransmit": ******
}

Error Codes

HTTP Status Code

Error Code

Error Description

Remarks

200

100203

The application is not existed.

The application does not exist.

Recommended handling:

  • Check whether appId carried in the HTTP request header is correct.
  • Check whether appId in the request path (URL) is correct.

200

100217

The application hasn't been authorized.

The application has not been authorized.

Recommended handling: In scenarios where applications are not authorized, ensure that request parameter appId is null.

200

100418

The deviceData is not existed.

The device data does not exist.

Recommended handling:

  • If deviceId carried in the request is incorrect, check whether deviceId belongs to appId or whether deviceId is incorrect.
  • Check whether appId carried in the header contains deviceId.
  • If the URL contains the optional parameter appId, check whether the value of appId is correct.

200

100428

The device is not online.

The device is not online.

Recommended handling: Check whether the connection between the device and the IoT platform is normal.

200

100431

The serviceType is not exist.

The service type does not exist.

Recommended handling:

  • Check whether the profile file of the device has been uploaded to the IoT platform.
  • Check whether the request parameters are correct and whether serviceId exists in the profile file.

400

100022

The input is invalid.

An input parameter is invalid.

Recommended handling: Check whether parameters carried in the API call request are valid.

400

100223

Command counts has reached the upLimit.

The number of cached commands reaches the limit. The number of commands in the PENDING state does not exceed the limit. The default value is 20.

Recommended handling: If the commands cached on the IoT platform need to be executed, enable the device to report data to trigger delivery of the cache commands. If a command cached on the IoT platform does not need to be executed, call the API used for modifying device commands V4 to change the state of the command from PENDING to CANCELED.

403

100217

The application hasn't been authorized.

The application has not been authorized.

Recommended handling: In scenarios where applications are not authorized, ensure that request parameter appId is null.

403

100612

Device is zombie.

The device is a zombie device. (The interval between the current system time and the time when the device went online exceeds the threshold. The default value is seven days.)

Recommended handling: Run the command again after the device goes online.

403

1010009

app throttle exceed.

The NA calls the API at a frequency that exceeds the flow control threshold (100 calls per minute by default).

Recommended handling: Contact IoT platform maintenance personnel to adjust the flow control threshold or control the API call frequency.

403

1010005

App_key or access_token is invalid.

The access token is invalid.

Recommended handling: Check whether accessToken carried in the API request is correct.

500

100001

Internal server error.

An internal server error occurs.

Recommended handling: An internal error occurs on the IoT platform. Contact IoT platform maintenance personnel.

500

100023

The data in database is abnormal.

The database is abnormal.

Recommended handling: An internal error occurs on the IoT platform. Contact IoT platform maintenance personnel.

500

100220

Get AppKey from header failed.

Failed to obtain the appKey.

Recommended handling: Check whether appId is carried in the API request header.

500

101016

Get iotws address failed.

Failed to obtain the IoTWS address.

Recommended handling: An internal error occurs on the IoT platform. Contact IoT platform maintenance personnel.

500

101017

Get newCallbackUrl from oss failed.

Obtaining a new callback URL from the OSS fails.

Recommended handling: An internal error occurs on the IoT platform. Contact IoT platform maintenance personnel.

503

100501

Congestion occurs, and the current network has been flow-controlled

Congestion occurs. The current network is under flow control.