MindSphere SDK V2 for Java - 入门指南¶
使用 MindSphere SDK V2 for Java 的先决条件¶
- Java 8 可用。
- 使用构建工具 Gradle 或 Maven。
- 具有 API 所需范围的授权令牌或服务凭证。
- 环境 variable
HOST_ENVIRONMENT
设置为当前区域。在 Cloud Foundry 中托管应用时,必须在 manifest 文件中添加 variable:
env:
HOST_ENVIRONMENT:eu1
如果未指定,HOST_ENVIRONMENT
默认为 eu1
。
提示
可以将服务凭证设置为环境 variables,以使客户端能够自行获取令牌。
-
MINDSPHERE_CLIENT_ID
指定服务凭证 ID -
MINDSPHERE_CLIENT_SECRET
指定服务凭证密钥 -
MINDSPHERE_TENANT
指定租户名称
注意
您负责保证凭证的安全。您决定通过环境 variables 提供凭证是否足够安全。
安装说明¶
下载 MindSphere SDK V2 for Java¶
从西门子工业在线支持 (SIOS) 门户下载 MindSphere SDK for Java。核心模块以及服务模块的 jar
和 pom
文件具有以下结构:
``` header tab="Core Module" com\siemens\mindsphere\mindsphere-sdk-java-core{x.y.z}\mindsphere-sdk-java-core-{x.y.z}.jar com\siemens\mindsphere\mindsphere-sdk-java-core{x.y.z}\mindsphere-sdk-java-core-javadoc.jar com\siemens\mindsphere\mindsphere-sdk-java-core{x.y.z}\mindsphere-sdk-java-core.pom
``` header tab="Service Modules"
com\siemens\mindsphere\{service_name}-sdk\{x.y}\{service_name}-sdk-{x.y}.jar
com\siemens\mindsphere\{service_name}-sdk\{x.y}\{service_name}-sdk-{x.y}-javadoc.jar
com\siemens\mindsphere\{service_name}-sdk\{x.y}\{service_name}-sdk-{x.y}.pom
说明
{x.y.z}
是 MindSphere Core SDK for Java 的版本号(例如2.0.0
)。下载 MindSphere Core SDK for Java 的传递相关项时需要文件{service_name}-sdk-{x.y.z}.pom
。{x.y}
是 API 规范的版本号(例如3.0
)。下载 MindSphere Service SDK for Java 的传递相关项时需要文件{service_name}-sdk-{x.y}.pom
。
添加 MindSphere SDK V2 相关项¶
- 创建核心和服务模块文件夹结构,作为本地 Maven 仓库:
header tab="Core Module" $PATH\com\siemens\mindsphere\mindsphere-sdk-java-core\{x.y.z}\mindsphere-sdk-java-core-{x.y.z}.jar $PATH\com\siemens\mindsphere\mindsphere-sdk-java-core\{x.y.z}\mindsphere-sdk-java-core-javadoc.jar $PATH\com\siemens\mindsphere\mindsphere-sdk-java-core\{x.y.z}\mindsphere-sdk-java-core.pom
``` header tab="Service Modules" $PATH\com\siemens\mindsphere{service_name}-sdk{x.y}{service_name}-sdk-{x.y}.jar $PATH\com\siemens\mindsphere{service_name}-sdk{x.y}{service_name}-sdk-{x.y}-javadoc.jar $PATH\com\siemens\mindsphere{service_name}-sdk{x.y}{service_name}-sdk-{x.y}.pom
其中 `$PATH` 为:
* 在 Mac 中:`~/.m2/repository`
* 在 Windows 中:`C:\Users\{user_name}\.m2\repository`
* 在 Linux 中:`/home/{user_name}/.m2/repository`
2. 注册仓库并在项目的构建配置文件中加入相关项。
``` header tab="Maven"
<!-- Modify the following sections of the pom.xml file -->
<!-- 1. Register the repository in the repositories section -->
<repositories>
<repository>
<id>maven-repository</id>
<url>file:///{absolute_path_of_the_repository_folder}</url>
</repository>
</repositories>
<!-- 2. Add Core dependency in the dependencies section (optional) -->
<dependency>
<groupId>com.siemens.mindsphere</groupId>
<artifactId>mindsphere-sdk-java-core</artifactId>
<version>{x.y.z}</version>
</dependency>
<!-- 3. Add Service dependency in the dependencies section -->
<dependency>
<groupId>com.siemens.mindsphere</groupId>
<artifactId>{service_name}-sdk</artifactId>
<version>{x.y}</version>
</dependency>
```
``` javascript tab="Gradle"
// Modify the following sections of the build.gradle file
// 1. Register the repository in the repositories section
repositories {
maven {
url file('{Absolute_path_of_the_created_repository_folder}')
}
mavenLocal()
mavenCentral()
}
// 2. Add Core dependency in the dependencies section (optional)
compile 'com.siemens.mindsphere:mindsphere-sdk-java-core:{x.y.z}'
// 3. Add Service dependency in the dependencies section
compile 'com.siemens.mindsphere:{service_name}-sdk:{x.y}'
```
!!! note "说明"
所有 MindSphere 服务模块都对核心模块具有隐式相关性,因此添加核心模块相关项是可选操作。
## API 客户端和凭证配置
API 的最低级别构建块为 <a href="./javadocs-v2/html/com/siemens/mindsphere/sdk/core/RestClientConfig.html" target="_blank">`RestClientConfig`</a> 和 <a href="./javadocs-v2/html/com/siemens/mindsphere/sdk/core/MindsphereCredentials.html" target="_blank">`MindsphereCredentials`</a>。这些对象使用构建器模式进行实例化,并在客户端实例之间共享。
### 客户端配置
以下代码块显示了如何构建 <a href="./javadocs-v2/html/com/siemens/mindsphere/sdk/core/RestClientConfig.html" target="_blank">`RestClientConfig`</a> 对象的示例:
```java
RestClientConfig config = RestClientConfig.builder()
.connectionTimeoutInSeconds(100)
.proxyHost("host")
.proxyPort(8080)
.build();
可以使用以下可选参数配置 RestClientConfig
:
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
connectionTimeoutInSeconds | 连接超时(秒) | Integer | 100 |
socketTimeoutInSeconds | 套接字超时(秒) | Integer | 100 |
proxyHost | 代理服务器的 host 地址 | String | |
proxyPort | 代理服务器端口 | Integer | |
proxyUsername | 登录代理服务器使用的用户名 | String | |
proxyPassword | 登录代理服务器使用的密码 | String | |
hostEnvironment | 当前区域 | String | eu1 |
proxySchema | 代理服务器使用的架构 | String | http |
凭证配置¶
MindsphereCredentials
对象可以通过用户令牌或通过服务凭证进行构建来获取技术令牌。
以下配置参数可用于 MindsphereCredentials
对象:
名称 | 描述 | 类型 | 用例 |
---|---|---|---|
authorization | Bearer 类型的令牌,如果可用 | String | 用户令牌 |
clientId | 应用服务凭证 Id | String | 技术令牌 |
clientSecret | 应用服务凭证密钥 | String | 技术令牌 |
appName | 应用名称 | String | 技术令牌 |
appVersion | 应用版本 | String | 技术令牌 |
hostTenant | 主租户 | String | 技术令牌 |
userTenant | 用户租户 | String | 技术令牌 |
clientId | 租户服务凭证 ID | String | 技术令牌 |
clientSecret | 租户服务凭证密钥 | String | 技术令牌 |
tenant | 租户 | String | 技术令牌 |
更多信息请参考创建 MindsphereCredentials 对象代码示例。
API 客户端实例化和使用¶
API 客户端实例使用构建器模式以参数形式(可选)传递 RestClientConfig
和 MindsphereCredentials
实例。
代码示例使用 IoT TimeSeries API 客户端,占位符由尖括号 < >
表示:
@RequestMapping(method = RequestMethod.GET, value = "/{entity}/{propertySetName}")
public Timeseries getTimeSeriesAsObject(@PathVariable("entity") String entity,
@PathVariable("propertySetName") String property_set_name,
@RequestHeader("Authorization") String token) throws MindsphereException
{
//Construct MindsphereCredentials object
MindsphereCredentials credentials = MindsphereCredentials.userTokenBuilder()
.authorization(token)
.build();
//Construct RestClientConfig object
RestClientConfig config = RestClientConfig.builder()
.connectionTimeoutInSeconds(100)
.proxyHost("my.proxy.host")
.proxyPort("1.2.3.4")
.hostEnvironment("eu1")
.build();
//Construct TimeSeriesClient object
TimeSeriesClient timeseries_client = TimeSeriesClient.builder()
.mindsphereCredentials(credentials)
.restClientConfig(config)
.build();
//Construct GetTimeseriesRequest object
GetTimeseriesRequest request_object = new GetTimeseriesRequest();
request_object.setEntity(entity);
request_object.setPropertysetname(propertySetName);
request_object.setFrom(<from_time>);
request_object.setTo(<to_time>);
List<TimeseriesData> timeseries_data_list = null;
try {
timeseries_data_list = timeseries_client.getTimeseries(request_object);
} catch (MindsphereException e) {
// Exception handling
}
return timeseries_data_list;
}