Skip to content

Commit fb4525b

Browse files
committed
init added max tag functionality
1 parent 1a673af commit fb4525b

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

kepconfig/adv_tags/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
specific objects within the Kepware Configuration API
99
"""
1010

11-
from . import adv_tag_group, average_tags, derived_tags, complex_tags, cumulative_tags, min_tags
11+
from . import adv_tag_group, average_tags, derived_tags, complex_tags, cumulative_tags, min_tags, max_tags
1212
ADV_TAGS_ROOT = '/project/_advancedtags'
1313

1414
def adv_tag_path_split(path: str, *, isItem=False) -> dict:

kepconfig/adv_tags/max_tags.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
r"""`maximum_tags` exposes an API to allow modifications (add, delete, modify) to
8+
maximum tag objects within the Kepware Configuration API
9+
"""
10+
11+
from ..connection import server
12+
from ..error import KepError, KepHTTPError
13+
from ..utils import _url_parse_object
14+
from typing import Union
15+
from .. import adv_tags
16+
17+
MAXIMUM_TAGS_ROOT = '/maximum_tags'
18+
19+
def _get_maximum_tags_url(tag: str = None) -> str:
20+
'''Creates url object for the "maximum_tags" branch of Kepware's project tree.
21+
22+
Returns the maximum tag specific url when a value is passed as the tag name.
23+
'''
24+
if tag is None:
25+
return MAXIMUM_TAGS_ROOT
26+
else:
27+
return f'{MAXIMUM_TAGS_ROOT}/{_url_parse_object(tag)}'
28+
29+
def add_maximum_tag(server: server, adv_tag_group_path: str, DATA: Union[dict, list]) -> Union[bool, list]:
30+
'''Add `"maximum_tag"` or multiple `"maximum_tag"` objects to a specific path in Kepware.
31+
Can be used to pass a list of maximum tags to be added at one path location.
32+
33+
:param server: instance of the `server` class
34+
:param adv_tag_group_path: path identifying where to add maximum tag(s). Standard Kepware address decimal
35+
notation string such as "_advancedtags.AdvTagGroup1" or "_advancedtags.AdvTagGroup1.AdvTagGroupChild"
36+
:param DATA: Dict or List of Dicts of the maximum tag(s) to add
37+
38+
:return: True - If a "HTTP 201 - Created" is received from Kepware server
39+
:return: If a "HTTP 207 - Multi-Status" is received from Kepware with a list of dict error responses for all
40+
maximum tags added that failed.
41+
42+
:raises KepHTTPError: If urllib provides an HTTPError
43+
:raises KepURLError: If urllib provides an URLError
44+
'''
45+
path_obj = adv_tags.adv_tag_path_split(adv_tag_group_path, isItem=False)
46+
url = adv_tags._create_adv_tags_base_url(server.url, path_obj) + _get_maximum_tags_url()
47+
48+
r = server._config_add(url, DATA)
49+
if r.code == 201:
50+
return True
51+
elif r.code == 207:
52+
errors = [item for item in r.payload if item['code'] != 201]
53+
return errors
54+
else:
55+
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
56+
57+
def modify_maximum_tag(server: server, max_tag_path: str, DATA: dict, force: bool = False) -> bool:
58+
'''Modify a `"maximum_tag"` object and its properties in Kepware.
59+
60+
:param server: instance of the `server` class
61+
:param max_tag_path: path identifying location and maximum tag to modify. Standard Kepware address decimal
62+
notation string including the maximum tag such as "_advancedtags.AdvTagGroup1.MaxTag1"
63+
:param DATA: Dict of the `maximum_tag` properties to be modified
64+
:param force: *(optional)* if True, will force the configuration update to the Kepware server
65+
66+
:return: True - If a "HTTP 200 - OK" is received from Kepware server
67+
68+
:raises KepHTTPError: If urllib provides an HTTPError
69+
:raises KepURLError: If urllib provides an URLError
70+
'''
71+
max_tag_data = server._force_update_check(force, DATA)
72+
path_obj = adv_tags.adv_tag_path_split(max_tag_path, isItem=True)
73+
url = adv_tags._create_adv_tags_base_url(server.url, path_obj) + _get_maximum_tags_url(path_obj['item'])
74+
75+
r = server._config_update(url, max_tag_data)
76+
if r.code == 200:
77+
return True
78+
else:
79+
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
80+
81+
def del_maximum_tag(server: server, max_tag_path: str) -> bool:
82+
'''Delete `"maximum_tag"` object at a specific path in Kepware.
83+
84+
:param server: instance of the `server` class
85+
:param max_tag_path: path identifying location and maximum tag to delete. Standard Kepware address decimal
86+
notation string including the maximum tag such as "_advancedtags.AdvTagGroup1.MaxTag1"
87+
88+
:return: True - If a "HTTP 200 - OK" is received from Kepware server
89+
90+
:raises KepHTTPError: If urllib provides an HTTPError
91+
:raises KepURLError: If urllib provides an URLError
92+
'''
93+
path_obj = adv_tags.adv_tag_path_split(max_tag_path, isItem=True)
94+
url = adv_tags._create_adv_tags_base_url(server.url, path_obj) + _get_maximum_tags_url(path_obj['item'])
95+
96+
r = server._config_del(url)
97+
if r.code == 200:
98+
return True
99+
else:
100+
raise KepHTTPError(r.url, r.code, r.msg, r.hdrs, r.payload)
101+
102+
def get_maximum_tag(server: server, max_tag_path: str) -> dict:
103+
'''Returns the properties of the `"maximum_tag"` object at a specific path in Kepware.
104+
105+
:param server: instance of the `server` class
106+
:param max_tag_path: path identifying location and maximum tag to retrieve. Standard Kepware address decimal
107+
notation string including the maximum tag such as "_advancedtags.AdvTagGroup1.MaxTag1"
108+
109+
:return: Dict of data for the maximum tag requested
110+
111+
:raises KepHTTPError: If urllib provides an HTTPError
112+
:raises KepURLError: If urllib provides an URLError
113+
'''
114+
path_obj = adv_tags.adv_tag_path_split(max_tag_path, isItem=True)
115+
url = adv_tags._create_adv_tags_base_url(server.url, path_obj) + _get_maximum_tags_url(path_obj['item'])
116+
117+
r = server._config_get(url)
118+
return r.payload
119+
120+
def get_all_maximum_tags(server: server, adv_tag_group_path: str, *, options: dict = None) -> list:
121+
'''Returns the properties of all `"maximum_tag"` objects at a specific path in Kepware.
122+
123+
:param server: instance of the `server` class
124+
:param adv_tag_group_path: path identifying location to retrieve maximum tag list. Standard Kepware address decimal
125+
notation string such as "_advancedtags.AdvTagGroup1" or "_advancedtags.AdvTagGroup1.AdvTagGroupChild"
126+
:param options: *(optional)* Dict of parameters to filter, sort or paginate the list of maximum tags. Options are `filter`,
127+
`sortOrder`, `sortProperty`, `pageNumber`, and `pageSize`
128+
129+
:return: List of data for all maximum tags
130+
131+
:raises KepHTTPError: If urllib provides an HTTPError
132+
:raises KepURLError: If urllib provides an URLError
133+
'''
134+
path_obj = adv_tags.adv_tag_path_split(adv_tag_group_path, isItem=False)
135+
url = adv_tags._create_adv_tags_base_url(server.url, path_obj) + _get_maximum_tags_url()
136+
137+
r = server._config_get(url, params=options)
138+
return r.payload

tests/adv_tags_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@
7272
}
7373
]
7474

75+
maximum_tag_name = 'MaximumTag1'
76+
maximum_tag_data = [
77+
{
78+
"common.ALLTYPES_NAME": maximum_tag_name,
79+
"common.ALLTYPES_DESCRIPTION": "",
80+
"advanced_tags.ENABLED": True,
81+
"advanced_tags.MAXIMUM_TAG": "_System._Time_Hour",
82+
"advanced_tags.RUN_TAG": "_System._Time_Second"
83+
}
84+
]
85+
7586
def HTTPErrorHandler(err):
7687
if err.__class__ is error.KepHTTPError:
7788
print(err.code)
@@ -368,6 +379,47 @@ def test_minimum_tag_del(server):
368379
minimum_tag_path = f'_advancedtags.{adv_tag_group_name}.{minimum_tag_name}'
369380
assert adv_tags.min_tags.del_minimum_tag(server, minimum_tag_path)
370381

382+
def test_maximum_tag_add(server):
383+
# Add a maximum tag to the root advanced tag plug-in
384+
assert adv_tags.max_tags.add_maximum_tag(server, f'_advancedtags', maximum_tag_data)
385+
386+
testTag = {
387+
"common.ALLTYPES_NAME": "newMaximumTag",
388+
"common.ALLTYPES_DESCRIPTION": "",
389+
"advanced_tags.ENABLED": True,
390+
"advanced_tags.MAXIMUM_TAG": "_System._Time_Hour",
391+
"advanced_tags.RUN_TAG": "_System._Time_Second"
392+
}
393+
maximum_tag_data.append(testTag)
394+
# Add a maximum tag to the advanced tag group
395+
assert adv_tags.max_tags.add_maximum_tag(server, f'_advancedtags.{adv_tag_group_name}', maximum_tag_data)
396+
397+
def test_maximum_tag_get(server):
398+
# Get the maximum tag
399+
maximum_tag_path = f'_advancedtags.{adv_tag_group_name}.{maximum_tag_name}'
400+
result = adv_tags.max_tags.get_maximum_tag(server, maximum_tag_path)
401+
assert type(result) == dict
402+
assert result.get("common.ALLTYPES_NAME") == maximum_tag_name
403+
404+
def test_maximum_tag_modify(server):
405+
# Modify the maximum tag
406+
maximum_tag_path = f'_advancedtags.{adv_tag_group_name}.{maximum_tag_name}'
407+
tag_data = {
408+
"common.ALLTYPES_DESCRIPTION": "Modified maximum tag"
409+
}
410+
assert adv_tags.max_tags.modify_maximum_tag(server, maximum_tag_path, tag_data, force=True)
411+
412+
def test_maximum_tag_get_all(server):
413+
# Get all maximum tags under the group
414+
result = adv_tags.max_tags.get_all_maximum_tags(server, f'_advancedtags.{adv_tag_group_name}')
415+
assert type(result) == list
416+
assert any(tag.get("common.ALLTYPES_NAME") == maximum_tag_name for tag in result)
417+
418+
def test_maximum_tag_del(server):
419+
# Delete the maximum tag
420+
maximum_tag_path = f'_advancedtags.{adv_tag_group_name}.{maximum_tag_name}'
421+
assert adv_tags.max_tags.del_maximum_tag(server, maximum_tag_path)
422+
371423
def test_adv_tag_group_del(server):
372424
# Delete parent advanced tag group
373425
assert adv_tags.adv_tag_group.del_tag_group(server, f'_advancedtags.{adv_tag_group_name}')

0 commit comments

Comments
 (0)