管理群组
本节介绍创建、更新和管理节点群组的操作,可通过 ESPRMUser 实例(用于用户级操作)或 ESPRMGroup 实例(用于群组级操作)执行。
说明
使用 ESPRMAuth.getLoggedInUser 方法获取 userInstance:
try {
const authInstance = ESPRMBase.getAuthInstance();
const userInstance = await authInstance.getLoggedInUser();
if (userInstance) {
// 使用 userInstance 进行群组操作
} else {
console.log("No user is currently logged in");
}
} catch (error) {
console.error("Error getting logged in user:", error);
}
使用 userInstance 的 getGroupById 或 getGroups 方法获取 group 实例(ESPRMGroup 实例):
// 通过 ID 获取群组
const group = await userInstance.getGroupById({
groupId: "group_id_123"
});
// 或从群组列表中获取
const response = await userInstance.getGroups();
const group = response.groups[0]; // 获取列表中的第一个群组
创建节点群组
使用 createGroup 方法创建新的节点群组。
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);
}
更新群组信息
使用 updateGroupInfo 方法更新群组信息。
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);
}