跳到主要内容

browse

browse(node_id, filter = NodeClass.Unspecified) — 列出指定节点的直接子节点 (一层).

前置阅读 / 配套

签名​

def browse(self,
node_id: str,
filter: NodeClass = NodeClass.Unspecified) -> List[Reference]: ...

def browse_many(self,
node_ids: List[str],
filter: NodeClass = NodeClass.Unspecified) -> List[List[Reference]]: ...

Reference 字段​

字段类型说明
node_idstr子节点 NodeId
browse_namestr浏览名
display_namestr显示名
node_classNodeClass子节点类别

Reference.to_node(session) 转为可操作的 OpcUaNode.

例子​

from darra_opcua import NodeClass

# 列出 Boiler1 下所有子节点
children = ua.browse("ns=2;s=Boiler1")
for c in children:
print(f" {c.node_class.name} {c.browse_name} -> {c.node_id}")

# 只看 Variable 类型
vars_ = ua.browse("ns=2;s=Boiler1", filter=NodeClass.Variable)

# 只看 Method
methods = ua.browse("ns=2;s=Calculator", filter=NodeClass.Method)

批量浏览 (browse_many)​

如果同时要浏览多个节点 (例如 GUI 初次展开树), 用 browse_many 一次 RPC:

roots = ["i=85", "i=86"]   # Objects / Types
results = ua.browse_many(roots)
for nid, refs in zip(roots, results):
print(f"{nid}:")
for c in refs:
print(f" {c.browse_name}")

如果某个节点浏览失败 (NodeId 错), 对应槽位返回空列表, 不抛异常; 但整次 RPC 本身失败 (底层返回非 0) 会抛 OpcUaException。

NodeClass filter​

值包含
Unspecified (0)全部
Object (1)仅 Object
Variable (2)仅 Variable
Method (4)仅 Method
Object | Variable (按位或)组合

filter 原样作为 BrowseDescription 的 nodeClassMask 下发, 所以可以按位或组合 (0 = 不限)。

异常​

OpcUaException(BadNodeIdUnknown) / OpcUaException(BadCommunicationError) 等. transport 失败抛, 单个子节点失败不抛.

大节点的分页 (ContinuationPoint)​

底层一次 Browse 请求的 requestedMaxReferencesPerNode 固定为 1000, 所以子节点超过 1000 个时服务端只回第一页并给出 ContinuationPoint。browse 不读也不返回 ContinuationPoint, 拿到的就是这一页 —— 不是"全部子节点"。

要看全部, 用 browse_with_paging + browse_next:

page = ua.browse_with_paging("ns=2;s=BigFolder")
all_refs = list(page.references)
while page.continuation_point is not None:
page = ua.browse_next(page.continuation_point)
all_refs.extend(page.references)

一次多少 RPC​

  • browse: 1 次 RPC, 只拿第一页 (≤ 1000 条)
  • browse_many: 1 次 RPC 带 N 个 BrowseDescription, 省掉 N-1 次往返 (底层注释: "网络往返从 N 降为 1")
  • browse 系列的 BrowseDescription 固定是: 方向 Forward、引用类型 References (i=31) 含子类型、resultMask=0x3F —— 这些不是参数; 想按方向 / 引用类型筛目前只能客户端筛 (见下方 browse_filtered 的实现限制)

browse_filtered — 方向 + 引用类型过滤​

browse_filtered 的目标是在 browse 的基础上加 BrowseDirection + reference_type_id + include_subtypes 过滤 (Part 4 §5.8.2.2).

def browse_filtered(self,
nid: str,
node_class_filter: NodeClass = NodeClass.Unspecified,
direction: BrowseDirection = None, # None = 按 FORWARD 处理
reference_type_id: str = None,
include_subtypes: bool = True) -> List[Reference]: ...

相关结构 (真名在 darra_opcua, 大写成员):

class BrowseDirection(IntEnum):
FORWARD = 0 # 仅正向 (出引用), 父→子
INVERSE = 1 # 仅反向 (入引用), 子→父
BOTH = 2 # 正反向都返回

示例​

from darra_opcua import BrowseDirection, NodeClass

# 1) 正向 + 仅 HierarchicalReferences (i=33)
children = ua.browse_filtered("ns=2;s=Boiler1",
node_class_filter=NodeClass.Variable,
direction=BrowseDirection.FORWARD,
reference_type_id="i=33",
include_subtypes=True)

# 2) 反向找父
parents = ua.browse_filtered("ns=2;s=Tank.Level",
direction=BrowseDirection.INVERSE)
for p in parents:
print(f"父: {p.browse_name}")

实现限制 (重要)​

Python 侧当前一律退化成 browse(nid, node_class_filter), direction / reference_type_id / include_subtypes 不会生效:

  • DarraUa_Session_BrowseWithFilter 缺失时退化, 并打一次 Warning
  • 即使存在该入口, 当前同样退化 (三个筛选参数不生效)

所以今天 browse_filtered 实际只做 NodeClass 过滤, 方向永远是 Forward。要真按方向 / 引用类型筛选, 自己拿全量 Reference 后在客户端筛。(C# 侧的 BrowseFiltered 有 resultMask 参数, 两语言不完全一致。)

自动续翻​

Python SDK 没有内置的自动续翻 API (C# 侧才有 BrowseAll 扩展方法), 自己写个循环就行:

def browse_all_refs(ua, node_id, filter=NodeClass.Unspecified):
all_refs = []
page = ua.browse_with_paging(node_id, filter)
all_refs.extend(page.references)
while page.continuation_point is not None:
page = ua.browse_next(page.continuation_point)
all_refs.extend(page.references)
return all_refs

下一步​