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

Querying Historical Device Data

Typical Scenario

The IoT platform receives and saves service data reported by devices during daily operation. If an NA needs to view the historical data reported by a device to the IoT platform, the NA can call this API to obtain the data.

API Function

This API is used by an NA to query historical data reported by a specified device to the IoT platform based on the device ID.

API Prototype

Method

GET

URL

https://server:port/iocm/app/data/v1.2.0/deviceDataHistory?deviceId={deviceId}&gatewayId={gatewayId}&appId={appId}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime}&endTime={endTime}

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

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

query

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

gatewayId

Mandatory

String

query

Identifies a gateway. 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 (that is, the gateway) with which it associates.

pageNo

Optional

Integer

query

Indicates the page number. The default value is 0, indicating that the first page is queried. You are advised to set pageNo and pageSize to query historical device data.

pageSize

Optional

Integer

query

Indicates the number of records to be displayed on each page. The default value is 1 and the maximum value is 2000. Set this parameter based on the average size of data reported by devices. Ensure that the data package of a single query does not exceed 16 MB, and set the timeout period based on the package size of a single query and network bandwidth.

startTime

Optional

String

query

Indicates the start time. Historical shadow data generated later than the specified start time is queried. The value is in the format of yyyyMMdd'T'HHmmss'Z'. An example value is 20151212T121212Z.

endTime

Optional

String

query

Indicates the end time. Historical shadow data generated earlier than the specified end time is queried. The value is in the format of yyyyMMdd'T'HHmmss'Z'. An example value is 20151212T121212Z.

Response Parameters

Status Code: 200 OK

Parameter

Type

Description

totalCount

Long

Indicates the number of queried records.

pageNo

Long

Indicates the page number.

pageSize

Long

Indicates the number of records on each page.

deviceDataHistoryDTOs

List<DeviceDataHistoryDTO>

Indicates the list of historical device data.

DeviceDataHistoryDTO structure

Parameter

Type

Description

serviceId

String(256)

Identifies a service.

deviceId

String(256)

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

gatewayId

String(256)

Identifies a gateway. 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 (that is, the gateway) with which it associates.

appId

String(256)

Identifies the application to which the device belongs.

data

JsonObject

Indicates the data reported by the device.

timestamp

String(256)

Indicates the timestamp when the data is reported. The value is in the format of yyyyMMdd'T'HHmmss'Z'. An example value is 20151212T121212Z.

Request Example

Method: GET
Request:
https://server:port/iocm/app/data/v1.2.0/deviceDataHistory?deviceId={deviceId}&gatewayId={gatewayId}&appId={appId}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime}&endTime={endTime}
Header:
app_key: ******
Authorization: Bearer *****
Content-Type: application/json

Response Example

Response:
Status Code: 200 OK
Content-Type: application/json
Body:
{
    "totalCount":"****",
    "pageNo":"*****",
    "pageSize":"*****",
    "deviceDataHistoryDTOs":[
        {
            "serviceId":"*****",
            "deviceId":"*****",
            "gatewayId":"*****",
            "appId":"*****",
            "data":"*****",
            "timestamp":"*****"
        }
    ]
}

Error Codes

HTTP Status Code

Error Code

Error Description

Remarks

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.

400

100216

The application input is invalid.

The application input is invalid.

Recommended handling: Check whether parameters in the API request are correct by referring to the request parameter description. For example, check whether the value of pageSize exceeds 2000.

400

100419

The deviceId and gatewayId can't be both null.

The deviceId and gatewayId parameters cannot be null at the same time.

Recommended handling: Check whether deviceId or gatewayId is set.

403

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.

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

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

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.

500

50252

Internal server error.

An internal server error occurs.

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