アラームと条件
概要
OPC UA Part 9 ConditionType / AlarmConditionType 上で、固定された NodeId の標準メソッドが定義されています。SDK 引数が明確なRustメソッドとしてカプセル化されています。
| Method | NodeId | 引数 |
|---|---|---|
| Acknowledge | i=9111 | (event_id: &[u8], comment: &str) |
| Confirm | i=9113 | (event_id, comment) |
| AddComment | i=9029 | (event_id, comment) |
| Enable | i=2803 | — |
| Disable | i=2805 | — |
| ConditionRefresh | i=3875 | (sub_id) |
| ConditionRefresh2 | i=12912 | (sub_id, mi_id) |
| OneShotShelve | i=9211 | — |
| TimedShelve | i=9213 | (shelving_time_ms) |
| Unshelve | i=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# | 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_*;エントリは DarraUa_Alarm_ConditionRefresh / DarraUa_Alarm_ConditionRefresh2、ブロック対象は DarraUa_Alarm_OneShotShelve / _TimedShelve / です _Unshelve。