Skip to content

Commit

Permalink
Add browse_nodes to SyncClient (FreeOpcUa#1412)
Browse files Browse the repository at this point in the history
* add browse_nodes to SyncClient

* fix ruff E721 warning
  • Loading branch information
okapies authored and Dmitrij Vinokour committed Sep 19, 2023
1 parent 3338ebf commit 38c9a54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions asyncua/client/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import socket
from typing import List, Union, Coroutine, Optional, Type
from typing import List, Tuple, Union, Coroutine, Optional, Type
from urllib.parse import urlparse, unquote
from pathlib import Path

Expand Down Expand Up @@ -894,7 +894,7 @@ async def write_values(self, nodes, values, raise_on_partial_error=True):
get_values = read_values # legacy compatibility
set_values = write_values # legacy compatibility

async def browse_nodes(self, nodes):
async def browse_nodes(self, nodes: List[Node]) -> List[Tuple[Node, ua.BrowseResult]]:
"""
Browses multiple nodes in one ua call
returns a List of Tuples(Node, BrowseResult)
Expand Down
11 changes: 6 additions & 5 deletions asyncua/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ def _to_async(args, kwargs):
def _to_sync(tloop, result):
if isinstance(result, node.Node):
return SyncNode(tloop, result)
if isinstance(result, (list, tuple)) and len(result) > 0:
if isinstance(result[0], node.Node):
return [SyncNode(tloop, i) for i in result]
elif isinstance(result[0], (list, tuple)):
return [_to_sync(tloop, item) for item in result]
if isinstance(result, (list, tuple)):
return [_to_sync(tloop, item) for item in result]
if isinstance(result, server.event_generator.EventGenerator):
return EventGenerator(tloop, result)
if isinstance(result, subscription.Subscription):
Expand Down Expand Up @@ -315,6 +312,10 @@ def read_values(self, nodes):
def write_values(self, nodes, values, raise_on_partial_error=True):
pass

@syncmethod
def browse_nodes(self, nodes: List["SyncNode"]) -> List[Tuple["SyncNode", ua.BrowseResult]]: # type: ignore[empty-body]
pass

@syncmethod
def translate_browsepaths( # type: ignore[empty-body]
self,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ def test_sync_client_get_node(client, idx):
assert vars[0].read_value() == 6.7


def test_sync_client_browse_nodes(client: Client, idx):
nodes = [
client.get_node("ns=0;i=2267"),
client.get_node("ns=0;i=2259"),
]
results = client.browse_nodes(nodes)
assert len(results) == 2
assert isinstance(results, list)
assert results[0][0] == nodes[0]
assert results[1][0] == nodes[1]
assert isinstance(results[0][0], SyncNode)
assert isinstance(results[1][0], SyncNode)
assert isinstance(results[0][1], ua.BrowseResult)
assert isinstance(results[1][1], ua.BrowseResult)


def test_sync_server_get_node(server, idx):
node = server.get_node(85)
assert node == server.nodes.objects
Expand Down

0 comments on commit 38c9a54

Please sign in to comment.