123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- //
- // 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")
- // }
- // }
-
-
- }
|