async def test_import_xml_data_type_definition(opc): nodes = await opc.opc.import_xml("tests/substructs.xml") await opc.opc.load_data_type_definitions() assert hasattr(ua, "MySubstruct") assert hasattr(ua, "MyStruct") datatype = opc.opc.get_node(ua.MySubstruct.data_type) sdef = await datatype.read_data_type_definition() assert isinstance(sdef, ua.StructureDefinition) s = ua.MyStruct() s.toto = 0.1 ss = ua.MySubstruct() assert ss.titi == 0 assert isinstance(ss.structs, list) ss.titi = 1 ss.structs.append(s) ss.structs.append(s) var = await opc.opc.nodes.objects.add_variable( 2, "MySubStructVar", ss, datatype=ua.MySubstruct.data_type) s2 = await var.read_value() assert s2.structs[1].toto == ss.structs[1].toto == 0.1 await opc.opc.delete_nodes([datatype, var]) n = [] [n.append(opc.opc.get_node(node)) for node in nodes] await opc.opc.delete_nodes(n)
def test_structs(modeler, mgr): mgr.new_model() urns = modeler.get_current_server().get_namespace_array() ns_node = mgr.server_mgr.get_node(ua.ObjectIds.Server_NamespaceArray) urns = ns_node.read_value() urns.append("urn://modeller/testing") ns_node.write_value(urns) path = "test_save_structs.xml" struct_node = mgr.server_mgr.get_node(ua.ObjectIds.Structure) modeler.tree_ui.expand_to_node(struct_node) mystruct = mgr.add_data_type(1, "MyStruct") var1 = mystruct.add_variable(1, "MyFloat", 0.1, varianttype=ua.VariantType.Float) var2 = mystruct.add_variable(1, "MyBytes", b'lkjlk', varianttype=ua.VariantType.ByteString) mgr.save_xml(path) assert len( mgr.new_nodes ) == 4 # one struct + TypeDictionary node + namespace and struct node under typedict # FIXME: test for presence of nodes under typedict for every new struct opc_binary = mgr.server_mgr.get_node( ua.ObjectIds.OPCBinarySchema_TypeSystem) typedict = opc_binary.get_child("1:TypeDictionary") xml = typedict.read_value() assert b"MyFloat" in xml assert b"MyStruct" in xml mgr.save_xml(path) mgr.close_model() mgr.open_xml(path) opc_binary = mgr.server_mgr.get_node( ua.ObjectIds.OPCBinarySchema_TypeSystem) typedict = opc_binary.get_child("1:TypeDictionary") xml = typedict.read_value() assert b"MyFloat" in xml assert b"MyStruct" in xml struct_node = mgr.server_mgr.get_node(ua.ObjectIds.Structure) struct_node.get_child("1:MyStruct") mgr.server_mgr.load_type_definitions() st = ua.MyStruct() assert hasattr(st, "MyFloat") st.MyBytes = b"klk"
def test_structs_2(modeler, mgr): mgr.new_model() urns = modeler.get_current_server().get_namespace_array() ns_node = mgr.server_mgr.get_node(ua.ObjectIds.Server_NamespaceArray) urns = ns_node.read_value() urns.append("urn://modeller/testing") ns_node.write_value(urns) path = "test_save_structs_2.xml" struct_node = mgr.server_mgr.get_node(ua.ObjectIds.Structure) modeler.tree_ui.expand_to_node(struct_node) mystruct = mgr.add_data_type(1, "MyStruct") QApplication.processEvents() modeler.tree_ui.expand_to_node("MyStruct") var_node = mgr.add_variable(1, "myvar", 0.1) mgr.save_xml(path) mgr.close_model() mgr.open_xml(path) struct_node = mgr.server_mgr.get_node(ua.ObjectIds.Structure) mystruct_node = struct_node.get_child("1:MyStruct") var2_node = mystruct_node.add_variable(1, "myvar2", 0.2) # following code break...why??!?!??! #QApplication.processEvents() #modeler.tree_ui.expand_to_node("MyStruct") #var2_node = mgr.add_variable(1, "myvar2", 0.2) mgr.save_xml(path) mgr.close_model() mgr.open_xml(path) struct_node = mgr.server_mgr.get_node(ua.ObjectIds.Structure) mystruct = struct_node.get_child("1:MyStruct") assert len(mystruct.get_children()) == 2 mgr.server_mgr.load_type_definitions() st = ua.MyStruct() assert hasattr(st, "myvar") assert hasattr(st, "myvar2") mgr.close_model()
async def main(): # setup our server server = Server() await server.init() server.set_endpoint('opc.tcp://0.0.0.0:4840/freeopcua/server/') # setup our own namespace, not really necessary but should as spec uri = 'http://examples.freeopcua.github.io' idx = await server.register_namespace(uri) snode1, _ = await new_struct(server, idx, "MyStruct", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True), ]) snode2, _ = await new_struct(server, idx, "MyOptionalStruct", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True), new_struct_field("MyInt64", ua.VariantType.Int64, optional=True), ]) enode = await new_enum(server, idx, "MyEnum", [ "titi", "toto", "tutu", ]) custom_objs = await server.load_data_type_definitions() print("Custom objects on server") for name, obj in custom_objs.items(): print(" ", obj) valnode = await server.nodes.objects.add_variable(idx, "my_enum", ua.MyEnum.toto) await server.nodes.objects.add_variable(idx, "my_struct", ua.Variant(ua.MyStruct(), ua.VariantType.ExtensionObject)) my_struct_optional = ua.MyOptionalStruct() my_struct_optional.MyUInt32List = [45, 67] my_struct_optional.MyInt64 = -67 await server.nodes.objects.add_variable(idx, "my_struct_optional", ua.Variant(my_struct_optional, ua.VariantType.ExtensionObject)) await server.export_xml([server.nodes.objects, server.nodes.root, snode1, snode2, enode, valnode], "structs_and_enum.xml") async with server: while True: await asyncio.sleep(1)
async def main(): # setup our server server = Server() await server.init() server.set_endpoint('opc.tcp://0.0.0.0:4840/freeopcua/server/') # setup our own namespace, not really necessary but should as spec uri = 'http://examples.freeopcua.github.io' idx = await server.register_namespace(uri) await new_struct(server, idx, "MyStruct", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32), ]) await new_struct(server, idx, "MyOptionalStruct", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32), new_struct_field("MyInt64", ua.VariantType.Int64, optional=True), ]) await new_enum(server, idx, "MyEnum", [ "titi", "toto", "tutu", ]) await server.load_data_type_definitions() await server.nodes.objects.add_variable(idx, "my_enum", ua.MyEnum.toto) await server.nodes.objects.add_variable( idx, "my_struct", ua.Variant(ua.MyStruct(), ua.VariantType.ExtensionObject)) my_struct_optional = ua.MyOptionalStruct() my_struct_optional.MyUInt32 = 45 my_struct_optional.MyInt64 = -67 await server.nodes.objects.add_variable( idx, "my_struct_optional", ua.Variant(my_struct_optional, ua.VariantType.ExtensionObject)) async with server: while True: await asyncio.sleep(1)