// // IHConfigGatawayDetailManager.swift // Inhealth // // Created by weclouds on 2020/8/25. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD let KNonitifiGetAsssociatedDevices = "KNonitifiGetAsssociatedDevices" let KNonitifiGetNotAsssociatedDevices = "KNonitifiGetNotAsssociatedDevices" class IHConfigGatawayDetailManager: NSObject { private lazy var mainView : IHConfigGatawayDetailView = { let mainView = Bundle.main.loadNibNamed("IHConfigGatawayDetailView", owner: nil, options: nil)?.last as! IHConfigGatawayDetailView mainView.delete = self return mainView }() lazy var clientSocket: GCDAsyncUdpSocket? = { var mainQueue = DispatchQueue.main var clientSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: mainQueue) return clientSocket }() private var deviceList : [(address: String, type: String)]? var connectCallback: (()->Void)? private weak var vc : IHConfigGatawayDetailCtr? func bindController(_ vc : IHConfigGatawayDetailCtr) { self.vc = vc vc.view.addSubview(mainView) mainView.gateway = vc.gateway //扫描添加过的和未添加的 //scanAllDevice() } deinit { log.debug("IHConfigGatawayDetailManager") clientSocket = nil } } extension IHConfigGatawayDetailManager : IHConfigGatawayDetailViewDelete{ //485灯的传感器工作模式 func configLightWorkModel(_ device: (address: String, type: String), _ dodgeEnable: String, _ bodyEnable: String) { } //开关灯(485/cw) func setLightTurnOn(_ isTurnOn: Bool, _ device: (address: String, type: String)) { IHConfigGatewayDetailService.share.setCWOrOtherLightTurnOn(isTurnOn: isTurnOn, device: device, ip: self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!) } //删除设备 func deleteDeviceFromGateway(deviceAddress: String) { IHConfigGatewayDetailService.share.deleteDeviceFromGateway(deviceAddress: deviceAddress,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!) } //添加未绑定的设备 func addDeviceToGateway(deviceUUid: String) { IHConfigGatewayDetailService.share.addDeviceToGateway(deviceUUid: deviceUUid,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!) } func selectIndexGetDeviceFromGateway(index: Int){ //扫描添加过的 // DispatchQueue.main.async { // HUD.flash(.progress, delay: 8) // } // scanAssociateDevice() } func selectIndexFoundDevice(index: Int) { //扫描未添加的 // DispatchQueue.main.async { // HUD.flash(.progress, delay: 8) // } scanNotAssociateDevice() } } extension IHConfigGatawayDetailManager { fileprivate func scanAssociateDevice(){ //[0x12, 0xa1, 0x04, 0x00,0xb7] do{ //不连接,直接发命令 try clientSocket?.enableBroadcast(true) try clientSocket?.beginReceiving() // 0117-设备地址 //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33] //"12a1 01 05 01 0001010c c8" //12a10203 011d 0400000000da // let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7] let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7] let command: Data = Data.init(bytes: bytes, count: bytes.count) // self.vc?.gateway?.ip! self.vc?.gateway?.port clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)! , withTimeout: -1, tag: 0) }catch{ log.debug("绑定失败\(error)") } } fileprivate func scanNotAssociateDevice(){ //[0x12, 0xa1, 0x01, 0x00,0xb4] do{ //不连接,直接发命令 try clientSocket?.enableBroadcast(true) try clientSocket?.beginReceiving() // 0117-设备地址 //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33] //"12a1 01 05 01 0001010c c8" //12a10203 011d 0400000000da let bytes: [UInt8] = [0x12, 0xa1, 0x01, 0x00,0xb4] let command: Data = Data.init(bytes: bytes, count: bytes.count) // self.vc?.gateway?.ip! self.vc?.gateway?.port clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)!, withTimeout: -1, tag: 0) }catch{ log.debug("绑定失败\(error)") } } } extension IHConfigGatawayDetailManager : GCDAsyncUdpSocketDelegate{ func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) { log.debug("didNotConnect") } func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) { log.debug("didConnectToAddress:\(String(data: address, encoding: String.Encoding.utf8))") } func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) { //12 A1 04 01 00 B8 let hexStr = dataConvertToHexString(data: data) if self.mainView.currentIndex! == 0 { //获取相关设备 getDeviceFromGateway(respondStr: hexStr) }else{ //获取未相关设备 foundDevice(respondStr: hexStr) } log.debug("didReceive:\(hexStr)") } func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) { log.debug("didSendDataWithTag") } func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?) { log.debug("withError") } func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) { log.debug("didNotSendDataWithTag") } } extension IHConfigGatawayDetailManager{ //扫描网关返回的设备 private func getDeviceFromGateway(respondStr : String) { let str = respondStr as NSString //"12a1 04 04 01 0117 01 d5" // 12a1 040702011e31011f3161 let componeStr = str.substring(with: NSRange.init(location: 8, length: 2)) let check = str.substring(with: NSRange.init(location: 4, length: 2)) //Found Device Count. var deviceArr = [(address : String ,type : String)]() if componeStr == "00" && check == "04" { log.debug("没有发现添加的设备") }else{ //如果 componeStr 是大于或等于01,里面可能是多个了 // var deviceArr = [(address : String ,type : String)]() for i in 0..