mellotanica

opcua_event_client.py

Mar 28th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from opcua import Client
  4.  
  5. evt_cnt = 0
  6. evt_limit = 100
  7. sub_period = 0.1
  8. server_addr = "opc.tcp://my.server.addr:port/" #obviusly this points to actual server address
  9.  
  10.  
  11. class EventHandler(object):
  12.     def event_notification(self, event):
  13.         global evt_cnt
  14.         evt_cnt += 1
  15.         print("new event: {}".format(event))
  16.  
  17. print("connecting to {}..".format(server_addr))
  18.  
  19. client = Client(server_addr)
  20.  
  21. try:
  22.     client.connect()
  23.  
  24.     print("connection ok, subscribing to event notifications..")
  25.  
  26.     #subscribe to event
  27.     rnode = client.get_root_node()
  28.  
  29.     mctl = EventHandler()
  30.  
  31.     sub = client.create_subscription(sub_period, mctl)
  32.     handle = sub.subscribe_events()
  33.  
  34.     while evt_cnt < evt_limit:
  35.         time.sleep(0.1)
  36.  
  37.     sub.unsubscribe(handle)
  38.     sub.delete()
  39.  
  40. except:
  41.     print("connection failed..")
  42.     if printTrace:
  43.         print(traceback.format_exc(), file=sys.stderr)
  44.  
  45. finally:
  46.     try:
  47.         client.disconnect()
  48.     except:
  49.         if printTrace:
  50.             print(traceback.format_exc(), file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment