更新时间:2024-07-02 GMT+08:00
分享

灵活搜索设备列表

功能介绍

接口说明

应用服务器使用SQL语句调用该接口,灵活的搜索所需要的设备资源列表

限制

  • 标准版实例、企业版实例支持该接口调用,基础版不支持。

  • 单账号调用该接口的 TPS 限制最大为1/S(每秒1次请求数)

类SQL语法使用说明

类SQL语句有select、from、where(可选)、order by(可选)、limit子句(可选)组成,长度限制为400个字符。子句里的内容大小写敏感,SQL语句的关键字大小写不敏感。

示例:

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

SELECT子句

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

其中field为需要获取的字段,请参考响应参数字段名称,也可填*,获取所有字段。

如果需要统计搜索的设备个数,请填count(*)或者count(1).

FROM子句

from device

from后为要查询的资源名,当前支持"device"

WHERE子句(可选)

WHERE [condition1] AND [condition2]

最多支持5个condition,不支持嵌套;支持的检索字段请参见下面的搜索条件字段说明支持的运算符章节

连接词支持AND、OR,优先级参考标准SQL语法,默认AND优先级高于OR。

LIMIT子句(可选)

limit [offset,] rows

offset标识搜索的偏移量,rows标识返回搜索结果的最大行数,例如:

  • limit n ;示例(select * from device limit 10)

    最大返回n条结果数据

  • limit m,n; 示例(select * from device limit 20,10) 搜索偏移量为m,最大返回n条结果数据

限制

offset 最大 500, rows最大50,如果不填写limit子句,默认为limit 10

ORDER BY子句(可选)

用于实现自定义排序,当前支持自定义排序的字段为:"marker"。

order by marker [asc]/[desc]

子句不填写时默认逻辑为随机排序

搜索条件字段说明

字段名 类型 说明 取值范围
app_id string 资源空间ID 长度不超过36,只允许字母、数字、下划线(_)、连接符(-)的组合。
device_id string 设备ID 长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。
gateway_id string 网关ID 长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。
product_id string 设备关联的产品ID 长度不超过36,只允许字母、数字、下划线(_)、连接符(-)的组合。
device_name string 设备名称 长度不超过256,只允许中文、字母、数字、以及_?'#().,&%@!-等字符的组合符。
node_id string 设备标识码 长度不超过64,只允许字母、数字、下划线(_)、连接符(-)的组合
status string 设备的状态 ONLINE(在线)、OFFLINE(离线)、ABNORMAL(异常)、INACTIVE(未激活)、FROZEN(冻结)
node_type string 设备节点类型 GATEWAY(直连设备或网关)、ENDPOINT(非直连设备)
tag_key string 标签键 长度不超过64,只允许中文、字母、数字、以及_.-等字符的组合。
tag_value string 标签值 长度不超过128,只允许中文、字母、数字、以及_.-等字符的组合。
sw_version string 软件版本 长度不超过64,只允许字母、数字、下划线(_)、连接符(-)、英文点(.)的组合。
fw_version string 固件版本 长度不超过64,只允许字母、数字、下划线(_)、连接符(-)、英文点(.)的组合。
group_id string 群组Id 长度不超过36,十六进制字符串和连接符(-)的组合
create_time string 设备注册时间 格式:yyyy-MM-dd'T'HH:mm:ss.SSS'Z',如:2015-06-06T12:10:10.000Z
marker string 结果记录ID 长度为24的十六进制字符串,如ffffffffffffffffffffffff

支持的运算符

运算符 支持的字段
= 所有
!= 所有
> create_time、marker
< create_time、marker
like device_name、node_id、tag_key、tag_value
in 除tag_key、tag_value以外字段
not in 除tag_key、tag_value以外字段

SQL 限制

  • like: 只支持前缀匹配,不支持后缀匹配或者通配符匹配。前缀匹配不得少于4个字符,且不能包含任何特殊字符(只允许中文、字母、数字、下划线(_)、连接符(-)). 前缀后必须跟上"%"结尾。

  • 不支持除了count(*)/count(1)以外的其他任何函数。

  • 不支持其他SQL用法,如嵌套SQL、union、join、别名(Alias)等用法

  • SQL长度限制为400个字符,单个请求条件最大支持5个。

  • 不支持"null"和空字符串等条件值匹配

调用方法

请参见如何调用API

URI

POST /v5/iot/{project_id}/search/query-devices

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数说明:项目ID。获取方法请参见 获取项目ID

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数说明:用户Token。通过调用IAM服务 获取IAM用户Token接口获取,接口返回的响应消息头中“X-Subject-Token”就是需要获取的用户Token。简要的获取方法样例请参见 Token认证

Instance-Id

String

参数说明:实例ID。物理多租下各实例的唯一标识,建议携带该参数,在使用专业版时必须携带该参数。您可以在IoTDA管理控制台界面,选择左侧导航栏“总览”页签查看当前实例的ID,具体获取方式请参考查看实例详情

表3 请求Body参数

参数

是否必选

参数类型

描述

sql

String

搜索sql语句,具体使用方法见类SQL语法使用说明章节

最小长度:1

最大长度:400

响应参数

状态码: 200

表4 响应Body参数

参数

参数类型

描述

devices

Array of SearchDevice objects

搜索设备结果列表。

count

Long

满足查询条件的记录总数(只有条件为select count(*)/count(1)时单独返回)。

表5 SearchDevice

参数

参数类型

描述

app_id

String

资源空间ID。

最大长度:36

app_name

String

资源空间名称。

device_id

String

设备ID,用于唯一标识一个设备。在注册设备时直接指定,或者由物联网平台分配获得。由物联网平台分配时,生成规则为"product_id" + "_" + "node_id"拼接而成。

最大长度:256

node_id

String

设备标识码,通常使用IMEI、MAC地址或Serial No作为node_id。

最大长度:64

gateway_id

String

网关ID,用于标识设备所属的父设备,即父设备的设备ID。当设备是直连设备时,gateway_id与设备的device_id一致。当设备是非直连设备时,gateway_id为设备所关联的父设备的device_id。

最大长度:256

device_name

String

设备名称。

最大长度:256

node_type

String

设备节点类型。

  • ENDPOINT:非直连设备。

  • GATEWAY:直连设备或网关。

  • UNKNOWN:未知。

fw_version

String

设备的固件版本。

最大长度:256

sw_version

String

设备的软件版本。

最大长度:256

device_sdk_version

String

设备的sdk信息。

最大长度:256

product_id

String

设备关联的产品ID,用于唯一标识一个产品模型。

product_name

String

设备关联的产品名称。

groups

Object

设备组列表。

status

String

设备的状态。

  • ONLINE:设备在线。

  • OFFLINE:设备离线。

  • ABNORMAL:设备异常。

  • INACTIVE:设备未激活。

  • FROZEN:设备冻结。

tags

Object

设备的标签列表。

marker

String

搜索结果记录Id。

请求示例

通过sql查询设备,查询所有设备。

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

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

响应示例

状态码: 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"
    } ]
  } ]
}

SDK代码示例

SDK代码示例如下。

通过sql查询设备,查询所有设备。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.model.*;


public class SearchDevicesSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
        String iotdaEndpoint = "<YOUR ENDPOINT>";

        ICredential auth = new BasicCredentials()
                // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
                .withRegion(new Region("cn-north-4", iotdaEndpoint))
                .build();
        SearchDevicesRequest request = new SearchDevicesRequest();
        SearchSql body = new SearchSql();
        body.withSql("select * from device");
        request.withBody(body);
        try {
            SearchDevicesResponse response = client.searchDevices(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

通过sql查询设备,查询所有设备。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.auth.credentials import DerivedCredentials
from huaweicloudsdkcore.region.region import Region as coreRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkiotda.v5 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    // ENDPOINT请在控制台的"总览"界面的"平台接入地址"中查看应用侧的https接入地址
    iotdaEndpoint = "<YOUR ENDPOINT>";

    credentials = BasicCredentials(ak, sk).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())

    client = IoTDAClient.new_builder() \
        .with_credentials(credentials) \
        # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4)
        .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \
        .build()

    try:
        request = SearchDevicesRequest()
        request.body = SearchSql(
            sql="select * from device"
        )
        response = client.search_devices(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

通过sql查询设备,查询所有设备。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
    core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址
    endpoint := "<YOUR ENDPOINT>"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate"
        WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
        Build()

    client := iotda.NewIoTDAClient(
        iotda.IoTDAClientBuilder().
            // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象
            WithRegion(region.NewRegion("cn-north-4", endpoint)).
            WithCredential(auth).
            Build())

    request := &model.SearchDevicesRequest{}
	request.Body = &model.SearchSql{
		Sql: "select * from device",
	}
	response, err := client.SearchDevices(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK

400

Bad Request

401

Unauthorized

403

Forbidden

500

Internal Server Error

错误码

请参见错误码

分享:

    相关文档

    相关产品