Node.js 的 MindSphere SDK– 入门指南¶
使用 Node.js 的 MindSphere SDK 的先决条件¶
- Node.js 版本8或更高版本
- 具有 MindSphere Service APIs 所需范围的用户授权令牌或服务凭证
-
环境变量
HOST_ENVIRONMENT
被设置为当前区域。在 Cloud Foundry 中托管应用程序时,必须将 variable 添加到 manifest 文件中:env: HOST_ENVIRONMENT: eu1
如果没有指明,Europe 1 区域 SDK 中
HOST_ENVIRONMENT
默认为eu1
,China 1 区域 SDK 中HOST_ENVIRONMENT
默认为cn1
。
安装说明¶
安装 Node.js 的 MindSphere SDK¶
从 Siemens Industry Online Support (SIOS) Portal 中下载 Node.js 的 MindSphere SDK。
为 Node.js 依赖添加 MindSphere SDK¶
-
复制
.tgz
文件到项目根目录中的文件夹,例如:repo
。 -
安装
mindsphere-sdk-node-core
模块:```header tab="Template" npm install {folder}\mindsphere-sdk-node-core-x.y.z.tgz
```header tab="Example" npm install repo\mindsphere-sdk-node-core-1.0.0.tgz
-
安装服务模块:
```header tab="Template" npm install {folder}{service_name}-sdk-x.y.z.tgz
```header tab="Example" npm install repo\assetmanagement-sdk-3.9.0.tgz
说明
{x.y.z}
是 Node.js 的 MindSphere Core 或 Service SDK 的版本号。
服务客户端和凭证配置¶
服务客户端的最低级别构建块是ClientConfig
和 MindsphereCredentials
。这些对象可以在客户端实例中分享。
客户端配置¶
以下代码块展示了如何构建ClientConfig
对象的示例:
let config = new ClientConfig({ proxy: <proxy_value>, timeout: <timeout_value> });
ClientConfig
可以使用以下可选参数进行配置:
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
proxy | 完全代理 URL | String | |
timeout | 以毫秒为单位的连接超时 | Integer | 10000 |
hostEnvironment | MindSphere 区域 | String | eu1 |
China 1 默认值
对于 China 1 区域 SDK,hostEnvironment
默认值为cn1
。
MindsphereCredentials 配置¶
MindsphereCredentials
有子类别 UserCredentials
、AppCredentials
和 TenantCredentials
。对于这些对象的可用配置参数在下面列出。
以下配置参数可以用编程方式为 UserCredentials
对象进行设置:
名称 | 描述 | 类型 | 使用情景 |
---|---|---|---|
authorization | 不记名令牌,如果可用 | String | 用户令牌 |
以下配置参数可以用编程方式为 TenantCredentials
对象进行设置:
名称 | 描述 | 类型 | 使用情景 |
---|---|---|---|
clientId | 租户服务凭证 ID | String | 技术令牌 |
clientSecret | 租户服务凭证 ID | String | 技术令牌 |
tenant | 租户服务凭证 ID | String | 技术令牌 |
以下配置参数可以用编程方式为 AppCredentials
对象进行设置:
名称 | 描述 | 类型 | 使用情景 |
---|---|---|---|
keyStoreClientId | 应用服务凭证 ID | String | 技术令牌 |
keyStoreClientSecret | 应用服务凭证密钥 | String | 技术令牌 |
appName | 应用名称 | String | 技术令牌 |
appVersion | 应用版本 | String | 技术令牌 |
hostTenant | 主租户 | String | 技术令牌 |
userTenant | 用户租户 | String | 技术令牌 |
更多信息请参考创建 MindsphereCredentials 对象的代码示例。
说明
如果通过环境变量设置服务凭证,则不需要构建 MindsphereCredentials
对象。
服务客户端安装和使用¶
服务客户端实例接受ClientConfig
和 MindsphereCredentials
对象以及作为可选参数的子类。
说明
UserCredentials
、AppCredentials
和 TenantCredentials
是 MindsphereCredentials
的子类。开发者可以传递这些子类的对象到客户端,如果各自的凭证以编程方式设置。
代码样本使用 IoT TimeSeries API 客户端,占位符由尖括号< >
表示:
// Require UserCredentials and ClientConfig from `mindsphere-sdk-node-core` module
const ClientConfig = require('mindsphere-sdk-node-core').ClientConfig;
const UserCredentials = require('mindsphere-sdk-node-core').UserCredentials;
// Require TimeseriesClient from `timeseries-sdk` module
const TimeSeriesClient = require('timeseries-sdk').TimeSeriesClient;
// Proxy value can be set in the ClientConfig object
let config = new ClientConfig({ proxy: <proxy_value>, timeout: <timeout_in_seconds> });
// Construct the UserCredentials object
let credentials = new UserCredentials({'authorization': '<user_token>'});
// Construct the TimeSeriesClient object using the config and credentials objects
let time_series_client = new TimeSeriesClient(config, credentials);
try {
let timeseries_data = await time_series_client.getTimeseries(
{
entity: <entity_id>,
propertysetname: <property_set_name>,
from: <from_date>,
to: <to_date>
}
);
} catch (err) {
//Exception Handling
}