房间管理

更新时间:2022-02-17 06:45:25下载pdf

功能介绍

TuyaCommunityRoom用于描述单个房间的信息,在一个房屋下会有多个房间。TuyaCommunityRoom提供的功能包括房间信息、设备列表、群组列表、设备和群组顺序等API。

房间相关信息由TuyaCommunityRoomModel提供。

初始化房间

通过房屋 ID 和房间 ID 可以创建一个TuyaCommunityRoom实例。

接口说明

+ (instancetype)roomWithRoomId:(long long)roomId
                       houseId:(long long)houseId;

- (instancetype)initWithRoomId:(long long)roomId
                       houseId:(long long)houseId;

参数说明

参数 说明
roomId 房间 ID
houseId 房屋 ID

示例代码

long long roomId;
long long houseId;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomId houseId:houseId];

添加房间

接口说明

- (void)addHouseRoomWithName:(NSString *)name
                     success:(void(^)(TuyaCommunityRoomModel *result))success
                     failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
name 房间名
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house addHouseRoomWithName:@"xxx" success:^(TuyaCommunityRoomModel * _Nonnull result) {
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

删除房间

接口说明

- (void)removeHouseRoomWithRoomId:(long long)roomId
                          success:(void(^)(void))success
                          failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
roomId 房间 ID
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house removeHouseRoomWithRoomId:0 success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

房间列表排序

接口说明

- (void)sortHouseRoomWithRoomList:(NSArray<TuyaCommunityRoomModel *>*)roomList
                          success:(void(^)(void))success
                          failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
roomList 房间列表
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
[house sortHouseRoomWithRoomList:@[] success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

更新房间名称

接口说明

- (void)updateRoomNameWithRoomName:(NSString *)roomName
                           success:(void(^)(void))success
                           failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
roomName 房间名称
success 成功回调
failure 失败回调

示例代码

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomId houseId:houseId];
[room updateRoomNameWithRoomName:@"xxx" success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

添加设备到房间

通过设备对应的设备 ID 给单个房间添加设备,设备模型为 TuyaSmartDeviceModel

接口说明

- (void)addDeviceWithDeviceId:(NSString *)deviceId
                      success:(void(^)(void))success
                      failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
deviceId 设备 ID
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartDeviceModel *deviceModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
if (house.deviceList.count && house.houseModel.rooms.count) {
    // 模拟获取设备模型
    deviceModel = house.deviceList.firstObject;
    // 模拟获取房间模型
    roomModel = house.houseModel.rooms.firstObject;
}

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
[room addDeviceWithDeviceId:deviceModel.devId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

从房间中移除设备

移除某个房间下的某个设备。

- (void)removeDeviceWithDeviceId:(NSString *)deviceId
                         success:(void(^)(void))success
                         failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
deviceId 设备 ID
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartDeviceModel *deviceModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
if (house.roomList.count) roomModel = house.roomList.firstObject;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
if (room.deviceList.count) deviceModel = room.deviceList.firstObject;

[room removeDeviceWithDeviceId:deviceModel.devId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

添加群组到房间

通过群组对应的群组 ID 给单个房间添加群组,设备模型为TuyaSmartGroupModel

- (void)addGroupWithGroupId:(NSString *)groupId
                    success:(void(^)(void))success
                    failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
groupId 群组 ID
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartGroupModel *groupModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
// 模拟房间
if (house.roomList.count) roomModel = house.roomList.firstObject;
// 模拟群组
if (house.groupList.count) groupModel = house.groupList.firstObject;

TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
// 添加群组
[room addGroupWithGroupId:groupModel.groupId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
   // failure handler
}];

从房间中移除群组

移除某个房间下的某个群组。

- (void)removeGroupWithGroupId:(NSString *)groupId
                       success:(void(^)(void))success
                       failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
groupId 群组 ID
success 成功回调
failure 失败回调

示例代码

long long houseId;
TuyaCommunityRoomModel *roomModel;
TuyaSmartGroupModel *groupModel;
TuyaCommunityHouse *house = [TuyaCommunityHouse houseWithHouseId:houseId];
// 模拟房间
if (house.roomList.count) roomModel = house.roomList.firstObject;
TuyaCommunityRoom *room = [TuyaCommunityRoom roomWithRoomId:roomModel.roomId houseId:houseId];
// 模拟群组
if (room.groupList.count) groupModel = room.groupList.firstObject;
// 移除群组
[room removeGroupWithGroupId:groupModel.groupId success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

批量修改房间与群组和设备的关系

此方法可以用来批量地将设备和群组移入或者移出房间。

- (void)batchModifyRoomRelationWithDeviceGroupList:(NSArray<NSString *>*)deviceGroupList
                                           success:(void(^)(void))success
                                           failure:(void(^)(NSError *error))failure;

参数说明

参数 说明
deviceGroupList 设备和群组 ID 数组
success 成功回调
failure 失败回调

示例代码

TuyaCommunityRoom *room;
[room batchModifyRoomRelationWithDeviceGroupList:@[@"xxx", @"xxx"] success:^{
    // success handler
} failure:^(NSError * _Nonnull error) {
    // failure handler
}];

对象说明

TuyaCommunityRoom

TuyaCommunityRoom用于描述单个房间的信息,在一个房屋下会有多个房间。

属性 类型 说明
roomModel TuyaCommunityRoomModel 房间信息
deviceList NSArray<TuyaSmartDeviceModel> 设备列表
groupList NSArray<TuyaSmartGroupModel> 群组列表

TuyaCommunityRoomModel

TuyaCommunityRoom 提供的功能包括房间信息、设备列表、群组列表、设备和群组顺序等API。

属性 类型 说明
roomId NSString 房间 ID
name NSString 房间名
displayOrder NSInteger 排列顺序,用于排序