Home Management

Last Updated on : 2024-01-30 01:44:41download

The BizBundle SDK is developed based on the unit of home where users can add, edit, remove devices, and monitor their status changes. After logging in to the app, users can create and manage multiple homes, including creating rooms and adding members to each home.

Create a home

After successful login, users will be redirected to the app’s homepage. Tapping on home management will display the current home.

if let home = ThingSmartFamilyBiz.sharedInstance().getCurrentFamily() {
    currentHomeName.text = home.name
} else {
    currentHomeName.text = "No Selection"
}

If there are no homes, use ThingSmartFamilyBiz to create and manage a home. Users must name the created home. The location and rooms can be set later.

let requestModel = ThingSmartFamilyRequestModel()
requestModel.name = homeName
requestModel.geoName = geoName
requestModel.latitude = latitude
requestModel.longitude = longitude

ThingSmartFamilyBiz.sharedInstance().addFamily(with: requestModel) {[weak self] homeId in
    guard let self = self else { return }
    let action = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { [weak self] _ in
        guard let self = self else { return }
        self.navigationController?.popViewController(animated: true)
    }

    Alert.showBasicAlert(on: self, with: NSLocalizedString("Success", comment: ""), message: NSLocalizedString("Successfully added new home.", comment: ""), actions: [action])
} failure: {[weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: NSLocalizedString("Failed to Add New Home", comment: ""), message: errorMessage)
}

Delete a home

  1. Retrieve all homes under the current account.

    • accept: Accepted home
    • pending: Pending acceptance
    • reject: Declined home
  2. Get the list of homes. Users should select the home they want to delete from their joined homes.

    ThingSmartFamilyBiz.sharedInstance().getFamilyList(success: { [weak self] homeList in
    guard let self = self else {return}
    if let homeList = homeList {
        for home in homeList {
            switch home.dealStatus {
            case .accept :
                self.homes.append(home)
            case .pending:
                print("pending")
            case .reject:
                print("reject")
            @unknown default:
                fatalError()
            }
        }
    }
    
  3. Pass in the homeId of the home to be deleted. If there are members other than the owner in the home, this method call will fail.

    After a home is deleted, all devices in the home will be reset.

    ThingSmartFamilyBiz.sharedInstance().deleteFamily(withHomeId: homeModel.homeId) {[weak self] in
        guard let self = self else {return}
        Alert.showBasicAlert(on: self, with: "Success", message: "Delete home \(homeModel.name!)")
        self.homes.remove(at: indexPath.row)
    
        self.selectHomeModel = nil
        self.index = nil
        tableView.reloadData()
    } failure: { [weak self] error in
        guard let self = self else {return}
        let errorMessage = error?.localizedDescription ?? ""
        Alert.showBasicAlert(on: self, with: "Failed to delete home", message: "\(errorMessage)")
    }
    

Get device list in a home

Get the home details. A success callback returns the home model that contains the list of devices.

ThingSmartFamilyBiz.sharedInstance().getFamilyDetail(withHomeId: homeModel!.homeId) { [weak self] home in
    guard let self = self else {return}
    deviceList = home?.deviceList ?? []
    self.tableView.reloadData()
} failure: { [weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: "Failed to Fetch home detail", message: errorMessage)
}

Listen for home messages

Implement ThingSmartFamilyBizDelegate. Implement the callback as needed. For more information, see the API reference documentation.

extension BEHomeDetailViewController : ThingSmartFamilyBizDelegate {
    func familyBiz(_ familyBiz: ThingSmartFamilyBiz!, didUpdateHome homeModel: ThingSmartHomeModel!) {
        if let home = homeModel {
            self.homeModel = home
            setupUI()
        }
    }
}

Add a room

Up to 200 rooms can be created in a home. The room name and the associated home ID are the required parameters. After a device is paired, it can be assigned to a room.

ThingSmartRoomBiz.sharedInstance().addHomeRoom(withName: roomName, homeId: currentHome.homeId) { [weak self] roomModel in
    guard let self = self else { return }
    let action = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { [weak self] _ in
        guard let self = self else { return }
        self.navigationController?.popViewController(animated: true)
    }

    Alert.showBasicAlert(on: self, with: NSLocalizedString("Success", comment: ""), message: "Add Room", actions: [action])
} failure: {[weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: "Failed to Add Room", message: errorMessage)
}

You can also retrieve the list of recommended room names for easy assignment.

ThingSmartRoomBiz.sharedInstance().getRecommendRooms {[weak self] rooms in
    guard let self = self else {return}
    self.recommendRoomNames = rooms ?? []
    self.tableView.reloadData()
} failure: {[weak self] error in
    guard let self = self else {return}
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: "Failed to Get Recommend Room", message: errorMessage)
}

Delete a room

  1. Get the list of rooms in a home.

    ThingSmartRoomBiz.sharedInstance().getRoomList(withHomeId: model.homeId) {[weak self] rooms in
        guard let self = self else {return}
        self.roomList = rooms ?? []
        self.tableView.reloadData()
    } failure: { [weak self] error in
        guard let self = self else { return }
        let errorMessage = error?.localizedDescription ?? ""
        Alert.showBasicAlert(on: self, with: "Failed to Get Room", message: errorMessage)
    }
    
  2. To delete a room, select the room to be deleted and pass in the homeId and roomId.

    ThingSmartRoomBiz.sharedInstance().removeHomeRoom(withRoomId: room.roomId, homeId: home.homeId) {[weak self] in
        guard let self = self else { return }
        let action = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { [weak self] _ in
            guard let self = self else { return }
            self.navigationController?.popViewController(animated: true)
        }
    
        Alert.showBasicAlert(on: self, with: NSLocalizedString("Success", comment: ""), message: "Delete Room", actions: [action])
    } failure: { error in
    
    }
    

Add a member

A home owner can add admins and common members, while an admin can only add common members.

Add a member by app account

To add a member, pass in the member name, country code, account, and role (0 for common member and 1 for admin).

let requestModel = ThingSmartHomeAddMemberRequestModel()
requestModel.name = nameField.text ?? ""
requestModel.account = accountField.text ?? ""
requestModel.countryCode = codeField.text ?? ""

if role == "0" {
    requestModel.role = .member
} else if role == "1" {
    requestModel.role = .admin
} else {
    Alert.showBasicAlert(on: self, with: "Failed to Add Member", message: "role value is invalid, input 0 or 1")
    return
}

ThingSmartMemberBiz.sharedInstance().addHomeMember(with: requestModel, homeId: homeId) {[weak self] member in
    guard let self = self else { return }
    let action = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { [weak self] _ in
        guard let self = self else { return }
        self.navigationController?.popViewController(animated: true)
    }

    Alert.showBasicAlert(on: self, with: NSLocalizedString("Success", comment: ""), message: "Add Member", actions: [action])
} failure: { [weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: "Failed to Add Member", message: errorMessage)
}

After a successful method call, switch to the account that is invited to join the home. A new home will be created under the invitee’s account, with the home status being pending. The invitee can choose to accept or decline this home invitation.

Accept a home invitation:

ThingSmartFamilyBiz.sharedInstance().acceptJoinFamily(withHomeId: model.homeId) {[weak self] _ in
    guard let self = self else {return}
    homes.remove(at: indexPath.row)
    self.tableView.reloadData()
} failure: {[weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: NSLocalizedString("Failed to Accept Home", comment: ""), message: errorMessage)
}

Decline a home invitation:

ThingSmartFamilyBiz.sharedInstance().rejectJoinFamily(withHomeId: model.homeId) {[weak self] _ in
    guard let self = self else {return}
    homes.remove(at: indexPath.row)
    self.tableView.reloadData()
} failure: {[weak self] error in
    guard let self = self else { return }
    let errorMessage = error?.localizedDescription ?? ""
    Alert.showBasicAlert(on: self, with: NSLocalizedString("Failed to Reject Home", comment: ""), message: errorMessage)
}

Delete a member

  1. Get the list of members in a home.

    ThingSmartMemberBiz.sharedInstance().getHomeMemberList(withHomeId: model.homeId) {[weak self] members  in
        guard let self = self else { return }
        self.memberList = members
        self.tableView.reloadData()
    } failure: {[weak self] error in
        guard let self = self else { return }
        let errorMessage = error?.localizedDescription ?? ""
        Alert.showBasicAlert(on: self, with: "Failed to get member list", message: errorMessage)
    }
    
  2. To delete a member, select the member to be deleted and pass in the memberId.

    • The home owner can delete admins and common members. Deleting the last member will delete the home. If the home owner wants to leave a home, they should first transfer the ownership to another member. Then, deleting themselves will leave that home.
    • The admin can delete common members. Deleting themselves will leave the associated home.
    • The common member can only delete themselves, meaning they leave the associated home.
    ThingSmartMemberBiz.sharedInstance().removeHomeMember(withMemberId: member!.memberId) {[weak self] in
        guard let self = self else { return }
        let action = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { [weak self] _ in
            guard let self = self else { return }
            self.navigationController?.popViewController(animated: true)
        }
    
        Alert.showBasicAlert(on: self, with: NSLocalizedString("Success", comment: ""), message: "Delete Member", actions: [action])
    } failure: {[weak self] error in
        guard let self = self else { return }
        let errorMessage = error?.localizedDescription ?? ""
        Alert.showBasicAlert(on: self, with: "Failed to Delete Member", message: errorMessage)
    }