报警与条件
概述
OPC UA Part 9 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 封装为形参清晰的 Java 方法。
| 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
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| ua.acknowledge(conditionId, eventId, comment) | 报警 | 写 | 用户已知 |
| ua.confirm(conditionId, eventId, comment) | 报警 | 写 | 现场已处理 |
| ua.addComment(conditionId, eventId, comment) | 报警 | 写 | 加备注 |
| ua.enableCondition(conditionId) | 报警 | 写 | 启用 |
| ua.disableCondition(conditionId) | 报警 | 写 | 禁用 |
| ua.conditionRefresh(subId) | 报警 | 写 | 补发激活报警 |
| ua.conditionRefresh2(subId, miId) | 报警 | 写 | 仅指定 MI |
| ua.oneShotShelve(shelvingStateId) | 报警 | 写 | 一次性屏蔽 |
| ua.timedShelve(shelvingStateId, ms) | 报警 | 写 | 限时屏蔽 |
| ua.unshelve(shelvingStateId) | 报警 | 写 | 取消屏蔽 |
返回 StatusCode, 失败不抛异常。
代码示例
import com.darra.opcua.*;
try (DarraOpcUa ua = new DarraOpcUa("opc.tcp://server:4840")) {
ua.connect();
OpcUaSubscription sub = ua.createSubscription(500);
OpcUaEventSubscription ev = sub.subscribeEvents("i=2253");
ev.addEventArrivedListener(args -> {
byte[] eventId = args.getField("EventId").asByteString();
String conditionId = args.getField("ConditionId").asString();
// a. 让服务端补发当前激活报警
ua.conditionRefresh(sub.getSubscriptionId());
// b. 用户点 "确认"
StatusCode rc = ua.acknowledge(conditionId, eventId, "操作员已知");
if (rc != StatusCode.Good)
JOptionPane.showMessageDialog(null, "Acknowledge 失败: " + rc);
// c. 现场处理
ua.confirm(conditionId, eventId, "现场已处理");
// d. 临时屏蔽 30 秒
ua.timedShelve(conditionId + ".ShelvingState", 30_000.0);
});
}
错误处理
| StatusCode | 含义 |
|---|---|
| Good | 成功 |
| BadEventIdUnknown | EventId 不匹配 |
| BadConditionAlreadyEnabled | 已启用 |
| BadConditionAlreadyShelved | 已屏蔽 |
最佳实践
- 首次订阅后必调 conditionRefresh
- acknowledge 后再 confirm, 两步
- TimedShelve 单位 ms, 不是秒
- 不要在监听器里同步 ack, 推队列后台处理
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| Acknowledge | acknowledge | acknowledge | Acknowledge | acknowledge | DarraUa_Condition_Acknowledge |
| Confirm | confirm | confirm | Confirm | confirm | DarraUa_Condition_Confirm |
| ConditionRefresh | condition_refresh | conditionRefresh | ConditionRefresh | condition_refresh | DarraUa_Condition_Refresh |