async def test_custom_method_with_struct(opc): idx = 4 data_type, nodes = await new_struct(opc.opc, idx, "MyStructArg", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True), ]) await opc.opc.load_data_type_definitions() @uamethod def func(parent, mystruct): print(mystruct) mystruct.MyUInt32.append(100) return mystruct methodid = await opc.server.nodes.objects.add_method( ua.NodeId("ServerMethodWithStruct", 10), ua.QualifiedName('ServerMethodWithStruct', 10), func, [ua.MyStructArg], [ua.MyStructArg] ) mystruct = ua.MyStructArg() mystruct.MyUInt32 = [78, 79] assert data_type.nodeid == mystruct.data_type result = await opc.opc.nodes.objects.call_method(methodid, mystruct) assert result.MyUInt32 == [78, 79, 100]
async def test_custom_struct_of_struct_with_spaces(opc): idx = 6 nodeid = ua.NodeId("toto.My Identifier", idx) qname = ua.QualifiedName("My Sub Struct 1", idx) dtype, encs = await new_struct(opc.opc, nodeid, qname, [ new_struct_field("My Bool", ua.VariantType.Boolean), new_struct_field("My UInt32", ua.VariantType.UInt32), ]) await new_struct(opc.opc, idx, "My Mother Struct", [ new_struct_field("My Bool", ua.VariantType.Boolean), new_struct_field("My Sub Struct", dtype), ]) await opc.opc.load_data_type_definitions() mystruct = ua.My_Mother_Struct() mystruct.My_Sub_Struct = ua.My_Sub_Struct_1() mystruct.My_Sub_Struct.My_UInt32 = 78 var = await opc.opc.nodes.objects.add_variable( idx, "my mother struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject)) val = await var.read_value() assert val.My_Sub_Struct.My_UInt32 == 78
async def test_custom_struct_export(opc): idx = 4 dtype, encs = await new_struct(opc.opc, idx, "MyMyStructExport", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True), ]) await opc.opc.export_xml([dtype, *encs], "custom_struct_export.xml")
async def test_custom_struct_(opc): idx = 4 await new_struct(opc.opc, idx, "MyMyStruct", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True), ]) await opc.opc.load_data_type_definitions() mystruct = ua.MyMyStruct() mystruct.MyUInt32 = [78, 79] var = await opc.opc.nodes.objects.add_variable(idx, "my_struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject)) val = await var.read_value() assert val.MyUInt32 == [78, 79]
async def test_xml_struct_optional(opc, tmpdir): idx = 4 o, _ = await new_struct(opc.opc, idx, "MyOptionalStruct2", [ new_struct_field("MyString", ua.VariantType.String, optional=True), new_struct_field("MyInt64", ua.VariantType.Int64, optional=True), ]) tmp_path = tmpdir.join("export-union.xml").strpath await opc.opc.export_xml([o], tmp_path) await opc.opc.delete_nodes([o]) new_nodes = await opc.opc.import_xml(tmp_path) o2 = opc.opc.get_node(new_nodes[0]) assert o == o2 await opc.opc.load_data_type_definitions() t = ua.MyOptionalStruct2() t.MyInt64 = 5 assert t.MyString is None assert t.MyInt64 == 5
async def test_custom_struct_with_optional_fields(opc): idx = 4 await new_struct(opc.opc, 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 opc.opc.load_data_type_definitions() my_struct_optional = ua.MyOptionalStruct() my_struct_optional.MyUInt32 = 45 my_struct_optional.MyInt64 = -67 var = await opc.opc.nodes.objects.add_variable(idx, "my_struct_optional", ua.Variant(my_struct_optional, ua.VariantType.ExtensionObject)) val = await var.read_value() assert val.MyUInt32 == 45 assert val.MyInt64 == -67
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)
async def test_custom_struct_with_enum(opc): idx = 4 dtype = await new_enum(opc.opc, idx, "MyCustEnum2", [ "titi", "toto", "tutu", ]) await new_struct(opc.opc, idx, "MyStructEnum", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyEnum", dtype), ]) await opc.opc.load_data_type_definitions() mystruct = ua.MyStructEnum() mystruct.MyEnum = ua.MyCustEnum2.tutu var = await opc.opc.nodes.objects.add_variable(idx, "my_struct2", ua.Variant(mystruct, ua.VariantType.ExtensionObject)) val = await var.read_value() assert val.MyEnum == ua.MyCustEnum2.tutu
async def test_custom_list_of_struct(opc): idx = 4 dtype, encs = await new_struct(opc.opc, idx, "MySubStruct3", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MyUInt32", ua.VariantType.UInt32), ]) await new_struct(opc.opc, idx, "MyMotherStruct3", [ new_struct_field("MyBool", ua.VariantType.Boolean), new_struct_field("MySubStruct", dtype, array=True), ]) await opc.opc.load_data_type_definitions() mystruct = ua.MyMotherStruct3() mystruct.MySubStruct = [ua.MySubStruct3()] mystruct.MySubStruct[0].MyUInt32 = 78 var = await opc.opc.nodes.objects.add_variable(idx, "my_mother_struct3", ua.Variant(mystruct, ua.VariantType.ExtensionObject)) val = await var.read_value() assert val.MySubStruct[0].MyUInt32 == 78