-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathiot gateway example.py
More file actions
190 lines (170 loc) · 7.46 KB
/
iot gateway example.py
File metadata and controls
190 lines (170 loc) · 7.46 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -------------------------------------------------------------------------
# Copyright (c) PTC Inc. and/or all its affiliates. All rights reserved.
# See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
# IoT Gateway Example - Simple example on how to manage a connection and
# execute various calls for the IoT Gateway components of the Kepware
# Configuration API
from kepconfig import connection, error
from kepconfig.connectivity import channel
import kepconfig.iot_gateway as iot
# Agent name and Type to be used - constants from kepconfig.iotgateway
# can be used to identify the type of agent
agent_name = 'MQTT Agent 1'
agent_type = iot.MQTT_CLIENT_AGENT
#Tag Address to add to the IoT agent
iot_item_name = "Channel1.Device1.Tag1"
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 = '')
# Add a Channel using the "Simulator Driver" with device and tags.
# These tags will be added to the IoT Agent.
channel_data = {
"common.ALLTYPES_NAME": "Channel1",
"common.ALLTYPES_DESCRIPTION": "This is the test channel created",
"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Simulator",
"devices": [
{
"common.ALLTYPES_NAME": "Device1",
"common.ALLTYPES_DESCRIPTION": "Hello, new description",
"servermain.MULTIPLE_TYPES_DEVICE_DRIVER": "Simulator",
"servermain.DEVICE_MODEL": 0,
"tags": [
{
"common.ALLTYPES_NAME": "Tag1",
"common.ALLTYPES_DESCRIPTION": "Ramping Read/Write tag used to verify client connection",
"servermain.TAG_ADDRESS": "R0001",
"servermain.TAG_DATA_TYPE": 5,
"servermain.TAG_READ_WRITE_ACCESS": 1,
"servermain.TAG_SCAN_RATE_MILLISECONDS": 100,
"servermain.TAG_SCALING_TYPE": 0
},
{
"common.ALLTYPES_NAME": "Tag2",
"common.ALLTYPES_DESCRIPTION": "Constant Read/Write tag used to verify client connection",
"servermain.TAG_ADDRESS": "K0001",
"servermain.TAG_DATA_TYPE": 5,
"servermain.TAG_READ_WRITE_ACCESS": 1,
"servermain.TAG_SCAN_RATE_MILLISECONDS": 100,
"servermain.TAG_SCALING_TYPE": 0
}
]
}
]
}
try:
print("{} - {}".format("Adding Channel, Device and tags", channel.add_channel(server,channel_data)))
except Exception as err:
ErrorHandler(err)
# Add the MQTT Agent with the appropriate parameters
agent_data = {
"common.ALLTYPES_NAME": agent_name,
"iot_gateway.AGENTTYPES_ENABLED": True,
"iot_gateway.MQTT_CLIENT_URL": "tcp://localhost:1883",
"iot_gateway.MQTT_CLIENT_TOPIC": "iotgateway",
"iot_gateway.MQTT_CLIENT_QOS": 1,
"iot_gateway.AGENTTYPES_RATE_MS": 10000,
"iot_gateway.AGENTTYPES_PUBLISH_FORMAT": 0,
"iot_gateway.AGENTTYPES_MAX_EVENTS": 1000,
"iot_gateway.AGENTTYPES_TIMEOUT_S": 5,
"iot_gateway.AGENTTYPES_MESSAGE_FORMAT": 0,
"iot_gateway.MQTT_CLIENT_CLIENT_ID": "",
"iot_gateway.MQTT_CLIENT_USERNAME": "",
"iot_gateway.MQTT_CLIENT_PASSWORD": ""
}
try:
print("{} - {}".format("Add the MQTT Agent", iot.agent.add_iot_agent(server, agent_data, agent_type)))
except Exception as err:
ErrorHandler(err)
# Modify a property of the Agent
agent_data = {
}
agent_data['common.ALLTYPES_DESCRIPTION'] = 'This is the test agent created'
try:
print("{} - {}".format("Modify property in the MQTT Agent", iot.agent.modify_iot_agent(server,agent_data, agent= agent_name, agent_type= agent_type)))
except Exception as err:
ErrorHandler(err)
# Get Agent the properties for the agent that was created. It will return the
# JSON of the properties
try:
print("{} - {}".format("Read properties of the MQTT Agent", iot.agent.get_iot_agent(server, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Get a list of all MQTT Agents that are configured
try:
print("{} - {}".format("Getting list of MQTT Agents", iot.agent.get_all_iot_agents(server, agent_type)))
except Exception as err:
ErrorHandler(err)
# Add an tag or IoT Item to the MQTT Agent to start publishing
iot_item_data = {
"common.ALLTYPES_NAME": iot_item_name,
"common.ALLTYPES_DESCRIPTION": "",
"iot_gateway.IOT_ITEM_SERVER_TAG": iot_item_name,
"iot_gateway.IOT_ITEM_USE_SCAN_RATE": True,
"iot_gateway.IOT_ITEM_SCAN_RATE_MS": 1000,
"iot_gateway.IOT_ITEM_SEND_EVERY_SCAN": False,
"iot_gateway.IOT_ITEM_DEADBAND_PERCENT": 0,
"iot_gateway.IOT_ITEM_ENABLED": True,
"iot_gateway.IOT_ITEM_DATA_TYPE": 5
}
try:
print("{} - {}".format("Add new tag to the MQTT Agent", iot.iot_items.add_iot_item(server, iot_item_data, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Modify properties of the tag or IoT Item. If the "common.ALLTYPES_Name" is defined
# the "modify_iot_item" function does not need have the agent name as an input
modify_iot_item = {
"common.ALLTYPES_NAME": iot_item_name,
"iot_gateway.IOT_ITEM_SCAN_RATE_MS": 500
}
try:
print("{} - {}".format("Modify the tag or IoT Item added", iot.iot_items.modify_iot_item(server, modify_iot_item, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Modify properties of the tag or IoT Item. (Version 2) It is not necessary to pass JSON
# with the "common.ALLTYPES_Name" of the tag to modify. It can be passed as a input
# for the "modify_iot_item" function. "Force" will force the
# update to the Kepware Server, if "FORCE_UPDATE" not provided in the JSON data.
modify_iot_item = {
"iot_gateway.IOT_ITEM_SCAN_RATE_MS": 2000
}
try:
print("{} - {}".format("Modify the tag or IoT Item added again", iot.iot_items.modify_iot_item(server, modify_iot_item, agent_name, agent_type, iot_item= iot_item_name, force = True)))
except Exception as err:
ErrorHandler(err)
# Read the tag or IoT Item configured in the MQTT Agent
try:
print("{} - {}".format("Read the properties of the IoT Item", iot.iot_items.get_iot_item(server, iot_item_name, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Get a list of all tags or IoT Items configured in the MQTT Agent
try:
print("{} - {}".format("Get a list of all the IoT Items configured in the MQTT Agent", iot.iot_items.get_all_iot_items(server, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Delete a tag or IoT Item configured in the MQTT Agent
try:
print("{} - {}".format("Delete the IoT Item", iot.iot_items.del_iot_item(server, iot_item_name, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)
# Delete the MQTT Agent
try:
print("{} - {}".format("Delete the MQTT Agent", iot.agent.del_iot_agent(server, agent_name, agent_type)))
except Exception as err:
ErrorHandler(err)