MethodBuilder
Overview
OPC UA Call The service requires that input parameters be packaged in a format that corresponds InputArguments to the server's order Vec<Variant>.MethodBuilder It supports chaining API.
Corresponding sections of the standard: Part 4 §5.11.2 / Part 5 §6.4.
API
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| MethodBuilder::new(ua, object_id, method_id) | Structure | — | Bound to a method |
| .load_input_args() | Chain-type | Read | Read the description of InputArguments and return Self (do not return Result). |
| .arg_by_name(name, value) | Chain-type | — | Set the value by parameter name; value is Variant |
| .arg_by_index(index, value) | Chain-type | — | Set the value at index (starting from 0); value is Variant |
| .invoke() | Terminal | Write | Returns MethodCallResult (not Result), Consumption self |
| .input_args() | Methods | Read | Post-list for MethodBuilder |
MethodCallResult { pub status: StatusCode, pub outputs: Vec<Variant> }, Use result.is_good() to determine success or failure; invoke internally, RPC errors have been unified into status, so it will not return Err, and cannot be added ?.
The input value must be Variant — SDK Do not perform automatic conversions from Rust types such as f64/i32 to Variant; use Variant::new() + set_xxx to construct. Use std::time::SystemTime (set_datetime) for timing, NoneVec<u8> / Byte array setter.
Code Example
use darra_opcua::{MethodBuilder, Session, Variant};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ua = Session::new("opc.tcp://localhost:4840")?;
ua.connect()?;
// 1) Flow invocation—invoke returns a value by name MethodCallResult, not a Result, so it cannot be `?`.
let mut vx = Variant::new(); vx.set_f64(100.0);
let mut vy = Variant::new(); vy.set_f64(200.0);
let mut vs = Variant::new(); vs.set_i32(50);
let r = MethodBuilder::new(&ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.load_input_args()
.arg_by_name("x", vx)
.arg_by_name("y", vy)
.arg_by_name("speed", vs)
.invoke();
if !r.is_good() { eprintln!("MoveTo 失败: {:?}", r.status); }
// 2) By index
let mut a = Variant::new(); a.set_f64(3.14);
let mut b = Variant::new(); b.set_f64(2.71);
let r2 = MethodBuilder::new(&ua, "ns=2;s=Calc", "ns=2;s=Calc.Add")
.arg_by_index(0, a)
.arg_by_index(1, b)
.invoke();
// as_f64() Return Option; do not add it to the Result context `?`
println!("Add → {:?}", r2.outputs.get(0).and_then(|v| v.as_f64()));
// 3) Recursive call: `invoke` consumes `self`; the `builder` must be reconstructed in each iteration
for rpm in [100, 500, 1000, 1500] {
let mut v = Variant::new();
v.set_i32(rpm);
let r = MethodBuilder::new(&ua, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed")
.load_input_args()
.arg_by_name("rpm", v)
.invoke();
if !r.is_good() { break; }
std::thread::sleep(std::time::Duration::from_millis(500));
}
Ok(())
}
Comparison with call
// Old notation
let mut v1 = Variant::new(); v1.set_f64(100.0);
let mut v2 = Variant::new(); v2.set_f64(200.0);
let mut v3 = Variant::new(); v3.set_i32(50);
let outs = ua.call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", &[v1, v2, v3])?;
// New Writing Style
let r = MethodBuilder::new(&ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.load_input_args()
.arg_by_name("x", { let mut v = Variant::new(); v.set_f64(100.0); v })
.arg_by_name("y", { let mut v = Variant::new(); v.set_f64(200.0); v })
.arg_by_name("speed", { let mut v = Variant::new(); v.set_i32(50); v })
.invoke();
Implementation Constraints
load_input_argsCurrently ExtensionObject, no decoding is performed; the system only checks for the presence of InputArguments and fills inarg0/arg1/...placeholders based on the number of parameters; field details are left blank.- Does not support complex structures as input parameters
invokeNormalize call 's Err toMethodCallResult.status, and useresult.is_good()to check; no additional Safe variant is needed or provided.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_* |