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

查询设备

功能介绍

应用服务器可调用此接口查询物联网平台中指定设备的详细信息。

调用方法

请参见如何调用API

URI

GET /v5/iot/{project_id}/devices/{device_id}

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

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

device_id

String

参数说明:设备ID,用于唯一标识一个设备。在注册设备时直接指定,或者由物联网平台分配获得。由物联网平台分配时,生成规则为"product_id" + "" + "node_id"拼接而成。 取值范围:长度不超过128,只允许字母、数字、下划线()、连接符(-)的组合。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

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

Instance-Id

String

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

响应参数

状态码: 200

表3 响应Body参数

参数

参数类型

描述

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:未知。

description

String

设备的描述信息。

最大长度:2048

fw_version

String

设备的固件版本。

最大长度:256

sw_version

String

设备的软件版本。

最大长度:256

device_sdk_version

String

设备的sdk信息。

最大长度:256

auth_info

AuthInfoRes object

设备的接入认证信息。

product_id

String

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

product_name

String

设备关联的产品名称。

status

String

设备的状态。

  • ONLINE:设备在线。

  • OFFLINE:设备离线。

  • ABNORMAL:设备异常。

  • INACTIVE:设备未激活。

  • FROZEN:设备冻结。

create_time

String

在物联网平台注册设备的时间。格式:yyyyMMdd'T'HHmmss'Z',如20151212T121212Z。

connection_status_update_time

String

设备最近一次连接状态(ONLINE:在线,OFFLINE:离线,ABNORMAL:异常)变化时间。格式:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' ,如2015-12-12T12:12:122Z。

active_time

String

设备激活时间。格式:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' ,如2015-12-12T12:12:122Z。

tags

Array of TagV5DTO objects

设备的标签列表。

extension_info

Object

设备扩展信息。用户可以自定义任何想要的扩展信息,如果在创建设备时为子设备指定该字段,将会通过MQTT接口“平台通知网关子设备新增“将该信息通知给网关。

表4 AuthInfoRes

参数

参数类型

描述

auth_type

String

参数说明:鉴权类型。注意:不填写auth_type默认为密钥认证接入方式(SECRET)。 取值范围

  • SECRET:使用密钥认证接入方式。

  • CERTIFICATES:使用证书认证接入方式。

secret

String

参数说明:设备密钥,认证类型使用密钥认证接入(SECRET)可填写该字段。注意:NB设备密钥由于协议特殊性,只支持十六进制密钥接入;查询设备列表接口不返回该参数。 取值范围:长度不低于8不超过32,只允许字母、数字、下划线(_)、连接符(-)的组合。

最小长度:8

最大长度:32

secondary_secret

String

参数说明:设备备用密钥,认证类型使用密钥认证接入(SECRET)该字段有效,当主密钥校验不通过时,会启用辅密钥校验,辅密钥与主密钥有相同的效力;辅密钥对coap协议接入的设备不生效。注意:NB设备密钥由于协议特殊性,只支持十六进制密钥接入;查询设备列表接口不返回该参数。 取值范围:长度不低于8不超过32,只允许字母、数字、下划线(_)、连接符(-)的组合。

最小长度:8

最大长度:32

fingerprint

String

参数说明:证书指纹,认证类型使用证书认证接入(CERTIFICATES)该字段有效,注册设备时不填写该字段则取第一次设备接入时的证书指纹。 取值范围:长度为40的十六进制字符串或者长度为64的十六进制字符串。

secondary_fingerprint

String

参数说明:证书备用指纹,认证类型使用证书认证接入(CERTIFICATES)该字段有效,当主指纹校验不通过时,会启用辅指纹校验,辅指纹与主指纹有相同的效力。 取值范围:长度为40的十六进制字符串或者长度为64的十六进制字符串。

secure_access

Boolean

参数说明:指设备是否通过安全协议方式接入。 取值范围

  • true:通过安全协议方式接入。

  • false:通过非安全协议方式接入。非安全接入的设备存在被仿冒等安全风险,请谨慎使用。

缺省值:true

timeout

Integer

参数说明:设备接入的有效时间,单位:秒,默认值:0 若设备在有效时间内未接入物联网平台并激活,则平台会删除该设备的注册信息。若设置为“0”,则表示平台不会删除该设备的注册信息(建议填写为“0”)。 注意:该参数只对直连设备生效。

最小值:0

最大值:2147483647

缺省值:0

表5 TagV5DTO

参数

参数类型

描述

tag_key

String

参数说明:标签键,在同一资源下标签键唯一。绑定资源时,如果设置的键已存在,则将覆盖之前的标签值。如果设置的键值不存在,则新增标签。 取值范围:长度不超过64,只允许中文、字母、数字、以及_.-等字符的组合。

tag_value

String

参数说明:标签值。 取值范围:长度不超过128,只允许中文、字母、数字、以及_.-等字符的组合。

请求示例

查询指定设备详情。

GET https://{endpoint}/v5/iot/{project_id}/devices/{device_id}

响应示例

状态码: 200

OK

{
  "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
  "app_name" : "testAPP01",
  "device_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
  "node_id" : "ABC123456789",
  "gateway_id" : "d4922d8a-6c8e-4396-852c-164aefa6638f",
  "device_name" : "dianadevice",
  "node_type" : "ENDPOINT",
  "description" : "watermeter device",
  "fw_version" : "1.1.0",
  "sw_version" : "1.1.0",
  "auth_info" : {
    "auth_type" : "SECRET",
    "secret" : "3b935a250c50dc2c6d481d048cefdc3c",
    "fingerprint" : "dc0f1016f495157344ac5f1296335cff725ef22f",
    "secure_access" : true,
    "timeout" : 0
  },
  "product_id" : "b640f4c203b7910fc3cbd446ed437cbd",
  "product_name" : "Thermometer",
  "status" : "ONLINE",
  "create_time" : "20190303T081011Z",
  "connection_status_update_time" : "2019-03-03T08:10:111Z",
  "active_time" : "2019-03-03T08:10:111Z",
  "tags" : [ {
    "tag_key" : "testTagName",
    "tag_value" : "testTagValue"
  } ],
  "extension_info" : {
    "aaa" : "xxx",
    "bbb" : 0
  }
}

SDK代码示例

SDK代码示例如下。

Java

 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
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.iotda.v5.region.IoTDARegion;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.model.*;


public class ShowDeviceSolution {

    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");

        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("<YOUR REGION>"))
                .build();
        ShowDeviceRequest request = new ShowDeviceRequest();
        try {
            ShowDeviceResponse response = client.showDevice(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());
        }
    }
}

Python

 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
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.auth.credentials import DerivedCredentials
from huaweicloudsdkiotda.v5.region.iotda_region import IoTDARegion
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 = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("CLOUD_SDK_SK")

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

    client = IoTDAClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(IoTDARegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ShowDeviceRequest()
        response = client.show_device(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

 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
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/services/iotda/v5/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")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
        Build()

    client := iotda.NewIoTDAClient(
        iotda.IoTDAClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ShowDeviceRequest{}
	response, err := client.ShowDevice(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

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

状态码

状态码

描述

200

OK

403

Forbidden

404

Not Found

错误码

请参见错误码

分享:

    相关文档

    相关产品