MethodBuilder
Overview
OPC UA Call The service requires that input parameters be packaged in a format OpcUaVariant[] that corresponds InputArguments to the server's order.MethodBuilder It provides chained methods API.
Corresponding sections of the standard: Part 4 §5.11.2 / Part 5 §6.4.
API
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| new MethodBuilder(ua, objectId, methodId) | Structure | — | Bound to a method |
| .loadInputArgs() | Chain-type | Read | Locate the InputArguments node (description not currently available) |
| .arg(String name, Object value) | Chain-type | — | Set Values by Name |
| .arg(int index, Object value) | Chain-type | — | Set Values by Index |
| .invoke() | Terminal | Write | Returns MethodCallResult { status, outputs } |
| .getInputArgs() | Properties | Read | List of Input Parameter Descriptions (Currently empty) |
| .isInputArgsLoaded() | Properties | Read | Was the InputArguments node found? |
Supported Java input types: Boolean / Byte / Short / Integer / Long / Float / Double / String / Instant, or you can pass directly OpcUaVariant. Other types are converted toString() to strings.
Code Example
import com.darra.opcua.*;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();
// 1) Stream invocation—by name
MethodCallResult r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.loadInputArgs()
.arg("x", 100.0)
.arg("y", 200.0)
.arg("speed", 50)
.invoke();
if (r.status() != Enums.StatusCode.Good)
System.out.println("MoveTo 失败: " + r.status());
// 2) By index
MethodCallResult r2 = new MethodBuilder(ua, "ns=2;s=Calc", "ns=2;s=Calc.Add")
.arg(0, 3.14).arg(1, 2.71).invoke();
System.out.println("Add → " + r2.outputs().get(0).asDouble());
// 3) Reusing the builder
MethodBuilder b = new MethodBuilder(ua, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed")
.loadInputArgs();
for (int rpm : new int[]{100, 500, 1000, 1500}) {
MethodCallResult r3 = b.arg("rpm", rpm).invoke();
if (r3.status() != Enums.StatusCode.Good) break;
Thread.sleep(500);
}
}
Comparison with call
// Direct call: hand OpcUaVariant[]
OpcUaVariant[] inputs = {
new OpcUaVariant().setDouble(100.0),
new OpcUaVariant().setDouble(200.0),
new OpcUaVariant().setInt32(50)
};
List<OpcUaVariant> outs = ua.call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", inputs);
// Use MethodBuilder
MethodCallResult r = new MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.loadInputArgs()
.arg("x", 100.0).arg("y", 200.0).arg("speed", 50)
.invoke();
Implementation Constraints
- Decoding of the parameter description is not currently supported; an empty list is returned for now
getInputArgs(). - If a parameter name cannot be obtained using name-based binding, it falls back to binding based on call
argorder; usearg(int, value) - Does not support complex structures as input parameters
invokeDo not throw exceptions; handle failures byMethodCallResult.status()
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_* |