群组元数据
本节介绍管理群组元数据和自定义数据的操作,通过 ESPRMGroup 实例实现。
说明
首先使用 ESPRMAuth.getLoggedInUser 获取用户实例,然后使用 getGroupById 或 getGroupByName 获取群组实例:
try {
const authInstance = ESPRMBase.getAuthInstance();
const userInstance = await authInstance.getLoggedInUser();
if (userInstance) {
// 通过 ID 获取群组
const group = await userInstance.getGroupById({
groupId: "group_id_123"
});
// 或通过名称获取群组
// const group = await userInstance.getGroupByName({
// groupName: "Living Room Devices"
// });
} else {
console.log("No user is currently logged in");
}
} catch (error) {
console.error("Error getting group:", error);
}
更新元数据
使用 updateMetadata 方法更新当前群组的元数据。
try {
const metadata = {
room: "kitchen",
floor: 1,
lastUpdated: new Date().toISOString()
};
const response = await group.updateMetadata(metadata);
console.log("Metadata updated successfully:", response);
} catch (error) {
console.error("Error updating metadata:", error);
}