Skip to content

Commit

Permalink
Merge branch 'master' into feature/sync-browse-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
okapies authored Aug 24, 2023
2 parents 3a90628 + 4f4ce99 commit 0b290b7
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 284 deletions.
16 changes: 15 additions & 1 deletion asyncua/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def get_objects_node(self):
def get_server_node(self):
return self.get_node(ua.FourByteNodeId(ua.ObjectIds.Server))

def get_node(self, nodeid: Union[ua.NodeId, str]) -> Node:
def get_node(self, nodeid: Union[Node, ua.NodeId, str, int]) -> Node:
"""
Get node using NodeId object or a string representing a NodeId.
"""
Expand Down Expand Up @@ -911,3 +911,17 @@ async def browse_nodes(self, nodes: List[Node]) -> List[Tuple[Node, ua.BrowseRes
parameters.NodesToBrowse = nodestobrowse
results = await self.uaclient.browse(parameters)
return list(zip(nodes, results))

async def translate_browsepaths(self, starting_node: ua.NodeId, relative_paths: List[Union[ua.RelativePath, str]]) -> List[ua.BrowsePathResult]:
bpaths = []
for p in relative_paths:
try:
rpath = ua.RelativePath.from_string(p) if isinstance(p, str) else p
except ValueError as e:
raise ua.UaStringParsingError(f"Failed to parse one of RelativePath: {p}") from e
bpath = ua.BrowsePath()
bpath.StartingNode = starting_node
bpath.RelativePath = rpath
bpaths.append(bpath)

return await self.uaclient.translate_browsepaths_to_nodeids(bpaths)
8 changes: 4 additions & 4 deletions asyncua/client/ua_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
import copy
import logging
from typing import Awaitable, Callable, Dict, List, Optional, Union, Coroutine, Any
from typing import Awaitable, Callable, Dict, List, Optional, Union

from asyncua import ua
from asyncua.common.session_interface import AbstractSession
Expand Down Expand Up @@ -499,7 +499,7 @@ async def translate_browsepaths_to_nodeids(self, browse_paths):

async def create_subscription( # type: ignore[override]
self, params: ua.CreateSubscriptionParameters, callback
) -> Coroutine[Any, Any, ua.CreateSubscriptionResult]:
) -> ua.CreateSubscriptionResult:
self.logger.debug("create_subscription")
request = ua.CreateSubscriptionRequest()
request.Parameters = params
Expand Down Expand Up @@ -780,7 +780,7 @@ async def write_attributes(self, nodeids, datavalues, attributeid=ua.AttributeId
response.ResponseHeader.ServiceResult.check()
return response.Results

async def set_monitoring_mode(self, params) -> ua.uatypes.StatusCode:
async def set_monitoring_mode(self, params) -> List[ua.uatypes.StatusCode]:
"""
Update the subscription monitoring mode
"""
Expand All @@ -793,7 +793,7 @@ async def set_monitoring_mode(self, params) -> ua.uatypes.StatusCode:
response.ResponseHeader.ServiceResult.check()
return response.Parameters.Results

async def set_publishing_mode(self, params) -> ua.uatypes.StatusCode:
async def set_publishing_mode(self, params) -> List[ua.uatypes.StatusCode]:
"""
Update the subscription publishing mode
"""
Expand Down
4 changes: 2 additions & 2 deletions asyncua/common/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class Node:
OPC-UA protocol. Feel free to look at the code of this class and call
directly UA services methods to optimize your code
"""
def __init__(self, session: AbstractSession, nodeid: Union[ua.NodeId, str, int]):
def __init__(self, session: AbstractSession, nodeid: Union["Node", ua.NodeId, str, int]):
self.session = session
self.nodeid: ua.NodeId
if isinstance(nodeid, Node):
self.nodeid = nodeid.nodeid
elif isinstance(nodeid, ua.NodeId):
self.nodeid = nodeid
elif type(nodeid) in (str, bytes):
elif isinstance(nodeid, str):
self.nodeid = ua.NodeId.from_string(nodeid)
elif isinstance(nodeid, int):
self.nodeid = ua.NodeId(nodeid, 0)
Expand Down
15 changes: 12 additions & 3 deletions asyncua/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ def delete_subscriptions(self, subscription_ids):
def get_namespace_index(self, url):
pass

def get_node(self, nodeid):
return SyncNode(self.tloop, self.aio_obj.get_node(nodeid))
def get_node(self, nodeid: Union["SyncNode", ua.NodeId, str, int]):
aio_nodeid = nodeid.aio_obj if isinstance(nodeid, SyncNode) else nodeid
return SyncNode(self.tloop, self.aio_obj.get_node(aio_nodeid))

def get_root_node(self):
return SyncNode(self.tloop, self.aio_obj.get_root_node())
Expand All @@ -315,6 +316,14 @@ def write_values(self, nodes, values, raise_on_partial_error=True):
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,
starting_node: ua.NodeId,
relative_paths: List[Union[ua.RelativePath, str]]
) -> List[ua.BrowsePathResult]:
pass

def __enter__(self):
try:
self.connect()
Expand Down Expand Up @@ -460,7 +469,7 @@ def new_node(sync_node, nodeid):


class SyncNode:
def __init__(self, tloop, aio_node):
def __init__(self, tloop: ThreadLoop, aio_node: node.Node):
self.aio_obj = aio_node
self.tloop = tloop

Expand Down
Loading

0 comments on commit 0b290b7

Please sign in to comment.