Updated on 2024-01-26 GMT+08:00

Query a Device Shadow

Function

This API is used by an application to query the device shadow of a specific device, including the desired device properties (in the desired section) and the latest properties reported by the device (in the reported section).

Introduction: A device shadow is a JSON file used to store and obtain device status.

  • Each device has only one device shadow, which is uniquely identified by the device ID.

  • A device shadow stores the properties (status) reported by a device and the properties (status) expected by an application.

  • You can obtain and set the device properties through the device shadow, regardless of whether the device is online.

  • After a device goes online or reports data, if the reported properties are different from the desired ones, the desired properties are delivered to the device. The desired properties can be delivered only when they have been defined in the product model and method is specified as W.

Restrictions: Keys in the device shadow JSON file cannot contain periods (.), dollar signs ($), and the null character (hexadecimal ASCII code 00). Otherwise, the device shadow file cannot be updated.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

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

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: device ID, which uniquely identifies 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]. 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. 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. This parameter is required only when the API is called from the management plane in the physical multi-tenant scenario. You can log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID.

Response Parameters

Status code: 200

Table 3 Response body parameters

Parameter

Type

Description

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].

shadow

Array of DeviceShadowData objects

Device shadow data structure.

Table 4 DeviceShadowData

Parameter

Type

Description

service_id

String

Device service ID, which is defined in the product model associated with the device.

Minimum: 1

Maximum: 256

desired

DeviceShadowProperties object

Latest desired properties delivered to the device. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model.

reported

DeviceShadowProperties object

Latest properties reported by the device. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model.

version

Long

Version of the device shadow. When this parameter is carried, the platform will check whether the value is the same as the current shadow version. The initial value is 0.

Table 5 DeviceShadowProperties

Parameter

Type

Description

properties

Object

Properties stored in the device shadow. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model. Only one-layer structure is supported, as shown in the example. Note: Keys in the JSON file cannot contain periods (.), dollar signs ($), and the null character (hexadecimal ASCII code 00). Keys that contain these special characters will cause device shadow update failures.

event_time

String

Time when the event occurred. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

Maximum: 256

Example Requests

Query the device shadow of a specified device.

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

Example Responses

Status code: 200

OK

{
  "device_id" : "40fe3542-f4cc-4b6a-98c3-61a49ba1acd4",
  "shadow" : [ {
    "service_id" : "WaterMeter",
    "desired" : {
      "properties" : {
        "temperature" : "60"
      },
      "event_time" : "20151212T121212Z"
    },
    "reported" : {
      "properties" : {
        "temperature" : "60"
      },
      "event_time" : "20151212T121212Z"
    },
    "version" : 1
  } ]
}

Status Codes

Status Code

Description

200

OK

403

FORBIDDEN

404

Not Found

500

INTERNAL SERVER ERROR

Error Codes

See Error Codes.