server-custom-object.py
Go to the documentation of this file.
00001 '''
00002    Show 3 different examples for creating an object:
00003    1) create a basic object
00004    2) create a new object type and a instance of the new object type
00005    3) import a new object from xml address space and create a instance of the new object type
00006 '''
00007 import sys
00008 sys.path.insert(0, "..")
00009 import time
00010 
00011 
00012 from opcua import ua, Server
00013 
00014 
00015 if __name__ == "__main__":
00016 
00017     # setup our server
00018     server = Server()
00019     server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
00020 
00021     # setup our own namespace, not really necessary but should as spec
00022     uri = "http://examples.freeopcua.github.io"
00023     idx = server.register_namespace(uri)
00024 
00025     # get Objects node, this is where we should put our custom stuff
00026     objects = server.get_objects_node()
00027 
00028     # Example 1 - create a basic object
00029     #-------------------------------------------------------------------------------
00030     myobj = objects.add_object(idx, "MyObject")    
00031     #-------------------------------------------------------------------------------
00032 
00033 
00034     # Example 2 - create a new object type and a instance of the new object type
00035     #-------------------------------------------------------------------------------
00036     types = server.get_node(ua.ObjectIds.BaseObjectType)
00037     
00038     object_type_to_derive_from = server.get_root_node().get_child(["0:Types", 
00039                                                                    "0:ObjectTypes", 
00040                                                                    "0:BaseObjectType"])
00041     mycustomobj_type = types.add_object_type(idx, "MyCustomObject")
00042     mycustomobj_type.add_variable(0, "var_should_be_there_after_instantiate", 1.0) # demonstrates instantiate
00043     
00044     myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid)
00045     #-------------------------------------------------------------------------------
00046 
00047 
00048     # Example 3 - import a new object from xml address space and create a instance of the new object type
00049     #-------------------------------------------------------------------------------
00050     # Import customobject type
00051     server.import_xml('customobject.xml')
00052         
00053 
00054     # get nodeid of custom object type by one of the following 3 ways:
00055     # 1) Use node ID
00056     # 2) Or Full path
00057     # 3) Or As child from parent
00058     myobject1_type_nodeid = ua.NodeId.from_string('ns=1;i=1002')
00059     myobject2_type_nodeid = server.get_root_node().get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType", "1:MyObjectType"]).nodeid
00060     myobject3_type_nodeid = server.get_node(ua.ObjectIds.BaseObjectType).get_child(["1:MyObjectType"]).nodeid
00061 
00062 
00063     # populating our address space    
00064     myobj = objects.add_object(idx, "MyCustomObjectB", myobject3_type_nodeid)
00065     #-------------------------------------------------------------------------------
00066 
00067     
00068     # starting!
00069     server.start()
00070 
00071     try:
00072         while True:
00073             time.sleep(1)
00074     finally:
00075         # close connection, remove subcsriptions, etc
00076         server.stop()


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23