Commit 8d8edbd6 authored by oroulet's avatar oroulet Committed by oroulet

correct vtype_to_argument to suport Node and NodeId and enums

parent a69566c5
......@@ -2,6 +2,9 @@
High level functions to create nodes
"""
import logging
from enum import Enum
import inspect
from asyncua import ua
from .instantiate_util import instantiate
from .node_factory import make_node
......@@ -425,9 +428,15 @@ def _vtype_to_argument(vtype):
arg = ua.Argument()
if hasattr(vtype, "data_type"):
arg.DataType = vtype.data_type
elif inspect.isclass(vtype) and issubclass(vtype, Enum):
arg.DataType = ua.enums_datatypes[vtype]
elif isinstance(vtype, ua.VariantType):
arg.DataType = ua.NodeId(vtype.value)
elif hasattr(ua.VariantType, vtype.__name__):
elif isinstance(vtype, ua.NodeId):
arg.DataType = vtype
elif hasattr(vtype, "nodeid"): # NodeId case but we cannot import Node object here
arg.DataType = vtype.nodeid
elif hasattr(vtype, "__name__") and hasattr(ua.VariantType, vtype.__name__):
arg.DataType = ua.NodeId(ua.VariantType[vtype.__name__].value)
else:
arg.DataType = ua.NodeId(vtype)
......
......@@ -1415,3 +1415,28 @@ async def test_custom_method_with_struct(opc):
result = await opc.opc.nodes.objects.call_method(methodid, mystruct)
assert result.MyUInt32 == [78, 79, 100]
async def test_custom_method_with_enum(opc):
idx = 4
enum_node = await new_enum(opc.opc, idx, "MyCustEnumForMethod", [
"titi",
"toto",
])
await opc.opc.load_data_type_definitions()
@uamethod
def func(parent, myenum1, myenum2, myenum3):
assert myenum1 == ua.MyCustEnumForMethod.titi
return ua.MyCustEnumForMethod.toto
methodid = await opc.server.nodes.objects.add_method(
ua.NodeId("servermethodwithenum", 10),
ua.QualifiedName('servermethodwithenum', 10),
func, [ua.MyCustEnumForMethod, enum_node, enum_node.nodeid], [ua.MyCustEnumForMethod]
)
result = await opc.opc.nodes.objects.call_method(methodid, ua.MyCustEnumForMethod.titi, ua.MyCustEnumForMethod.titi, ua.MyCustEnumForMethod.titi)
assert result == ua.MyCustEnumForMethod.toto
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