-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathservices and logs example.py
More file actions
128 lines (112 loc) · 5.04 KB
/
services and logs example.py
File metadata and controls
128 lines (112 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -------------------------------------------------------------------------
# Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
# See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
# Services and Logs Example - Simple example on how to call services and
# get logs from the Kepware Configuration API
from kepconfig import connection, error
from kepconfig.connectivity import channel, device
import json
import datetime
# Channel and Device name to be used
ch_name = 'ControlLogix_Channel'
dev_name = 'Device1'
device_IP = '192.168.1.100'
# Service Response Global
r = {}
def ErrorHandler(err):
# Generic Handler for exception errors
if isinstance(err, error.KepHTTPError):
print(err.code)
print(err.msg)
print(err.url)
print(err.hdrs)
print(err.payload)
elif isinstance(err, error.KepURLError):
print(err.url)
print(err.reason)
elif isinstance(err, error.KepError):
print(err.msg)
else:
print('Different Exception Received: {}'.format(err))
# This creates a server reference that is used to target all modifications of
# the Kepware configuration
server = connection.server(host = '127.0.0.1', port = 57412, user = 'Administrator', pw = '')
# This Reinitializes Kepware's Server Runtime process, similar to manually reinitializing
# using the Configuration UI or the Administrator tool.
try:
r = server.reinitialize()
print('{} - {}'.format("Execute Reinitialize Service", r))
except Exception as err:
ErrorHandler(err)
# This reads the Reinitialize Service Job Status using the returned KepServiceResponse
try:
print('{} - {}'.format("Read Reinitialize Service Status", server.service_status(r)))
except Exception as err:
ErrorHandler(err)
# Add a Channel using the "ControlLogix Driver" with a ControlLogix 5500 family device.
# This will be used to demonstrate the "Auto Tag Generation" service available.
channel_data = {
"common.ALLTYPES_NAME": ch_name,
"common.ALLTYPES_DESCRIPTION": "This is the test channel created",
"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Allen-Bradley Controllogix Ethernet",
"devices": [
{
"common.ALLTYPES_NAME": dev_name,
"common.ALLTYPES_DESCRIPTION": "Hello, new description",
"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Allen-Bradley Controllogix Ethernet",
"servermain.DEVICE_MODEL": 0,
"servermain.DEVICE_ID_STRING": "<{}>,1,0".format(device_IP)
}
]
}
try:
print("{} - {}".format("Adding Controllogix Channel and Device", channel.add_channel(server,channel_data)))
except Exception as err:
ErrorHandler(err)
# Execute the "TagGeneration" service available in the Kepware Configuration API
try:
r = device.auto_tag_gen(server, '{}.{}'.format(ch_name, dev_name))
print("{} - {}".format("Executing ATG for Controllogix Device", r))
except Exception as err:
ErrorHandler(err)
# This reads the TagGeneration Service Job Status using the returned KepServiceResponse
try:
print('{} - {}'.format("Read Service Status", server.service_status(r)))
except Exception as err:
ErrorHandler(err)
# Get Event Log from Kepware instance.
# Time parameters need to be UTC values.
try:
print("{} - {}".format("Here is the last Event Log Entry", json.dumps(server.get_event_log(1), indent=4)))
except Exception as err:
ErrorHandler(err)
try:
print("{} - {}".format("Here are the last 25 entries of the Event Log", json.dumps(server.get_event_log(25, datetime.datetime.fromisoformat('2019-11-03T23:35:23.000'), datetime.datetime.utcnow()), indent=4)))
except Exception as err:
ErrorHandler(err)
try:
print("{} - {}".format("Here are only the Information entries of the Event Log", json.dumps(server.get_event_log(None, datetime.datetime.fromisoformat('2019-11-03T23:35:23.000'), datetime.datetime.utcnow(), options= {'event': 'Information'}), indent=4)))
except Exception as err:
ErrorHandler(err)
#Get Configuration API Transaction Log
try:
print("{} - {}".format("Here is the last API Transaction Log Entry", json.dumps(server.get_transaction_log(1), indent=4)))
except Exception as err:
ErrorHandler(err)
# Export the Project Configuration as JSON from the Kepware Server instance. Provides the same information as saving the
# project file as JSON in the Configuration UI locally.
projectJson = server.export_project_configuration()
try:
print("{} - {}".format("Here is the full Project Configuration as JSON", json.dumps(projectJson, indent=4)))
except Exception as err:
ErrorHandler(err)
# Import the Project Configuration from JSON. This service acts like a FILE->OPEN action and
# stop communications while the new project replaces the current project in the Kepware runtime.
try:
r = server.import_project_configuration(projectJson)
print("{} - {}".format("Executing Project Import service", r))
print('{} - {}'.format("Read Service Status", server.service_status(r)))
except Exception as err:
ErrorHandler(err)