event_client.py
Go to the documentation of this file.
00001 
00002 import sys
00003 import time
00004 sys.path.append(".")
00005 
00006 from IPython import embed
00007 import opcua
00008 
00009 class SubHandler(opcua.SubscriptionHandler):
00010     """
00011     Client to subcsription. It will receive events from server
00012     """
00013     def __init__(self, *args):
00014         opcua.SubscriptionHandler.__init__(self, *args)
00015         self.ev = MessageSecurityMode::None
00016 
00017     def data_change(self, handle, node, val, attr):
00018         print("Python: New data change event", handle, node, val, attr)
00019 
00020     def event(self, handle, event):
00021         print("Python: New event", handle, event)
00022         self.ev = event
00023 
00024 
00025 if __name__ == "__main__":
00026     # create our client object
00027     client = opcua.Client(False)
00028     client.connect("opc.tcp://localhost:4841/freeopcua/server/")
00029     #s.connect("opc.tcp://192.168.56.101:48030")
00030     try:
00031         # get server namespace. You may want to get all namespaces
00032         # with client.get_server_namespaces()
00033         uri = "http://examples.freeopcua.github.io"
00034         idx = client.get_namespace_index(uri)
00035         
00036         # read a node from standard opcua address space
00037         statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
00038         print("Server state is: ", statenode.get_value())
00039 
00040         # get root node of server and browse it
00041         root = client.get_root_node()
00042         print("I got root: ", root)
00043         print("Childs are: ", root.get_children())
00044 
00045         # get objects node of server 
00046         # this is where the interesting data from server should be
00047         print("Objects is: ", client.get_objects_node())
00048         objects = client.get_objects_node()
00049         print("Children of objects are: ", objects.get_children())
00050         
00051         # get child using browse path
00052         myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
00053         print("yvar is: ", myvar)
00054         
00055         # create a subsription we will use to subscribe to nodes or events
00056         sclt = SubHandler()
00057         sub = client.create_subscription(100, sclt)
00058 
00059         # subscribe to a specific node  
00060         #handle = sub.subscribe_data_change(myvar)
00061         #print("Subscribe handle is: ", handle)
00062 
00063         # subscribe to events from server  
00064         evhandle = sub.subscribe_events()
00065         print("Subscribe handle is: ", evhandle)
00066 
00067 
00068         embed()
00069     finally:
00070         client.disconnect()


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40