アラームと条件
概要
OPC UA Part 9 ConditionType / AlarmConditionType 上で、固定された NodeId の標準メソッドが定義されています。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 つのメソッドは条件インスタンス/Server オブジェクトに紐付けられており、三つの Shelve メソッドはアラームインスタンスの 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 つのフィールドに基づいてイベントを送信し、さらに、そのフィルタ条件が無視されたことを示す「InfoDiagnostic」イベントを 一件追加で送信します。acknowledge/confirm/add_commentは、ロケールラベルを付けずに、純粋commentな文字列として送信されます。
エラー処理
このメソッドは常に を返し 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)。