Skip to content

Commit

Permalink
make sure passing structc as method argument works
Browse files Browse the repository at this point in the history
  • Loading branch information
oroulet committed Mar 9, 2021
1 parent 12c0e7a commit d5c7acb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 3 additions & 1 deletion asyncua/common/manage_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@ def _vtype_to_argument(vtype):
if isinstance(vtype, ua.Argument):
return vtype
arg = ua.Argument()
if isinstance(vtype, ua.VariantType):
if hasattr(vtype, "data_type"):
arg.DataType = vtype.data_type
elif isinstance(vtype, ua.VariantType):
arg.DataType = ua.NodeId(vtype.value)
else:
arg.DataType = ua.NodeId(vtype)
Expand Down
6 changes: 3 additions & 3 deletions examples/server-data-type-definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ async def main():

await new_struct(server, idx, "MyStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32),
new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True),
])
await new_struct(server, idx, "MyOptionalStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32),
new_struct_field("MyUInt32List", ua.VariantType.UInt32, array=True),
new_struct_field("MyInt64", ua.VariantType.Int64, optional=True),
])
await new_enum(server, idx, "MyEnum", [
Expand All @@ -38,7 +38,7 @@ async def main():
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.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))

Expand Down
32 changes: 32 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,3 +1343,35 @@ async def test_custom_struct_of_struct_with_spaces(opc):
var = await opc.opc.nodes.objects.add_variable(idx, "my mother struct", mystruct)
val = await var.read_value()
assert val.My_Sub_Struct.My_UInt32 == 78


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]
12 changes: 11 additions & 1 deletion tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,17 @@ def test_guid():
assert v == v2


def test_nodeid():
def test_nodeid_guid_string():
n = ua.GuidNodeId(identifier=uuid.uuid4())
s = n.to_string()
n2 = ua.NodeId.from_string(s)
s2 = n2.to_string()
print(n, n2, s, s2)
assert n == n2
assert s == s2


def test__nodeid():
nid = ua.NodeId()
assert nid.NodeIdType == ua.NodeIdType.TwoByte
nid = ua.NodeId(446, 3, ua.NodeIdType.FourByte)
Expand Down

0 comments on commit d5c7acb

Please sign in to comment.