Event subscription #1274
-
Hello there! I'm currently in a project that uses opc-ua protocol and I've been trying to use this library to subscribe to events and watch for node data changes. I'm facing some difficulties handling the events incoming from the subscription. class SubHandler:
def event_notification(self, event):
print(datetime.datetime.now())
print("Python: New event", event) But, what actually outputs is this: So, it seems like it never enters the Am I missing something? Here's my full code: import asyncio
import logging
import time
import datetime
from asyncua import Client, Node, ua
from asyncua.crypto.security_policies import SecurityPolicyBasic256Sha256
from asyncua.common.events import Event
logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger(__name__)
class SubHandler:
"""
The SubscriptionHandler is used to handle the data that is received for the subscription.
"""
def datachange_notification(self, node: Node, val, data):
"""
Callback for asyncua Subscription.
This method will be called when the Client received a data change message from the Server.
"""
print("Python: New data change event", node, val)
def event_notification(self, event):
print(datetime.datetime.now())
print("Python: New data change event", event)
async def task():
## Set project URL
url = "opc.tcp://my_ip_here"
client = Client(url=url, timeout=10)
client.set_user('my_user_here')
client.set_password('my_password_here')
await client.set_security(
SecurityPolicyBasic256Sha256,
certificate='./certificados/google_certificado.pem',
private_key='./certificados/google_chave_privada.pem',
server_certificate="./certificados/sage_certificado.der"
)
async with client:
await client.open_secure_channel(renew=False)
print("#"*25)
## Get root node
root_node = client.get_root_node()
print("The root node id is:", root_node)
# Subscribe to events on the node
handler = SubHandler()
subscription = await client.create_subscription(500, handler)
await subscription.subscribe_events()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(task()) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 13 replies
-
Which version are you using? We just released a version to pip, that fixed some issues with subscript to events. |
Beta Was this translation helpful? Give feedback.
-
I upgraded the package using |
Beta Was this translation helpful? Give feedback.
-
how to close the subscription depending on a value of a node in the code above |
Beta Was this translation helpful? Give feedback.
If you want to get a data change then call
subscribe_data_change
, events in opc ua is something totally different.You directly disconnect in your code. This code is what you want: