IHConfigGatawayDetailManager.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // IHConfigGatawayDetailManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/8/25.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. let KNonitifiGetAsssociatedDevices = "KNonitifiGetAsssociatedDevices"
  11. let KNonitifiGetNotAsssociatedDevices = "KNonitifiGetNotAsssociatedDevices"
  12. class IHConfigGatawayDetailManager: NSObject {
  13. private lazy var mainView : IHConfigGatawayDetailView = {
  14. let mainView = Bundle.main.loadNibNamed("IHConfigGatawayDetailView", owner: nil, options: nil)?.last as! IHConfigGatawayDetailView
  15. mainView.delete = self
  16. return mainView
  17. }()
  18. lazy var clientSocket: GCDAsyncUdpSocket? = {
  19. var mainQueue = DispatchQueue.main
  20. var clientSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: mainQueue)
  21. return clientSocket
  22. }()
  23. private var deviceList : [(address: String, type: String)]?
  24. var connectCallback: (()->Void)?
  25. private weak var vc : IHConfigGatawayDetailCtr?
  26. func bindController(_ vc : IHConfigGatawayDetailCtr) {
  27. self.vc = vc
  28. vc.view.addSubview(mainView)
  29. mainView.gateway = vc.gateway
  30. //扫描添加过的和未添加的
  31. //scanAllDevice()
  32. }
  33. deinit {
  34. log.debug("IHConfigGatawayDetailManager")
  35. clientSocket = nil
  36. }
  37. }
  38. extension IHConfigGatawayDetailManager : IHConfigGatawayDetailViewDelete{
  39. //485灯的传感器工作模式
  40. func configLightWorkModel(_ device: (address: String, type: String), _ dodgeEnable: String, _ bodyEnable: String) {
  41. }
  42. //开关灯(485/cw)
  43. func setLightTurnOn(_ isTurnOn: Bool, _ device: (address: String, type: String)) {
  44. IHConfigGatewayDetailService.share.setCWOrOtherLightTurnOn(isTurnOn: isTurnOn, device: device, ip: self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
  45. }
  46. //删除设备
  47. func deleteDeviceFromGateway(deviceAddress: String) {
  48. IHConfigGatewayDetailService.share.deleteDeviceFromGateway(deviceAddress: deviceAddress,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
  49. }
  50. //添加未绑定的设备
  51. func addDeviceToGateway(deviceUUid: String) {
  52. IHConfigGatewayDetailService.share.addDeviceToGateway(deviceUUid: deviceUUid,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
  53. }
  54. func selectIndexGetDeviceFromGateway(index: Int){
  55. //扫描添加过的
  56. // DispatchQueue.main.async {
  57. // HUD.flash(.progress, delay: 8)
  58. // }
  59. //
  60. scanAssociateDevice()
  61. }
  62. func selectIndexFoundDevice(index: Int) {
  63. //扫描未添加的
  64. // DispatchQueue.main.async {
  65. // HUD.flash(.progress, delay: 8)
  66. // }
  67. scanNotAssociateDevice()
  68. }
  69. }
  70. extension IHConfigGatawayDetailManager {
  71. fileprivate func scanAssociateDevice(){
  72. //[0x12, 0xa1, 0x04, 0x00,0xb7]
  73. do{
  74. //不连接,直接发命令
  75. try clientSocket?.enableBroadcast(true)
  76. try clientSocket?.beginReceiving() // 0117-设备地址
  77. //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33]
  78. //"12a1 01 05 01 0001010c c8"
  79. //12a10203 011d 0400000000da
  80. // let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7]
  81. let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7]
  82. let command: Data = Data.init(bytes: bytes, count: bytes.count)
  83. // self.vc?.gateway?.ip! self.vc?.gateway?.port
  84. clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)! , withTimeout: -1, tag: 0)
  85. }catch{
  86. log.debug("绑定失败\(error)")
  87. }
  88. }
  89. fileprivate func scanNotAssociateDevice(){
  90. //[0x12, 0xa1, 0x01, 0x00,0xb4]
  91. do{
  92. //不连接,直接发命令
  93. try clientSocket?.enableBroadcast(true)
  94. try clientSocket?.beginReceiving() // 0117-设备地址
  95. //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33]
  96. //"12a1 01 05 01 0001010c c8"
  97. //12a10203 011d 0400000000da
  98. let bytes: [UInt8] = [0x12, 0xa1, 0x01, 0x00,0xb4]
  99. let command: Data = Data.init(bytes: bytes, count: bytes.count)
  100. // self.vc?.gateway?.ip! self.vc?.gateway?.port
  101. clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)!, withTimeout: -1, tag: 0)
  102. }catch{
  103. log.debug("绑定失败\(error)")
  104. }
  105. }
  106. }
  107. extension IHConfigGatawayDetailManager : GCDAsyncUdpSocketDelegate{
  108. func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
  109. log.debug("didNotConnect")
  110. }
  111. func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
  112. log.debug("didConnectToAddress:\(String(data: address, encoding: String.Encoding.utf8))")
  113. }
  114. func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
  115. //12 A1 04 01 00 B8
  116. let hexStr = dataConvertToHexString(data: data)
  117. if self.mainView.currentIndex! == 0 {
  118. //获取相关设备
  119. getDeviceFromGateway(respondStr: hexStr)
  120. }else{
  121. //获取未相关设备
  122. foundDevice(respondStr: hexStr)
  123. }
  124. log.debug("didReceive:\(hexStr)")
  125. }
  126. func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
  127. log.debug("didSendDataWithTag")
  128. }
  129. func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?) {
  130. log.debug("withError")
  131. }
  132. func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) {
  133. log.debug("didNotSendDataWithTag")
  134. }
  135. }
  136. extension IHConfigGatawayDetailManager{
  137. //扫描网关返回的设备
  138. private func getDeviceFromGateway(respondStr : String) {
  139. let str = respondStr as NSString
  140. //"12a1 04 04 01 0117 01 d5"
  141. // 12a1 040702011e31011f3161
  142. let componeStr = str.substring(with: NSRange.init(location: 8, length: 2))
  143. let check = str.substring(with: NSRange.init(location: 4, length: 2))
  144. //Found Device Count.
  145. var deviceArr = [(address : String ,type : String)]()
  146. if componeStr == "00" && check == "04" {
  147. log.debug("没有发现添加的设备")
  148. }else{
  149. //如果 componeStr 是大于或等于01,里面可能是多个了
  150. // var deviceArr = [(address : String ,type : String)]()
  151. for i in 0..<Int(componeStr)! {
  152. let deviceAdress = str.substring(with: NSRange.init(location: 10 + i * 6, length: 4))
  153. let deviceType = str.substring(with: NSRange.init(location: 14 + i * 6, length: 2))
  154. deviceArr.append((address : deviceAdress,type : deviceType))
  155. }
  156. }
  157. DispatchQueue.main.async {
  158. HUD.hide()
  159. // if deviceArr.count == 0{
  160. // HUD.flash(.label("未发现设备"), delay: 0.6)
  161. // }
  162. }
  163. if self.mainView.assciateDeviceCallBack == nil {
  164. NotificationCenter.default.post(name: NSNotification.Name(KNonitifiGetAsssociatedDevices), object: nil, userInfo: ["associatedDevice" : deviceArr])
  165. }else{
  166. self.mainView.assciateDeviceCallBack?(deviceArr)
  167. }
  168. }
  169. //扫描未添加的设备
  170. private func foundDevice(respondStr : String) {
  171. let str = respondStr as NSString
  172. //"12 a1 01 01 00 b5"
  173. //12 A1 01 05 01 00 01 01 69 25
  174. let componeStr = str.substring(with: NSRange.init(location: 8, length: 2))
  175. //Found Device Count.
  176. var deviceUUidArr = [String]()
  177. if componeStr == "00" {
  178. log.debug("没有发现新设备")
  179. }else{
  180. //如果 componeStr 是大于或等于01,里面可能是多个了
  181. for i in 0..<Int(componeStr)! {
  182. let compone = str.substring(with: NSRange.init(location: 10 + i * 8, length: 8))
  183. deviceUUidArr.append(compone)
  184. }
  185. }
  186. // let str1 = "0001010c"
  187. // deviceUUidArr.append(str1)
  188. DispatchQueue.main.async {
  189. HUD.hide()
  190. if deviceUUidArr.count == 0{
  191. HUD.flash(.label("未发现设备"), delay: 0.6)
  192. }
  193. }
  194. if self.mainView.notAssciateDeviceCallBack == nil {
  195. NotificationCenter.default.post(name: NSNotification.Name(KNonitifiGetNotAsssociatedDevices), object: nil, userInfo: ["notAssociatedDevice" : deviceUUidArr])
  196. }else{
  197. self.mainView.notAssciateDeviceCallBack?(deviceUUidArr)
  198. }
  199. }
  200. }