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
00018 server = Server()
00019 server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
00020
00021
00022 uri = "http://examples.freeopcua.github.io"
00023 idx = server.register_namespace(uri)
00024
00025
00026 objects = server.get_objects_node()
00027
00028
00029
00030 myobj = objects.add_object(idx, "MyObject")
00031
00032
00033
00034
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)
00043
00044 myobj = objects.add_object(idx, "MyCustomObjectA", mycustomobj_type.nodeid)
00045
00046
00047
00048
00049
00050
00051 server.import_xml('customobject.xml')
00052
00053
00054
00055
00056
00057
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
00064 myobj = objects.add_object(idx, "MyCustomObjectB", myobject3_type_nodeid)
00065
00066
00067
00068
00069 server.start()
00070
00071 try:
00072 while True:
00073 time.sleep(1)
00074 finally:
00075
00076 server.stop()