Room Information Management

Last Updated on : 2024-08-23 09:47:59download

This topic describes how to implement the features of room management using APIs. These APIs allow you to create, modify, and remove a room, and register listeners for receiving room-based event callbacks.

  • Create a room: How to create a new room and set its basic information.
  • Modify a room: How to update the room name, location, and other information.
  • Remove a room: How to delete a room and all its associated information.
  • Register event listener: How to register listeners for receiving room-based event callbacks.

Description

The class ThingSmartRoomBiz is used for room management to implement operations on a room.

Compared to ThingSmartRoom, ThingSmartRoomBiz provides more features. If you are still using ThingSmartRoom, please refer to this document.

Class (or protocol) Description
ThingSmartRoomBiz Room management class
ThingSmartRoomModel Room model class
ThingSmartRoomBizDelegate Room delegate protocol

Data model of ThingSmartRoomModel

Field Type Description
roomId long long The room ID.
iconUrl NSString The room icon.
name NSString The name of the room.
displayOrder NSInteger The order of rooms.

Add a room

Add a room in a specific home. You need to pass in the room name and home ID. Make a callback on success to return the room model. Additionally, you can register a listener and implement the delegate method ThingSmartRoomBizDelegate to receive a callback when a room is added.

API description

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

Parameter description

Parameters Description
roomName The name of the room to add.
homeId The home ID.
success The success callback.
failure The failure callback.

Example

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

    }
}

Remove a room

Remove a room from a specific home. You need to pass in the room ID and home ID. Make a callback on success. Additionally, you can register a listener and implement the delegate method ThingSmartRoomBizDelegate to receive a callback when a room is removed.

API description

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

Parameter description

Parameters Description
roomId The ID of the room to remove.
homeId The ID of the home to which the room belongs.
success The success callback.
failure The failure callback.

Example

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

    }
}

Rename a room

Change the name of a room. The new name cannot be empty. Make a callback on success to return the room model. Additionally, you can register a listener and implement the delegate method ThingSmartRoomBizDelegate to receive a callback when a room is renamed.

API description

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

Parameter description

Parameters Description
roomName The new name of the room.
roomId The ID of the target room.
homeId The ID of the home to which the room belongs.
success The success callback.
failure The failure callback.

Example

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

    }
}

Query the list of rooms in a home

Query the list of rooms in a home. You need to pass in the home ID. Make a callback on success to return the array of the room model.

API description

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

Parameter description

Parameters Description
homeId The home ID.
success The success callback.
failure The failure callback.

Example

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

    }
}

Query the list of default rooms

Get the list of default rooms when the user creates a home. The default room model includes the room name and whether it is selected by default.

API description

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

Parameter description

Parameters Description
success The success callback.
failure The failure callback.

Data model of ThingSmartDefaultRoomItem

Field Type Description
name NSString The name of the room.
isSelected BOOL Specifies whether the room is selected by default.

Example

Objective-C:

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

    } failure:^(NSError *error) {

    }];
}

Swift:

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

    } failure: { error in

    }
}

Get recommended room names

When the user adds a room, get the recommended room names. Make a callback on success to return the list of recommended names.

API description

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

Parameter description

Parameters Description
success The success callback.
failure The failure callback.

Example

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#>
    }
}

Sort the list of rooms

Sort the list of rooms in a home. Pass in the list of rooms after sorting and the home ID. Make a callback on success. Additionally, you can register a listener and implement the delegate method ThingSmartRoomBizDelegate to receive a callback when rooms are sorted.

API description

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

Parameter description

Parameters Description
rooms The list of rooms to sort.
homeId The ID of the home to which the room belongs.
success The success callback.
failure The failure callback.

Example

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

    }
}

Event callback

ThingSmartRoomBizDelegate is the delegate protocol for the room management class. You can implement it and register a listener to receive room event callbacks.

Register a listener

API description

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

Parameter description

Parameters Description
observer The listener.

Example

Objective-C:

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

Swift:

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

Remove a listener

API description

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

Parameter description

Parameters Description
observer The listener.

Example

Objective-C:

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

Swift:

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

Room delegate

ThingSmartRoomBizDelegate is the delegate protocol for room management. If you implement this protocol, ThingSmartRoomBiz can notify you of room-based events through a callback.

API description

@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

Parameter description

Parameters Description
roomBiz Room management class
roomModel The room model.
homeModel The home model.
roomId The room ID.
roomList The list of rooms.