Subscription in OPCUA Server #1239
Replies: 2 comments 8 replies
-
Ahoi, for subscriptions I recommened the example file for a better "quick start" in that topic. With create_subscription(params, handler) you create a Subscription object and telling the server "I want to subscribe something, which should be checked every period/params milliseconds and how should be deald with it shall be organised in handler. A handler can be like this one: class SubHandler(object):
"""
Subscription Handler. To receive events from server for a subscription
"""
def datachange_notification(self, node, val, data):
print("Python: New data change event", node, val)
def event_notification(self, event):
print("Python: New event", event)
[...add here standard main code like you did ...]
params = 500 # One possible option, for more details see in sourcecode
sub = client.create_subscription(params, Subhandler) but you can add other handlers, more methods, depending on your needs. So now you told the server "This is one way, how I want to deal a Subscription". But what shall the server looking for? Which Node shall be subscribed now? With sub.subscribe_data_change(mynode) you say: "I want to Subscribe the node mynode, which should be handled like mentioned in the Subscriptionobject sub above, to an datachange_notification event. If such an event happens, do whatever is inside the method which you find in the Subhandler() class. So your code may have to change a bit (no guaranty): class SubHandler(object):
"""
Subscription Handler. To receive events from server for a subscription.
Never add long running code inside these methods! They are blocking the server otherwise!
"""
def datachange_notification(self, node, val, data):
print("Python: New data change event", node, val)
def event_notification(self, event):
print("Python: New event", event)
name = "PYTHON_OPCUA_SERVER"
addspace = server.register_namespace(name)
node = server.get_objects_node()
Param = node.add_object(addspace, "Parameters")
Temp = Param.add_variable(addspace, "Temperature", 0)
Temp.set_writable()
handler = SubHandler()
sub = server.create_subscription(500, datachange_notification) # example!
handle = sub.subscribing_data_cahnge(Temp) # sub to value changes of the node Temp
server.start()
# just an ugly out-of-the-box idea to trigger datachange events.
for i in 1000:
Temp.set_value(i) In case of faults, please correct me. |
Beta Was this translation helpful? Give feedback.
-
Hi @swamper123 class SubHandler(object):
name = "PYTHON_OPCUA_SERVER" Temp.set_writable() In the highlighted line of the above code, I changed the argument of create_subscription to "handler" as I was getting an error when used datachange_notification. The rest are the same. I have another question. Your help is much appreciated .. |
Beta Was this translation helpful? Give feedback.
-
I am trying to add subscription in my OPCUA Server code but the keyword "create_subscription" ( the one as per the OPCUA docs) seems to be not being recognized by the code.
Below is a part of my code where I am trying to add subscription
name = "PYTHON_OPCUA_SERVER"
addspace = server.register_namespace(name)
node = server.get_objects_node()
Param = node.add_object(addspace, "Parameters")
Temp = Param.add_variable(addspace, "Temperature", 0)
server.start()
Temp.set_writable()
Temp.create_subscription()
I am an amateur when it comes to python with OPCUA . Kindly bear with me :)
Thanks
Beta Was this translation helpful? Give feedback.
All reactions