Skip to main content

advanced method scenarios

1. sequential method calls

// In a Function node - execute methods in sequence
const steps = [
{
object: "ns=2;s=System",
method: "ns=2;s=Prepare",
params: { "mode": "production" }
},
{
object: "ns=2;s=Reactor",
method: "ns=2;s=Heat",
params: { "temperature": 150.0, "rate": 5.0 }
},
{
object: "ns=2;s=Mixer",
method: "ns=2;s=Start",
params: { "speed": 500 }
}
];

const currentStep = flow.get("currentStep") || 0;

if (currentStep < steps.length) {
const step = steps[currentStep];
msg.objectId = step.object;
msg.methodId = step.method;
msg.payload = step.params;
msg.stepNumber = currentStep + 1;

flow.set("currentStep", currentStep + 1);
return msg;
} else {
// All steps completed
flow.set("currentStep", 0);
node.log("All method calls completed");
return null;
}

2. conditional method execution

// Execute method based on conditions
const systemState = flow.get("systemState") || "idle";
const currentTemp = msg.payload.temperature || 0;

let methodCall = null;

switch (systemState) {
case "idle":
if (currentTemp < 20) {
methodCall = {
objectId: "ns=2;s=Heater",
methodId: "ns=2;s=TurnOn",
payload: { "targetTemp": 25.0 }
};
}
break;

case "heating":
if (currentTemp > 30) {
methodCall = {
objectId: "ns=2;s=Heater",
methodId: "ns=2;s=TurnOff",
payload: {}
};
}
break;

case "error":
methodCall = {
objectId: "ns=2;s=System",
methodId: "ns=2;s=Reset",
payload: { "operatorId": "AUTO" }
};
break;
}

return methodCall;

3. method result processing

// Process method results in a Function node after Call node
const result = msg.payload || {};
const statusCode = msg.statusCode;

if (statusCode === "Good") {
// Method executed successfully
if (result.success) {
node.log(`Method completed successfully: ${result.message || 'No message'}`);

// Store result for later use
flow.set("lastMethodResult", result);

// Continue to next step
msg.nextAction = "continue";
} else {
node.warn(`Method reported failure: ${result.errorMessage || 'Unknown error'}`);
msg.nextAction = "retry";
}
} else {
// Method execution failed
node.error(`Method call failed: ${statusCode}`);

// Handle specific error cases
switch (statusCode) {
case "BadUserAccessDenied":
msg.nextAction = "requestPermission";
break;
case "BadMethodInvalid":
msg.nextAction = "validateMethod";
break;
default:
msg.nextAction = "abort";
}
}

return msg;

error handling pattern

// Error handling in Function node after Call node
if (msg.statusCode !== "Good") {
const errorInfo = {
timestamp: new Date().toISOString(),
objectId: msg.objectId,
methodId: msg.methodId,
statusCode: msg.statusCode,
inputParams: msg.originalPayload
};

// Log error details
node.error(`Method call failed: ${JSON.stringify(errorInfo)}`);

// Send error notification
const errorMsg = {
topic: "method-error",
payload: errorInfo
};

return [null, errorMsg]; // Send to error output
}

// Success path
return [msg, null];