IOT TimeSeries Client for Python¶
Introduction¶
The IoT Time Series Service allows you to interact with time series data related to assets. Refer to IoT Time Series for more information about the service.
Further implementation of the IOT TimeSeries SDK library 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: industrial-iot-python-sdk-examples
Hint
In the IoT context, assets are referred to as entity
and aspects as propertyset
.
Placeholders in the following samples are indicated by angular brackets < >
.
IOT TimeSeries Operations¶
Client Name: TimeSeriesOperationsClient
Create or update time series data for multiple unique asset-aspect (entity-property set) combinations¶
Create or update time series data for multiple unique combinations of assets (entities) and aspects (property sets). In case of an update of data at an already existing time, all properties at that time will be replaced by the ones provided in the request. All asset-aspect (entity-property set) combinations need to belong to the same tenant. Request body limitations: 1. A maximum of 5 asset-aspect (entity-property set) combinations can be provided 2. The request body size must be equal or less than 100 kb 3. A maximum of 100 time series data items can be provided overall
# 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 the TimeSeriesOperationsClient from mindconnect module
from mindconnect import TimeSeriesOperationsClient
# Create the TimeSeriesOperationsClient object using the RestClientConfig and UserToken objects
timeSeriesOperationsClient = TimeSeriesOperationsClient(rest_client_config = config, mindsphere_credentials = credentials)
try:
# Create the request object
timeseries = UpdatedTimeSeries()
timeSeriesItem = TimeSeriesItem()
timeSeriesItem.entity_id="5908ae5c5e4f4e18b0be58cd21ee675f"
timeSeriesItem.property_set_name="test_2020_11_11"
timeSeriesDataItem = TimeSeriesDataItem()
timeSeriesDataItem.fields = {
"test": 15
}
timeSeriesDataItem.time="2020-11-11T02:52:00Z"
timeSeriesDataItems=[timeSeriesDataItem]
timeSeriesItem.data=timeSeriesDataItems
timeSeriesItems = [timeSeriesItem]
timeseries.timeseries=tim=timeSeriesItems
createOrUpdateTimeseriesRequest=CreateOrUpdateTimeseriesRequest()
createOrUpdateTimeseriesRequest.timeseries=timeseries
# Initiate the API call
response = timeSeriesOperationsClient.create_or_update_timeseries(createOrUpdateTimeseriesRequest)
except MindsphereError as err:
# Exception Handling
Create or update time series data¶
Create or update time series data for one combination of an asset (entity) and an(a) aspect (property set). In case of an update of data at an already existing time, all properties at that time will be replaced by the ones provided in the request.
try:
# Create the request object
requestObject=CreateOrUpdateTimeseriesDataRequest();
requestObject.entity_id="5908ae5c5e4f4e18b0be58cd21ee675f"
requestObject.property_set_name="test_2020_11_11"
timeSeriesDataItem = TimeSeriesDataItem()
timeSeriesDataItem.fields = {
"test": 15
}
timeSeriesDataItem.time = "2020-11-11T02:52:00Z"
timeSeriesDataItems = [timeSeriesDataItem]
requestObject.timeseries=timeSeriesDataItems
# Initiate the API call
timeSeriesOperationsClient.create_or_update_timeseries_data(requestObject)
except MindsphereError as err:
# Exception Handling
Delete time series data¶
Delete time series data for one combination of an asset (entity) and an(a) aspect (property set). All property values within the given time range are deleted.
# Create the DiagnosticInformationClient object using the RestClientConfig and UserToken objects
readOperationsClient = ReadOperationsClient(rest_client_config = config, mindsphere_credentials = credentials)
try:
# Create the request object
requestObject = DeleteUpdatedTimeseriesRequest();
requestObject._from="2020-12-12T15:10:00Z"
requestObject.to="2020-12-15T16:18:00Z"
requestObject.entity_id="5908ae5c5e4f4e18b0be58cd21ee675f"
requestObject.property_set_name="test_2020_11_11"
# Initiate the API call
timeSeriesOperationsClient.delete_timeseries(requestObject)
except MindsphereError as err:
# Exception Handling
Retrieve time series data¶
Retrieve time series data for one combination of an asset (entity) and an(a) aspect (property set). The maximum number of time series data items returned per request is defined by parameter limit. In case more time series data items are present in the requested time range, only a subset of data items will be returned and a header link is added to the response. The header value contains the request URL to fetch the next set of time series data items, by increasing the from parameter accordingly. Returns the latest record if no range is provided.
try:
# Create the request object
retrieveTimeseriesRequest = RetrieveTimeseriesRequest(
entity_id="5908ae5c5e4f4e18b0be58cd21ee675f",
property_set_name="test_2020_11_11"
)
# Initiate the API call
response = timeSeriesOperationsClient.retrieve_timeseries(retrieveTimeseriesRequest)
except MindsphereError as err:
# Exception Handling