跳到主要内容

Batch Operations: Update Multiple Nodes

The setMultipleNodesParams method allows you to update parameters for multiple nodes in a single API call. This is the recommended approach for batch operations as it can update multiple device parameters and service parameters across multiple nodes efficiently.

Recommended for Batch Operations

Use setMultipleNodesParams when you need to update multiple parameters across multiple nodes. This is more efficient than making individual setValue calls for each parameter.

Update Multiple Nodes with Device and Service Parameters

You can update both device parameters and service parameters for multiple nodes in a single batch operation:

try {
const payload = [
{
nodeId: "node_id_1",
payload: {
// Device parameters - use device name as key
"Light": {
"Power": true,
"Brightness": 80,
"CCT": 4000
},
"Fan": {
"Power": true,
"Speed": 5
},
// Service parameters - use service name as key
"Time": {
"TZ": "America/Los_Angeles",
"TZ-POSIX": "PST8PDT,M3.2.0,M11.1.0"
}
}
},
{
nodeId: "node_id_2",
payload: {
"Light": {
"Power": false
},
"Switch": {
"Power": true
}
}
}
];

const response = await userInstance.setMultipleNodesParams(payload);
console.log("Batch update successful:", response);
} catch (error) {
console.error("Error updating multiple nodes:", error);
}

Example: Update Device Parameters Only

You can update only device parameters for multiple nodes:

try {
const payload = [
{
nodeId: "node_id_1",
payload: {
"Light": {
"Power": true,
"Brightness": 100
}
}
},
{
nodeId: "node_id_2",
payload: {
"Light": {
"Power": true,
"Brightness": 50
},
"Fan": {
"Power": true,
"Speed": 3
}
}
}
];

const response = await userInstance.setMultipleNodesParams(payload);
console.log("Device parameters updated:", response);
} catch (error) {
console.error("Error updating device parameters:", error);
}

Example: Update Service Parameters Only

You can update only service parameters for multiple nodes:

try {
const payload = [
{
nodeId: "node_id_1",
payload: {
"Time": {
"TZ": "America/New_York",
"TZ-POSIX": "EST5EDT,M3.2.0,M11.1.0"
}
}
},
{
nodeId: "node_id_2",
payload: {
"Time": {
"TZ": "Asia/Kolkata"
}
}
}
];

const response = await userInstance.setMultipleNodesParams(payload);
console.log("Service parameters updated:", response);
} catch (error) {
console.error("Error updating service parameters:", error);
}

Example: Mixed Device and Service Parameters

Update both device and service parameters in the same batch operation:

try {
const payload = [
{
nodeId: "node_id_1",
payload: {
// Device parameters
"Light": {
"Power": true,
"Brightness": 75
},
// Service parameters
"Time": {
"TZ": "Europe/London"
}
}
}
];

const response = await userInstance.setMultipleNodesParams(payload);
console.log("Mixed parameters updated:", response);
} catch (error) {
console.error("Error updating parameters:", error);
}

Example: Building Payload from Node Data

You can build the payload dynamically from your node data:

try {
// Get all user nodes
const nodesResponse = await userInstance.getUserNodes();

// Build payload for batch update
const payload = nodesResponse.nodes.map((node) => {
const nodeConfig = node.nodeConfig;
const nodePayload = {};

// Add device parameters
if (nodeConfig && nodeConfig.devices) {
nodeConfig.devices.forEach((device) => {
nodePayload[device.name] = {};
// Example: Update Power parameter if it exists
const powerParam = device.params.find(p => p.name === "Power");
if (powerParam) {
nodePayload[device.name]["Power"] = true;
}
});
}

// Add service parameters
if (nodeConfig && nodeConfig.services) {
nodeConfig.services.forEach((service) => {
nodePayload[service.name] = {};
// Example: Update TZ parameter if it exists
const tzParam = service.params.find(p => p.name === "TZ");
if (tzParam) {
nodePayload[service.name]["TZ"] = "America/Los_Angeles";
}
});
}

return {
nodeId: node.id,
payload: nodePayload
};
});

// Execute batch update
const response = await userInstance.setMultipleNodesParams(payload);
console.log("Batch update completed:", response);
} catch (error) {
console.error("Error in batch update:", error);
}

setMultipleNodesParams API

ParameterTypeDescription
payload*MultipleNodePayload[]Array of payload objects, each containing a node ID and its parameters to update.

MultipleNodePayload Interface

PropertyTypeDescription
nodeId*stringThe ID of the node to update.
payload*NodePayloadObject containing device and service parameters, keyed by device/service name.

NodePayload Structure

The NodePayload is an indexable object where:

  • Keys are device names or service names (strings)
  • Values are DeviceParams objects (indexable by parameter name)

Example structure:

{
"Light": { // Device name
"Power": true, // Parameter name: value
"Brightness": 80
},
"Time": { // Service name
"TZ": "America/Los_Angeles" // Parameter name: value
}
}
备注
  • Device names and service names in the payload must match the actual names from the node configuration.
  • Parameter names must match the actual parameter names from the device or service configuration.
  • You can update both device parameters and service parameters in the same batch operation.
  • The API returns a single response for the entire batch operation.

On this page