报警与条件 (Acknowledge / Confirm / Refresh / Shelve)
概述
OPC UA Part 9 (Alarms & Conditions) 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 提供形参清晰的 C# 方法 (都是 DarraOpcUa 的成员), 上层报警面板不用手拼 Variant[] 入参。
固定 NodeId (Part 9 / Part 5 标准命名空间 0):
| Method | NodeId | 入参 |
|---|---|---|
| Acknowledge | i=9111 | (eventId : ByteString, comment : LocalizedText) |
| Confirm | i=9113 | (eventId : ByteString, comment : LocalizedText) |
| AddComment | i=9029 | (eventId : ByteString, comment : LocalizedText) |
| Enable | i=2803 | 无 |
| Disable | i=2805 | 无 |
| ConditionRefresh | i=3875 | (subscriptionId : IntegerId) |
| ConditionRefresh2 | i=12912 | (subscriptionId, monitoredItemId) |
| OneShotShelve | i=9211 | 无 |
| TimedShelve | i=9213 | (shelvingTime : Duration ms) |
| Unshelve | i=9215 | 无 |
API
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| ua.Acknowledge(conditionId, eventId, comment = "") | 报警 | 写 | 用户已知 |
| ua.Confirm(conditionId, eventId, comment = "") | 报警 | 写 | 现场已处理 |
| ua.AddComment(conditionId, eventId, comment) | 报警 | 写 | 加备注 (comment 必填, 无默认值) |
| ua.EnableCondition(conditionId) | 报警 | 写 | 启用 |
| ua.DisableCondition(conditionId) | 报警 | 写 | 禁用 |
| ua.ConditionRefresh(subscriptionId) | 报警 | 写 | 让服务端补发当前所有激活报警 |
| ua.ConditionRefresh2(subscriptionId, monitoredItemId) | 报警 | 写 | 仅指定 MI 的补发 |
| ua.OneShotShelve(shelvingStateId) | 报警 | 写 | 一次性屏蔽 (下次激活自动恢复) |
| ua.TimedShelve(shelvingStateId, durationMs) | 报警 | 写 | 限时屏蔽 N 毫秒 |
| ua.Unshelve(shelvingStateId) | 报警 | 写 | 取消屏蔽 |
真签名 (都是 DarraOpcUa 的成员):
public StatusCode Acknowledge(string conditionId, byte[] eventId, string comment = "");
public StatusCode Confirm(string conditionId, byte[] eventId, string comment = "");
public StatusCode AddComment(string conditionId, byte[] eventId, string comment);
public StatusCode EnableCondition(string conditionId);
public StatusCode DisableCondition(string conditionId);
public StatusCode ConditionRefresh(uint subscriptionId);
public StatusCode ConditionRefresh2(uint subscriptionId, uint monitoredItemId);
public StatusCode OneShotShelve(string shelvingStateId);
public StatusCode TimedShelve(string shelvingStateId, double durationMs);
public StatusCode Unshelve(string shelvingStateId);
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
| conditionId | string | 报警条件实例 NodeId; null / 空串直接返回 raw 0x80AB0000 (BadInvalidArgument), 不发 RPC |
| eventId | byte[] | 该次报警的 EventId (ByteString); null 同样直接返回 BadInvalidArgument |
| comment | string | LocalizedText 文本; Acknowledge / Confirm 默认空串, AddComment 必填 |
| subscriptionId / monitoredItemId | uint | Part 4 IntegerId |
| shelvingStateId | string | 条件实例上的 ShelvingState 子节点 NodeId (通常 "{conditionId}.ShelvingState") |
| durationMs | double | 搁置时长, 毫秒; NaN 或负数直接返回 BadInvalidArgument |
返回 StatusCode, 失败 不抛异常 (调用中抛出的 OpcUaException 会被折叠成 StatusCode 返回), 上层自决重试。
代码示例
using System;
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://server:4840");
ua.Connect();
// 1) 订阅报警事件 (nodeId 默认就是 Server=i=2253, 这里显式写出来)
using var sub = ua.CreateSubscription(500);
using var ev = ua.SubscribeEvents("i=2253", sub);
ev.EventArrived += (s, e) =>
{
// 回调只给 5 个字段: Severity / Message / SourceName / Time / EventType
// (Time 是 DateTime?, 服务端没给时间时为 null)
Console.WriteLine($"[{e.Time:HH:mm:ss}] sev={e.Severity} {e.SourceName}: {e.Message} ({e.EventType})");
};
// a. 让服务端补发当前所有激活报警 (面板首次打开时)
var rc = ua.ConditionRefresh(sub.SubscriptionId);
Console.WriteLine(rc == StatusCode.Good ? "ConditionRefresh OK" : $"ConditionRefresh 失败: {rc}");
// 2) 报警处置 — conditionId / eventId / shelvingStateId 由上层从服务端的条件实例模型取,
// 当前 EventArrivedEventArgs 不透出这三个 id。
static void HandleAlarm(DarraOpcUa ua, string conditionId, byte[] eventId, string shelvingStateId)
{
// b. 用户点 "确认"
var rc1 = ua.Acknowledge(conditionId, eventId, "操作员已知");
Console.WriteLine(rc1 == StatusCode.Good ? "已确认" : $"Acknowledge 失败: {rc1}");
// c. 现场处理后点 "确认完成"
ua.Confirm(conditionId, eventId, "现场已处理");
// d. 临时屏蔽 30 秒 (30_000 毫秒)
ua.TimedShelve(shelvingStateId, 30_000.0);
// e. 加备注 (不影响 Ack/Confirm 状态)
ua.AddComment(conditionId, eventId, "等待夜班巡检");
}
// 3) 启用 / 禁用整条 Condition (维护期间)
ua.DisableCondition("ns=2;s=Tank.HighLevel");
// ... 维护完
ua.EnableCondition("ns=2;s=Tank.HighLevel");
备注
Acknowledge / Confirm / AddComment 需要的 conditionId 与 eventId 当前不从事件回调透出, 由上层按自己掌握的条件实例 NodeId 与事件 ID 提供。
错误处理
以下为服务端在这些 Method 上可能返回的状态码名称 (OPC UA 标准定义)。
| StatusCode | 含义 |
|---|---|
| Good (0x00000000) | 成功 |
| BadEventIdUnknown | EventId 不匹配当前 Condition |
| BadConditionAlreadyEnabled | 已启用 |
| BadConditionAlreadyDisabled | 已禁用 |
| BadConditionAlreadyShelved | 已屏蔽 |
| BadConditionNotShelved | 未屏蔽, 无法 Unshelve |
这些名字不在 C# 的
StatusCode 枚举里C# 的 StatusCode 枚举只定义到 BadHistoryOperationUnsupported 等常见码, 上表这些 Part 9 专属码没有对应的枚举成员。服务端返回时是原样透传的 StatusCode 值, 判定只能比数值:
// 用 uint 常量补枚举缺口
const uint SC_BadInvalidArgument = 0x80AB0000u;
if ((uint)rc == SC_BadInvalidArgument) { /* 入参为空被本地挡下, 没发 RPC */ }
这两个 raw 码的数值: BadInvalidArgument = 0x80AB0000 (conditionId / eventId / shelvingStateId 为空时直接返回, 不发 RPC)、BadInternalError = 0x80020000。
最佳实践
- 首次订阅后必调
ConditionRefresh— 不然只能收到新事件, 不见已激活的旧报警 Acknowledge后再Confirm— 两步流程, 不要跳 Confirm; 部分条件类型只支持其中一个- TimedShelve 单位是毫秒, 形参是
double— 30 秒写30_000.0; 服务端通常受MaxTimeShelved约束, 超出会返回BadShelvingTimeOutOfRange - comment 的 locale 固定为
"en"— 发送时 locale 一律写"en", 中文文本会被标成 en locale; 文本本身按 UTF-8 传 eventId按 ISO-8859-1 传 —byte[]逐字节映射成 ISO-8859-1 字符串发送, 不做 UTF-8 多字节展开; 严格校验实参类型的服务端可能拒收- 不要在
EventArrived里同步 Ack — 用ConcurrentQueue推队列, 后台线程处理
跨语言对照
| 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 |
| TimedShelve | timed_shelve | timedShelve | TimedShelve | timed_shelve | DarraUa_Alarm_TimedShelve |