MethodBuilder
Overview
OPC UA Call The service requires that input parameters be packaged according to the specification Variant[], with the order matching InputArguments that of the server on a one-to-one basis.MethodBuilder It supports chaining API, allowing parameters to be strung together on a single line load_input_args / arg / invoke, and automatically selects the encoding OpcUaVariant based on the Python type.
Corresponding section of the specification: Part 4 §5.11.2 (Call) / Part 5 §12.3 (Argument).
API
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| MethodBuilder(ua, object_id, method_id) | Structure | — | Bound to a method |
| .load_input_args() | Chain-type | Read | Check whether the InputArguments node exists (see "Implementation Constraints") |
| .arg(name_or_index, value) | Chain-type | — | Set Value by Name (str) / Set Value by Index (int) |
| .invoke() | Terminal | Write | Returns MethodCallResult |
| .input_args | Properties | Read | List[MethodArgument] (The table is currently empty) |
| .input_args_loaded | Properties | Read | bool — Whether the InputArguments node was read |
Authentic signature:
class MethodBuilder:
def __init__(self, ua: OpcUaSession, object_id: str, method_id: str): ...
def load_input_args(self) -> "MethodBuilder": ...
def arg(self, name_or_index, value: Any) -> "MethodBuilder": ...
def invoke(self) -> MethodCallResult: ...
@property
def input_args(self) -> List[MethodArgument]: ...
@property
def input_args_loaded(self) -> bool: ...
@dataclass(frozen=True)
class MethodArgument:
name: str = ""
description: Optional[str] = None
data_type: Optional[str] = None
builtin_type: BuiltinType = BuiltinType.Null
value_rank: int = -1
@dataclass
class MethodCallResult:
status: StatusCode = StatusCode.Bad
outputs: List[OpcUaVariant] = field(default_factory=list)
@property
def is_good(self) -> bool: ... # status == StatusCode.Good
Python types supported for input parameters: bool / int (Int32, type casting Int64) / float (Double) / str / datetime / bytes·bytearray (passed as a string) / pre-constructed OpcUaVariant (passed as-is without copying). Other types fall back to str(value) the catch-all.
Code Example
import time
from darra_opcua import DarraOpcUa, MethodBuilder, StatusCode # Module Name darra_opcua
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
# 1) Stream-based invocation—passing parameters by name
r = (MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.load_input_args()
.arg("x", 100.0)
.arg("y", 200.0)
.arg("speed", 50)
.invoke())
if not r.is_good:
print(f"MoveTo 失败: {r.status.name}")
# 2) By index
r2 = (MethodBuilder(ua, "ns=2;s=Calc", "ns=2;s=Calc.Add")
.arg(0, 3.14).arg(1, 2.71).invoke())
for v in r2.outputs:
with v:
print(f"Add → {v.as_double}")
# 3) Reusing the builder
b = MethodBuilder(ua, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed").load_input_args()
for rpm in (100, 500, 1000, 1500):
rr = b.arg("rpm", rpm).invoke()
if not rr.is_good:
break
time.sleep(0.5)
Comparison with call
from darra_opcua import OpcUaVariant
# Old approach: Hand-coded list (you have to decide the type of OpcUaVariant each element yourself)
inputs = [OpcUaVariant().set_double(100.0),
OpcUaVariant().set_double(200.0),
OpcUaVariant().set_int32(50)]
try:
outs = ua.call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", inputs)
finally:
for v in inputs:
v.dispose()
# New syntax: MethodBuilder (Do not throw on failure; the status is in r.status)
r = (MethodBuilder(ua, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.load_input_args()
.arg("x", 100.0).arg("y", 200.0).arg("speed", 50)
.invoke())
Implementation Constraints
load_input_args()Do only Existence Detection: SDK Since the decoding ExtensionObject is currently notOpcUaVariantexposed, fieldArgument[]details cannot be retrieved, so it isinput_argsalways an empty list; check whether a node has been detectedinput_args_loaded. Value assignment relies entirely on by name.arg()/ by index.- Structure parameters are
ExtensionObjectnot supported;bytespass as a String (SDK none currently availableSetByteString) invoke()Failure does not throw an exception — If the status isr.status, you'll have to handle the exception semantics yourselfraiseinvoke()It will be disposed of when it ends. All Input parameters —OpcUaVariantincluding the one you created yourself and passed to.arg()— should not be used again afterward; the returned should be disposed of byr.outputsthe caller.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_* |
| .Arg(name, value) | .arg(name, value) | .arg(name, value) | .Arg(name, value) | .arg(name, value) | DarraUa_MethodBuilder_ArgByName |