Industrial IoT SDK for Python– Getting Started¶
Prerequisites to use the Industrial IoT SDK for Python¶
- Python version 3.0 or higher is available.
- virtualenv is available.
- User authorization token or service credentials with required scopes for Industrial IoT Service APIs are available.
Environment variable
HOST_ENVIRONMENT
is set to the current region. When hosting an application in Cloud Foundry, the variable must be added in the manifest file:env: HOST_ENVIRONMENT: eu1
If not specified,
HOST_ENVIRONMENT
defaults toeu1
. * Environment variableHOST_BASEDOMAIN
is set optionally if domain is other that '*.mindsphere.io'. When hosting an application in Cloud Foundry, the variable must be added in the manifest file:env: HOST_BASEDOMAIN: example.orgname.com
Hint
Service credentials can be set as environment variables, so the client can fetch a technical token itself.
MINDSPHERE_CLIENT_ID
Specifies service credentials IDMINDSPHERE_CLIENT_SECRET
Specifies service credentials secretMINDSPHERE_TENANT
Specifies tenant name
Attention
You are responsible for keeping the credentials safe. You decide whether it is safe to supply the credentials via environment variables.
Installation Instructions¶
Downloading the Industrial IoT SDK for Python¶
Download the Industrial IoT SDK for Python from the Siemens Industry Online Support (SIOS) Portal.
Adding Industrial IoT SDK for Python Dependencies¶
- Copy the downloaded
.whl
files into a folder in the root of the project. - Set up your environment and install the required dependencies using virtualenv:
# Create virtual environment
virtualenv venv
# Activate the virtual environment
. venv/bin/activate
# Create virtual environment
virtualenv venv
# Activate the virtual environment
\{path_to_virtualenv_scripts}\activate
3.Include the required modules in the requirements.txt
of your project.
repo/mindsphere_core-x.y.z-py3-none-any.whl
repo/{servicename}-x.y.z-py3-none-any.whl
repo/mindsphere_core-1.0.0-py3-none-any.whl
repo/assetmanagement-3.9.0-py3-none-any.whl
4.Install the dependencies.
```cmd
pip install -r requirements.txt
```
Note
{x.y.z}
is the version number of the Industrial IoT Core or Service module (e.g. 1.0.0
).
Further implementation of the SDK libraries has been shown in a sample project that you can download and test in local or on Insights Hub application. Please refer to this repository: mindsphere-python-sdk-examples
Service Client and Credentials Configuration¶
The lowest-level building blocks of the service client are RestClientConfig
and the following credentials objects:
These building blocks can be shared between clients.
Client Configuration¶
The following code block shows an example of how to build a RestClientConfig
object:
# Import the RestClientConfig class from the mindsphere_core module
from mindsphere_core import RestClientConfig
clientConfig = RestClientConfig("<proxy_host>", <proxy_port>)
The RestClientConfig
can be configured using the following optional parameters:
Name | Description | Type | Default value |
---|---|---|---|
connectionTimeoutInSeconds | Connection timeout in seconds | Integer | 100 |
socketTimeoutInSeconds | Socket timeout in seconds | Integer | 100 |
proxyHost | Host address of the proxy | String | |
proxyPort | Proxy port | Integer | |
proxyUsername | User name to login to the proxy | String | |
proxyPassword | Password to login to the proxy | String | |
hostEnvironment | Current Region | String | eu1 |
proxySchema | Schema used by the proxy | String | http |
Credentials Configuration¶
A credentials object can be built with a user token or with service credentials to fetch a technical token.
The following configuration parameters are available for the credentials objects:
Name | Description | Type | Use Case |
---|---|---|---|
authorization | Bearer token, if available | String | user token |
keyStoreClientId | App specific service credentials ID | String | technical token |
keyStoreClientSecret | App specific service credentials secret | String | technical token |
appName | Application name | String | technical token |
appVersion | Application version | String | technical token |
hostTenant | Host tenant | String | technical token |
userTenant | User tenant | String | technical token |
Refer to code samples for creating credentials objects for more information.
Service Client Instantiation and Usage¶
A Service client instance accepts the RestClientConfig
and credentials objects as optional parameters.
Code sample using the IoT TimeSeries API client, placeholder are indicated by angular brackets < >
:
# Import the RestClientConfig and UserToken from mindsphere_core module
from mindsphere_core import RestClientConfig
from mindsphere_core import UserToken
# Import the MindsphereError from mindsphere_core.exceptions module
from mindsphere_core.exceptions import MindsphereError
# Import TimeseriesClient from timeseries module
from timeseries.clients.time_series_client import TimeSeriesClient
# Import GetTimeseriesRequest from timeseries.models module
from timeseries.models import GetTimeseriesRequest
# Instantiate RestClientConfig and UserToken objects
clientConfig = RestClientConfig(proxy_host = "<proxy_host>", proxy_port = <proxy_port>)
credentials = UserToken(authorization = "<bearer_token>")
# Construct the TimeSeriesClient object using the clientConfig and credentials objects
timeseriesClient = TimeSeriesClient(rest_client_config = clientConfig, mindsphere_credentials = credentials)
try:
request_object = GetTimeseriesRequest(
_from="start_time",
to="end_time",
entity="entity_id",
propertysetname="property_set_name"
)
timeseries = timeseriesClient.get_timeseries(request_object)
except MindsphereError as err:
//Exception Handling