kepconfig.iot_gateway.iot_items

iot_items exposes an API to allow modifications (add, delete, modify) to iot_items objects within the Kepware Configuration API

  1# -------------------------------------------------------------------------
  2# Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
  3# See License.txt in the project root for
  4# license information.
  5# --------------------------------------------------------------------------
  6
  7
  8r"""`iot_items` exposes an API to allow modifications (add, delete, modify) to 
  9iot_items objects within the Kepware Configuration API
 10"""
 11
 12from typing import Union
 13from .. import utils
 14from ..connection import server
 15from .. import iot_gateway as IOT
 16from ..error import KepError, KepHTTPError
 17from ..utils import _url_parse_object
 18
 19IOT_ITEMS_ROOT = '/iot_items'
 20
 21def _create_url(tag = None):
 22    '''Creates url object for the "iot items" branch of Kepware's IoT Agents property model. Used 
 23    to build a part of Kepware Configuration API URL structure
 24    
 25    Returns the device specific url when a value is passed as the iot item name.
 26    '''
 27    if tag == None:
 28        return IOT_ITEMS_ROOT
 29    else: 
 30        normalized_tag = utils._address_dedecimal(tag)
 31        return '{}/{}'.format(IOT_ITEMS_ROOT, _url_parse_object(normalized_tag))
 32
 33
 34def add_iot_item(server: server, DATA: Union[dict, list], agent: str, agent_type: str) -> Union[bool, list]:
 35    '''Add a `"iot item"` or multiple `"iot item"` objects to Kepware's IoT Gateway agent. Additionally 
 36    it can be used to pass a list of iot items to be added to an agent all at once.
 37
 38    :param server: instance of the `server` class
 39    :param DATA: Dict or List of Dicts of the iot item or list of items
 40    expected by Kepware Configuration API
 41    :param agent: name of IoT Agent
 42    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
 43
 44    :return: True - If a "HTTP 201 - Created" is received from Kepware server
 45    :return: If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all 
 46    iot items added that failed.
 47
 48    :raises KepHTTPError: If urllib provides an HTTPError
 49    :raises KepURLError: If urllib provides an URLError
 50    '''
 51    r = server._config_add(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(), DATA)
 52    if r.code == 201: return True
 53    elif r.code == 207:
 54            errors = [] 
 55            for item in r.payload:
 56                if item['code'] != 201:
 57                    errors.append(item)
 58            return errors 
 59    else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
 60
 61def del_iot_item(server: server, iot_item: str, agent: str, agent_type: str) -> bool:
 62    '''Delete an `"iot item"` object in Kepware.
 63
 64    :param server: instance of the `server` class
 65    :param iot_item: IoT item to delete
 66    :param agent: name of IoT Agent
 67    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
 68
 69    :return: True - If a "HTTP 200 - OK" is received from Kepware server
 70
 71    :raises KepHTTPError: If urllib provides an HTTPError
 72    :raises KepURLError: If urllib provides an URLError
 73    '''
 74    r = server._config_del(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item))
 75    if r.code == 200: return True 
 76    else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
 77
 78def modify_iot_item(server: server, DATA: dict, agent: str, agent_type: str, *, iot_item: str = None, force: bool = False) -> bool:
 79    '''Modify an `"iot item"` object and it's properties in Kepware. If a `"iot item"` is not provided as an input,
 80    you need to identify the iot item in the *'common.ALLTYPES_NAME'* property field in the `"DATA"`. It will 
 81    assume that is the iot item that is to be modified.
 82
 83    :param server: instance of the `server` class
 84    :param DATA: Dict of the iot item properties to be modified.
 85    :param agent: name of IoT Agent
 86    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
 87    :param iot_item: *(optional)* name of IoT item to modify. Only needed if not existing in `"DATA"`
 88    :param force: *(optional)* if True, will force the configuration update to the Kepware server
 89    
 90    :return: True - If a "HTTP 200 - OK" is received from Kepware server
 91
 92    :raises KepHTTPError: If urllib provides an HTTPError
 93    :raises KepURLError: If urllib provides an URLError
 94    '''
 95    
 96    agent_data = server._force_update_check(force, DATA)
 97    if iot_item == None:
 98        try:
 99            r = server._config_update(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(agent_data['common.ALLTYPES_NAME']), agent_data)
100            if r.code == 200: return True 
101            else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
102        except KeyError as err:
103            err_msg = 'Error: No agent identified in DATA | Key Error: {}'.format(err)
104            raise KepError(err_msg)
105    else:
106        r = server._config_update(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item), agent_data)
107        if r.code == 200: return True 
108        else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
109
110def get_iot_item(server: server, iot_item: str, agent: str, agent_type: str)-> dict:
111    '''Returns the properties of the `"iot item"` object.
112
113    :param server: instance of the `server` class
114    :param iot_item: name of IoT item to retrieve properties
115    :param agent: name of IoT Agent
116    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
117
118    :return: Dict of properties for the iot item requested
119
120    :raises KepHTTPError: If urllib provides an HTTPError
121    :raises KepURLError: If urllib provides an URLError
122    '''
123    r = server._config_get(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item))
124    return r.payload
125
126def get_all_iot_items(server: server, agent: str, agent_type: str, *, options: dict = None) -> list:
127    '''Returns the properties of all `"iot item"` objects for an agent.
128    
129    :param server: instance of the `server` class
130    :param iot_item: name of IoT item to retrieve properties
131    :param agent: name of IoT Agent
132    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
133    :param options: *(optional)* Dict of parameters to filter, sort or pagenate the list of IoT items. Options are 'filter', 
134    'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'. Only used when exchange_name is not defined.
135
136    :return: list of properties for all IoT items
137
138    :raises KepHTTPError: If urllib provides an HTTPError
139    :raises KepURLError: If urllib provides an URLError
140    '''
141    r = server._config_get(f'{server.url}{IOT.agent._create_url(agent_type, agent)}{_create_url()}', params= options)
142    return r.payload
IOT_ITEMS_ROOT = '/iot_items'
def add_iot_item( server: kepconfig.connection.server, DATA: Union[dict, list], agent: str, agent_type: str) -> Union[bool, list]:
35def add_iot_item(server: server, DATA: Union[dict, list], agent: str, agent_type: str) -> Union[bool, list]:
36    '''Add a `"iot item"` or multiple `"iot item"` objects to Kepware's IoT Gateway agent. Additionally 
37    it can be used to pass a list of iot items to be added to an agent all at once.
38
39    :param server: instance of the `server` class
40    :param DATA: Dict or List of Dicts of the iot item or list of items
41    expected by Kepware Configuration API
42    :param agent: name of IoT Agent
43    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
44
45    :return: True - If a "HTTP 201 - Created" is received from Kepware server
46    :return: If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all 
47    iot items added that failed.
48
49    :raises KepHTTPError: If urllib provides an HTTPError
50    :raises KepURLError: If urllib provides an URLError
51    '''
52    r = server._config_add(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(), DATA)
53    if r.code == 201: return True
54    elif r.code == 207:
55            errors = [] 
56            for item in r.payload:
57                if item['code'] != 201:
58                    errors.append(item)
59            return errors 
60    else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)

Add a "iot item" or multiple "iot item" objects to Kepware's IoT Gateway agent. Additionally it can be used to pass a list of iot items to be added to an agent all at once.

Parameters
  • server: instance of the server class
  • DATA: Dict or List of Dicts of the iot item or list of items expected by Kepware Configuration API
  • agent: name of IoT Agent
  • agent_type: agent type. Valid values are MQTT Client, REST Client or REST Server
Returns

True - If a "HTTP 201 - Created" is received from Kepware server

Returns

If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all iot items added that failed.

Raises
  • KepHTTPError: If urllib provides an HTTPError
  • KepURLError: If urllib provides an URLError
def del_iot_item( server: kepconfig.connection.server, iot_item: str, agent: str, agent_type: str) -> bool:
62def del_iot_item(server: server, iot_item: str, agent: str, agent_type: str) -> bool:
63    '''Delete an `"iot item"` object in Kepware.
64
65    :param server: instance of the `server` class
66    :param iot_item: IoT item to delete
67    :param agent: name of IoT Agent
68    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
69
70    :return: True - If a "HTTP 200 - OK" is received from Kepware server
71
72    :raises KepHTTPError: If urllib provides an HTTPError
73    :raises KepURLError: If urllib provides an URLError
74    '''
75    r = server._config_del(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item))
76    if r.code == 200: return True 
77    else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)

Delete an "iot item" object in Kepware.

Parameters
  • server: instance of the server class
  • iot_item: IoT item to delete
  • agent: name of IoT Agent
  • agent_type: agent type. Valid values are MQTT Client, REST Client or REST Server
Returns

True - If a "HTTP 200 - OK" is received from Kepware server

Raises
  • KepHTTPError: If urllib provides an HTTPError
  • KepURLError: If urllib provides an URLError
def modify_iot_item( server: kepconfig.connection.server, DATA: dict, agent: str, agent_type: str, *, iot_item: str = None, force: bool = False) -> bool:
 79def modify_iot_item(server: server, DATA: dict, agent: str, agent_type: str, *, iot_item: str = None, force: bool = False) -> bool:
 80    '''Modify an `"iot item"` object and it's properties in Kepware. If a `"iot item"` is not provided as an input,
 81    you need to identify the iot item in the *'common.ALLTYPES_NAME'* property field in the `"DATA"`. It will 
 82    assume that is the iot item that is to be modified.
 83
 84    :param server: instance of the `server` class
 85    :param DATA: Dict of the iot item properties to be modified.
 86    :param agent: name of IoT Agent
 87    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
 88    :param iot_item: *(optional)* name of IoT item to modify. Only needed if not existing in `"DATA"`
 89    :param force: *(optional)* if True, will force the configuration update to the Kepware server
 90    
 91    :return: True - If a "HTTP 200 - OK" is received from Kepware server
 92
 93    :raises KepHTTPError: If urllib provides an HTTPError
 94    :raises KepURLError: If urllib provides an URLError
 95    '''
 96    
 97    agent_data = server._force_update_check(force, DATA)
 98    if iot_item == None:
 99        try:
100            r = server._config_update(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(agent_data['common.ALLTYPES_NAME']), agent_data)
101            if r.code == 200: return True 
102            else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
103        except KeyError as err:
104            err_msg = 'Error: No agent identified in DATA | Key Error: {}'.format(err)
105            raise KepError(err_msg)
106    else:
107        r = server._config_update(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item), agent_data)
108        if r.code == 200: return True 
109        else: raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)

Modify an "iot item" object and it's properties in Kepware. If a "iot item" is not provided as an input, you need to identify the iot item in the 'common.ALLTYPES_NAME' property field in the "DATA". It will assume that is the iot item that is to be modified.

Parameters
  • server: instance of the server class
  • DATA: Dict of the iot item properties to be modified.
  • agent: name of IoT Agent
  • agent_type: agent type. Valid values are MQTT Client, REST Client or REST Server
  • iot_item: (optional) name of IoT item to modify. Only needed if not existing in "DATA"
  • force: (optional) if True, will force the configuration update to the Kepware server
Returns

True - If a "HTTP 200 - OK" is received from Kepware server

Raises
  • KepHTTPError: If urllib provides an HTTPError
  • KepURLError: If urllib provides an URLError
def get_iot_item( server: kepconfig.connection.server, iot_item: str, agent: str, agent_type: str) -> dict:
111def get_iot_item(server: server, iot_item: str, agent: str, agent_type: str)-> dict:
112    '''Returns the properties of the `"iot item"` object.
113
114    :param server: instance of the `server` class
115    :param iot_item: name of IoT item to retrieve properties
116    :param agent: name of IoT Agent
117    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
118
119    :return: Dict of properties for the iot item requested
120
121    :raises KepHTTPError: If urllib provides an HTTPError
122    :raises KepURLError: If urllib provides an URLError
123    '''
124    r = server._config_get(server.url + IOT.agent._create_url(agent_type, agent) + _create_url(iot_item))
125    return r.payload

Returns the properties of the "iot item" object.

Parameters
  • server: instance of the server class
  • iot_item: name of IoT item to retrieve properties
  • agent: name of IoT Agent
  • agent_type: agent type. Valid values are MQTT Client, REST Client or REST Server
Returns

Dict of properties for the iot item requested

Raises
  • KepHTTPError: If urllib provides an HTTPError
  • KepURLError: If urllib provides an URLError
def get_all_iot_items( server: kepconfig.connection.server, agent: str, agent_type: str, *, options: dict = None) -> list:
127def get_all_iot_items(server: server, agent: str, agent_type: str, *, options: dict = None) -> list:
128    '''Returns the properties of all `"iot item"` objects for an agent.
129    
130    :param server: instance of the `server` class
131    :param iot_item: name of IoT item to retrieve properties
132    :param agent: name of IoT Agent
133    :param agent_type: agent type. Valid values are `MQTT Client`, `REST Client` or `REST Server`
134    :param options: *(optional)* Dict of parameters to filter, sort or pagenate the list of IoT items. Options are 'filter', 
135    'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'. Only used when exchange_name is not defined.
136
137    :return: list of properties for all IoT items
138
139    :raises KepHTTPError: If urllib provides an HTTPError
140    :raises KepURLError: If urllib provides an URLError
141    '''
142    r = server._config_get(f'{server.url}{IOT.agent._create_url(agent_type, agent)}{_create_url()}', params= options)
143    return r.payload

Returns the properties of all "iot item" objects for an agent.

Parameters
  • server: instance of the server class
  • iot_item: name of IoT item to retrieve properties
  • agent: name of IoT Agent
  • agent_type: agent type. Valid values are MQTT Client, REST Client or REST Server
  • options: (optional) Dict of parameters to filter, sort or pagenate the list of IoT items. Options are 'filter', 'sortOrder', 'sortProperty', 'pageNumber', and 'pageSize'. Only used when exchange_name is not defined.
Returns

list of properties for all IoT items

Raises
  • KepHTTPError: If urllib provides an HTTPError
  • KepURLError: If urllib provides an URLError