Skip to content

Commit 0978599

Browse files
committed
Initial Updates
1 parent 6a95285 commit 0978599

3 files changed

Lines changed: 401 additions & 0 deletions

File tree

kepconfig/datalogger/log_items.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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""":mod:`log_items` exposes an API to allow modifications (add, delete, modify) to
8+
log item (tag) objects in a Datalogger log group within the Kepware Configuration API
9+
"""
10+
from . import log_group as Log_Group
11+
12+
LOG_ITEMS_ROOT = '/log_items'
13+
14+
def _create_url(log_item = None):
15+
'''Creates url object for the "log_item" branch of Kepware's project tree. Used
16+
to build a part of Kepware Configuration API URL structure
17+
18+
Returns the log_item specific url when a value is passed as the log_item name.
19+
'''
20+
21+
if log_item == None:
22+
return '{}'.format(LOG_ITEMS_ROOT)
23+
else:
24+
return '{}/{}'.format(LOG_ITEMS_ROOT, log_item)
25+
26+
27+
def add_log_item(server, log_group, DATA):
28+
'''Add a "log item" or multiple "log item" objects to a log group in Kepware's Datalogger. It can
29+
be used to pass a list of log items to be added all at once.
30+
31+
INPUTS:
32+
"server" - instance of the "server" class
33+
34+
"log_group" - name of log group that the log items will be added
35+
36+
*DATA* - properly JSON object (dict) of the log item expected by Kepware Configuration API
37+
38+
RETURNS:
39+
True - If a "HTTP 201 - Created" is received from Kepware
40+
41+
EXCEPTIONS:
42+
KepHTTPError - If urllib provides an HTTPError
43+
KepURLError - If urllib provides an URLError
44+
'''
45+
46+
r = server._config_add(server.url + Log_Group._create_url(log_group) + _create_url(), DATA)
47+
if r.code == 201: return True
48+
else: return False
49+
50+
def del_log_item(server, log_group, log_item):
51+
'''Delete a "log item" object of a log group in Kepware's Datalogger.
52+
53+
INPUTS:
54+
"server" - instance of the "server" class
55+
56+
"log_group" - name of log group that log item exists
57+
58+
"log_item" - name of log item to delete
59+
60+
RETURNS:
61+
True - If a "HTTP 200 - OK" is received from Kepware
62+
63+
EXCEPTIONS:
64+
KepHTTPError - If urllib provides an HTTPError
65+
KepURLError - If urllib provides an URLError
66+
'''
67+
r = server._config_del(server.url + Log_Group._create_url(log_group) + _create_url(log_item))
68+
if r.code == 200: return True
69+
else: return False
70+
71+
def modify_log_item(server, log_group, DATA, log_item = None, force = False):
72+
'''Modify a log_item object and it's properties in Kepware. If a "log_item" is not provided as an input,
73+
you need to identify the log_item in the 'common.ALLTYPES_NAME' property field in the "DATA". It will
74+
assume that is the log_item that is to be modified.
75+
76+
INPUTS:
77+
"server" - instance of the "server" class
78+
79+
"log_group" - name of log group that log item exists
80+
81+
"DATA" - properly JSON object (dict) of the agent properties to be modified
82+
83+
"log_item" (optional) - log item to modify in the log group. Only needed if not existing in "DATA"
84+
85+
"force" (optional) - if True, will force the configuration update to the Kepware server
86+
87+
RETURNS:
88+
True - If a "HTTP 200 - OK" is received from Kepware
89+
90+
EXCEPTIONS:
91+
KepHTTPError - If urllib provides an HTTPError
92+
KepURLError - If urllib provides an URLError
93+
'''
94+
95+
log_item_data = server._force_update_check(force, DATA)
96+
97+
if log_item == None:
98+
try:
99+
r = server._config_update(server.url + Log_Group._create_url(log_group) + _create_url(log_item_data['common.ALLTYPES_NAME']), log_item_data)
100+
if r.code == 200: return True
101+
else: return False
102+
except KeyError as err:
103+
print('Error: No log item identified in DATA | Key Error: {}'.format(err))
104+
return False
105+
# except:
106+
# return 'Error: Error with {}'.format(inspect.currentframe().f_code.co_name)
107+
else:
108+
r = server._config_update(server.url + Log_Group._create_url(log_group) + _create_url(log_item), log_item_data)
109+
if r.code == 200: return True
110+
else: return False
111+
112+
def get_log_item(server, log_group, log_item):
113+
'''Returns the properties of the log item object. Returned object is JSON.
114+
115+
INPUTS:
116+
"server" - instance of the "server" class
117+
118+
"log_group" - name of log group that log item exists
119+
120+
"log_item" - name of log item to retrieve properties for
121+
122+
RETURNS:
123+
JSON - data for the log item requested
124+
125+
EXCEPTIONS:
126+
KepHTTPError - If urllib provides an HTTPError
127+
KepURLError - If urllib provides an URLError
128+
'''
129+
r = server._config_get(server.url + Log_Group._create_url(log_group) + _create_url(log_item))
130+
return r.payload
131+
132+
def get_all_log_items(server, log_group):
133+
'''Returns the properties of all log item objects for a log group. Returned object is JSON list.
134+
135+
INPUTS:
136+
"server" - instance of the "server" class
137+
138+
"log_group" - name of log group
139+
140+
RETURNS:
141+
JSON - data for the log items requested
142+
143+
EXCEPTIONS:
144+
KepHTTPError - If urllib provides an HTTPError
145+
KepURLError - If urllib provides an URLError
146+
'''
147+
r = server._config_get(server.url + Log_Group._create_url(log_group) + _create_url())
148+
return r.payload

kepconfig/datalogger/mapping.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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""":mod:`mapping` exposes an API to allow modifications (add, delete, modify) to
8+
column mapping objects in a Datalogger log group within the Kepware Configuration API
9+
"""
10+
11+
from . import log_group as Log_Group
12+
13+
MAPPING_ROOT = '/column_mappings'
14+
15+
def _create_url(mapping = None):
16+
'''Creates url object for the "column_mappings" branch of Kepware's project tree. Used
17+
to build a part of Kepware Configuration API URL structure
18+
19+
Returns the mapping specific url when a value is passed as the column_mapping name.
20+
'''
21+
22+
if mapping == None:
23+
return '{}'.format(MAPPING_ROOT)
24+
else:
25+
return '{}/{}'.format(MAPPING_ROOT, mapping)
26+
27+
def modify_mapping(server, log_group, DATA, mapping = None, force = False):
28+
'''Modify a column mapping object and it's properties in Kepware. If a "mapping" is not provided as an input,
29+
you need to identify the column mapping in the 'common.ALLTYPES_NAME' property field in the "DATA". It will
30+
assume that is the column mapping that is to be modified.
31+
32+
INPUTS:
33+
"server" - instance of the "server" class
34+
35+
"log_group" - name of log group that mapping exists
36+
37+
"DATA" - properly JSON object (dict) of the agent properties to be modified
38+
39+
"mapping" (optional) - column mapping to modify in the log group. Only needed if not existing in "DATA"
40+
41+
"force" (optional) - if True, will force the configuration update to the Kepware server
42+
43+
RETURNS:
44+
True - If a "HTTP 200 - OK" is received from Kepware
45+
46+
EXCEPTIONS:
47+
KepHTTPError - If urllib provides an HTTPError
48+
KepURLError - If urllib provides an URLError
49+
'''
50+
51+
mapping_data = server._force_update_check(force, DATA)
52+
53+
if mapping == None:
54+
try:
55+
r = server._config_update(server.url + Log_Group._create_url(log_group) + _create_url(mapping_data['common.ALLTYPES_NAME']), mapping_data)
56+
if r.code == 200: return True
57+
else: return False
58+
except KeyError as err:
59+
print('Error: No column mapping identified in DATA | Key Error: {}'.format(err))
60+
return False
61+
# except:
62+
# return 'Error: Error with {}'.format(inspect.currentframe().f_code.co_name)
63+
else:
64+
r = server._config_update(server.url + Log_Group._create_url(log_group) + _create_url(mapping), mapping_data)
65+
if r.code == 200: return True
66+
else: return False
67+
68+
def get_mapping(server, log_group, mapping):
69+
'''Returns the properties of the mapping object. Returned object is JSON.
70+
71+
INPUTS:
72+
"server" - instance of the "server" class
73+
74+
"log_group" - name of log group that mapping exists
75+
76+
"mapping" - name of column mapping to retrieve properties for
77+
78+
RETURNS:
79+
JSON - data for the column mapping requested
80+
81+
EXCEPTIONS:
82+
KepHTTPError - If urllib provides an HTTPError
83+
KepURLError - If urllib provides an URLError
84+
'''
85+
r = server._config_get(server.url + Log_Group._create_url(log_group) + _create_url(mapping))
86+
return r.payload
87+
88+
def get_all_mappings(server, log_group):
89+
'''Returns the properties of all column mapping objects for a log group. Returned object is JSON list.
90+
91+
INPUTS:
92+
"server" - instance of the "server" class
93+
94+
"log_group" - name of log group
95+
96+
RETURNS:
97+
JSON - data for the column mappings requested
98+
99+
EXCEPTIONS:
100+
KepHTTPError - If urllib provides an HTTPError
101+
KepURLError - If urllib provides an URLError
102+
'''
103+
r = server._config_get(server.url + Log_Group._create_url(log_group) + _create_url())
104+
return r.payload

0 commit comments

Comments
 (0)