跳到主要内容

报警与条件 (Acknowledge / Confirm / Refresh / Shelve)

概述​

OPC UA Part 9 (Alarms & Conditions) 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 提供形参清晰的 C# 方法 (都是 DarraOpcUa 的成员), 上层报警面板不用手拼 Variant[] 入参。

固定 NodeId (Part 9 / Part 5 标准命名空间 0):

MethodNodeId入参
Acknowledgei=9111(eventId : ByteString, comment : LocalizedText)
Confirmi=9113(eventId : ByteString, comment : LocalizedText)
AddCommenti=9029(eventId : ByteString, comment : LocalizedText)
Enablei=2803无
Disablei=2805无
ConditionRefreshi=3875(subscriptionId : IntegerId)
ConditionRefresh2i=12912(subscriptionId, monitoredItemId)
OneShotShelvei=9211无
TimedShelvei=9213(shelvingTime : Duration ms)
Unshelvei=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);

参数:

参数类型说明
conditionIdstring报警条件实例 NodeId; null / 空串直接返回 raw 0x80AB0000 (BadInvalidArgument), 不发 RPC
eventIdbyte[]该次报警的 EventId (ByteString); null 同样直接返回 BadInvalidArgument
commentstringLocalizedText 文本; Acknowledge / Confirm 默认空串, AddComment 必填
subscriptionId / monitoredItemIduintPart 4 IntegerId
shelvingStateIdstring条件实例上的 ShelvingState 子节点 NodeId (通常 "{conditionId}.ShelvingState")
durationMsdouble搁置时长, 毫秒; 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)成功
BadEventIdUnknownEventId 不匹配当前 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#PythonJavaC++RustC
AcknowledgeacknowledgeacknowledgeAcknowledgeacknowledgeDarraUa_Alarm_Acknowledge
ConfirmconfirmconfirmConfirmconfirmDarraUa_Alarm_Confirm
ConditionRefreshcondition_refreshconditionRefreshConditionRefreshcondition_refreshDarraUa_Alarm_ConditionRefresh
TimedShelvetimed_shelvetimedShelveTimedShelvetimed_shelveDarraUa_Alarm_TimedShelve

下一步​