Skip to content

Commit a300b0c

Browse files
committed
Added new methods (enable, reset mapping)
1 parent b4f326f commit a300b0c

1 file changed

Lines changed: 88 additions & 36 deletions

File tree

kepconfig/datalogger/log_group.py

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
# -------------------------------------------------------------------------
2-
# Copyright (c) 2020, PTC Inc. and/or all its affiliates. All rights reserved.
2+
# Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
33
# See License.txt in the project root for
44
# license information.
55
# --------------------------------------------------------------------------
66

77
r""":mod:`log_group` exposes an API to allow modifications (add, delete, modify) to
8-
Log Group objects within the Kepware Configuration API
8+
log group objects in DataLogger within the Kepware Configuration API
99
"""
10+
from kepconfig import connection, error as KepError
1011

11-
12-
LOG_GROUP_ROOT_URL = '/project/_datalogger/log_groups'
13-
12+
ENABLE_PROPERTY = 'datalogger.LOG_GROUP_ENABLED'
13+
LOG_GROUP_ROOT = '/project/_datalogger/log_groups'
14+
SERVICES_ROOT = '/services'
1415
def _create_url(log_group = None):
15-
'''Creates url object for the "agent" branch of Kepware's project tree. Used
16+
'''Creates url object for the "log_group" branch of Kepware's project tree. Used
1617
to build a part of Kepware Configuration API URL structure
1718
1819
Returns the agent specific url when a value is passed as the agent name.
1920
'''
2021

2122
if log_group == None:
22-
return '{}'.format(LOG_GROUP_ROOT_URL)
23+
return '{}'.format(LOG_GROUP_ROOT)
2324
else:
24-
return '{}/{}'.format(LOG_GROUP_ROOT_URL, log_group)
25+
return '{}/{}'.format(LOG_GROUP_ROOT, log_group)
2526

2627

2728
def add_log_group(server, DATA):
28-
'''Add a "agent" or multiple "agent" objects of a specific type to Kepware's IoT Gateway. Can be used to pass children of an
29-
agent object such as iot items. This allows you to create an agent and iot items if desired.
30-
31-
Additionally it can be used to pass a list of agents and it's children to be added all at once.
29+
'''Add a "log group" or multiple "log groups" objects to Kepware's DataLogger. It can be used
30+
to pass a list of log groups to be added all at once.
3231
3332
INPUTS:
3433
"server" - instance of the "server" class
3534
36-
*DATA* - properly JSON object (dict) of the agent and it's children
37-
expected by Kepware Configuration API
38-
39-
"agent_type" (optional) - agent type to add to IoT Gateway. Only needed if not existing in "DATA"
35+
*DATA* - properly JSON object (dict) of the log group expected by Kepware Configuration API
4036
4137
RETURNS:
4238
True - If a "HTTP 201 - Created" is received from Kepware
@@ -51,14 +47,12 @@ def add_log_group(server, DATA):
5147
else: return False
5248

5349
def del_log_group(server, log_group):
54-
'''Delete a "agent" object in Kepware. This will delete all children as well
50+
'''Delete a "log group" object in Kepware's Datalogger.
5551
5652
INPUTS:
5753
"server" - instance of the "server" class
5854
59-
"agent" - name of IoT Agent
60-
61-
"agent_type" - agent type to delete to IoT Gateway
55+
"log_group" - name of log group
6256
6357
RETURNS:
6458
True - If a "HTTP 200 - OK" is received from Kepware
@@ -72,18 +66,16 @@ def del_log_group(server, log_group):
7266
else: return False
7367

7468
def modify_log_group(server, DATA, log_group = None, force = False):
75-
'''Modify a agent object and it's properties in Kepware. If a "agent" is not provided as an input,
76-
you need to identify the agent in the 'common.ALLTYPES_NAME' property field in the "DATA". It will
77-
assume that is the agent that is to be modified.
69+
'''Modify a log group object and it's properties in Kepware's Datalogger. If a "log group" is not provided as an input,
70+
you need to identify the log group in the 'common.ALLTYPES_NAME' property field in the "DATA". It will
71+
assume that is the log group that is to be modified.
7872
7973
INPUTS:
8074
"server" - instance of the "server" class
8175
8276
"DATA" - properly JSON object (dict) of the agent properties to be modified
8377
84-
"agent" (optional) - name of IoT Agent. Only needed if not existing in "DATA"
85-
86-
"agent_type" (optional) -agent type to modify to IoT Gateway. Only needed if not existing in "DATA"
78+
"log_group" (optional) - name of log group. Only needed if not existing in "DATA"
8779
8880
"force" (optional) - if True, will force the configuration update to the Kepware server
8981
@@ -103,7 +95,7 @@ def modify_log_group(server, DATA, log_group = None, force = False):
10395
if r.code == 200: return True
10496
else: return False
10597
except KeyError as err:
106-
print('Error: No agent identified in DATA | Key Error: {}'.format(err))
98+
print('Error: No log group identified in DATA | Key Error: {}'.format(err))
10799
return False
108100
# except:
109101
# return 'Error: Error with {}'.format(inspect.currentframe().f_code.co_name)
@@ -113,17 +105,15 @@ def modify_log_group(server, DATA, log_group = None, force = False):
113105
else: return False
114106

115107
def get_log_group(server, log_group):
116-
'''Returns the properties of the agent object. Returned object is JSON.
108+
'''Returns the properties of the log group object. Returned object is JSON.
117109
118110
INPUTS:
119111
"server" - instance of the "server" class
120112
121-
"agent" - name of IoT Agent
122-
123-
"agent_type" - agent type
113+
"log_group" - name of log group
124114
125115
RETURNS:
126-
JSON - data for the IoT Agent requested
116+
JSON - data for the log group requested
127117
128118
EXCEPTIONS:
129119
KepHTTPError - If urllib provides an HTTPError
@@ -133,19 +123,81 @@ def get_log_group(server, log_group):
133123
return r.payload
134124

135125
def get_all_log_groups(server):
136-
'''Returns the properties of all agent objects for a specific agent type. Returned object is JSON list.
126+
'''Returns the properties of all log group objects for Kepware's Datalogger. Returned object is JSON list.
137127
138128
INPUTS:
139129
"server" - instance of the "server" class
140130
141-
"agent_type" - agent type
142-
143131
RETURNS:
144-
JSON - data for the IoT Agents requested
132+
JSON - data for the log groups requested
145133
146134
EXCEPTIONS:
147135
KepHTTPError - If urllib provides an HTTPError
148136
KepURLError - If urllib provides an URLError
149137
'''
150138
r = server._config_get(server.url + _create_url())
151139
return r.payload
140+
141+
def enable_log_group(server, log_group):
142+
'''Enable the log group. Returned object is JSON.
143+
144+
INPUTS:
145+
"server" - instance of the "server" class
146+
147+
"log_group" - name of log group
148+
149+
RETURNS:
150+
True - If a "HTTP 200 - OK" is received from Kepware
151+
152+
EXCEPTIONS:
153+
KepHTTPError - If urllib provides an HTTPError
154+
KepURLError - If urllib provides an URLError
155+
'''
156+
DATA = {ENABLE_PROPERTY: True}
157+
modify_log_group(server, DATA, log_group)
158+
159+
def disable_log_group(server, log_group):
160+
'''Enable the log group. Returned object is JSON.
161+
162+
INPUTS:
163+
"server" - instance of the "server" class
164+
165+
"log_group" - name of log group
166+
167+
RETURNS:
168+
True - If a "HTTP 200 - OK" is received from Kepware
169+
170+
EXCEPTIONS:
171+
KepHTTPError - If urllib provides an HTTPError
172+
KepURLError - If urllib provides an URLError
173+
'''
174+
DATA = {ENABLE_PROPERTY: False}
175+
modify_log_group(server, DATA, log_group)
176+
177+
def reset_column_mapping_service(server, log_group):
178+
'''Executes a ResetColumnMapping serivce call to the log group
179+
180+
INPUTS:
181+
"server" - instance of the "server" class
182+
183+
"log_group" - name of log group
184+
185+
RETURNS:
186+
KepServiceResponse instance with job information
187+
188+
EXCEPTIONS (If not HTTP 200 or 429 returned):
189+
KepHTTPError - If urllib provides an HTTPError
190+
KepURLError - If urllib provides an URLError
191+
'''
192+
193+
try:
194+
r = server._config_update(server.url + _create_url(log_group) + SERVICES_ROOT + '/ResetColumnMapping', None)
195+
job = connection.KepServiceResponse(r.payload['code'],r.payload['message'], r.payload['href'])
196+
return job
197+
except KepError.KepHTTPError as err:
198+
if err.code == 429:
199+
job.code = err.code
200+
job.message = err.payload
201+
return job
202+
else:
203+
raise err

0 commit comments

Comments
 (0)