IHRoomLightManager.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // IHRoomLightManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/16.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. class IHRoomLightManager: NSObject ,IHViewManagerProtocolDelegate{
  11. var energyConsumptionData :ReportData?
  12. lazy var mainView: IHRoomLightView = {
  13. let mainView = IHRoomLightView()
  14. mainView.delegate = self
  15. return mainView
  16. }()
  17. // private var vc = UIViewController()
  18. private weak var vc : UIViewController?
  19. override init() {
  20. super.init()
  21. NotificationCenter.default.addObserver(self, selector: #selector(reloadNotify), name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: nil)
  22. }
  23. deinit {
  24. NotificationCenter.default.removeObserver(self)
  25. }
  26. @objc func reloadNotify(_ notif:Notification){
  27. let lightVC = self.vc as! IHRoomLightVCtr
  28. requestData((lightVC.dataSource?.id!)!)
  29. self.vc?.navigationBarTitle = notif.object as? String
  30. }
  31. func bindController(_ vc: UIViewController) {
  32. self.vc = vc
  33. createUI()
  34. }
  35. func createUI() {
  36. mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight)
  37. self.vc!.view.addSubview(mainView)
  38. setNavigationBar()
  39. let lightVC = self.vc as! IHRoomLightVCtr
  40. self.vc?.navigationBarTitle = lightVC.dataSource?.name
  41. requestData((lightVC.dataSource?.id!)!)
  42. }
  43. func requestData(_ lightId:String) {
  44. // HUD.show(.progress)
  45. // HUD.hide(afterDelay: 20) { (isSuccess) in
  46. // if isSuccess == true{
  47. // g_showHUD("Request timeout")
  48. // }
  49. // }
  50. let queue = DispatchQueue.global()
  51. let group = DispatchGroup()
  52. queue.async(group: group, qos: .default, flags: []) {
  53. IHRoomLightService.share.getLightInfo(lightId, requestSuccess: { (data) in
  54. self.mainView.lightData = data
  55. }) {
  56. }
  57. }
  58. queue.async(group: group, qos: .default, flags: []) {
  59. IHRoomLightService.share.getLampReport(lightId, dateType: "0", dataType: "0", date: nil, requestSuccess: { (data) in
  60. self.mainView.reportData = data
  61. self.energyConsumptionData = data
  62. }) {
  63. }
  64. }
  65. group.notify(queue: queue) {
  66. log.debug("结束了")
  67. // HUD.hide()
  68. }
  69. }
  70. func setNavigationBar() {
  71. //导航栏的渲染方式
  72. let searchItem = UIBarButtonItem(image: UIImage(named: "历史数据")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(mapAction1))
  73. let setItem = UIBarButtonItem(image: UIImage(named: "设置")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(modeSetAction))
  74. self.vc?.navigationItem.rightBarButtonItems = [searchItem,setItem]
  75. }
  76. @objc func modeSetAction() {
  77. let eqment = IHNewEquipmentVCtr()
  78. eqment.roomStyle = .equipmentEdit
  79. let aVC = self.vc as! IHRoomLightVCtr
  80. eqment.devData = aVC.dataSource
  81. eqment.navigationBarTitle = aVC.dataSource?.name
  82. self.vc?.navigationController?.pushViewController(eqment, animated: true)
  83. }
  84. @objc func mapAction1(){
  85. log.debug("点击了")
  86. let history = IHRoomLightHistoryVCtr()
  87. let lightVC = self.vc as! IHRoomLightVCtr
  88. let lightId = lightVC.dataSource?.id!
  89. history.devId = lightId
  90. history.devType = lightVC.dataSource?.devType
  91. IHRoomLightService.share.getLampReport(lightId!, dateType: "0", dataType: "1", date: nil, requestSuccess: { (data) in
  92. let lampHistory = (energy_consumption:self.energyConsumptionData,lighting_use_schedule:data)
  93. log.debug("self.lampHistory - \(lampHistory)")
  94. history.lampHistory = lampHistory as! (energy_consumption: ReportData, lighting_use_schedule: ReportData)
  95. self.vc?.navigationController?.pushViewController(history, animated: true)
  96. }) {
  97. }
  98. }
  99. }
  100. extension IHRoomLightManager: IHRoomLightViewDelegate{
  101. func rl_switchDataType(_ index: Int) {
  102. let lightVC = self.vc as! IHRoomLightVCtr
  103. let lightId = lightVC.dataSource?.id!
  104. var dateType = "0"
  105. if index == 0 {
  106. dateType = "0"
  107. }else if index == 1 {
  108. dateType = "4"
  109. }else if index == 2 {
  110. dateType = "1"
  111. }
  112. else if index == 3 {
  113. dateType = "2"
  114. }
  115. IHRoomLightService.share.getLampReport(lightId!, dateType: dateType, dataType: "0", date: nil, requestSuccess: { (data) in
  116. self.mainView.reportData = data
  117. }) {
  118. }
  119. }
  120. func rl_controlSwitch(_ status: String) {
  121. let lightVC = self.vc as! IHRoomLightVCtr
  122. let lightId = lightVC.dataSource?.id!
  123. IHRoomListService.share.setLampTurn(lightId: lightId!, status: status, requestSuccess: { (isSuccess) in
  124. self.requestLightInfo(lightId!)
  125. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  126. }) {
  127. }
  128. }
  129. func rl_adjustBrightness(_ light: Int) {
  130. let lightVC = self.vc as! IHRoomLightVCtr
  131. let lightId = lightVC.dataSource?.id!
  132. IHRoomLightService.share.individualDimming(lightId: lightId!, light: "\(light)", requestSuccess: { (isSuccess) in
  133. self.requestLightInfo(lightId!)
  134. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  135. }) {
  136. }
  137. }
  138. func rl_adjustColorTemperature(_ color: Int) {
  139. let lightVC = self.vc as! IHRoomLightVCtr
  140. let lightId = lightVC.dataSource?.id!
  141. IHRoomLightService.share.setLampColor(lightId: lightId!, color: "\(color)", requestSuccess: { (isSuccess) in
  142. self.requestLightInfo(lightId!)
  143. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  144. }) {
  145. }
  146. }
  147. func rl_adjustColor(_ red: Int, green: Int, blue: Int) {
  148. let lightVC = self.vc as! IHRoomLightVCtr
  149. let lightId = lightVC.dataSource?.id!
  150. IHRoomLightService.share.setLampColorRGB(lightId!, colorR: "\(red)", colorG: "\(green)", colorB: "\(blue)", requestSuccess: { (isSuccess) in
  151. self.requestLightInfo(lightId!)
  152. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  153. }) {
  154. }
  155. }
  156. func requestLightInfo(_ lightId:String) {
  157. log.debug("requestLightInfo")
  158. IHRoomLightService.share.getLightInfo(lightId, requestSuccess: { (data) in
  159. self.mainView.lightData = data
  160. }) {
  161. }
  162. }
  163. }