// // IHConfigGatawayDetailView.swift // Inhealth // // Created by weclouds on 2020/8/25. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import JXSegmentedView import PKHUD protocol IHConfigGatawayDetailViewDelete : NSObjectProtocol{ //选择关联的设备 func selectIndexGetDeviceFromGateway( index : Int) //不关联的设备 func selectIndexFoundDevice( index : Int) //添加未关联的设备 func addDeviceToGateway(deviceUUid : String) //删除关联的设备 func deleteDeviceFromGateway(deviceAddress : String) //开关灯(485/cw) func setLightTurnOn(_ isTurnOn : Bool, _ device : (address: String, type: String)) //配置485灯传感器工作模式 // 参数2:自动调光 ,参数3: 检测人体 // 0: 不开启, 1: 开启 func configLightWorkModel( _ device : (address: String, type: String),_ dodgeEnable : String, _ bodyEnable : String) // //给cw/485灯配置情景模式 // func setAllLightScence(<#parameters#>) -> <#return type#> { // <#function body#> // } } class IHConfigGatawayDetailView: UIView { // var delegate:IHGatewayDetailViewDelegate? // var associatedList:[GatewayLamp]?{ // didSet{ // segmentedView.reloadData() // if let list = self.associatedList { // self.associatedCountLabel.text = "\(list.count)" // } // } // } // // var notAssociatedList:[GatewayLamp]?{ // didSet{ // segmentedView.reloadData() // } // } // // var gateway : GatewayData?{ // didSet{ // if let gateway = self.gateway { // if let gatewayName = gateway.network_name,let gatewayId = gateway.gatewayId { // self.gatewayNameLabel.text = gatewayName.isBlanck == false ? gatewayName : "网关" // self.gatewayIdLabel.text = "网关 · " + gatewayId // } // } // // } // } weak var delete : IHConfigGatawayDetailViewDelete? // let associateGatewayView = IHAssociateGatewayView() var assciateDeviceCallBack : (([(address : String ,type : String)]) -> Void)? var notAssciateDeviceCallBack : (([String]) -> Void)? // var deviceList : [(address : String ,type : String)]? @IBOutlet weak var gatewayNameLabel: UILabel! @IBOutlet weak var gatewayIdLabel: UILabel! @IBOutlet weak var associatedCountLabel: UILabel! @IBOutlet weak var listContentView: UIView! @IBOutlet weak var rightLayoutConstraint: NSLayoutConstraint! var gateway : gatewayInfor?{ didSet{ if let localGateway = self.gateway{ self.gatewayNameLabel.text = "未命名" self.gatewayIdLabel.text = "GateWay . " + localGateway.SN! } } } var associatedDeviceList : [(address : String ,type : String)]?{ didSet{ segmentedView.reloadData() } } var notAssociateDeviceList : [String]?{ didSet{ segmentedView.reloadData() } } let titles = ["关联", "未关联"] var currentIndex : Int? = 0 var totalItemWidth: CGFloat = 0 //总长度 var segmentedDataSource:JXSegmentedBaseDataSource? var segmentedView = JXSegmentedView() lazy var listContainerView: JXSegmentedListContainerView! = { return JXSegmentedListContainerView(dataSource: self) }() override func awakeFromNib() { super.awakeFromNib() //添加设备后返回刷新 NotificationCenter.default.addObserver(self, selector: #selector(updataNotAssciateDevice), name: NSNotification.Name(KNotifiAddDeviceToGateway), object: nil) //删除设备后返回刷新 NotificationCenter.default.addObserver(self, selector: #selector(updataAssciateDevice), name: NSNotification.Name(KNotifiDeleteDeviceFromGateway), object: nil) //获取相关的设备 NotificationCenter.default.addObserver(self, selector: #selector(getAssciateDevice), name: NSNotification.Name(KNonitifiGetAsssociatedDevices), object: nil) //获取未关联的设备 NotificationCenter.default.addObserver(self, selector: #selector(getNotAssciateDevice), name: NSNotification.Name(KNonitifiGetNotAsssociatedDevices), object: nil) //开/关灯返回的状态 NotificationCenter.default.addObserver(self, selector: #selector(turnOnLightFalse), name: NSNotification.Name(KNotifiTurnOnLightFalse), object: nil) //保存设备的位置在本地 // let defaults = UserDefaults.standard // let locationArr = defaults.object(forKey: "allLocation") // if locationArr == nil { // //都是16进制数 // let blackboardLightLocation = ["10","11","12"] // let classLightLocation = ["20","21","22","23","24","25","26","27","28"] // let curtainLocation = ["01","02"] // var allLocation = [[String : [String]]]() // allLocation.append(["blackboardLightLocation" : blackboardLightLocation]) // allLocation.append(["classLightLocation" : classLightLocation]) // allLocation.append(["curtainLocation" : curtainLocation]) // defaults.set(allLocation, forKey: "allLocation") // defaults.synchronize() // } cinfigSegmentdView() } deinit { log.debug("IHConfigGatawayDetailView销毁") NotificationCenter.default.removeObserver(self) } //关开灯返回更新状态 @objc private func turnOnLightFalse(noti : Notification){ let oldDevice = noti.userInfo?["turnOn"] as? (address : String,type : String,turnOn : 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"] if oldDevice!.address == address && oldDevice!.turnOn == true && oldDevice!.type == "31"{ newDevice.remove(at: index) newDevice.append(["address" : oldDevice!.address,"type" : oldDevice!.type,"bright" : "50","color" : "10"]) break } if oldDevice!.address == address && oldDevice!.turnOn == false && oldDevice!.type == "31"{ newDevice.remove(at: index) newDevice.append(["address" : oldDevice!.address,"type" : oldDevice!.type,"bright" : "0","color" : "10"]) break } if oldDevice!.address == address && oldDevice!.turnOn == true && (oldDevice!.type == "04" || oldDevice!.type == "01"){ newDevice.remove(at: index) newDevice.append(["address" : oldDevice!.address,"type" : oldDevice!.type,"bright" : "255","color" : "255"]) break } if oldDevice!.address == address && oldDevice!.turnOn == false && (oldDevice!.type == "04" || oldDevice!.type == "01"){ newDevice.remove(at: index) newDevice.append(["address" : oldDevice!.address,"type" : oldDevice!.type,"bright" : "0","color" : "255"]) break } } defaults.set(newDevice, forKey: "saveDeviceList") defaults.synchronize() //刷新列表 DispatchQueue.main.async { self.segmentedView.reloadData() } } } @objc private func getAssciateDevice(noti : Notification){ let deviceList = noti.userInfo?["associatedDevice"] as? [(address : String,type : String)] if let deviceList = deviceList { self.associatedCountLabel.text = "\(deviceList.count)" //保存新数据在本地 saveDeviceToPlist(deviceList: deviceList) //1. 保存485灯传感器的工作模式 //2 . cw/485灯配置情景模式 configDeviceWorkModel(deviceList: deviceList) self.associatedDeviceList = deviceList } } //2. 给新的灯配置模式 //配置485灯传感器工作模式 private func configDeviceWorkModel(deviceList :[(address : String ,type : String)]){ let defults = UserDefaults.standard let deviceArr = defults.object(forKey: "deviceWorkModel") as? [[String : String]] //(address,type) if deviceList.count > 0 { if deviceArr != nil { //来新的了 for subDevice in deviceList { var i = 0 for subDic in deviceArr! { i = i + 1 let address = subDic["address"]! if subDevice.address == address { break } if i == deviceArr!.count && subDevice.address != address && subDevice.type == "31"{ //485的面板灯 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.delete?.configLightWorkModel(subDevice, "1", "1") } } } } }else{ //第一次 for subDevice in deviceList { if subDevice.type == "31"{ //485的面板灯 DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.delete?.configLightWorkModel(subDevice, "1", "1") } } } } }else{ //没有值 defults.set(nil, forKey: "deviceWorkModel") defults.synchronize() } } //更新后的数据保存在本地 //绑定的与解绑的都来这里 //1. 保存灯(address,type,bright,color) private func saveDeviceToPlist(deviceList :[(address : String ,type : String)]){ let defults = UserDefaults.standard if deviceList.count > 0 { //保存灯(address,type,bright,color) let deviceArr = defults.object(forKey: "saveDeviceList") as? [[String : String]] if deviceArr != nil{ //找新的 var newDeviceList = [[String : String]]() newDeviceList = deviceArr! if deviceList.count > deviceArr!.count{ //1.新增的 - 绑定 bindedDevice(newDeviceList: newDeviceList, deviceList: deviceList, deviceArr: deviceArr!, defults: defults) }else{ //2. 减少的 - 解绑 reduceDevice(deviceList: deviceList, defults: defults) } // if newDeviceList.count > 0 { // for subDevice in deviceList{ // var i = 0 // for saveDevice in deviceArr! { // i = i + 1 // let address = saveDevice["address"]! // if address == subDevice.address{ // break // } // if i == deviceArr!.count && subDevice.type == "31" && address != subDevice.address{ // newDeviceList.append(["address" : subDevice.address,"type" : subDevice.type,"bright" : "50","color" : "10"]) // } // if i == deviceArr!.count && (subDevice.type == "04" || subDevice.type == "01" ) && address != subDevice.address { // newDeviceList.append(["address" : subDevice.address,"type" : subDevice.type,"bright" : "50","color" : "255"]) // } // } // } // } // // defults.set(newDeviceList, forKey: "saveDeviceList") // defults.synchronize() }else{ //第一次 var saveDeviceList = [[String : String]]() for device in deviceList { //485灯的亮度: 0-50 ,cw灯的亮度: 0-255 //485灯的色温 : 0-10, cw灯的色温: 0-255 if device.type == "31"{ //485 saveDeviceList.append(["address" : device.address,"type" : device.type,"bright" : "50","color" : "10"]) } if device.type == "04" || device.type == "01"{ saveDeviceList.append(["address" : device.address,"type" : device.type,"bright" : "255","color":"255"]) } } defults.set(saveDeviceList, forKey: "saveDeviceList") defults.synchronize() } }else{ //没有值时 defults.set(nil, forKey: "saveDeviceList") defults.synchronize() } } //相关联 -- 解绑后,会减少设备了 private func reduceDevice( deviceList : [(address : String ,type : String)],defults : UserDefaults ){ //deviceArr是保存在本地 var newDeviceArr = [[String : String]]() for subDevice in deviceList{ newDeviceArr.append(["address" : subDevice.address,"type" : subDevice.type,"bright" : "50","color" : "10"]) } defults.set(newDeviceArr, forKey: "saveDeviceList") defults.synchronize() } //绑定 - 新添加的 private func bindedDevice(newDeviceList :[[String : String]], deviceList : [(address : String ,type : String)],deviceArr : [[String : String]],defults : UserDefaults ){ //deviceArr是保存在本地 var newDeviceList = newDeviceList if newDeviceList.count > 0 { for subDevice in deviceList{ var i = 0 for saveDevice in deviceArr{ i = i + 1 let address = saveDevice["address"]! if address == subDevice.address{ break } if i == deviceArr.count && subDevice.type == "31" && address != subDevice.address{ newDeviceList.append(["address" : subDevice.address,"type" : subDevice.type,"bright" : "50","color" : "10"]) } if i == deviceArr.count && (subDevice.type == "04" || subDevice.type == "01" ) && address != subDevice.address { newDeviceList.append(["address" : subDevice.address,"type" : subDevice.type,"bright" : "50","color" : "255"]) } } } } defults.set(newDeviceList, forKey: "saveDeviceList") defults.synchronize() } @objc private func getNotAssciateDevice(noti : Notification){ let deviceList = noti.userInfo?["notAssociatedDevice"] as? [String] if let deviceList = deviceList { self.notAssociateDeviceList = deviceList } } @objc private func updataAssciateDevice(){ self.delete?.selectIndexGetDeviceFromGateway(index: self.currentIndex!) } @objc private func updataNotAssciateDevice(){ self.delete?.selectIndexFoundDevice(index : self.currentIndex!) } override func layoutSubviews() { super.layoutSubviews() segmentedView.frame = CGRect(x: 20, y: 0, width: totalItemWidth, height: 30) listContainerView.frame = CGRect(x: 0, y: 40, width: KSCREENWIDTH, height: self.listContentView.bounds.size.height - 40) if KNavBarHeight == 64 { self.rightLayoutConstraint.constant = 60 } } } extension IHConfigGatawayDetailView{ func cinfigSegmentdView() { totalItemWidth = UIScreen.main.bounds.size.width - 20*2 backgroundColor = .white let titleDataSource = JXSegmentedTitleDataSource() titleDataSource.itemContentWidth = totalItemWidth/CGFloat(titles.count) titleDataSource.titles = titles titleDataSource.titleNormalFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 13)! titleDataSource.titleSelectedFont = UIFont(name: Alibaba_PuHuiTi_Bold, size: 13)! titleDataSource.isTitleMaskEnabled = true titleDataSource.titleNormalColor = UIColor(hexString: "#657085")! titleDataSource.titleSelectedColor = UIColor(hexString: "#FFFFFF")! titleDataSource.itemSpacing = 0 // titleDataSource.itemContentWidth = 120 //titleDataSource.isItemSpacingAverageEnabled = false segmentedDataSource = titleDataSource segmentedView.dataSource = segmentedDataSource segmentedView.layer.masksToBounds = true segmentedView.layer.cornerRadius = 15 segmentedView.backgroundColor = UIColor(hexString: "#F6F8F7") segmentedView.delegate = self self.listContentView.addSubview(segmentedView) let indicator = JXSegmentedIndicatorBackgroundView() indicator.indicatorHeight = 25 indicator.backgroundWidthIncrement = -10 // #05CFAB indicator.indicatorColor = UIColor(hexString: "#573F95")! indicator.layer.shadowColor = UIColor(hexString: "#573F95")!.cgColor indicator.layer.shadowRadius = 3 indicator.layer.shadowOffset = CGSize.zero indicator.layer.shadowOpacity = 0.7 segmentedView.indicators = [indicator] segmentedView.listContainer = listContainerView self.listContentView.addSubview(listContainerView) self.segmentedView.contentScrollView = self.listContainerView.scrollView } } extension IHConfigGatawayDetailView : JXSegmentedViewDelegate{ func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { // if let delegate = self.delegate { // delegate.equipmentlistViewdidSelectedItemAt(index) // } // self.associateGatewayView.delegate = self // self.currentIndex = index if index == 0 { HUD.flash(.progress) self.delete?.selectIndexGetDeviceFromGateway(index: index) self.assciateDeviceCallBack = { (deviceList : [(address : String ,type : String)]) in //保存新数据在本地 self.saveDeviceToPlist(deviceList: deviceList) //保存485灯传感器的工作模式 self.configDeviceWorkModel(deviceList: deviceList) self.associatedCountLabel.text = "\(deviceList.count)" self.associatedDeviceList = deviceList self.segmentedDataSource?.reloadData(selectedIndex: self.currentIndex!) // if deviceList.count > 0{ // // // // // self.deviceList = deviceList // // view.associatedDeviceList = deviceList // //// self.associatedCountLabel.text = "\(deviceList.count)" // // self.deviceList = deviceList // //// view.associatedDeviceList = deviceList // } } }else{ HUD.flash(.progress) self.delete?.selectIndexFoundDevice(index: index) self.notAssciateDeviceCallBack = { (deviceUUidList : [String]) in self.notAssociateDeviceList = deviceUUidList // if deviceUUidList.count > 0{ // //view.notAssociatedDeviceList = deviceUUidList // // //// self.segmentedDataSource?.reloadData(selectedIndex: self.currentIndex!) // } } // self.delete?.selectIndexFoundDevice(index: index) // self.notAssciateDeviceCallBack = { (deviceList : [(address : String ,type : String)]) in // // if deviceList.count > 0{ // // self.associatedCountLabel.text = "\(deviceList.count)" // // view.notAssociatedDeviceList = deviceList // // } // // } } // self.currentIndex = index // if let dotDataSource = segmentedDataSource as? JXSegmentedDotDataSource { // //先更新数据源的数据 // dotDataSource.dotStates[index] = false // //再调用reloadItem(at: index) // segmentedView.reloadItem(at: index) // } } } extension IHConfigGatawayDetailView : JXSegmentedListContainerViewDataSource{ func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int { if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource { return titleDataSource.dataSource.count } return 0 } func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate { let view = IHAssociateGatewayView() view.delegate = self view.currentIndex = index // associateGatewayView.delegate = self // associateGatewayView.currentIndex = index // self.associateGatewayView.currentIndex = index if index == 0 { if let associatedDeviceList = self.associatedDeviceList { self.associatedCountLabel.text = "\(associatedDeviceList.count)" view.associatedDeviceList = self.associatedDeviceList } // if let associate = self.associatedList { // view.associatedList = associate // } // if let deviceList = self.deviceList{ // self.associateGatewayView.associatedDeviceList = deviceList // } // // HUD.flash(.progress) // // self.delete?.selectIndexGetDeviceFromGateway(index: index) // self.assciateDeviceCallBack = { (deviceList : [(address : String ,type : String)]) in // if deviceList.count > 0{ // self.associatedCountLabel.text = "\(deviceList.count)" //// self.deviceList = deviceList // view.associatedDeviceList = deviceList // // } // // } }else{ if let notAssociateDeviceList = self.notAssociateDeviceList { view.notAssociatedDeviceList = notAssociateDeviceList } // if let notassciate = self.notAssociatedList { // view.notAssociatedList = notassciate // } // if let deviceList = self.deviceList{ // self.associateGatewayView.notAssociatedDeviceList = deviceList // } // HUD.flash(.progress) // self.delete?.selectIndexFoundDevice(index: index) // self.notAssciateDeviceCallBack = { (deviceUUidList : [String]) in // // if deviceUUidList.count > 0{ // view.notAssociatedDeviceList = deviceUUidList // // } // // } // if let notassciateList = self.deviceList{ // view.notAssociatedDeviceList = notassciateList // } } return view } } extension IHConfigGatawayDetailView : IHAssociateGatewayViewDelete{ //开关灯(485/cw) func controlightOn(_ isTurnON: Bool, _ device: (address: String, type: String)) { delete?.setLightTurnOn(isTurnON, device) } //删除设备 func deviceDisconteGateway(_ deviceAddress: String) { delete?.deleteDeviceFromGateway(deviceAddress: deviceAddress) } //设备添加到网关 func deviceConnectGateway(_ deviceUUId: String) { HUD.flash(.progress) delete?.addDeviceToGateway(deviceUUid: deviceUUId) } //搜索相关连的和不相关连的设备 func emptyViewSearchDataSegment(_ index: Int) { if index == 0{ delete?.selectIndexGetDeviceFromGateway(index: index) }else{ delete?.selectIndexFoundDevice(index: index) } } // func deviceConnectGateway(_ deviceId: String) { // guard let gateway = self.gateway else { return } // if let delegate = self.delegate { // delegate.deviceConnectGateway(deviceId, gatewayId: gateway.gatewayId!) // } // } // func deviceDisconteGateway(_ deviceId: String) { // guard let gateway = self.gateway else { return } // if let delegate = self.delegate { // delegate.deviceDisconteGateway(deviceId, gatewayId: gateway.gatewayId!) // } // } // func controlightOn(_ isTurnON: Bool, lampId: String) { // guard let gateway = self.gateway else { return } // if let delegate = self.delegate { // delegate.controlightOn(lampId: lampId, gatewayId: gateway.gatewayId!, status: isTurnON == false ? "0" : "1") // } // } }