报警与条件
概述
OPC UA Part 9 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 封装为形参清晰的 Rust 方法。
| Method | NodeId | 入参 |
|---|---|---|
| Acknowledge | i=9111 | (event_id: &[u8], comment: &str) |
| Confirm | i=9113 | (event_id, comment) |
| AddComment | i=9029 | (event_id, comment) |
| Enable | i=2803 | — |
| Disable | i=2805 | — |
| ConditionRefresh | i=3875 | (sub_id) |
| ConditionRefresh2 | i=12912 | (sub_id, mi_id) |
| OneShotShelve | i=9211 | — |
| TimedShelve | i=9213 | (shelving_time_ms) |
| Unshelve | i=9215 | — |
API
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| ua.acknowledge(condition_id, event_id, comment) | 报警 | 写 | 用户已知 |
| ua.confirm(condition_id, event_id, comment) | 报警 | 写 | 现场已处理 |
| ua.add_comment(condition_id, event_id, comment) | 报警 | 写 | 加备注 |
| ua.enable_condition(condition_id) | 报警 | 写 | 启用 |
| ua.disable_condition(condition_id) | 报警 | 写 | 禁用 |
| ua.condition_refresh(sub_id) | 报警 | 写 | 补发激活报警 |
| ua.condition_refresh2(sub_id, mi_id) | 报警 | 写 | 仅指定 MI |
| ua.one_shot_shelve(shelving_state_id) | 报警 | 写 | 一次性屏蔽 |
| ua.timed_shelve(shelving_state_id, ms) | 报警 | 写 | 限时屏蔽 |
| ua.unshelve(shelving_state_id) | 报警 | 写 | 取消屏蔽 |
返回裸 StatusCode (不是 Result), 10 个方法全部不返回 Err、不能加 ?, 失败不 panic, 业务级成败由 StatusCode 表达, 用 rc.is_good() / rc.is_bad() 判断。
代码示例
use darra_opcua::{Session, well_known_nodes};
use std::sync::{mpsc, Arc};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://server:4840")?;
ua.connect()?;
// 事件订阅挂在 Session 上 (不在 Subscription 上); 回调在注册时一次性传入,
// 之后不能追加或替换 — EventSubscription 只暴露 monitored_item_handle().
let sub = Arc::new(ua.create_subscription(500.0)?);
let sub_id = sub.subscription_id();
let (tx, rx) = mpsc::channel();
let _ev = ua.subscribe_events(
Some(well_known_nodes::SERVER), // "i=2253"
Some(sub.clone()),
move |args| {
// EventArrivedEventArgs 只有 5 个平铺字段: severity / message /
// source_name / time / event_type. EventId / ConditionId 的原始
// ByteString 当前不透出, 需要业务侧自行映射.
let _ = tx.send((args.severity, args.source_name.clone().unwrap_or_default()));
},
)?;
for (sev, src) in rx {
let condition_id = format!("ns=2;s=Alarm.{}", src); // 业务侧自己的映射
let event_id: &[u8] = b""; // 当前不透出, 见上
// a. 让服务端补发当前激活报警 (返回裸 StatusCode, 不能 `?`)
let rc = ua.condition_refresh(sub_id);
if !rc.is_good() { eprintln!("refresh 失败: {:?} (sev={})", rc, sev); }
// b. 用户点 "确认" — 第二参是 &[u8]
let rc = ua.acknowledge(&condition_id, event_id, "操作员已知");
if !rc.is_good() { eprintln!("Acknowledge 失败: {:?}", rc); }
// c. 现场处理
let _ = ua.confirm(&condition_id, event_id, "现场已处理");
// d. 临时屏蔽 30 秒 (TimedShelve 入参是 f64 毫秒)
let _ = ua.timed_shelve(&format!("{}.ShelvingState", condition_id), 30_000.0);
}
Ok(())
}
错误处理
10 个方法都返回裸 StatusCode, 不返回 Err。以下失败情形在协议层有专属状态码, 但不在 SDK 的 StatusCode 常量集中, 不要写成可照抄的 StatusCode::Xxx:
| 失败情形 | 协议层含义 |
|---|---|
| 成功 | 状态码 0 |
| EventId 不匹配 | acknowledge / confirm 传入的 event_id 与服务端记录对不上 |
| 条件已启用 | enable_condition 打到一个已 Enabled 的 Condition |
| 条件已屏蔽 | timed_shelve / one_shot_shelve 打到一个已 Shelved 的条件 |
诊断手法:
if rc.is_bad() {
eprintln!("raw = 0x{:08X}, name = {}", rc.raw(), darra_opcua::status_name(rc));
}
StatusCode 是 newtype pub struct StatusCode(pub u32), 常量全大写 (StatusCode::GOOD / StatusCode::BAD / StatusCode::BAD_TIMEOUT ...), 判定用 is_good() / is_bad() / is_uncertain(), 取原始码用 raw()。
最佳实践
- 首次订阅后必调 condition_refresh
- acknowledge 后再 confirm, 两步
- TimedShelve 单位是毫秒 (f64), 不是秒
acknowledge/confirm/add_comment的event_id形参是&[u8], 不是&String- 事件回调是
FnMut(&EventArrivedEventArgs) + Send + 'static, 拿不到Session引用, 不要在回调里同步 ack — 用 mpsc 推队列, 由外层线程带着 Session 调用
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| Acknowledge | acknowledge | acknowledge | Acknowledge | acknowledge | DarraUa_Alarm_Acknowledge |
| Confirm | confirm | confirm | Confirm | confirm | DarraUa_Alarm_Confirm |
| ConditionRefresh | condition_refresh | conditionRefresh | ConditionRefresh | condition_refresh | DarraUa_Alarm_ConditionRefresh |
C 的家族名是 DarraUa_Alarm_*,不是 DarraUa_Condition_*;确认入口是 DarraUa_Alarm_ConditionRefresh / DarraUa_Alarm_ConditionRefresh2,屏蔽类是 DarraUa_Alarm_OneShotShelve / _TimedShelve / _Unshelve。