IHGatewayDetailManager.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // IHGatewayDetailManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/9.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. class IHGatewayDetailManager: NSObject ,IHViewManagerProtocolDelegate{
  11. // private var vc = UIViewController()
  12. private weak var vc : UIViewController?
  13. lazy var mainView: IHGatewayDetailView = {
  14. let mainView = Bundle.main.loadNibNamed("IHGatewayDetailView", owner: nil, options: nil)?.last as! IHGatewayDetailView
  15. mainView.delegate = self
  16. return mainView
  17. }()
  18. func bindController(_ vc: UIViewController) {
  19. self.vc = vc
  20. let aVC = self.vc as! IHGatewayDetailVCtr
  21. if let gateway = aVC.gateway {
  22. self.mainView.gateway = aVC.gateway
  23. if let title = gateway.network_name {
  24. aVC.navigationBarTitle = title.isBlanck == false ? title : "Gateway"
  25. }
  26. }
  27. requestNetworkDevice()
  28. createUI()
  29. }
  30. func createUI() {
  31. mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT )
  32. self.vc?.view.addSubview(mainView)
  33. setNavigationBar()
  34. }
  35. func setNavigationBar() {
  36. //导航栏的渲染方式
  37. let setItem = UIBarButtonItem(image: UIImage(named: "设置")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(editGateway))
  38. self.vc?.navigationItem.rightBarButtonItem = setItem
  39. }
  40. @objc func editGateway() {
  41. let aVC = self.vc as! IHGatewayDetailVCtr
  42. let eqment = IHNewEquipmentVCtr()
  43. eqment.roomStyle = .gatewayEdit
  44. eqment.gateway = aVC.gateway
  45. eqment.navigationBarTitle = aVC.gateway?.network_name ?? "Gateway"
  46. self.vc?.navigationController?.pushViewController(eqment, animated: true)
  47. }
  48. func requestNetworkDevice() {
  49. HUD.show(.progress)
  50. HUD.hide(afterDelay: 15) { (isSuccess) in
  51. g_showHUD("请求超时")
  52. }
  53. let aVC = self.vc as! IHGatewayDetailVCtr
  54. guard let gateway = aVC.gateway else { return }
  55. let queue = DispatchQueue(label: "com.custom.thread.requestNetworkDevice", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent)
  56. let group = DispatchGroup()
  57. var associatelist:[GatewayLamp]? = nil
  58. var notassociatelist :[GatewayLamp]? = nil
  59. queue.async(group: group, qos: .default, flags: []) {
  60. // group.enter()
  61. IHGatewayService.share.getNetworkDevice(floorId: gateway.floorId!, roomId: gateway.roomId!, gatewayid: gateway.gatewayId!, bindLamp: "1", requestSuccess: { (list) in
  62. HUD.hide()
  63. // group.leave()
  64. associatelist = list
  65. if let list = associatelist{
  66. self.mainView.associatedList = list
  67. }
  68. }) {
  69. HUD.hide()
  70. // group.leave()
  71. associatelist = nil
  72. }
  73. }
  74. // 请求网关列表
  75. queue.async(group: group, qos: .default, flags: []) {
  76. group.enter()
  77. IHGatewayService.share.getNetworkDevice(floorId: gateway.floorId!, roomId: gateway.roomId!, gatewayid: gateway.gatewayId!, bindLamp: "2", requestSuccess: { (list) in
  78. // group.leave()
  79. notassociatelist = list
  80. HUD.hide()
  81. if let list = notassociatelist{
  82. self.mainView.notAssociatedList = list
  83. }
  84. }) {
  85. HUD.hide()
  86. // group.leave()
  87. notassociatelist = nil
  88. }
  89. //回到主线程
  90. // group.notify(queue: DispatchQueue.main) {
  91. //
  92. //
  93. //
  94. // }
  95. }
  96. }
  97. }
  98. extension IHGatewayDetailManager:IHGatewayDetailViewDelegate{
  99. func emptyViewSearchDataSegment(_ index: Int, floorId: String, roomId: String, gatewayId: String) {
  100. if index == 0 {
  101. HUD.show(.progress)
  102. HUD.hide(afterDelay: 15) { (isSuccess) in
  103. g_showHUD("请求超时")
  104. }
  105. IHGatewayService.share.getNetworkDevice(floorId: floorId, roomId: roomId, gatewayid: gatewayId, bindLamp: "1", requestSuccess: { (list) in
  106. HUD.hide()
  107. self.mainView.associatedList = list
  108. }) {
  109. HUD.hide()
  110. }
  111. }else{
  112. HUD.show(.progress)
  113. HUD.hide(afterDelay: 15) { (isSuccess) in
  114. g_showHUD("请求超时")
  115. }
  116. IHGatewayService.share.getNetworkDevice(floorId: floorId, roomId: roomId, gatewayid: gatewayId, bindLamp: "2", requestSuccess: { (list) in
  117. HUD.hide()
  118. self.mainView.notAssociatedList = list
  119. }) {
  120. HUD.hide()
  121. }
  122. }
  123. }
  124. func deviceConnectGateway(_ deviceId: String, gatewayId: String) {
  125. IHGatewayService.share.bindGateway(deviceId, gatewayId: gatewayId, bind: "1") { (isSuccess) in
  126. self.requestNetworkDevice()
  127. }
  128. }
  129. func deviceDisconteGateway(_ deviceId: String, gatewayId: String) {
  130. IHGatewayService.share.bindGateway(deviceId, gatewayId: gatewayId, bind: "0") { (isSuccess) in
  131. self.requestNetworkDevice()
  132. }
  133. }
  134. func controlightOn(lampId: String, gatewayId: String, status: String) {
  135. IHGatewayService.share.controlLampStatus(lampId, gatewayId: gatewayId, status: status) { (isSuccess) in
  136. //self.requestNetworkDevice() //控制灯的时候不需要刷新界面
  137. if isSuccess == false {
  138. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHAssociatedCellCommandFailure), object: nil)
  139. }
  140. }
  141. }
  142. }