报警与条件
概述
OPC UA Part 9 在 ConditionType / AlarmConditionType 上定义了固定 NodeId 的标准 Method。SDK 封装为形参清晰的 Python 方法。
| Method | NodeId | 入参 |
|---|---|---|
| Acknowledge | i=9111 | (event_id: bytes, comment: str) |
| Confirm | i=9113 | (event_id, comment) |
| AddComment | i=9029 | (event_id, comment) |
| Enable | i=2803 | — |
| Disable | i=2805 | — |
| ConditionRefresh | i=3875 | (subscription_id) |
| ConditionRefresh2 | i=12912 | (subscription_id, monitored_item_id) |
| OneShotShelve | i=9211 | — |
| TimedShelve | i=9213 | (duration_ms: float) |
| Unshelve | i=9215 | — |
前 7 个 Method 挂在条件实例 / Server 对象上, 三个 Shelve Method 挂在报警实例的 ShelvingState 子节点 上。
API
| 方法 | 类别 | 读写 | 说明 |
|---|---|---|---|
| ua.acknowledge(condition_id, event_id, comment="") | 报警 | 写 | 用户已知 |
| ua.confirm(condition_id, event_id, comment="") | 报警 | 写 | 现场已处理 |
| ua.add_comment(condition_id, event_id, comment) | 报警 | 写 | 加备注 (comment 必填) |
| ua.enable_condition(condition_id) | 报警 | 写 | 启用 |
| ua.disable_condition(condition_id) | 报警 | 写 | 禁用 |
| ua.condition_refresh(subscription_id) | 报警 | 写 | 补发激活报警 |
| ua.condition_refresh2(subscription_id, monitored_item_id) | 报警 | 写 | 仅指定 MI |
| ua.one_shot_shelve(shelving_state_id) | 报警 | 写 | 一次性屏蔽 |
| ua.timed_shelve(shelving_state_id, duration_ms) | 报警 | 写 | 限时屏蔽 |
| ua.unshelve(shelving_state_id) | 报警 | 写 | 取消屏蔽 |
def acknowledge(self, condition_id: str, event_id: bytes, comment: str = "") -> StatusCode: ...
def confirm(self, condition_id: str, event_id: bytes, comment: str = "") -> StatusCode: ...
def add_comment(self, condition_id: str, event_id: bytes, comment: str) -> StatusCode: ...
def enable_condition(self, condition_id: str) -> StatusCode: ...
def disable_condition(self, condition_id: str) -> StatusCode: ...
def condition_refresh(self, subscription_id: int) -> StatusCode: ...
def condition_refresh2(self, subscription_id: int, monitored_item_id: int) -> StatusCode: ...
def one_shot_shelve(self, shelving_state_id: str) -> StatusCode: ...
def timed_shelve(self, shelving_state_id: str, duration_ms: float) -> StatusCode: ...
def unshelve(self, shelving_state_id: str) -> StatusCode: ...
| 参数 | 默认 | 说明 |
|---|---|---|
| condition_id | — | 报警条件实例 NodeId; 空串返回 StatusCode.Bad 归一状态 |
| event_id | — | 报警事件 ID (ByteString, bytes); None 返回归一后的 StatusCode |
| comment | "" | LocalizedText 注释 (SDK 当前退化为 String 传给服务端) |
| subscription_id | — | 承载 Publish 的 SubscriptionId |
| monitored_item_id | — | MonitoredItem Id |
| shelving_state_id | — | 报警实例上的 ShelvingState 子节点 NodeId |
| duration_ms | — | 搁置时长, 毫秒 (float); 负数 / NaN 返回归一后的 StatusCode |
返回 StatusCode, 失败不抛异常 (内部把 OpcUaException 归一成 StatusCode)。
代码示例
from darra_opcua import DarraOpcUa, StatusCode
with DarraOpcUa("opc.tcp://server:4840") as ua:
ua.connect()
# 自己建容器订阅, 这样才拿得到 subscription_id 传给 condition_refresh
sub = ua.create_subscription(500)
es = ua.subscribe_events("i=2253", existing_subscription=sub) # ServerObject
def on_event(args):
# args 是 EventArrivedEventArgs: severity / message / source_name / time / event_type
print(f"sev={args.severity} {args.source_name}: {args.message}")
# a. 让服务端补发当前激活报警
ua.condition_refresh(sub.subscription_id)
es.event_arrived.append(on_event) # List[Callable], 用 append 注册
# b. 用户点 "确认" —— condition_id / event_id 由报警源侧提供 (见"实现限制")
def ack_alarm(condition_id: str, event_id: bytes):
rc = ua.acknowledge(condition_id, event_id, "操作员已知")
if rc != StatusCode.Good:
print(f"Acknowledge 失败: {rc}")
# c. 现场处理
ua.confirm(condition_id, event_id, "现场已处理")
# d. 临时屏蔽 30 秒 (取报警实例上的 ShelvingState 子节点)
node = ua.resolve("ShelvingState", condition_id)
if node is not None:
ua.timed_shelve(node.node_id, 30_000.0)
es.dispose()
sub.dispose()
实现限制
- 事件回调里拿不到
EventId/ConditionId。EventArrivedEventArgs只有 5 个字段 (severity/message/source_name/time/event_type), 默认订阅就是这 5 个字段。acknowledge/confirm/add_comment需要的condition_id+event_id: bytes必须由业务侧自己的报警模型 / 服务端对象树提供。 EventSubscription.subscribe(node_id, event_filter)接受自定义EventFilter, 但自定义 SelectClauses 当前不生效 — 仍按默认 5 字段推事件, 并额外推一条DiagnosticInfo 事件说明该过滤条件已被忽略。acknowledge/confirm/add_comment的comment按纯字符串发送, 不带 locale 标签。
错误处理
方法一律返回 StatusCode、不抛异常。常见结果:
| 结果 | 含义 |
|---|---|
StatusCode.Good | 成功, 服务端已接受 |
StatusCode.Bad | 失败 (统一归一态, 见下) |
参数为空 / event_id 为 None / duration_ms 为负或 NaN | 直接返回归一后的 StatusCode, 不发 RPC |
状态码归一
Python StatusCode 枚举只收录常用码。Part 4 附录 A 里未收录的码 (如 BadConditionAlreadyEnabled / BadConditionAlreadyShelved / BadEventIdUnknown) 一律归一成 StatusCode.Bad, 无法在返回值上区分。要区分具体原因, 读 OpcUaException.status_code 的原始整型值, 或看服务端返回的诊断文本。
最佳实践
- 首次订阅后必调
condition_refresh(sub.subscription_id) acknowledge后再confirm, 两步timed_shelve单位 ms, 不是秒- 不要在
event_arrived回调里同步 ack, 推队列后台处理
跨语言对照
| 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_*;Enable / Disable 在 C 侧是 DarraUa_Alarm_EnableCondition / DarraUa_Alarm_DisableCondition,Refresh 是 DarraUa_Alarm_ConditionRefresh(不是 _Refresh)。