Commit f6b09f2a authored by oroulet's avatar oroulet

export struct104 methods in sync.py

parent c30f32bc
......@@ -15,7 +15,13 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
def new_struct_field(name: str, dtype: Union[ua.NodeId, Node, ua.VariantType], array: bool = False, optional: bool = False, description: str = "") -> ua.StructureField:
def new_struct_field(
name: str,
dtype: Union[ua.NodeId, Node, ua.VariantType],
array: bool = False,
optional: bool = False,
description: str = "",
) -> ua.StructureField:
"""
simple way to create a StructureField
"""
......@@ -43,7 +49,12 @@ def new_struct_field(name: str, dtype: Union[ua.NodeId, Node, ua.VariantType], a
return field
async def new_struct(server: Union["Server", "Client"], idx: Union[int, ua.NodeId], name: Union[int, ua.QualifiedName], fields: List[ua.StructureField]) -> Tuple[Node, List[Node]]:
async def new_struct(
server: Union["Server", "Client"],
idx: Union[int, ua.NodeId],
name: Union[int, ua.QualifiedName],
fields: List[ua.StructureField],
) -> Tuple[Node, List[Node]]:
"""
simple way to create a new structure
return the created data type node and the list of encoding nodes
......@@ -70,7 +81,12 @@ async def new_struct(server: Union["Server", "Client"], idx: Union[int, ua.NodeI
return dtype, [enc]
async def new_enum(server: Union["Server", "Client"], idx: Union[int, ua.NodeId], name: Union[int, ua.QualifiedName], values: List[str]) -> Node:
async def new_enum(
server: Union["Server", "Client"],
idx: Union[int, ua.NodeId],
name: Union[int, ua.QualifiedName],
values: List[str],
) -> Node:
edef = ua.EnumDefinition()
counter = 0
for val_name in values:
......
......@@ -4,6 +4,7 @@ sync API of asyncua
import asyncio
from threading import Thread, Condition
import logging
from typing import List, Tuple, Union
from asyncua import ua
from asyncua import client
......@@ -61,7 +62,7 @@ class ThreadLoop(Thread):
def _to_async(args, kwargs):
args = list(args) # FIXME: might be very inefficient...
for idx, arg in enumerate(args):
if isinstance(arg, SyncNode):
if isinstance(arg, (SyncNode, Client, Server)):
args[idx] = arg.aio_obj
elif isinstance(arg, (list, tuple)):
args[idx] = _to_async(arg, {})[0]
......@@ -92,6 +93,7 @@ def syncmethod(func):
aio_func = getattr(self.aio_obj, func.__name__)
result = self.tloop.post(aio_func(*args, **kwargs))
return _to_sync(self.tloop, result)
return wrapper
......@@ -113,7 +115,9 @@ def syncfunc(aio_func):
args, kwargs = _to_async(args, kwargs)
result = tloop.post(aio_func(*args, **kwargs))
return _to_sync(tloop, result)
return wrapper
return decorator
......@@ -162,6 +166,7 @@ class Client:
def __str__(self):
return "Sync" + self.aio_obj.__str__()
__repr__ = __str__
@syncmethod
......@@ -241,6 +246,7 @@ class Server:
def __str__(self):
return "Sync" + self.aio_obj.__str__()
__repr__ = __str__
def __enter__(self):
......@@ -305,6 +311,10 @@ class Server:
def load_type_definitions(self):
pass
@syncmethod
def load_data_type_definitions(self, node=None):
pass
@syncmethod
def write_attribute_value(self, nodeid, datavalue, attr=ua.AttributeIds.Value):
pass
......@@ -403,9 +413,7 @@ class SyncNode:
pass
@syncmethod
def get_children(
self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified
):
def get_children(self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified):
pass
@syncmethod
......@@ -489,7 +497,7 @@ class SyncNode:
def read_data_type_as_variant_type(self):
pass
get_data_type_as_variant_type = read_data_type_as_variant_type #legacy
get_data_type_as_variant_type = read_data_type_as_variant_type #legacy
@syncmethod
def call_method(self, methodid, *args):
......@@ -592,3 +600,35 @@ class DataTypeDictionaryBuilder:
@syncmethod
def set_dict_byte_string(self):
pass
def new_struct_field(
name: str,
dtype: Union[ua.NodeId, SyncNode, ua.VariantType],
array: bool = False,
optional: bool = False,
description: str = "",
) -> ua.StructureField:
if isinstance(dtype, SyncNode):
dtype = dtype.aio_obj
return common.structures104.new_struct_field(name, dtype, array, optional, description)
@syncfunc(aio_func=common.structures104.new_enum)
def new_enum(
server: Union["Server", "Client"],
idx: Union[int, ua.NodeId],
name: Union[int, ua.QualifiedName],
values: List[str],
) -> SyncNode:
pass
@syncfunc(aio_func=common.structures104.new_struct)
def new_struct(
server: Union["Server", "Client"],
idx: Union[int, ua.NodeId],
name: Union[int, ua.QualifiedName],
fields: List[ua.StructureField],
) -> Tuple[SyncNode, List[SyncNode]]:
pass
......@@ -1194,7 +1194,7 @@ async def test_custom_struct_of_struct(opc):
mystruct = ua.MyMotherStruct2()
mystruct.MySubStruct = ua.MySubStruct2()
mystruct.MySubStruct.MyUInt32 = 78
var = await opc.opc.nodes.objects.add_variable(idx, "my_mother_struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject))
var = await opc.opc.nodes.objects.add_variable(idx, "my_mother_struct", mystruct)
val = await var.read_value()
assert val.MySubStruct.MyUInt32 == 78
......@@ -1340,6 +1340,6 @@ async def test_custom_struct_of_struct_with_spaces(opc):
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))
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
......@@ -2,7 +2,7 @@ from concurrent.futures import Future
import pytest
from asyncua.sync import Client, Server, ThreadLoop, SyncNode, call_method_full, XmlExporter
from asyncua.sync import Client, Server, ThreadLoop, SyncNode, call_method_full, XmlExporter, new_enum, new_struct, new_struct_field
from asyncua import ua, uamethod
......@@ -132,3 +132,65 @@ def test_sync_xml_export(server):
exp = XmlExporter(server)
exp.build_etree([server.nodes.objects])
exp.write_xml("toto_test_export.xml")
def test_create_enum_sync(server):
idx = 4
new_enum(server, idx, "MyCustEnum", [
"titi",
"toto",
"tutu",
])
server.load_data_type_definitions()
var = server.nodes.objects.add_variable(idx, "my_enum", ua.MyCustEnum.toto)
val = var.read_value()
assert val == 1
def test_create_enum_sync_client(client):
idx = 4
new_enum(client, idx, "MyCustEnum2", [
"titi",
"toto",
"tutu",
])
client.load_data_type_definitions()
var = client.nodes.objects.add_variable(idx, "my_enum", ua.MyCustEnum2.toto)
val = var.read_value()
assert val == 1
def test_create_struct_sync(server):
idx = 4
new_struct(server, idx, "MyMyStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True),
])
server.load_data_type_definitions()
mystruct = ua.MyMyStruct()
mystruct.MyUInt32 = [78, 79]
var = server.nodes.objects.add_variable(idx, "my_struct", mystruct)
val = var.read_value()
assert val.MyUInt32 == [78, 79]
def test_create_struct_sync_client(client):
idx = 4
new_struct(client, idx, "MyMyStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True),
])
client.load_data_type_definitions()
mystruct = ua.MyMyStruct()
mystruct.MyUInt32 = [78, 79]
var = client.nodes.objects.add_variable(idx, "my_struct", mystruct)
val = var.read_value()
assert val.MyUInt32 == [78, 79]
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