Skip to content

MindSphere SDK for Python– Getting Started

Prerequisites to use the MindSphere SDK for Python

  • Python version 3.0 or higher is available.
  • virtualenv is available.
  • User authorization token or service credentials with required scopes for MindSphere 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:

    1
    2
    env:
      HOST_ENVIRONMENT: eu1
    

    If not specified, HOST_ENVIRONMENT defaults to eu1.

Hint

Service credentials can be set as environment variables, so the client can fetch a technical token itself.

  • MINDSPHERE_CLIENT_ID
    Specifies service credentials ID

  • MINDSPHERE_CLIENT_SECRET
    Specifies service credentials secret

  • MINDSPHERE_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 MindSphere SDK for Python

Download the MindSphere SDK for Python from the Siemens Industry Online Support (SIOS) Portal.

Adding MindSphere SDK for Python Dependencies

  1. Copy the downloaded .whl files into a folder in the root of the project.
  2. Set up your environment and install the required dependencies using virtualenv:

    1
    2
    3
    4
    5
    # Create virtual environment
    virtualenv venv
    
    # Activate the virtual environment
    . venv/bin/activate
    
    1
    2
    3
    4
    5
    # 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.

    1
    2
    repo/mindsphere_core-x.y.z-py3-none-any.whl
    repo/{servicename}-x.y.z-py3-none-any.whl
    
    1
    2
    repo/mindsphere_core-1.0.0-py3-none-any.whl
    repo/assetmanagement-3.9.0-py3-none-any.whl
    
  4. Install the dependencies.

    1
    pip install -r requirements.txt
    

Note

{x.y.z} is the version number of the MindSphere Core or Service module (e.g. 1.0.0).

Service Client and Credentials Configuration

The lowest-level building blocks of the service client are RestClientConfig and the following credentials objects:

  1. UserToken
  2. AppCredentials
  3. TenantCredentials

These building blocks can be shared between clients.

Client Configuration

The following code block shows an example of how to build a RestClientConfig object:

1
2
3
4
# 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 < >:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Any questions left?

Ask the community


Except where otherwise noted, content on this site is licensed under the MindSphere Development License Agreement.