Event Subscription
OPC UA events (Alarms & Conditions) are subscribed to through Session::subscribe_events. These are
OPC UA protocol events, not the same thing as the SDK internal s.events.*.
s.subscribe_events(...)— OPC UA Protocol Events (Alarms / Conditions, Server-Side Push Notifications, This Section)s.events.*— SDK Internal Events (Session / Subscription Lifecycle, Protocol-Independent)
Signature
pub fn Session::subscribe_events<F>(
&self,
node_id: Option<&str>,
existing: Option<Arc<Subscription>>,
callback: F,
) -> Result<EventSubscription, OpcUaError>
where F: FnMut(&EventArrivedEventArgs) + Send + 'static;
| Parameters | Default | Note |
|---|---|---|
| node_id | None (Server, i=2253) | Event Source Node |
| existing | None | Reuse an existing Arc<Subscription>; Create a new one inside "None" |
| callback | (Required) | Event arrival callback |
EventSubscription
| Methods | Note |
|---|---|
| monitored_item_handle() | Current Event MI Local Handle |
Drop EventSubscription Time:
- Container = Owned (Created Internally): Delete MI + Destroy Subscription
- Container = Borrowed (provided by the user): Delete only MI; do not modify the subscription
EventArrivedEventArgs
pub struct EventArrivedEventArgs {
pub severity: u16,
pub message: Option<String>,
pub source_name: Option<String>,
pub time: Option<SystemTime>,
pub event_type: Option<String>,
}
| Field | Note |
|---|---|
| severity | Severity 1–1000 |
| message | Event Text |
| source_name | Event Source Name |
| time | Time of the incident |
| event_type | Event Type NodeId |
Usage
Default Subscription Server Node
use darra_opcua::Session;
let mut s = Session::new("opc.tcp://localhost:4840")?;
s.connect()?;
let _ev_sub = s.subscribe_events(None, None, |e| {
println!("[{}] {:?}: {:?}", e.severity, e.source_name, e.message);
println!(" EventType={:?} Time={:?}", e.event_type, e.time);
})?;
std::thread::sleep(std::time::Duration::from_secs(60));
// _ev_sub Drop—the internal subscription is also deleted
Subscribe to events from a specific node
let _ev_sub = s.subscribe_events(
Some("ns=2;s=Line1"), // Line1 It must be EventNotifier
None,
|e| { println!("Line1 event: {:?}", e.message); },
)?;
Reusing an Existing Subscription Container
use std::sync::Arc;
let data_sub = Arc::new(s.create_subscription(500.0)?);
data_sub.add_node("ns=2;s=Counter", |args| {
println!("counter = {:?}", args.value_string);
})?;
// Attach the Event to MI the same container
let _ev_sub = s.subscribe_events(
Some("ns=2;s=Line1"),
Some(data_sub.clone()),
|e| { println!("event = {:?}", e.message); },
)?;
// _ev_sub "Drop" only removes the event MI; it does not destroy it. data_sub
EventNotifier
Only nodes with a non-0 EventNotifier attribute (12) can generate events. By default, Server(i=2253) is an
EventNotifier. Whether a custom node can act as an event source depends on the server implementation.
EventFilter (Advanced)
Current SDK Default Subscription BaseEventType (All Events), callback exposes 5 fixed fields
(Severity / Message / SourceName / Time / EventType), see above EventArrivedEventArgs.
Subscription entry points with custom EventFilter settingsCurrently unavailable —— EventFilter /
SimpleAttributeOperand It is simply a constructible data structure; it API has not been publicly released to the server.
For more details, see EventFilter。
Alarm Operations (Acknowledge / Confirm)
Upon receiving an AlarmConditionType event, the operator is typically required to acknowledge it. By
call Acknowledgment Method for Calling Alarm Nodes:
use darra_opcua::Variant;
let mut event_id = Variant::new();
event_id.set_string("event-123");
let mut comment = Variant::new();
comment.set_string("Operator confirmed");
s.call(
&condition_id,
&format!("{}.Acknowledge", condition_id),
&[event_id, comment],
)?;
Closure Requirements
F: FnMut(&EventArrivedEventArgs) + Send + 'static
Cross-thread closures work the same way as DataChange callbacks, using Arc<Mutex<...>> / to pass data mpsc between threads.
Historical Events
Real-time events and historical events are often used in combination:
use std::time::{Duration, SystemTime};
// Load the past 24 hours of historical events at startup
let now = SystemTime::now();
let history = s.read_history_events(
"i=2253",
now - Duration::from_secs(24 * 3600), now, 1000,
)?;
for e in &history {
println!("HISTORY [{}] {:?}: {:?}", e.severity, e.source_name, e.message);
}
// Then subscribe to real-time updates
let _live = s.subscribe_events(None, None, |e| {
println!("LIVE [{}] {:?}: {:?}", e.severity, e.source_name, e.message);
})?;