房间信息管理

更新时间:2024-08-23 09:47:59下载pdf

本文将介绍房间管理相关的各项功能,包括以下内容:

  • 创建房间:如何新建一个房间,并设置其基本信息。
  • 修改房间:如何更新房间的名称、位置等信息。
  • 移除房间:如何删除一个房间及其所有关联信息。
  • 注册事件监听:如何注册监听来接收房间维度的事件回调。

功能说明

ThingSmartRoomBiz 类,调用接口来实现对单个房间的操作。

相比 ThingSmartRoomThingSmartRoomBiz 提供了更多的功能。如果您仍在使用 ThingSmartRoom,请参考 此文档

类名(或协议名) 说明
ThingSmartRoomBiz 房间管理类
ThingSmartRoomModel 房间模型类
ThingSmartRoomBizDelegate 房间代理协议

ThingSmartRoomModel 数据模型

字段 类型 描述
roomId long long 房间 ID
iconUrl NSString 房间图标
name NSString 房间名称
displayOrder NSInteger 房间排序

新增房间

在指定家庭下添加房间,您需要传入房间名和家庭 ID。房间创建成功后,调用 succes 回调房间模型。
此外还可以通过注册监听的方式,实现 ThingSmartRoomBizDelegate 代理方法,接收新增房间回调。

接口说明

- (void)addHomeRoomWithName:(NSString *)roomName
                     homeId:(long long)homeId
                    success:(void(^)(ThingSmartRoomModel *roomModel))success
                    failure:(ThingFailureError)failure;

参数说明

参数 说明
roomName 待添加房间名
homeId 家庭 ID
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)addRoom:(NSString *)roomName homeId:(long long)homeId {
    [[ThingSmartRoomBiz sharedInstance] addHomeRoomWithName:roomName homeId:homeId success:^(ThingSmartRoomModel *roomModel) {

    } failure:^(NSError *error) {

    }];
}

Swift

func addRoom(roomName:String, homeId:Int64) {
    ThingSmartRoomBiz.sharedInstance().addHomeRoom(withName: roomName, homeId: homeId) { roomModel in

    } failure: { error in

    }
}

移除房间

将房间从家庭下移除,您需要传入房间 ID 和家庭 ID。移除成功后,调用 success 回调。
此外还可以通过注册监听的方式,实现 ThingSmartRoomBizDelegate 代理方法,接收移除房间回调。

接口说明

- (void)removeHomeRoomWithRoomId:(long long)roomId
                          homeId:(long long)homeId
                         success:(ThingSuccessHandler)success
                         failure:(ThingFailureError)failure;

参数说明

参数 说明
roomId 将要删除的房间 ID
homeId 房间所属的家庭 ID
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)removeRoom:(long long)roomId homeId:(long long)homeId {
    [[ThingSmartRoomBiz sharedInstance] removeHomeRoomWithRoomId:roomId homeId:homeId success:^{

    } failure:^(NSError *error) {

    }];
}

Swift

func removeRoom(roomId:Int64, homeId:Int64) {
    ThingSmartRoomBiz.sharedInstance().removeHomeRoom(withRoomId: roomId, homeId: homeId) {

    } failure: { error in

    }
}

修改房间名称

修改房间名称时,传入新的房间名不能为空。修改成功后,调用 succes 回调房间模型。
此外还可以通过注册监听的方式,实现 ThingSmartRoomBizDelegate 代理方法,接收修改房间回调。

接口说明

- (void)updateHomeRoomWithName:(NSString *)roomName
                        roomId:(long long)roomId
                        homeId:(long long)homeId
                       success:(void(^)(ThingSmartRoomModel *roomModel))success
                       failure:(ThingFailureError)failure;

参数说明

参数 说明
roomName 将要更新的房间名
roomId 将要更新的房间 ID
homeId 房间所属的家庭 ID
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)updateRoomWithName:(NSString *)roomName roomId:(long long)roomId homeId:(long long)homeId {
    [[ThingSmartRoomBiz sharedInstance] updateHomeRoomWithName:roomName roomId:roomId homeId:homeId success:^(ThingSmartRoomModel *roomModel) {

    } failure:^(NSError *error) {

    }];
}

Swift

func updateRoom(roomName:String, roomId:Int64, homeId:Int64) {
    ThingSmartRoomBiz.sharedInstance().updateHomeRoom(withName: roomName, roomId: roomId, homeId: homeId) { roomModel in

    } failure: { error in

    }
}

查询家庭下房间列表

查询家庭下的房间列表,传入对应的家庭 ID。查询成功后,调用 succes 回调房间模型数组。
接口说明

- (void)getRoomListWithHomeId:(long long)homeId
                      success:(void(^)(NSArray <ThingSmartRoomModel *> *roomList))success
                      failure:(ThingFailureError)failure;

参数说明

参数 说明
homeId 家庭 ID
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)getRoomListWithHomeId:(long long)homeId {
    [[ThingSmartRoomBiz sharedInstance] getRoomListWithHomeId:homeId success:^(NSArray<ThingSmartRoomModel *> *roomList) {

    } failure:^(NSError *error) {

    }];
}

Swift

func getRoomList(with homeId:Int64) {
    ThingSmartRoomBiz.sharedInstance().getRoomList(withHomeId: homeId) { roomModelArray in

    } failure: { error in

    }
}

查询默认房间列表

创建家庭时调用接口,返回家庭下默认房间列表。默认房间模型包含房间名、是否默认选中。
接口说明

- (void)getDefaultRoomsWithSuccess:(void(^)(NSArray <ThingSmartDefaultRoomItem *>*rooms))success
                           failure:(ThingFailureError)failure;

参数说明

参数 说明
success 成功回调
failure 失败回调

ThingSmartDefaultRoomItem 数据模型

字段 类型 描述
name NSString 房间名称
isSelected BOOL 房间是否默认选中

调用示例

Objective-C

- (void)getDefaultRoomList {
    [[ThingSmartRoomBiz sharedInstance] getDefaultRoomsWithSuccess:^(NSArray<ThingSmartDefaultRoomItem *> *rooms) {

    } failure:^(NSError *error) {

    }];
}

Swift

func getDefaultRoomList() {
    ThingSmartRoomBiz.sharedInstance().getDefaultRooms { roomList in

    } failure: { error in

    }
}

获取推荐房间名称

新增房间时,可以调用接口获取推荐的房间名称,调用 success 返回房间名称列表。
接口说明

- (void)getRecommendRoomsWithSuccess:(void(^)(NSArray<NSString *> *rooms))success
                             failure:(ThingFailureError)failure;

参数说明

参数 说明
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)getRecommendRoomList {
    [[ThingSmartRoomBiz sharedInstance] getRecommendRoomsWithSuccess:^(NSArray<NSString *> *rooms) {

    } failure:^(NSError *error) {

    }];
}

Swift

func getRecommendRoomList() {
    ThingSmartRoomBiz.sharedInstance().getRecommendRooms { roomList in

    } failure: { error in
        <#code#>
    }
}

房间列表排序

对家庭下的房间列表排序,传入排序后的房间列表和家庭 ID,设置成功调用 success 回调。
此外,还可以通过注册监听的方式,实现 ThingSmartRoomBizDelegate 代理方法,接收房间排序回调。

接口说明

- (void)sortRoomList:(NSArray <ThingSmartRoomModel *> *)rooms
              homeId:(long long)homeId
             success:(ThingSuccessHandler)success
             failure:(ThingFailureError)failure;

参数说明

参数 说明
rooms 将要设置排序的房间列表
homeId 房间所属的家庭 ID
success 成功回调
failure 失败回调

调用示例

Objective-C

- (void)sortRoomList:(NSArray *)roomList homeId:(long long)homeId {
    [[ThingSmartRoomBiz sharedInstance] sortRoomList:roomList homeId:homeId success:^{

    } failure:^(NSError *error) {

    }];
}

Swift

func sort(roomList:[ThingSmartRoomModel], homeId:Int64) {
    ThingSmartRoomBiz.sharedInstance().sortRoomList(roomList, homeId: homeId) {

    } failure: { error in

    }
}

事件回调

ThingSmartRoomBizDelegate 是房间管理类的代理协议,您可实现该协议并通过注册监听的方式,接收房间事件回调。

注册监听

接口说明

- (void)addObserver:(id<ThingSmartRoomBizDelegate>)observer;

参数说明

参数 说明
observer 监听者

调用示例

Objective-C

- (void)addObserver:(id<ThingSmartRoomBizDelegate>)observer {
    [[ThingSmartRoomBiz sharedInstance] addObserver:observer];
}

Swift

func add(observer:ThingSmartRoomBizDelegate) {
    ThingSmartRoomBiz.sharedInstance().addObserver(observer)
}

移除监听

接口说明

- (void)removeObserver:(id<ThingSmartRoomBizDelegate>)observer;

参数说明

参数 说明
observer 监听者

调用示例

Objective-C

- (void)removeObserver:(id<ThingSmartRoomBizDelegate>)observer {
    [[ThingSmartRoomBiz sharedInstance] removeObserver:observer];
}

Swift

func remove(observer:ThingSmartRoomBizDelegate) {
    ThingSmartRoomBiz.sharedInstance().removeObserver(observer)
}

房间代理

ThingSmartRoomBizDelegate 是房间管理代理协议,ThingSmartRoomBiz 通过该协议回调房间相关的事件,以及房间下设备关系的变化等,您可实现该协议来接收事件以处理相关逻辑。

接口说明

@protocol ThingSmartRoomBizDelegate <NSObject>

@optional

/**
* The delegate that is instantiated when a room is added.
*
* @param roomBiz        Instance of room management
* @param roomModel    The room model
* @param homeModel    The home model
*/
- (void)roomBiz:(ThingSmartRoomBiz *)roomBiz
     didAddRoom:(ThingSmartRoomModel *)roomModel
         atHome:(ThingSmartHomeModel *)homeModel;

/**
* The delegate that is instantiated when an existing room is removed.
*
* @param roomBiz         Instance of room management
* @param roomId           Room ID
* @param homeModel    The home model
*/
- (void)roomBiz:(ThingSmartRoomBiz *)roomBiz
  didRemoveRoom:(long long)roomId
         atHome:(ThingSmartHomeModel *)homeModel;

/**
* The delegate of room update information, such as the name.
*
* @param roomBiz        Instance of room management
* @param roomModel    The room model
* @param homeModel    The home model
*/
- (void)roomBiz:(ThingSmartRoomBiz *)roomBiz
  didUpdateRoom:(ThingSmartRoomModel *)roomModel
         atHome:(ThingSmartHomeModel *)homeModel;

/**
* The delegate of updating the relationship between room and device or group
*
* @param roomBiz        Instance of room management
* @param roomModel    The room model
* @param homeModel    The home model.
*/
- (void)roomBiz:(ThingSmartRoomBiz *)roomBiz
didUpdateRoomRelation:(ThingSmartRoomModel *)roomModel
         atHome:(ThingSmartHomeModel *)homeModel;

/**
* The delegate of sorting rooms
*
* @param roomBiz         Instance of room management
* @param roomList       The room list
* @param homeModel     The home model.
*/
- (void)roomBiz:(ThingSmartRoomBiz *)roomBiz
didSortRoomList:(NSArray<ThingSmartRoomModel *>*)roomList
         atHome:(ThingSmartHomeModel *)homeModel;

@end

参数说明

参数 说明
roomBiz 房间管理类
roomModel 房间模型
homeModel 家庭模型
roomId 房间 ID
roomList 房间列表