跳到主要内容

Browse

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

前置阅读 / 配套

签名​

pub fn Session::browse(&self, node_id: &str, filter: NodeClass)
-> Result<Vec<Reference>, OpcUaError>;

pub fn Session::browse_many(&self, node_ids: &[&str], filter: NodeClass)
-> Result<Vec<Vec<Reference>>, OpcUaError>;

Reference 字段​

pub struct Reference {
pub node_id: String,
pub browse_name: String, // 含 NamespaceIndex 前缀, 如 "2:Temperature"
pub display_name: String,
pub node_class: NodeClass,
}

示例​

use darra_opcua::NodeClass;

// 列出 Boiler1 下所有子节点
let children = s.browse("ns=2;s=Boiler1", NodeClass::Unspecified)?;
for c in &children {
println!(" {:?} {} -> {}", c.node_class, c.browse_name, c.node_id);
}

// 只看 Variable 类型
let vars = s.browse("ns=2;s=Boiler1", NodeClass::Variable)?;

// 只看 Method
let methods = s.browse("ns=2;s=Calculator", NodeClass::Method)?;

批量浏览 (browse_many)​

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

let roots = ["i=85", "i=86"];   // Objects + Types
let results = s.browse_many(&roots, NodeClass::Unspecified)?;
for (root, children) in roots.iter().zip(results.iter()) {
println!("{}:", root);
for c in children {
println!(" {}", c.browse_name);
}
}

如果某个节点浏览失败 (NodeId 错), 对应槽位返回空 Vec, 不返回 Err.

NodeClass filter​

pub enum NodeClass {
Unspecified = 0,
Object = 1,
Variable = 2,
Method = 4,
ObjectType = 8,
VariableType = 16,
ReferenceType = 32,
DataType = 64,
View = 128,
}
值包含
Unspecified全部
Object仅 Object
Variable仅 Variable
Method仅 Method

当前 SDK 只支持单一 NodeClass filter (不支持按位或组合); 要多 class 请走 Unspecified 拿全部再客户端过滤.

错误​

OpcUaError 携带:

  • BAD_NODE_ID_UNKNOWN — 节点不存在
  • BAD_COMMUNICATION_ERROR — 网络故障
  • INVALID_ARGUMENT — node_id 含 NUL

transport 失败返回 Err, 单个子节点解析失败不返回 Err.

大节点的分页 (ContinuationPoint)​

单次 browse 能返回多少个引用由服务端决定, 超出服务端的单次返回上限就会截断. 截断时 browse 只返回第一页.

要看全部, 用 BrowseWithPaging + BrowseNext:

let mut page = s.browse_with_paging("ns=2;s=BigFolder", NodeClass::Unspecified)?;
let mut all_refs = page.references.clone();
while let Some(cp) = page.continuation_point.take() {
if cp.is_empty() { break; }
page = s.browse_next(&cp, false)?;
all_refs.extend(page.references.iter().cloned());
}

或一行式:

let all = s.browse_all("ns=2;s=BigFolder", NodeClass::Unspecified, 100)?;

Reference → Node 转换​

for r in s.browse("ns=2;s=Boiler1", NodeClass::Variable)? {
let node = r.to_node(&s); // 转可操作 Node
let dv = node.value()?;
println!("{} = {:?}", r.display_name, dv.variant());
}

性能​

  • 单次 Browse: 一次网络往返 + 服务端遍历, 耗时随网络与服务端而变, 按你的环境实测
  • browse_many 批量: 总耗时 ≈ 单次 (省 N-1 次往返)
  • 子节点数量超过服务端单次返回上限时会分页, 用 browse_all / browse_next 处理

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

browse_filtered 的 API 在 browse 的基础上多出 BrowseDirection + reference_type_id + include_subtypes + result_mask 四个参数 (Part 4 §5.8.2.2). 当前实现这四个参数不生效, 见下方「实现说明」.

pub fn browse_filtered(
&self,
node_id: &str,
node_class_filter: NodeClass,
direction: BrowseDirection,
reference_type_id: Option<&str>,
include_subtypes: bool,
result_mask: u32,
) -> Result<Vec<Reference>, OpcUaError>

相关结构:

pub enum BrowseDirection {
Forward = 0, // 仅正向, 父→子
Inverse = 1, // 仅反向, 子→父
Both = 2, // 正反向都返回
}

示例​

use darra_opcua::BrowseDirection;

// 1) 传正向 + HierarchicalReferences (i=33) —— 当前实现只按 NodeClass 过滤
let children = ua.browse_filtered("ns=2;s=Boiler1",
NodeClass::Unspecified, BrowseDirection::Forward,
Some("i=33"), true, 0x3F)?;

// 2) 传反向 —— 需要"只留父节点"时请在客户端自行筛选
let parents = ua.browse_filtered("ns=2;s=Tank.Level",
NodeClass::Unspecified, BrowseDirection::Inverse, None, true, 0x3F)?;
for p in &parents {
println!("父: {}", p.browse_name);
}

// 3) 双向
let all = ua.browse_filtered("ns=2;s=Robot.Joint1",
NodeClass::Unspecified, BrowseDirection::Both, None, true, 0x3F)?;

实现说明​

当前实现一律回落到 browse(node_id, node_class_filter), 只有 NodeClass 过滤生效:

  • direction / reference_type_id / include_subtypes / result_mask 四个参数在实现里被直接丢弃 (let _ = (direction, reference_type_id, include_subtypes, result_mask);), 既不生效, 也不打 Warning.
  • DarraUa_Session_BrowseWithFilter 当前未导出 (浏览相关的底层入口只有 DarraUa_Session_BrowseNode / DarraUa_Session_BrowseNodes / DarraUa_Session_BrowseNext), 照这个名字调用会失败.

后续底层导出该入口后可切换为原生调用, 公共 API 不变.

当前行为

上面示例里的 direction / reference_type_id 参数现在不影响返回结果. 需要按方向 / 引用类型过滤时, 请在客户端对返回的 Reference 自行筛选.

下一步​