メインコンテンツまでスキップ

アラームと条件

概要​

OPC UA Part 9 ConditionType / AlarmConditionType 上で、固定された NodeId の標準メソッドが定義されています。SDK 引数が明確な Python メソッドとしてカプセル化されています。

MethodNodeId引数
Acknowledgei=9111(event_id: bytes, comment: str)
Confirmi=9113(event_id, comment)
AddCommenti=9029(event_id, comment)
Enablei=2803—
Disablei=2805—
ConditionRefreshi=3875(subscription_id)
ConditionRefresh2i=12912(subscription_id, monitored_item_id)
OneShotShelvei=9211—
TimedShelvei=9213(duration_ms: float)
Unshelvei=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 つのフィールドに基づいてイベントを送信し、さらに、そのフィルタ条件が無視されたことを示す「Info Diagnostic 」イベントを 一件追加で送信します。
  • 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#PythonJavaC++RustC
AcknowledgeacknowledgeacknowledgeAcknowledgeacknowledgeDarraUa_Alarm_Acknowledge
ConfirmconfirmconfirmConfirmconfirmDarraUa_Alarm_Confirm
ConditionRefreshcondition_refreshconditionRefreshConditionRefreshcondition_refreshDarraUa_Alarm_ConditionRefresh

Cの姓は DarraUa_Alarm_*、ではない; DarraUa_Condition_*;Enable / Disable C側では DarraUa_Alarm_EnableCondition / DarraUa_Alarm_DisableCondition、Refreshは DarraUa_Alarm_ConditionRefresh(ではない _Refresh)。

次のステップ​