跳到主要内容

Manage Groups

This section covers operations to create, update, and manage node groups. These operations can be performed using either the ESPRMUser instance (for user-level operations) or an ESPRMGroup instance (for group-level operations).

备注

To get the userInstance, use ESPRMAuth.getLoggedInUser method:

try {
const authInstance = ESPRMBase.getAuthInstance();
const userInstance = await authInstance.getLoggedInUser();

if (userInstance) {
// Use userInstance for group operations
} else {
console.log("No user is currently logged in");
}
} catch (error) {
console.error("Error getting logged in user:", error);
}

To get the group instance (an ESPRMGroup instance), use methods like getGroupById or getGroups on the userInstance:

// Get group by ID
const group = await userInstance.getGroupById({
groupId: "group_id_123"
});

// Or get from groups list
const response = await userInstance.getGroups();
const group = response.groups[0]; // Get first group from the list

Create Node Group

To create a new node group, use the createGroup method.

const groupInfo = {
name: "Living Room Devices",
nodeIds: ["node_id_1", "node_id_2"],
description: "All devices in the living room",
customData: {
room: "living_room",
floor: 1
},
type: "user",
mutuallyExclusive: false
};

try {
const newGroup = await userInstance.createGroup(groupInfo);
console.log("Group created successfully:", newGroup);
} catch (error) {
console.error("Error creating group:", error);
}

Update Group Info

To update the information of a group, use the updateGroupInfo method.

try {
const updateInfo = {
name: "Updated Group Name",
description: "Updated description",
customData: {
room: "bedroom",
floor: 2
}
};

const response = await group.updateGroupInfo(updateInfo);
console.log("Group info updated successfully:", response);
} catch (error) {
console.error("Error updating group info:", error);
}

Delete Group

To delete a group, use the delete method.

try {
const response = await group.delete();
console.log("Group deleted successfully:", response);
} catch (error) {
console.error("Error deleting group:", error);
}

Leave Group

To remove the current user from the group, use the leave method.

try {
const response = await group.leave();
console.log("Left group successfully:", response);
} catch (error) {
console.error("Error leaving group:", error);
}

Add Nodes to Group

To add nodes to an existing node group, use the addNodes method. This method requires a list of node IDs to be added to the node group.

/*
Assuming user has two node instances of ESPRMNode stored in:
- firstNode
- secondNode
*/

const nodesToAdd = [firstNode.id, secondNode.id];

try {
const response = await group.addNodes(nodesToAdd);
console.log("Nodes added successfully:", response);
} catch (error) {
console.error("Error adding nodes to group:", error);
}

Remove Nodes from Group

To remove nodes from a group, use the removeNodes method.

const nodesToRemove = ["node_id_1", "node_id_2"];

try {
const response = await group.removeNodes(nodesToRemove);
console.log("Nodes removed successfully:", response);
} catch (error) {
console.error("Error removing nodes from group:", error);
}

Get Nodes List

To retrieve the list of node IDs associated with the group, use the getNodesList method.

try {
const nodeIds = await group.getNodesList();
console.log("Node IDs in group:", nodeIds);
} catch (error) {
console.error("Error getting nodes list:", error);
}

Get Nodes With Details

To retrieve a list of nodes with detailed information, use the getNodesWithDetails method.

try {
const nodes = await group.getNodesWithDetails();
console.log("Nodes with details:", nodes);
nodes.forEach(node => {
console.log(`Node: ${node.name}, Type: ${node.type}`);
});
} catch (error) {
console.error("Error getting nodes with details:", error);
}

Create SubGroup

To create a sub-group under the current group, use the createSubGroup method.

try {
const subGroupInfo = {
name: "SubGroup Name",
nodeIds: ["node_id_1"],
description: "Subgroup description"
};

const subGroup = await group.createSubGroup(subGroupInfo);
console.log("SubGroup created successfully:", subGroup);
} catch (error) {
console.error("Error creating subgroup:", error);
}

Get SubGroups

To retrieve the list of sub-groups for the current group, use the getSubGroups method.

try {
const subGroups = await group.getSubGroups();
console.log("SubGroups:", subGroups);
subGroups.forEach(subGroup => {
console.log(`SubGroup: ${subGroup.name}, ID: ${subGroup.id}`);
});
} catch (error) {
console.error("Error getting subgroups:", error);
}

On this page