报警与条件
概述
OPC UA Part 9 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 封装为 AlarmConditionApi 的静态方法 — OpcUaSession 作为第一个形参传入, 全部返回 Enums.StatusCode, 失败不抛异常。
| Method | NodeId | 入参 |
|---|---|---|
| Acknowledge | i=9111 | (eventId: byte[], comment: String) |
| Confirm | i=9113 | (eventId, comment) |
| AddComment | i=9029 | (eventId, comment) |
| Enable | i=2803 | — |
| Disable | i=2805 | — |
| ConditionRefresh | i=3875 | (subscriptionId) |
| ConditionRefresh2 | i=12912 | (subscriptionId, monitoredItemId) |
| OneShotShelve | i=9211 | — |
| TimedShelve | i=9213 | (shelvingTimeMs) |
| Unshelve | i=9215 | — |
API
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| AlarmConditionApi.acknowledge(ua, conditionId, eventId, comment) | 报警 | 写 | 用户已知 |
| AlarmConditionApi.confirm(ua, conditionId, eventId, comment) | 报警 | 写 | 现场已处理 |
| AlarmConditionApi.addComment(ua, conditionId, eventId, comment) | 报警 | 写 | 加备注 |
| AlarmConditionApi.enableCondition(ua, conditionId) | 报警 | 写 | 启用 |
| AlarmConditionApi.disableCondition(ua, conditionId) | 报警 | 写 | 禁用 |
| AlarmConditionApi.conditionRefresh(ua, subscriptionId) | 报警 | 写 | 补发激活报警 |
| AlarmConditionApi.conditionRefresh2(ua, subscriptionId, monitoredItemId) | 报警 | 写 | 仅指定 MI |
| AlarmConditionApi.oneShotShelve(ua, shelvingStateId) | 报警 | 写 | 一次性屏蔽 |
| AlarmConditionApi.timedShelve(ua, shelvingStateId, durationMs) | 报警 | 写 | 限时屏蔽 |
| AlarmConditionApi.unshelve(ua, shelvingStateId) | 报警 | 写 | 取消屏蔽 |
形参类型: ua = OpcUaSession; conditionId / shelvingStateId = String (NodeId); eventId = byte[]; comment = String; subscriptionId / monitoredItemId = int; durationMs = double。
返回 Enums.StatusCode, 失败不抛异常。
代码示例
import com.darra.opcua.*;
import javax.swing.JOptionPane;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://server:4840")) {
ua.connect();
OpcUaSubscription sub = ua.createSubscription(500.0);
OpcUaEventSubscription ev = ua.subscribeEvents("i=2253");
ev.onEventArrived.add(args -> {
// 事件快照只有 6 个扁平字段: severity / message / sourceName / timeFt / eventType / arrivedAtUtc
System.out.printf("[%d] %s: %s (%s)%n",
args.severity, args.sourceName, args.message, args.eventType);
// conditionId = 报警条件实例 NodeId (String); eventId = 该事件的 ByteString 原始字节 (byte[])
// 两者都不在事件快照内, 由上层报警模型持有 — 见下方说明
String conditionId = /* 上层给出 */ null;
byte[] eventId = /* 上层给出 */ null;
// a. 让服务端补发当前激活报警 — 作用在订阅上, 传 SubscriptionId
AlarmConditionApi.conditionRefresh(ua, sub.getSubscriptionId());
// b. 用户点 "确认" — conditionId 与 eventId 由上层报警模型持有, 见下方说明
Enums.StatusCode rc = AlarmConditionApi.acknowledge(ua, conditionId, eventId, "操作员已知");
if (rc != Enums.StatusCode.Good)
JOptionPane.showMessageDialog(null, "Acknowledge 失败: " + rc);
// c. 现场处理
AlarmConditionApi.confirm(ua, conditionId, eventId, "现场已处理");
// d. 临时屏蔽 30 秒 — shelvingStateId 是条件的 ShelvingState 子节点
AlarmConditionApi.timedShelve(ua, conditionId + ".ShelvingState", 30_000.0);
});
}
OpcUaEventSubscription.EventArrivedEventArgs 只有 6 个 final 字段: severity (int) / message (String) / sourceName (String) / timeFt (long) / eventType (String) / arrivedAtUtc (Instant)。它不带 EventId, 也不带 ConditionId。
acknowledge / confirm / addComment 的 eventId (byte[]) 与 conditionId (String) 需由调用方自行持有 — 从服务端读该事件的 EventId 字段, 或由上层报警模型维护。
错误处理
AlarmConditionApi.* 把底层异常折叠成 Enums.StatusCode 返回。Enums.StatusCode 只注册了常用码, 未注册的服务端码经 Enums.StatusCode.fromCode(int) 归并为 Bad 且不保留原始码, 因此下列情况无法从返回值区分。
| OPC UA 服务端码 (Part 4 §7.34) | 含义 | SDK 返回值 |
|---|---|---|
| Good | 成功 | Enums.StatusCode.Good |
| BadEventIdUnknown | EventId 不匹配 | Enums.StatusCode.Bad (原始码不保留) |
| BadConditionAlreadyEnabled | 已启用 | Enums.StatusCode.Bad (原始码不保留) |
| BadConditionAlreadyShelved | 已屏蔽 | Enums.StatusCode.Bad (原始码不保留) |
上表左列是 OPC UA 标准服务端状态码, 不是 Enums.StatusCode 的枚举常量, 不能写成 Enums.StatusCode.BadEventIdUnknown 这样照抄。
需要精确区分时, 改用底层 ua.call(objectNodeId, methodNodeId, inputs) 直接调 Method, 捕获 OpcUaException 读 ex.rawCode (OpcUaException.rawCode 保留服务端原始 int 码)。
最佳实践
- 首次订阅后必调 conditionRefresh
- acknowledge 后再 confirm, 两步
- TimedShelve 单位 ms, 不是秒
- 不要在监听器里同步 ack, 推队列后台处理
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| Acknowledge | acknowledge | AlarmConditionApi.acknowledge | Acknowledge | acknowledge | DarraUa_Alarm_Acknowledge |
| Confirm | confirm | AlarmConditionApi.confirm | Confirm | confirm | DarraUa_Alarm_Confirm |
| AddComment | add_comment | AlarmConditionApi.addComment | AddComment | add_comment | DarraUa_Alarm_AddComment |
| EnableCondition | enable_condition | AlarmConditionApi.enableCondition | EnableCondition | enable_condition | DarraUa_Alarm_EnableCondition |
| DisableCondition | disable_condition | AlarmConditionApi.disableCondition | DisableCondition | disable_condition | DarraUa_Alarm_DisableCondition |
| ConditionRefresh | condition_refresh | AlarmConditionApi.conditionRefresh | ConditionRefresh | condition_refresh | DarraUa_Alarm_ConditionRefresh |
| ConditionRefresh2 | condition_refresh2 | AlarmConditionApi.conditionRefresh2 | ConditionRefresh2 | condition_refresh2 | DarraUa_Alarm_ConditionRefresh2 |
| OneShotShelve | one_shot_shelve | AlarmConditionApi.oneShotShelve | OneShotShelve | one_shot_shelve | DarraUa_Alarm_OneShotShelve |
| TimedShelve | timed_shelve | AlarmConditionApi.timedShelve | TimedShelve | timed_shelve | DarraUa_Alarm_TimedShelve |
| Unshelve | unshelve | AlarmConditionApi.unshelve | Unshelve | unshelve | DarraUa_Alarm_Unshelve |
C 侧十个入口都是 DarraUa_Alarm_* 家族, 第一个参数是 DarraUa_SessionHandle: DarraUa_Alarm_Acknowledge / _Confirm / _AddComment / _EnableCondition / _DisableCondition / _ConditionRefresh / _ConditionRefresh2 / _OneShotShelve / _TimedShelve / _Unshelve, 返回 DarraUa_Status, 不抛异常。