// // IHGatewayDetailManager.swift // Inhealth // // Created by weclouds on 2020/4/9. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD class IHGatewayDetailManager: NSObject ,IHViewManagerProtocolDelegate{ // private var vc = UIViewController() private weak var vc : UIViewController? lazy var mainView: IHGatewayDetailView = { let mainView = Bundle.main.loadNibNamed("IHGatewayDetailView", owner: nil, options: nil)?.last as! IHGatewayDetailView mainView.delegate = self return mainView }() func bindController(_ vc: UIViewController) { self.vc = vc let aVC = self.vc as! IHGatewayDetailVCtr if let gateway = aVC.gateway { self.mainView.gateway = aVC.gateway if let title = gateway.network_name { aVC.navigationBarTitle = title.isBlanck == false ? title : "Gateway" } } requestNetworkDevice() createUI() } func createUI() { mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT ) self.vc?.view.addSubview(mainView) setNavigationBar() } func setNavigationBar() { //导航栏的渲染方式 let setItem = UIBarButtonItem(image: UIImage(named: "设置")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(editGateway)) self.vc?.navigationItem.rightBarButtonItem = setItem } @objc func editGateway() { let aVC = self.vc as! IHGatewayDetailVCtr let eqment = IHNewEquipmentVCtr() eqment.roomStyle = .gatewayEdit eqment.gateway = aVC.gateway eqment.navigationBarTitle = aVC.gateway?.network_name ?? "Gateway" self.vc?.navigationController?.pushViewController(eqment, animated: true) } func requestNetworkDevice() { HUD.show(.progress) HUD.hide(afterDelay: 15) { (isSuccess) in g_showHUD("请求超时") } let aVC = self.vc as! IHGatewayDetailVCtr guard let gateway = aVC.gateway else { return } let queue = DispatchQueue(label: "com.custom.thread.requestNetworkDevice", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent) let group = DispatchGroup() var associatelist:[GatewayLamp]? = nil var notassociatelist :[GatewayLamp]? = nil queue.async(group: group, qos: .default, flags: []) { // group.enter() IHGatewayService.share.getNetworkDevice(floorId: gateway.floorId!, roomId: gateway.roomId!, gatewayid: gateway.gatewayId!, bindLamp: "1", requestSuccess: { (list) in HUD.hide() // group.leave() associatelist = list if let list = associatelist{ self.mainView.associatedList = list } }) { HUD.hide() // group.leave() associatelist = nil } } // 请求网关列表 queue.async(group: group, qos: .default, flags: []) { group.enter() IHGatewayService.share.getNetworkDevice(floorId: gateway.floorId!, roomId: gateway.roomId!, gatewayid: gateway.gatewayId!, bindLamp: "2", requestSuccess: { (list) in // group.leave() notassociatelist = list HUD.hide() if let list = notassociatelist{ self.mainView.notAssociatedList = list } }) { HUD.hide() // group.leave() notassociatelist = nil } //回到主线程 // group.notify(queue: DispatchQueue.main) { // // // // } } } } extension IHGatewayDetailManager:IHGatewayDetailViewDelegate{ func emptyViewSearchDataSegment(_ index: Int, floorId: String, roomId: String, gatewayId: String) { if index == 0 { HUD.show(.progress) HUD.hide(afterDelay: 15) { (isSuccess) in g_showHUD("请求超时") } IHGatewayService.share.getNetworkDevice(floorId: floorId, roomId: roomId, gatewayid: gatewayId, bindLamp: "1", requestSuccess: { (list) in HUD.hide() self.mainView.associatedList = list }) { HUD.hide() } }else{ HUD.show(.progress) HUD.hide(afterDelay: 15) { (isSuccess) in g_showHUD("请求超时") } IHGatewayService.share.getNetworkDevice(floorId: floorId, roomId: roomId, gatewayid: gatewayId, bindLamp: "2", requestSuccess: { (list) in HUD.hide() self.mainView.notAssociatedList = list }) { HUD.hide() } } } func deviceConnectGateway(_ deviceId: String, gatewayId: String) { IHGatewayService.share.bindGateway(deviceId, gatewayId: gatewayId, bind: "1") { (isSuccess) in self.requestNetworkDevice() } } func deviceDisconteGateway(_ deviceId: String, gatewayId: String) { IHGatewayService.share.bindGateway(deviceId, gatewayId: gatewayId, bind: "0") { (isSuccess) in self.requestNetworkDevice() } } func controlightOn(lampId: String, gatewayId: String, status: String) { IHGatewayService.share.controlLampStatus(lampId, gatewayId: gatewayId, status: status) { (isSuccess) in //self.requestNetworkDevice() //控制灯的时候不需要刷新界面 if isSuccess == false { NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHAssociatedCellCommandFailure), object: nil) } } } }