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

Query Device List Flexibly

Function

API description

This API is used by an application to query a desired device list using SQL statements.

Constraints

  • This API is supported only by Standard and Enterprise editions.

  • The maximum TPS for an account to call this API is 1 (one request per second).

SQL-like syntax description

SQL-like statements include select, from, where (optional), order by (optional), and limit clauses (optional). The maximum length is 400 characters. The content in the clause is case sensitive, but keywords in SQL statements are case insensitive.

Example:

select * from device where device_id = 'as********' limit 0,5

SELECT clause

select [field]/[count(*)/count(1)] from device

field indicates the parameter to be obtained. Replace it with the response parameter name. You can also enter an asterisk (*) to obtain all parameters.

To obtain the number of queried devices, use count(*) or count(1).

FROM clause

from device

The parameter following from indicates the name of the resource to be queried. Currently, device is supported.

WHERE clause (optional)

WHERE [condition1] AND [condition2]

Up to five conditions are supported. Conditions cannot be nested. For details about the parameters that support query, see Description of query condition parameters and Supported operators.

AND and OR are supported. For details about the priority, see the standard SQL syntax. By default, the priority of AND is higher than that of OR.

LIMIT clause (optional)

limit [offset,] rows

offset indicates the query offset. rows indicates the maximum number of rows in the query result. Examples:

  • limit n ; Example: (select * from device limit 10)

Up to n records can be returned.

  • limit m,n; Example: (select * from device limit 20,10) The query offset is m. Up to n records can be returned.

Constraints

The maximum value of offset is 500, and the maximum value of rows is 50. If the limit clause is not specified, limit 10 is used by default.

ORDER BY clause (optional)

This clause is used for customizing sorting. Currently, the marker parameter supports custom sorting.

order by marker [asc]/[desc]

If the clause is not specified, random sorting is used by default.

Description of query condition parameters

Parameter Type Description Value Range
app_id string Resource space ID. The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
device_id string Device ID. The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
gateway_id string Gateway ID. The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
product_id string ID of the product associated with the device. The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
device_name string Device name. The value can contain a maximum of 256 characters. Only letters, digits, and special characters (_?'#(),&%@!-) are allowed.
node_id string Node ID. The value can contain a maximum of 64 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.
status string Device status. ONLINE, OFFLINE, ABNORMAL, INACTIVE, and FROZEN
node_type string Device node type. GATEWAY (directly connected device or gateway) and ENDPOINT (indirectly connected device)
tag_key string Tag key. The value can contain a maximum of 64 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
tag_value string Tag value. The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
sw_version string Software version. The value can contain a maximum of 64 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
fw_version string Firmware version. The value can contain a maximum of 64 characters. Only letters, digits, underscores (_), periods (.), and hyphens (-) are allowed.
group_id string Group ID. The value can contain a maximum of 36 characters. Only hexadecimal characters and hyphens (-) are allowed.
create_time string Device registration time. Format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'. Example: 2015-06-06T12:10:10.000Z
marker string Result record ID. The value is a string of 24 hexadecimal characters. Example: ffffffffffffffffffffffff

Supported operators

Operator Parameter that Support the Operator
= All
!= All
> create_time and marker
< create_time and marker
like device_name, node_id, tag_key, and tag_value
in Fields except tag_key and tag_value
not in Fields except tag_key and tag_value

SQL restrictions

  • like: Only prefix match is supported. Suffix match or wildcard match is not supported. At least four characters must be contained for prefix match. Special characters cannot be contained. Only letters, digits, underscores (_), and hyphens (-) are allowed. The prefix must end with %.

  • Only count(*) or count(1) is supported.

  • Other SQL statements, such as nested SQL statements, union, join, and alias, are not supported.

  • The SQL statement can contain up to 400 characters. Up to five request conditions are supported.

  • The condition value cannot be null or an empty string.

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}/search/query-devices

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.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

sql

Yes

String

SQL statement. For details, see the SQL-like syntax description.

Minimum: 1

Maximum: 400

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

devices

Array of SearchDevice objects

Device query result list.

count

Long

Total number of records that meet query conditions. This parameter is returned only when select count(*)/count(1) is used.

Table 5 SearchDevice

Parameter

Type

Description

app_id

String

Resource space ID.

Maximum: 36

app_name

String

Resource space name.

device_id

String

Device ID, used to uniquely identify a device. The value of this parameter is specified during device registration or allocated by the platform. If the value is allocated by the platform, the value is in the format of [product_id]_[node_id].

Maximum: 256

node_id

String

Device identifier. This parameter is set to the IMEI, MAC address, or serial number.

Maximum: 64

gateway_id

String

Gateway ID, which is the device ID of the parent device. The gateway ID is the same as the device ID if the device is a directly connected device. If the device is an indirectly connected device, the gateway ID is the device ID of the directly connected device with which it associates.

Maximum: 256

device_name

String

Device name.

Maximum: 256

node_type

String

Device node type.

  • ENDPOINT: an indirectly connected device.

  • GATEWAY: a directly connected device or gateway.

  • UNKNOWN: The device node type is unknown.

fw_version

String

Firmware version of the device.

Maximum: 256

sw_version

String

Software version of the device.

Maximum: 256

device_sdk_version

String

Device SDK information.

Maximum: 256

product_id

String

Unique ID of the product associated with the device.

product_name

String

Name of the product associated with the device.

groups

Object

Device group list.

status

String

Device status.

  • ONLINE: The device is online.

  • OFFLINE: The device is offline.

  • ABNORMAL: The device is abnormal.

  • INACTIVE: The device is not activated.

  • FROZEN: The device is frozen.

tags

Object

List of device tags.

marker

String

Query result record ID.

Example Requests

Runs the SQL statement to query all devices.

POST https://{endpoint}/v5/iot/{project_id}/search/query-devices

{
  "sql" : "select * from device"
}

Example Responses

Status code: 200

OK

{
  "devices" : [ {
    "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
    "marker" : "5c8f3d2d3df1f10d803adbda",
    "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
    "node_id" : "ABC123456789",
    "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
    "device_name" : "dianadevice",
    "node_type" : "ENDPOINT",
    "fw_version" : "1.1.0",
    "sw_version" : "1.1.0",
    "device_sdk_version" : "1.1.0",
    "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
    "status" : "INACTIVE",
    "tags" : [ {
      "tag_key" : "testTagName",
      "tag_value" : "testTagValue"
    } ]
  } ]
}

Status Codes

Status Code

Description

200

OK

400

Bad Request

401

Unauthorized

403

Forbidden

500

Internal Server Error

Error Codes

See Error Codes.