家庭管理业务拓展

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

智能生活业务拓展 SDK 是基于家庭维度进行开发的,智能设备的添加、编辑、移除、状态变化的监听都是基于当前家庭。用户登录后,可以在账号下创建任意多个家庭,并对单个家庭进行管理,包括创建房间、新增成员等。

创建家庭

登录成功后,跳转到 App 首页,单击 Home Management-Business Extension SDK 进入家庭管理页面,展示当前家庭。

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

如果账号下没有任何家庭,可以使用 ThingSmartFamilyBiz 来创建和管理家庭。创建家庭时名称是必须的,家庭位置和家庭下房间可以稍后再设置。

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

删除家庭

  1. 获取当前账号下所有家庭。

    • accept 为已加入的家庭
    • pending 为待加入的家庭
    • reject 为拒绝加入的家庭
  2. 删除家庭应从已加入的家庭中选择,获取家庭列表代码。

    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. 选择要删除的家庭,传入 homeId 删除家庭。当家庭下还有除了家庭所有者以外的成员时,调用该方法会失败。

    删除家庭后,家庭下所有设备会重置。

    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)")
    }
    

获取家庭下设备列表

获取家庭详情,成功后回调家庭模型,从家庭模型中获取设备列表。

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

监听家庭维度消息

实现 ThingSmartFamilyBizDelegate 协议,根据需要实现回调方法。具体 API 信息,参考接口文档。

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

添加房间

在家庭下最多可以创建 200 个房间,房间名、房间所属家庭 ID 是必须参数。设备配网成功后,可以绑定到房间下。

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

此外,还可以获取推荐的房间名列表,以便进行快速选择。

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

删除房间

  1. 获取家庭下的房间列表。

    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. 选择要删除的房间,传入 homeIdroomId 删除房间。

    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
    
    }
    

添加成员

家庭所有者可以添加管理员和普通成员,而管理员可以添加普通成员。

通过 App 账号添加成员

需要传入成员名称、国家码、账号、角色枚举值(0 是普通成员,1 是管理员),添加成员代码:

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

调用该方法成功后,切换到待添加成员的账号,待添加的成员账号下将新增一个家庭,家庭状态为 pending,可以选择接受或者拒绝加入家庭。

接受加入家庭代码如下:

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

拒绝加入家庭代码如下:

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

删除成员

  1. 获取家庭下的成员列表。

    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. 选择要删除的成员,传入 memberId 删除成员。

    • 家庭所有者可以删除管理员和普通成员。当家庭下没有别的成员时,删除自身等于删除家庭。如果家庭所有者要离开家庭,首先转移家庭所有者权限给别的成员,然后删除自身等同于离开家庭。
    • 管理员可以删除普通成员,删除自身等同于离开家庭。
    • 普通成员只可以删除自身,即离开家庭。
    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)
    }