Session — Session
Session This is the main Client class; one instance corresponds to one OPC UA Session. All access operations (read / write / browse / subscribe / call / history) originate from this object.
Subpage Redirect
- For structural and connection specifications, please refer to Constructor.
- For connecting/disconnecting, please refer to Connection Management.
- For single-item and batch read/write operations, please refer to Read / Write.
- For information on the unified channel for internal incidents, please refer to Events.
- For namespace lookup, please refer to Namespaces.
- For information on heartbeat and session keep-alive, please refer to KeepAlive.
Public Fields / Property Methods
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Connections | endpoint_url() | &str | R | The endpoint URL specified at construction |
handle() | u32 | R | C-layer handle (for internal use) | |
| Status | state() | SessionState | R | Current session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed) |
is_connected() | bool | R | Whether the connection is active (= state() == Connected) | |
| Child Objects | nodes() | NodeCollection | R | Lazy Load node accessor |
namespaces() | NamespaceCollection | R | Namespace URI ↔ Index conversion | |
| Events | events | Arc<Mutex<OpcUaEvents>> | RW | Unified channel for session-level events; access using .lock() |
| Configuration | keep_alive_interval_ms() | u32 | R | KeepAlive heartbeat interval (default 10000 ms, 0 = disabled) |
set_keep_alive_interval_ms(ms) | () | W | Change the heartbeat interval (&mut self) | |
auto_publish() | bool | R | Whether the background publish thread is enabled | |
set_auto_publish(b) | () | W | Enable or disable the background publish thread (&mut self) |
Public Methods
Structure
Session::new(url: &str)— Simplified Construction (None-safe, Anonymous)Session::with_config(cfg: ConnectionConfig)— Full configuration (encryption / username / timeout)
Connection Management
connect(&mut self)— Establish a connection (Hello → SecureChannel → Session → Activate)disconnect(&mut self)— Connection terminated voluntarily; the instance can reconnectDrop(Implicit) — Automatically disconnects and closes when leaving the scope
Heartbeat and Release
publish(timeout_ms: u32) -> StatusCode— Manually trigger a Publish once (usually not necessary)
Read / Write
read(&self, node_id) -> Result<DataValue>— Reading the Value property of a single noderead_attr(&self, node_id, attr) -> Result<DataValue>— Read a specific property from a single noderead_many(&self, node_ids, attr) -> Result<Vec<Option<DataValue>>>— Batch Readwrite(&self, node_id, &Variant) -> Result<StatusCode>— Order-writing node Valuewrite_attr(&self, node_id, attr, &Variant) -> Result<StatusCode>— Write a specified property
Browse
browse(&self, node_id, filter)— Single-page viewbrowse_many(&self, node_ids, filter)— Batch Viewbrowse_with_paging(&self, node_id, filter)— Includes ContinuationPoint page-turning supportbrowse_next(&self, cp, release)— Continue to the next pagebrowse_all(&self, node_id, filter, max_pages)— Auto-Repeat BrowseNexttranslate_browse_paths(&self, paths)— Batch parse the paths to NodeIdresolve(&self, path)— Parsing a Single Absolute Pathresolve_from(&self, start_node_id, path)— Parsing a Single Relative Pathregister_nodes(&self, node_ids)— Register for a Temporary High-Efficiency Plan NodeIdunregister_nodes(&self, registered_ids)— Cancel Temporary NodeId
Subscribe
create_subscription(&self, interval_ms: f64)— Create a data subscriptionsubscribe_events(&self, node_id, existing, callback)— Subscribe to Events (Alarms / Conditions)transfer_subscriptions(&self, sub_ids, send_initial_values)— Migrating Subscriptions Across Sessions
Method Call
call(&self, object_id, method_id, &[Variant])— Call OPC UA Method node
History
read_history(&self, node_id, start, end, max_values)— Reading Ancient Historyread_history_modified(...)— Reading a history that includes revision recordsread_history_at_time(...)— Read the interpolated value at a specified time pointread_history_processed(...)— Aggregation (Average / Min / Max / TimeAverage / ...)read_history_events(...)— Reading About Historical Eventsupdate_history(...)— Insert / Replace / Update / Remove Historydelete_history_range(...)— Delete history by range
Control
cancel(&self, request_handle: u32)— Cancel Unfinished Requests
Complete Example
use darra_opcua::{Session, Variant};
fn main() -> Result<(), Box<dyn std::error::Error>> {
darra_opcua::initialize()?;
let mut s = Session::new("opc.tcp://localhost:4840")?;
s.events.lock().unwrap().on_connected.push(Box::new(|e| {
println!("Connected to {}", e.endpoint_url);
}));
s.events.lock().unwrap().on_disconnected.push(Box::new(|e| {
println!("Disconnected: {}", e.status_code);
}));
s.connect()?;
// Read
let dv = s.read("ns=2;s=Temperature")?;
println!("T = {} °C", dv.variant().try_get_f64().unwrap_or(0.0));
// Write
let mut v = Variant::new();
v.set_f64(42.0);
let st = s.write("ns=2;s=Setpoint", &v)?;
println!("Write status = {}", st);
// Subscribe
let sub = s.create_subscription(500.0)?;
sub.add_node("ns=2;s=Counter", |args| {
println!("{:?} = {:?} ({})",
args.node_id, args.value_string, args.status);
})?;
// Methods
let mut a = Variant::new(); a.set_i32(3);
let mut b = Variant::new(); b.set_i32(4);
let outputs = s.call("ns=2;s=Calc", "ns=2;s=Calc.Add", &[a, b])?;
for v in &outputs {
println!("sum = {}", v.try_get_i32().unwrap_or(0));
}
std::thread::sleep(std::time::Duration::from_secs(60));
Ok(()) // s / sub: Automatically release when dropped
}