Call
Signature
pub fn Session::call(
&self,
object_node_id: &str,
method_node_id: &str,
inputs: &[Variant],
) -> Result<Vec<Variant>, OpcUaError>;
| Parameters | Note |
|---|---|
| object_node_id | The method is located in the Object node |
| method_node_id | The Method Itself NodeId |
| inputs | Input parameter: slice Variant (optional &[]) |
Returns List of output Vec<Variant> parameters.
Memory Ownership
In the returned list, Each Variant one holds a native handle, automatically released by the Rust trait Drop.
No need to manually dispose of it, andNo Native memory Variant leak:
{
let outputs = s.call("ns=2;s=Obj", "ns=2;s=Obj.Method", &[])?;
for v in &outputs {
println!("{}", v.try_get_i32().unwrap_or(0));
}
} // <- Outputs a Vec of drop values; all native Variant memory is fully released
Examples
Call Calculator.Add(3, 4)
use darra_opcua::Variant;
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=Calculator",
"ns=2;s=Calculator.Add",
&[a, b],
)?;
for v in &outputs {
println!("sum = {}", v.try_get_i32().unwrap_or(0));
}
Methods Without Parameters
let outputs = s.call(
"ns=2;s=Boiler1",
"ns=2;s=Boiler1.Reset",
&[], // Empty Slice
)?;
Passing Multiple Input Parameters of Different Types
let mut name = Variant::new(); name.set_string("Boiler1");
let mut sp = Variant::new(); sp.set_f64(85.5);
let outputs = s.call(
"ns=2;s=Plant",
"ns=2;s=Plant.SetTemperature",
&[name, sp],
)?;
Error
| StatusCode | Meaning |
|---|---|
| BAD_NODE_ID_INVALID | NodeId Parsing Failed |
| BAD_COMMUNICATION_ERROR | Transport Error |
Protocol Code BadMethodInvalid | That's not NodeId a method. |
| INVALID_ARGUMENT | Input parameter type / number does not match |
| BAD_USER_ACCESS_DENIED | No permission to call |
| BAD_* | Business-level errors returned by the method |
BadMethodInvalid SDK Not yet Definition: Constants StatusCode (for a complete list StatusCode of constants, see "Related Constants"),
Do not copy verbatim StatusCode::BAD_METHOD_INVALID; use status code values / help functions to determine:
e.status.is_bad() / e.status.raw() / darra_opcua::status_name(e.status).
Business-level failures (methods that return a value other than "Good") are also Err returned as e.status business codes:
match s.call("ns=2;s=O", "ns=2;s=O.M", &[]) {
Ok(outs) => println!("ok, {} outputs", outs.len()),
Err(e) if e.status == darra_opcua::StatusCode::BAD_USER_ACCESS_DENIED
=> eprintln!("无权限"),
Err(e) => eprintln!("call failed: {}", e),
}
Looking for a way to... NodeId
use darra_opcua::NodeClass;
// 1. Browse Object Child Node Filtering Method
let methods = s.browse("ns=2;s=Calculator", NodeClass::Method)?;
for m in &methods {
println!(" {} -> {}", m.browse_name, m.node_id);
}
// 2. Check the input/output parameter signatures (Browse the Property of the method node)
let props = s.browse(&methods[0].node_id, NodeClass::Variable)?;
// Includes InputArguments / OutputArguments
Input Parameter Construction
Constructing method signatures Variant:
use darra_opcua::Variant;
use std::time::SystemTime;
let mut a = Variant::new(); a.set_i32(3); // Int32
let mut b = Variant::new(); b.set_i32(4); // Int32
let mut name = Variant::new(); name.set_string("Boiler1");
let mut sp = Variant::new(); sp.set_f64(85.5);
let mut t = Variant::new(); t.set_datetime(SystemTime::now());
let mut flag = Variant::new(); flag.set_bool(true);
Output Variant Decoding
use darra_opcua::BuiltinType;
let outputs = s.call(/* ... */, /* ... */, &[])?;
for v in &outputs {
match v.data_type() {
BuiltinType::Int32 => println!("i32: {}", v.try_get_i32().unwrap_or(0)),
BuiltinType::Double => println!("f64: {}", v.try_get_f64().unwrap_or(0.0)),
BuiltinType::String => println!("str: {:?}", v.try_get_string()),
t => println!("other type: {:?}", t),
}
}
Asynchronous
Currently, it is Call synchronous and blocking (internally, it uses RPC + waits for a response). Session::call Using &self, the internal stack is locked,
ensuring safety for concurrent cross-thread calls; Tokio users can simply wrap tokio::task::spawn_blocking it with.