Device Control
Each ESPRMDevice instance under ESPRMNode instance will have a set of ESPRMDeviceParam that can be updated using the setValue API. This method requires the value to be set and returns a response indicating the success or failure of the operation.
Navigating the Hierarchy to Device Parameters
To control a device, you need to navigate through the node hierarchy:
- Get the node - Retrieve the node using
getUserNodes()orgetNodeDetails() - Access node configuration - Get the
nodeConfigfrom the node - Find the device - Access the
devicesarray fromnodeConfig - Get device parameters - Access the
paramsarray from the device - Update parameter value - Call
setValue()on the specific parameter
Example: Complete Flow for Device Parameter Control
try {
// Step 1: Get user nodes
const response = await userInstance.getUserNodes();
// Step 2: Find the specific node (e.g., by node ID or iterate through nodes)
const targetNode = response.nodes.find(node => node.id === "your_node_id");
if (!targetNode) {
console.error("Node not found");
return;
}
// Step 3: Get node configuration (if not already available)
const nodeConfig = targetNode.nodeConfig || await targetNode.getNodeConfig();
// Step 4: Find the specific device (e.g., a Light device)
const lightDevice = nodeConfig.devices.find(
device => device.type === "esp.device.light"
);
if (!lightDevice) {
console.error("Device not found");
return;
}
// Step 5: Find the specific parameter (e.g., Power parameter)
const powerParam = lightDevice.params.find(
param => param.name === "Power"
);
if (!powerParam) {
console.error("Parameter not found");
return;
}
// Step 6: Update the parameter value
const newValue = true; // Turn on the light
const updateResponse = await powerParam.setValue(newValue);
console.log("Device parameter updated successfully:", updateResponse);
} catch (error) {
console.error("Error updating device parameter:", error);
}
Example: Finding Parameters by Type
You can also find parameters by their type instead of name:
try {
const response = await userInstance.getUserNodes();
const node = response.nodes[0];
const nodeConfig = node.nodeConfig || await node.getNodeConfig();
const device = nodeConfig.devices[0];
// Find parameter by type (e.g., esp.param.power)
const powerParam = device.params.find(
param => param.type === "esp.param.power"
);
if (powerParam) {
await powerParam.setValue(true);
console.log("Power parameter updated");
}
} catch (error) {
console.error("Error updating parameter:", error);
}
Device Parameter setValue API
| Parameter | Type | Description |
|---|---|---|
| value* | any | The value of parameter to update. |
note
The setValue method returns a response indicating the success or failure of the operation. The response structure depends on the SDK implementation and may include status information, updated parameter values, or error details.