// // IHLightCommandView.swift // Inhealth // // Created by weclouds on 2020/8/21. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD protocol IHLightCommandViewDelegate : NSObjectProtocol { // func regulateLampControlSwitch(_ status: String) // // func regulateLampControlBrightness(_ light: String) // // func regulateLampControlColorTemperature(_ color: String) // //学校的 - 亮度 // func schoolRegulateLampControlBrightness(_ light: String) // // //学校的 - 色温 // func schoolRegulateLampControlColorTemperature(_ color: String) // //学校的单个灯 - 开关 // func schoolRegulateLampControlSwitch(_ status: String) // // func regulateLampControlColor(_ red: String, green: String, blue: String) //不是485的灯 func setLampCWBrightnessAndColorValue(device : (address : String, type : String,location: String),brightness : String, color : String,isColor : Bool) //开关灯 func setDeviceTurnOn(_ isTurnOn : Bool, _ device : (address: String, type: String,location : String)) } class IHLightCommandView: UIView { //加--- // var isClassRoom :Bool? { // didSet{ // if let isClassRoom = isClassRoom{ // if isClassRoom == true { // self.colorBtn.isHidden = true // self.whiteBtn.isHidden = true // let x = (KSCREENWIDTH - 50) * 0.5 // self.closeBtn.frame = CGRect(x: x, y: 0, width: 50, height: 75) // } // } // // } // } // var schoolLight : ShoolDeviceList?{ // didSet{ // guard let schoolLight = schoolLight else { // return // } // // if let colorTemp = schoolLight.color,let bright = schoolLight.light ,let status = schoolLight.status{ // // whiteView.lightMode = "5" // whiteView.model = schoolLight.model // whiteView.currentBrightness = "\(bright)" // whiteView.currentColorTemp = "\(colorTemp)" // colorView.currentColor = ColorStruct(Red: 0 , Green: 0, Blue: 0) // if status == "0" { // closeBtn.isSelected = false // closeView.isHidden = false // colorView.isHidden = true // whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = false // }else if status == "1"{ // // if Intermediate.isRegulateColor == "0" { // closeBtn.isSelected = true // closeView.isHidden = true // colorView.isHidden = true // whiteView.isHidden = false // whiteBtn.isSelected = true // colorBtn.isSelected = false // }else if Intermediate.isRegulateColor == "1"{ // closeBtn.isSelected = true // closeView.isHidden = true // colorView.isHidden = false // whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = true // } // // // } // } // // } // } // var delegate : IHLampControlViewDelegate? // var lightData : LightInfodata?{ // didSet{ // if let lightData = self.lightData { // if let model = lightData.model ,let colorTemp = lightData.color,let r = lightData.colorR,let g = lightData.colorG,let b = lightData.colorB,let bright = lightData.light ,let status = lightData.status{ // setButtonFrame(model) // whiteView.lightMode = model // whiteView.currentBrightness = "\(bright)" // whiteView.currentColorTemp = "\(colorTemp)" // colorView.currentColor = ColorStruct(Red: r , Green: g, Blue: b) // if status == "0" { // closeBtn.isSelected = false // closeView.isHidden = false // colorView.isHidden = true // whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = false // }else if status == "1"{ // // if Intermediate.isRegulateColor == "0" { // closeBtn.isSelected = true // closeView.isHidden = true // colorView.isHidden = true // whiteView.isHidden = false // whiteBtn.isSelected = true // colorBtn.isSelected = false // }else if Intermediate.isRegulateColor == "1"{ // closeBtn.isSelected = true // closeView.isHidden = true // colorView.isHidden = false // whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = true // } // // // } // } // } // } // } var schollDeviceInfor : (address: String, type: String, location: String)?{ didSet{ if let deviceInfor = schollDeviceInfor{ setLightAtribut(deviceInfor: deviceInfor) // HUD.flash(.label("还没有亮度与色温值"), delay: 0.6) // if let status = deviceInfor.status,let light = deviceInfor.light,let color = deviceInfor.color{ // if status == "0"{ // //关灯 // colorView.isHidden = false // whiteView.isHidden = true // colorView.isHidden = true // }else{ // //whiteView,colorView二个都可以亮度与色温 // colorView.isHidden = true // whiteView.isHidden = false // colorView.isHidden = true // // whiteView.currentBrightness = "\(light)" // whiteView.currentColorTemp = "\(color)" // } // } } } } //初始化灯的值 private func setLightAtribut(deviceInfor : (address: String, type: String, location: String)){ //获取当前灯的状态 let defaults = UserDefaults.standard let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]] if let arrDevice = arrDevice { if arrDevice.count > 0 { for subDic in arrDevice { let address = subDic["address"]! let bright = subDic["bright"] let color = subDic["color"] if deviceInfor.address == address { if bright == "0" { //关灯 colorView.isHidden = true whiteView.isHidden = true closeView.isHidden = false closeBtn.isSelected = false }else{ colorView.isHidden = true whiteView.isHidden = false closeView.isHidden = true closeBtn.isSelected = true //0-100 //亮度cw : 0-255, 485 : 0-50 let brightI = Int(bright!)! let colorI = Int(color!)! var lastBright = 0 var lastColor = 0 if deviceInfor.type == "31" { //485 lastBright = brightI * 100 / 50 lastColor = colorI * 6000 / 10 }else{ lastBright = brightI * 100 / 255 lastColor = colorI * 6000 / 255 } whiteView.currentBrightness = "\(lastBright)" //3000-6000 //色温cw : 0-255, 485 : 0-10 whiteView.currentColorTemp = "\(lastColor)" } break } } } } } private let gradientLayer = IHRadialGradientLayer() weak var delegate : IHLightCommandViewDelegate? // @IBOutlet weak var buttonBar: UIView! // @IBOutlet weak var whiteBtn: UIButton! @IBOutlet weak var closeBtn: UIButton! // @IBOutlet weak var colorBtn: UIButton! lazy var closeView: IHLightCloseView = { let closeView = Bundle.main.loadNibNamed("IHLightCloseView", owner: nil, options: nil)?.first as! IHLightCloseView closeView.isHidden = true return closeView }() lazy var whiteView: IHLightBrightView = { let whiteView = Bundle.main.loadNibNamed("IHLightBrightView", owner: nil, options: nil)?.first as! IHLightBrightView whiteView.isHidden = true return whiteView }() lazy var colorView: IHLightColorView = { let colorView = Bundle.main.loadNibNamed("IHLightColorView", owner: nil, options: nil)?.first as! IHLightColorView colorView.isHidden = true return colorView }() override func awakeFromNib() { super.awakeFromNib() //设置灯的亮度与色温后返回更新 NotificationCenter.default.addObserver(self, selector: #selector(updateLightBrightAndColor), name: NSNotification.Name(KNotifiSetLightBrightAndColor), object: nil) //灯的开关 NotificationCenter.default.addObserver(self, selector: #selector(updateLightState), name: NSNotification.Name(KNotifiDeviceTurnOn), object: nil) setupUI() } deinit { log.debug("IHLightCommandView销毁") NotificationCenter.default.removeObserver(self) } //开灯后更新界面 @objc private func updateLightState(noti : Notification){ let deviceState = noti.userInfo?["turnOn"] as? (oldDevice :(address: String, type: String,location : String),state : Bool,turnOn : Bool) //state是成功还是失败 if deviceState?.state == true{ //turnOn == true 为开,否则是关 turnOnLightState(operationLightState: deviceState!.turnOn) //刷新 self.schollDeviceInfor = deviceState?.oldDevice } } //关开灯 private func turnOnLightState(operationLightState : Bool){ //更改此light的状态 let defaults = UserDefaults.standard let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]] var newDevice = [[String : String]]() if let arrDevice = arrDevice { newDevice = arrDevice for (index , subDic) in arrDevice.enumerated() { let address = subDic["address"]! //let bright = subDic["bright"] let color = subDic["color"] if self.schollDeviceInfor!.address == address && operationLightState == true && self.schollDeviceInfor!.type == "31"{ newDevice.remove(at: index) newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "50","color" : color!]) break } if self.schollDeviceInfor!.address == address && operationLightState == false && self.schollDeviceInfor!.type == "31"{ newDevice.remove(at: index) newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "0","color" : color!]) break } if self.schollDeviceInfor!.address == address && operationLightState == true && (self.schollDeviceInfor!.type == "04" || self.schollDeviceInfor!.type == "01"){ newDevice.remove(at: index) newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "255","color" : color!]) break } if self.schollDeviceInfor!.address == address && operationLightState == false && (self.schollDeviceInfor!.type == "04" || self.schollDeviceInfor!.type == "01"){ newDevice.remove(at: index) newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "0","color" : color!]) break } } defaults.set(newDevice, forKey: "saveDeviceList") defaults.synchronize() //刷新列表 // DispatchQueue.main.async { // self.segmentedView.reloadData() // } } } @objc private func updateLightBrightAndColor(){ if let deviceInfor = schollDeviceInfor{ setLightAtribut(deviceInfor: deviceInfor) } } override func layoutSubviews() { super.layoutSubviews() self.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) closeView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) whiteView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) colorView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) } func setupUI() { // whiteBtn.layoutButton(style: .Top, imageTitleSpace: 10) closeBtn.layoutButton(style: .Top, imageTitleSpace: 10) //colorBtn.layoutButton(style: .Top, imageTitleSpace: 10) insertSubview(closeView, at: 0) insertSubview(whiteView, at: 0) insertSubview(colorView, at: 0) callbackAction() } @IBAction func closeAction(_ sender: UIButton) { sender.isSelected = !sender.isSelected //这里不需要立即处理 刷新之后才处理 if sender.isSelected == true { closeView.isHidden = true colorView.isHidden = true whiteView.isHidden = false // whiteBtn.isSelected = true // colorBtn.isSelected = false }else{ closeView.isHidden = false colorView.isHidden = true whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = false } //let device = (address : self.schollDeviceInfor!.address, type : self.schollDeviceInfor!.type) delegate?.setDeviceTurnOn(sender.isSelected, self.schollDeviceInfor!) // if let delegate = self.delegate { // if isClassRoom == true { // delegate.schoolRegulateLampControlSwitch(sender.isSelected == true ? "1" : "0") // }else{ // delegate.regulateLampControlSwitch(sender.isSelected == true ? "1" : "0") // } // // } } @IBAction func whiteAction(_ sender: UIButton) { if closeBtn.isSelected == false { g_showHUD("设备已离线") return } //sender.isSelected = !sender.isSelected closeView.isHidden = true colorView.isHidden = true whiteView.isHidden = false // whiteBtn.isSelected = true // colorBtn.isSelected = false closeBtn.isSelected = true } @IBAction func colorAction(_ sender: UIButton) { // sender.isSelected = !sender.isSelected if closeBtn.isSelected == false { g_showHUD("设备已离线") return } closeView.isHidden = true colorView.isHidden = false whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = true closeBtn.isSelected = true } func callbackAction() { whiteView.lampControlTrunoff = {[unowned self] in self.closeView.isHidden = false self.colorView.isHidden = true self.whiteView.isHidden = true // self.whiteBtn.isSelected = false // self.colorBtn.isSelected = false } whiteView.brightnessCallback = { [unowned self] (bright) in //100-0 log.debug("----亮度") guard let deviceInfor = self.schollDeviceInfor else { return } //转换 //亮度cw : 0-255, 485 : 0-50 var lastBright = 0 let brightI = Int(bright)! var lastColor = 0 let colorI = Int(self.whiteView.currentColorTemp!)! if deviceInfor.type == "31" { //485 lastBright = brightI * 50 / 100 lastColor = colorI * 10 / 6000 }else{ lastBright = brightI * 255 / 100 lastColor = colorI * 255 / 6000 } self.delegate?.setLampCWBrightnessAndColorValue(device: deviceInfor , brightness: "\(lastBright)", color: "\(lastColor)",isColor: false) // if let delegate = self.delegate { // if self.isClassRoom == true { // //学校的 // delegate.schoolRegulateLampControlBrightness(bright) // }else{ // //酒店的 // delegate.regulateLampControlBrightness(bright) // } // // } } whiteView.colorTempCallback = {[unowned self] (color) in //6000-3000 0-255 log.debug("---色温") guard let deviceInfor = self.schollDeviceInfor else { return } //转换 //色温cw : 0-255, 485 : 0-10 var lastBright = 0 let brightI = Int(self.whiteView.currentBrightness!)! var lastColor = 0 let colorI = Int(color)! if deviceInfor.type == "31" { //485 lastBright = brightI * 50 / 100 lastColor = colorI * 10 / 6000 }else{ lastBright = brightI * 255 / 100 lastColor = colorI * 255 / 6000 } // var lastColor = 0 // let colorI = Int(color)! // if deviceInfor.type == "31" { // //485 // lastColor = colorI * 10 / 6000 // }else{ // lastColor = colorI * 255 / 6000 // } self.delegate?.setLampCWBrightnessAndColorValue(device: deviceInfor, brightness: "\(lastBright)", color: "\(lastColor)",isColor : true) // if let delegate = self.delegate { // if self.isClassRoom == true{ // //学校的 // delegate.schoolRegulateLampControlColorTemperature(color) // }else{ // //酒店的 // if self.colorBtn.isHidden == false{ // delegate.regulateLampControlColorTemperature(color) // } // } // } } } // colorView.paletteComplition = {(rgb) in // if let delegate = self.delegate { // delegate.regulateLampControlColor("\(rgb.Red!)", green: "\(rgb.Green!)", blue: "\(rgb.Blue!)") // } // } // } } extension IHLightCommandView { func setButtonFrame(_ model:String) { if model == "0" || model == "1" || model == "4" || model == "5" { //self.colorBtn.isHidden = true let beginX = (KSCREENWIDTH - 50 * 2 - 90) / 2 self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75) // self.whiteBtn.frame = CGRect(x: beginX + 50 + 90, y: 0, width: 50, height: 75) }else { // self.colorBtn.isHidden = false let beginX = (KSCREENWIDTH - 50 * 3 - 50 * 2) / 2 self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75) // self.whiteBtn.frame = CGRect(x: beginX + 50 * 2, y: 0, width: 50, height: 75) // self.colorBtn.frame = CGRect(x: beginX + 50 * 4, y: 0, width: 50, height: 75) } } }