Commit 4036f23b authored by Alexander Schrode's avatar Alexander Schrode Committed by oroulet

struct arrays in customtypes

- Fix default value for structs with lists
- Rename server-custom-structures-amd-enums.py
- Fix Enum in server-custom-structures-amd-enums.py
parent 6c957580
......@@ -198,7 +198,7 @@ class {struct_name}{base_class}:
# maybe we can use ua_utils.get_base_data_type()
raise RuntimeError(f"Unknown datatype for field: {sfield} in structure:{struct_name}, please report")
if sfield.ValueRank >= 1:
if sfield.ValueRank >= 0:
default_value = "field(default_factory=list)"
else:
default_value = get_default_value(uatype)
......
......@@ -27,6 +27,9 @@ async def main():
new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True),
new_struct_field("MyInt64", ua.VariantType.Int64, optional=True),
])
snode3, _ = await new_struct(server, idx, "MyNestedStruct", [
new_struct_field("MyStructArray", snode1, array=True),
])
enode = await new_enum(server, idx, "MyEnum", [
"titi",
"toto",
......@@ -38,14 +41,11 @@ async def main():
for name, obj in custom_objs.items():
print(" ", obj)
valnode = await server.nodes.objects.add_variable(idx, "my_enum", ua.MyEnum.toto)
valnode = await server.nodes.objects.add_variable(idx, "my_enum", ua.MyEnum.toto, datatype=enode.nodeid)
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")
await server.nodes.objects.add_variable(idx, "my_struct_optional", ua.Variant(ua.MyOptionalStruct(), ua.VariantType.ExtensionObject))
await server.nodes.objects.add_variable(idx, "t1", ua.Variant(ua.MyNestedStruct(), ua.VariantType.ExtensionObject))
await server.export_xml([server.nodes.objects, server.nodes.root, snode1, snode2, snode3, enode, valnode], "structs_and_enum.xml")
async with server:
while True:
......
......@@ -1353,6 +1353,28 @@ async def test_custom_struct_with_enum(opc):
assert val.MyEnum == ua.MyCustEnum2.tutu
async def test_nested_struct_arrays(opc):
idx = 4
snode1, _ = await new_struct(opc.opc, idx, "MyStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True),
])
snode2, _ = await new_struct(opc.opc, idx, "MyNestedStruct", [
new_struct_field("MyStructArray", snode1, array=True),
])
await opc.opc.load_data_type_definitions()
mystruct = ua.MyNestedStruct()
mystruct.MyStructArray = [ua.MyStruct(), ua.MyStruct()]
var = await opc.opc.nodes.objects.add_variable(idx, "nested", ua.Variant(mystruct, ua.VariantType.ExtensionObject))
val = await var.read_value()
assert len(val.MyStructArray) == 2
assert mystruct.MyStructArray == val.MyStructArray
@contextlib.contextmanager
def expect_file_creation(filename:str):
with tempfile.TemporaryDirectory() as tmpdir:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment