// // IHLampControlManager.swift // Inhealth // // Created by weclouds on 2020/4/17. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD class IHLampControlManager: NSObject { lazy var mainView: IHLampControlView = { let mainView = Bundle.main.loadNibNamed("IHLampControlView", owner: nil, options: nil)?.first as! IHLampControlView mainView.delegate = self return mainView }() private weak var vc : UIViewController? var schoolLightColor = 0 var schoolLightBright = 0 var isFirstColor = false var isFirstBright = false override init() { super.init() NotificationCenter.default.addObserver(self, selector: #selector(reloadNotify), name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: nil) //学校编辑了设备,更新设备信息 NotificationCenter.default.addObserver(self, selector: #selector(schoolDeviUpdateInfor), name: NSNotification.Name(kNotificationReloadEquipmentInfo), object: nil) } deinit { NotificationCenter.default.removeObserver(self) } @objc func reloadNotify(_ notif:Notification){ let lightVC = self.vc as! IHLampControlVCtr requestData((lightVC.dataSource?.id!)!) self.vc?.navigationBarTitle = notif.object as? String } //更新设备信息 @objc func schoolDeviUpdateInfor(noti : Notification){ let dic = noti.userInfo let aVC = self.vc as! IHLampControlVCtr aVC.dataSource?.name = dic?["name"] as? String } func bindController(_ vc: UIViewController) { self.vc = vc createUI() } func createUI() { let lightVC = self.vc as! IHLampControlVCtr if lightVC.isClassRoom != true { HUD.show(.progress) HUD.hide(afterDelay: 15) { (isSuccess) in // 15s超时提醒 g_showHUD("请求超时") } } self.vc!.view.addSubview(mainView) setNavigationBar() if (lightVC.isClassRoom == true){ //学校的 - 色温 // requestData((lightVC.shoolDevice!.id)!) // //加 // let lightVC = self.vc as! IHLampControlVCtr self.mainView.isClassRoom = lightVC.isClassRoom Intermediate.isRegulateColor = "0" self.mainView.schoolLight = lightVC.shoolDevice HUD.hide() }else{ //酒店的 requestData((lightVC.dataSource?.id!)!) Intermediate.isRegulateColor = "0" } } //循环 func setSchoolLightColorAndBright(color : String? ,light: String?,status: String?) { let lightVC = self.vc as! IHLampControlVCtr if light != nil && color == nil && status == nil { //亮度 lightVC.shoolDevice?.light = light } if color != nil && light == nil && status == nil { //色温 lightVC.shoolDevice?.light = light } if status != nil && color == nil && light == nil { //开关 lightVC.shoolDevice?.status = status } self.mainView.schoolLight = lightVC.shoolDevice HUD.hide() } func requestData(_ lightId:String) { IHRoomLightService.share.getLightInfo(lightId, requestSuccess: { (data) in self.mainView.lightData = data HUD.hide() }) { HUD.hide() } } } extension IHLampControlManager{ func setNavigationBar() { //导航栏的渲染方式 let searchItem = UIBarButtonItem(image: UIImage(named: "ic_history")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(mapAction1)) let setItem = UIBarButtonItem(image: UIImage(named: "设置_白")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(modeSetAction)) self.vc?.navigationItem.rightBarButtonItems = [searchItem,setItem] } @objc func modeSetAction() { let eqment = IHNewEquipmentVCtr() eqment.roomStyle = .equipmentEdit let aVC = self.vc as! IHLampControlVCtr if aVC.isClassRoom == true{ eqment.isSchool = "1" } eqment.devData = aVC.dataSource eqment.navigationBarTitle = aVC.dataSource?.name self.vc?.navigationController?.pushViewController(eqment, animated: true) } @objc func mapAction1(){ log.debug("点击了") let history = IHRoomLightHistoryVCtr() let lightVC = self.vc as! IHLampControlVCtr let lightId = lightVC.dataSource?.id! history.devId = lightId history.devType = lightVC.dataSource?.devType getLampHistory(lightVC.dataSource!) } func getLampHistory(_ devData: DevData) { let devId = devData.id HUD.show(.progress) HUD.hide(afterDelay: 20) { (isSuccess) in if isSuccess == true{ g_showHUD("请求超时") } } var energy_consumption : ReportData? var lighting_use_schedule :ReportData? let queue = DispatchQueue(label: "com.custom.thread", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent) let group = DispatchGroup() queue.async(group: group, qos: .default, flags: []) { group.enter() IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "0", date: nil, requestSuccess: { (data) in group.leave() energy_consumption = data }) { group.leave() energy_consumption = nil } } queue.async(group: group, qos: .default, flags: []) { group.enter() IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "1", date: nil, requestSuccess: { (data) in group.leave() lighting_use_schedule = data }) { group.leave() lighting_use_schedule = nil } } //回到主线程 group.notify(queue: DispatchQueue.main) { HUD.hide() if energy_consumption != nil || lighting_use_schedule != nil{ let history = IHRoomLightHistoryVCtr() history.devId = devData.id history.devType = devData.devType let lampHistory = (energy_consumption:energy_consumption,lighting_use_schedule:lighting_use_schedule) as! (energy_consumption: ReportData, lighting_use_schedule: ReportData) history.lampHistory = lampHistory self.vc?.navigationController?.pushViewController(history, animated: true) }else{ g_showHUD("未发现相关数据") } } } } extension IHLampControlManager: IHLampControlViewDelegate{ func regulateLampControlSwitch(_ status: String) { let lightVC = self.vc as! IHLampControlVCtr let lightId = lightVC.dataSource?.id! IHRoomListService.share.setLampTurn(lightId: lightId!, status: status, requestSuccess: { (isSuccess) in self.requestData(lightId!) NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) }) { } } func regulateLampControlBrightness(_ light: String) { let lightVC = self.vc as! IHLampControlVCtr let lightId = lightVC.dataSource?.id! IHRoomLightService.share.individualDimming(lightId: lightId!, light: light, requestSuccess: { (isSuccess) in self.requestData(lightId!) NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) }) { } } func regulateLampControlColorTemperature(_ color: String) { let lightVC = self.vc as! IHLampControlVCtr let lightId = lightVC.dataSource?.id! IHRoomLightService.share.setLampColor(lightId: lightId!, color: color, requestSuccess: { (isSuccess) in self.requestData(lightId!) NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) }) { } } func regulateLampControlColor(_ red: String, green: String, blue: String) { let lightVC = self.vc as! IHLampControlVCtr let lightId = lightVC.dataSource?.id! IHRoomLightService.share.setLampColorRGB(lightId!, colorR: red, colorG: green, colorB: blue, requestSuccess: { (isSuccess) in // self.requestLightInfo(lightId!) self.requestData(lightId ?? "0") NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) }) { } } //学校灯的-亮度 func schoolRegulateLampControlBrightness(_ light: String) { //color是调节的值 HUD.show(.progress) let lightVC = self.vc as! IHLampControlVCtr guard let lightId = lightVC.shoolDevice!.id else { return } IHRoomLightService.share.lightsBright(lightId: lightId, light: light) { (isSuccessStr,code) in g_showHUD(isSuccessStr) HUD.hide(afterDelay: 1.5) if code != "0000"{ let lightVC = self.vc as! IHLampControlVCtr self.setSchoolLightColorAndBright(color: nil, light: lightVC.shoolDevice?.light, status: nil) }else{ //成功 NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateBright), object: nil) self.setSchoolLightColorAndBright(color: nil, light: light, status: nil) } } } //学校灯的-色度 - 调 func schoolRegulateLampControlColorTemperature(_ color: String) { //color是调节的值 HUD.show(.progress) let lightVC = self.vc as! IHLampControlVCtr guard let lightId = lightVC.shoolDevice!.id else { return } IHRoomLightService.share.lightsColor(lightId: lightId, color: color) { (isSuccessStr,code) in // if(isSuccessStr == "") g_showHUD(isSuccessStr) HUD.hide(afterDelay: 1.5) if code != "0000"{ let lightVC = self.vc as! IHLampControlVCtr self.setSchoolLightColorAndBright(color: lightVC.shoolDevice?.color, light: nil, status: nil) }else{ //成功 NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateTempColor), object: nil) self.setSchoolLightColorAndBright(color: color, light: nil, status: nil) } } } // schoolRegulateLampControlswitch //单个灯的开关 func schoolRegulateLampControlSwitch(_ status: String) { // HUD.show(.progress) let lightVC = self.vc as! IHLampControlVCtr guard let lightId = lightVC.shoolDevice!.id else { return } IHRoomLightService.share.setSchoolLightOn_off(lightId: lightId, status: status) { (isSuccessStr,code) in g_showHUD(isSuccessStr) HUD.hide(afterDelay: 1.5) if(code == "0000"){ //成功 NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateSwitch), object: nil) self.setSchoolLightColorAndBright(color: nil, light: nil, status: status) }else{ //开/关灯失败 - 保持原来的状态 let lightVC = self.vc as! IHLampControlVCtr self.setSchoolLightColorAndBright(color: nil, light: nil, status: lightVC.shoolDevice?.status) } } // IHRoomLightService.share.lightsColor(lightId: lightId, color: color) { (isSuccessStr) in //// if(isSuccessStr == "") // // self.setSchoolLightColorAndBright(color: color, light: nil) // } } }