群组管理业务拓展

更新时间:2024-01-08 07:39:24下载pdf

将具备共性的设备组成一个群组,达到群控的目的。本文介绍如何实现群组简单控制的功能。

简单的界面

本章节介绍如何简单实现一个界面。该界面通过 isGroup 字段,控制展示当前家庭的设备还是群组。在 viewDidLoad 的时候会刷新 home 的数据,然后更新界面。

另外,也实现了 home 的代理方法,当 home 有事件时,会触发界面的刷新。当 cell 被单击的时候,会调用 handle(device:) 或者 handle(group:) 接口。

class DeviceListVC: UITableViewController {

    var home: ThingSmartHome
    var isGroup: Bool = false

    init(home: ThingSmartHome, isGroup: Bool = false) {
        self.home = home
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")
        home.delegate = self
        loadData()
    }


    private func loadData() {
        home.getDataWithSuccess({ (model) in
            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 Fetch Home", comment: ""), message: errorMessage)
        })
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.isGroup ? self.home.groupList.count : self.home.deviceList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .value1, reuseIdentifier: "DeviceDetailListViewControllerReuseIdentifier")

        let title = self.isGroup ? self.home.groupList[indexPath.row].name : self.home.deviceList[indexPath.row].name
        cell.textLabel?.text = title

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if (self.isGroup) {
            handle(group: self.home.groupList[indexPath.row])
        }else{
            handle(device: self.home.deviceList[indexPath.row])
        }
    }

    func handle(device: ThingSmartDeviceModel) {}
    func handle(group: ThingSmartGroupModel) {}
}

extension DeviceListVC: ThingSmartHomeDelegate {
    func homeDidUpdateInfo(_ home: ThingSmartHome!) {
        tableView.reloadData()
    }

    func home(_ home: ThingSmartHome!, didAddDeivice device: ThingSmartDeviceModel!) {
        tableView.reloadData()
    }

    func home(_ home: ThingSmartHome!, didRemoveDeivice devId: String!) {
        tableView.reloadData()
    }

    func home(_ home: ThingSmartHome!, deviceInfoUpdate device: ThingSmartDeviceModel!) {
        tableView.reloadData()
    }

    func home(_ home: ThingSmartHome!, device: ThingSmartDeviceModel!, dpsUpdate dps: [AnyHashable : Any]!) {
        tableView.reloadData()
    }
}

创建群组

isGroupfalse 时展示的是设备,可以在 cell 被单击的时候,创建一个群组。

  1. 通过 ThingGroupMakerHelper.groupBizType(fromDeviceId:) 获取群组类型。

  2. 根据群组类型,构建参数。如果设备是蓝牙 Mesh、蓝牙 Beacon 或者涂鸦私有 Mesh 设备,那么需要把当前家庭的设备列表构造到参数里面。

  3. 创建添加群组服务。

  4. 获取群组下符合条件的设备列表。

  5. 创建群组。

    class DeviceListVC: UITableViewController {
    
        var addGroupService: ThingGroupServiceProtocol?
    
        func handle(device: ThingSmartDeviceModel) {
            // 根据设备 ID 查询群组类型
            let groupType = ThingGroupMakerHelper.groupBizType(fromDeviceId: device.devId)
    
            // 根据群组类型构建参数
            var params: [AnyHashable: AnyObject] = [:]
            params["devId"] = device.devId! as AnyObject
    
            if (groupType == .bleMesh || groupType == .sigMesh || groupType == .beacon) {
                let impl = ThingSmartBizCore.sharedInstance().service(of: ThingBusinessGroupProtocol.self) as? ThingBusinessGroupProtocol
                let deviceList = impl?.deviceListForCurrentSpace() ?? []
                params["deviceList"] = deviceList as AnyObject
            }
    
            // 创建服务
            self.addGroupService = ThingGroupServiceMaker.groupServiceMaker(withBuildQuery: params)
    
    
            SVProgressHUD.show()
            // 获取群组下符合条件的设备列表
            self.addGroupService?.fetchDeviceList?(success: { list in
    
                guard let listIds = list?.compactMap({ return $0.devId }) else {
                    SVProgressHUD.dismiss()
                    return
                }
    
                // 创建群组
                self.addGroupService?.createGroup?(withName: "My group Name", deviceList: listIds, process: { process in
    
                }, success: { groupId in
                    SVProgressHUD.dismiss()
                    return
                }, failure: { e in
                    SVProgressHUD.dismiss()
                    return
                })
    
            }, failure: { e in
                SVProgressHUD.dismiss()
                return
            })
        }
    }
    

更新群组

isGrouptrue 时展示的是群组,可以在 cell 被单击的时候,进行更新群组。使用 actionSheet 进行展示及事件处理。

  1. 通过 ThingGroupMakerHelper.groupBizType(fromDeviceId:) 获取群组类型。

  2. 根据群组类型,构建参数。如果设备是蓝牙 Mesh、蓝牙 Beacon 或者涂鸦私有 Mesh 设备,那么需要把当前家庭的设备列表构造到参数里面。

  3. 创建更新群组服务。

  4. 获取群组下符合条件的设备列表。

  5. 更新群组。

    class DeviceListVC: UITableViewController {
    
        func handle(group: ThingSmartGroupModel) {
            let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
            alert.addAction(UIAlertAction(title: "编辑群组", style: .default, handler: { action in
                self.editGroup(with: group)
            }))
    
            alert.addAction(UIAlertAction(title: "取消", style: .cancel))
            self.present(alert, animated: true)
        }
    
        var editService: ThingGroupServiceProtocol?
        func editGroup(with group: ThingSmartGroupModel) {
            // 根据群组 ID 查询群组类型
            let groupType = ThingGroupMakerHelper.groupBizType(fromDeviceId: group.groupId)
    
            // 根据群组类型构建参数
            var params: [AnyHashable: AnyObject] = [:]
            params["groupId"] = group.groupId! as AnyObject
    
            if (groupType == .bleMesh | | groupType == .sigMesh | | groupType == .beacon) {
                let impl = ThingSmartBizCore.sharedInstance().service(of: ThingBusinessGroupProtocol.self) as? ThingBusinessGroupProtocol
                let deviceList = impl?.deviceListForCurrentSpace() ?? []
                params["deviceList"] = deviceList as AnyObject
            }
    
    
            // 创建服务
            self.editService = ThingGroupServiceMaker.groupServiceMaker(withBuildQuery: params)
    
            SVProgressHUD.show()
            // 获取群组下符合条件的设备列表
            self.editService?.fetchDeviceList?(success: { list in
    
                guard let listIds = list?.compactMap({ return $0.devId }) else {
                    SVProgressHUD.dismiss()
                    return
                }
    
    
                // 更新群组设备
                self.editService?.updateGroup?(withDeviceList: listIds, process: { process in
    
                }, success: { groupId in
                    SVProgressHUD.dismiss()
                    return
                }, failure: { e in
                    SVProgressHUD.dismiss()
                    return
                })
    
            }, failure: { e in
                SVProgressHUD.dismiss()
                return
            })
        }
    
    }
    

删除群组

isGrouptrue 时展示的是群组,可以在 cell 被单击的时候,进行删除群组。复用 actionSheet 进行展示及事件处理。

  1. 通过 ThingGroupMakerHelper.groupBizType(fromDeviceId:) 获取群组类型。

  2. 根据群组类型,构建参数。如果设备是蓝牙 Mesh、蓝牙 Beacon 或者涂鸦私有 Mesh 设备,那么需要把当前家庭的设备列表构造到参数里面。

  3. 创建删除群组服务。

  4. 删除群组。

    class DeviceListVC: UITableViewController {
    
        func handle(group: ThingSmartGroupModel) {
            let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
            alert.addAction(UIAlertAction(title: "编辑群组", style: .default, handler: { action in
                self.editGroup(with: group)
            }))
    
            alert.addAction(UIAlertAction(title: "删除群组", style: .default, handler: { action in
                self.deleteGroup(with: group)
            }))
    
            alert.addAction(UIAlertAction(title: "取消", style: .cancel))
            self.present(alert, animated: true)
    
        }
    
        var deleteService: ThingGroupServiceProtocol?
    
        func deleteGroup(with group: ThingSmartGroupModel) {
            // 根据群组 ID 查询群组类型
            let groupType = ThingGroupMakerHelper.groupBizType(fromDeviceId: group.groupId)
    
            // 根据群组类型构建参数
            var params: [AnyHashable: AnyObject] = [:]
            params["groupId"] = group.groupId! as AnyObject
    
            if (groupType == .bleMesh | | groupType == .sigMesh | | groupType == .beacon) {
                let impl = ThingSmartBizCore.sharedInstance().service(of: ThingBusinessGroupProtocol.self) as? ThingBusinessGroupProtocol
                let deviceList = impl?.deviceListForCurrentSpace() ?? []
                params["deviceList"] = deviceList as AnyObject
            }
    
    
            // 创建服务
            self.deleteService = ThingGroupServiceMaker.groupServiceMaker(withBuildQuery: params)
    
            // 删除
            SVProgressHUD.show()
            self.deleteService?.removeGroup?(withGroupId: group.groupId, success: {
                SVProgressHUD.dismiss()
                self.tableView.reloadData()
                return
            }, failure: { e in
                SVProgressHUD.dismiss()
                return
            })
        }
    
    }