Skip to content

Node.js 的 IoT Time Series Client

简介

IoT Time Series Node.js 客户端允许你与 assets 相关的时间序列数据进行交互。有关此服务的更多信息请参考IoT Time Series

提示

在 IoT 环境中,assetsentityaspectspropertyset
下面示例中的占位符用尖括号 < > 表示。

时间序列操作

客户端名称: TimeSeriesClient

读取时间序列数据

此操作为以下使用情景调用 IoT Time Series API:

  • 读取 asset 单个 aspect 的时间序列数据。
  • 返回指定时间范围的数据。
  • 如果没有提供范围,则返回最新值。
  • 返回选定字段和限制的时间序列数据。
// Require AppCredentials and ClientConfig from mindsphere-sdk-node-core module
let AppCredentials = require('mindsphere-sdk-node-core').AppCredentials;
let ClientConfig = require('mindsphere-sdk-node-core').ClientConfig;

// Require TimeSeriesClient from timeseries-sdk module
const TimeSeriesClient = require('timeseries-sdk').TimeSeriesClient;

// Construct the ClientConfig and AppCredentials objects
let config = new ClientConfig();
let credentials = new AppCredentials();

// Construct the TimeseriesAggregateClient object
let time_series_client = new TimeSeriesClient(config, credentials);

let timeseries_data;
try {

  const request_object = {
    entity: <entity_id>,
    propertysetname: <property_set>,
    from: <from_date>,
    to: <to_date>,
    limit: <limit>,
    select: <select>
  };

  timeseries_data = await time_series_client.getTimeseries(request_object);
} catch (ex) {
  // Exception handling
}

读取最新的时间序列数据

读取 asset 单个 aspect 的时间序列数据的最新值。

// Construct the TimeseriesAggregateClient object as shown above

let timeseries_data;
try {

  const request_object = {
    entity: <entity_id>,
    propertysetname: <property_set>
  };

  timeseries_data = await time_series_client.getTimeseries(request_object);
} catch (ex) {
  // Exception handling
}

写时间序列数据

写入 asset 单个 aspect 的时间序列数据。这会覆写现在的时间序列数据。

说明

写入有效负载的最大大小为1 MB。

// Construct the TimeseriesAggregateClient object as shown above

let timeseries_data = [
  {
    FRWheel: 5.0,
    FLWheel: 78.0,
    RLWheel: 6.0,
    RRWheel: 10.0,
    _time: '2018-01-24T05:04:41.363Z'
  }
];

try {

  const request_object = {
    entity: <entity_id>,
    propertysetname: <property_set>,
    timeseries: <timeseries_data>
  };

  await time_series_client.putTimeseries(request_object);
} catch (ex) {
  // Exception handling
}

删除时间序列数据

在给定的时间范围内从 asset 的某个 aspect 删除时间序列数据。这也会删除 aspect 中所有变量的数据。

说明

只有不超过6个月的数据才可以立即删除。

// Construct the TimeseriesAggregateClient object as shown above

try {

  const request_object = {
    entity: <entity_id>,
    propertysetname: <property_set>,
    from: <from_date>,
    to: <to_date>
  };

  await time_series_client.deleteTimeseries(request_object);
} catch (ex) {
  // Exception handling
}

Last update: March 22, 2023