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

アラームと条件

概要​

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

MethodNodeId引数
Acknowledgei=9111(event_id: &[u8], comment: &str)
Confirmi=9113(event_id, comment)
AddCommenti=9029(event_id, comment)
Enablei=2803—
Disablei=2805—
ConditionRefreshi=3875(sub_id)
ConditionRefresh2i=12912(sub_id, mi_id)
OneShotShelvei=9211—
TimedShelvei=9213(shelving_time_ms)
Unshelvei=9215—

API​

方法カテゴリ読み書き説明
ua.acknowledge(condition_id, event_id, comment)アラーム書くユーザーはすでに知っている
ua.confirm(condition_id, event_id, comment)アラーム書く現場での対応は完了しました
ua.add_comment(condition_id, event_id, comment)アラーム書く備考を追加
ua.enable_condition(condition_id)アラーム書く有効にする
ua.disable_condition(condition_id)アラーム書く無効にする
ua.condition_refresh(sub_id)アラーム書く再発行・有効化アラーム
ua.condition_refresh2(sub_id, mi_id)アラーム書く指定のみ MI
ua.one_shot_shelve(shelving_state_id)アラーム書くワンタイム・シールド
ua.timed_shelve(shelving_state_id, ms)アラーム書く期間限定の非表示
ua.unshelve(shelving_state_id)アラーム書くブロックを解除

StatusCode をそのまま返します(Result ではありません)、10つのメソッドのいずれもErrを返さない、追加できません ?, 失敗しても慌てない。業務上の成否は StatusCode によって表され、 rc.is_good() / を用いて判断 rc.is_bad() する。

コード例​

use darra_opcua::{Session, well_known_nodes};
use std::sync::{mpsc, Arc};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://server:4840")?;
ua.connect()?;

// イベントのサブスクリプションはセッションに紐付けられており(サブスクリプション自体には紐付けられていない)、コールバックは登録時に一度だけ渡され、
// その後、追加や置換はできません — .のみ EventSubscription が公開されます monitored_item_handle()。
let sub = Arc::new(ua.create_subscription(500.0)?);
let sub_id = sub.subscription_id();

let (tx, rx) = mpsc::channel();
let _ev = ua.subscribe_events(
Some(well_known_nodes::SERVER), // "i=2253"
Some(sub.clone()),
move |args| {
// EventArrivedEventArgs タイル表示されるフィールドは 5 つだけです:severity / message /
// source_name / time / event_type. EventId / ConditionId のオリジナル
// ByteString 現時点では公開されておらず、業務側で独自にマッピングする必要があります。
let _ = tx.send((args.severity, args.source_name.clone().unwrap_or_default()));
},
)?;

for (sev, src) in rx {
let condition_id = format!("ns=2;s=Alarm.{}", src); // 業務側独自のマッピング
let event_id: &[u8] = b""; // 現時点では公開されていません。上記を参照してください。

// a. サーバーに現在のアクティベーションアラームを再送信させる(「裸」を返す StatusCode、不可 `?`)
let rc = ua.condition_refresh(sub_id);
if !rc.is_good() { eprintln!("refresh 失败: {:?} (sev={})", rc, sev); }

// b. ユーザーが「確認」をクリックする — 二番目の引数は &[u8]
let rc = ua.acknowledge(&condition_id, event_id, "操作员已知");
if !rc.is_good() { eprintln!("Acknowledge 失败: {:?}", rc); }

// c. 現場での対応
let _ = ua.confirm(&condition_id, event_id, "现场已处理");

// d. 30 秒間一時的にブロックする(TimedShelve 引数はミリ f64 秒単位)
let _ = ua.timed_shelve(&format!("{}.ShelvingState", condition_id), 30_000.0);
}
Ok(())
}

エラー処理​

10つのメソッドはすべて「naked」を返し StatusCode、「Err」は返しません。以下の失敗ケースにはプロトコル層で専用のステータスコードがありますが、その定数 StatusCode 集合 SDK には含まれていない, そのまま書き写せるような内容にしないでください StatusCode::Xxx:

失敗のケースプロトコル層の意味
成功ステータスコード 0
EventId 一致しませんacknowledge / confirm:渡された event_id がサーバー側の記録と一致しない
条件が有効になっていますenable_condition が、すでに有効になっているコンディションにヒットした
条件は非表示になっていますtimed_shelve / one_shot_shelve が、すでにシェルブされた条件にヒットした

診断手法:

if rc.is_bad() {
eprintln!("raw = 0x{:08X}, name = {}", rc.raw(), darra_opcua::status_name(rc));
}

StatusCode newtype であり pub struct StatusCode(pub u32)、定数であるすべて大文字 (StatusCode::GOOD / StatusCode::BAD / StatusCode::BAD_TIMEOUT ...), 判定には is_good() / is_bad() / を使用し is_uncertain()、ソースコードの取得には を使用する raw()。

ベストプラクティス​

  • 初回購読後は必ず condition_refresh を設定してください
  • 「acknowledge」の後、「confirm」を行う。二つのステップ
  • TimedShelve の単位はミリ秒 (f64) であり、秒ではありません。
  • acknowledge / confirm / add_comment の event_id 引数は &[u8]、ではなく &String
  • イベントコールバックは FnMut(&EventArrivedEventArgs) + Send + 'static、参照 Session を取得できない、 コールバック内でackを同期処理しないでください — mpsc を使ってキューを処理し、外側のスレッドが Session を保持して呼び出す

多言語対照​

C#PythonJavaC++RustC
AcknowledgeacknowledgeacknowledgeAcknowledgeacknowledgeDarraUa_Alarm_Acknowledge
ConfirmconfirmconfirmConfirmconfirmDarraUa_Alarm_Confirm
ConditionRefreshcondition_refreshconditionRefreshConditionRefreshcondition_refreshDarraUa_Alarm_ConditionRefresh

Cの姓は DarraUa_Alarm_*、ではありません DarraUa_Condition_*;エントリは DarraUa_Alarm_ConditionRefresh / DarraUa_Alarm_ConditionRefresh2、ブロック対象は DarraUa_Alarm_OneShotShelve / _TimedShelve / です _Unshelve。

次のステップ​