IHLampControlManager.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // IHLampControlManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/17.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. class IHLampControlManager: NSObject {
  11. lazy var mainView: IHLampControlView = {
  12. let mainView = Bundle.main.loadNibNamed("IHLampControlView", owner: nil, options: nil)?.first as! IHLampControlView
  13. mainView.delegate = self
  14. return mainView
  15. }()
  16. private weak var vc : UIViewController?
  17. var schoolLightColor = 0
  18. var schoolLightBright = 0
  19. var isFirstColor = false
  20. var isFirstBright = false
  21. override init() {
  22. super.init()
  23. NotificationCenter.default.addObserver(self, selector: #selector(reloadNotify), name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: nil)
  24. //学校编辑了设备,更新设备信息
  25. NotificationCenter.default.addObserver(self, selector: #selector(schoolDeviUpdateInfor), name: NSNotification.Name(kNotificationReloadEquipmentInfo), object: nil)
  26. }
  27. deinit {
  28. NotificationCenter.default.removeObserver(self)
  29. }
  30. @objc func reloadNotify(_ notif:Notification){
  31. let lightVC = self.vc as! IHLampControlVCtr
  32. requestData((lightVC.dataSource?.id!)!)
  33. self.vc?.navigationBarTitle = notif.object as? String
  34. }
  35. //更新设备信息
  36. @objc func schoolDeviUpdateInfor(noti : Notification){
  37. let dic = noti.userInfo
  38. let aVC = self.vc as! IHLampControlVCtr
  39. aVC.dataSource?.name = dic?["name"] as? String
  40. }
  41. func bindController(_ vc: UIViewController) {
  42. self.vc = vc
  43. createUI()
  44. }
  45. func createUI() {
  46. let lightVC = self.vc as! IHLampControlVCtr
  47. if lightVC.isClassRoom != true {
  48. HUD.show(.progress)
  49. HUD.hide(afterDelay: 15) { (isSuccess) in // 15s超时提醒
  50. g_showHUD("请求超时")
  51. }
  52. }
  53. self.vc!.view.addSubview(mainView)
  54. setNavigationBar()
  55. if (lightVC.isClassRoom == true){
  56. //学校的 - 色温
  57. // requestData((lightVC.shoolDevice!.id)!)
  58. // //加
  59. // let lightVC = self.vc as! IHLampControlVCtr
  60. self.mainView.isClassRoom = lightVC.isClassRoom
  61. Intermediate.isRegulateColor = "0"
  62. self.mainView.schoolLight = lightVC.shoolDevice
  63. HUD.hide()
  64. }else{
  65. //酒店的
  66. requestData((lightVC.dataSource?.id!)!)
  67. Intermediate.isRegulateColor = "0"
  68. }
  69. }
  70. //循环
  71. func setSchoolLightColorAndBright(color : String? ,light: String?,status: String?) {
  72. let lightVC = self.vc as! IHLampControlVCtr
  73. if light != nil && color == nil && status == nil {
  74. //亮度
  75. lightVC.shoolDevice?.light = light
  76. }
  77. if color != nil && light == nil && status == nil {
  78. //色温
  79. lightVC.shoolDevice?.light = light
  80. }
  81. if status != nil && color == nil && light == nil {
  82. //开关
  83. lightVC.shoolDevice?.status = status
  84. }
  85. self.mainView.schoolLight = lightVC.shoolDevice
  86. HUD.hide()
  87. }
  88. func requestData(_ lightId:String) {
  89. IHRoomLightService.share.getLightInfo(lightId, requestSuccess: { (data) in
  90. self.mainView.lightData = data
  91. HUD.hide()
  92. }) {
  93. HUD.hide()
  94. }
  95. }
  96. }
  97. extension IHLampControlManager{
  98. func setNavigationBar() {
  99. //导航栏的渲染方式
  100. let searchItem = UIBarButtonItem(image: UIImage(named: "ic_history")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(mapAction1))
  101. let setItem = UIBarButtonItem(image: UIImage(named: "设置_白")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(modeSetAction))
  102. self.vc?.navigationItem.rightBarButtonItems = [searchItem,setItem]
  103. }
  104. @objc func modeSetAction() {
  105. let eqment = IHNewEquipmentVCtr()
  106. eqment.roomStyle = .equipmentEdit
  107. let aVC = self.vc as! IHLampControlVCtr
  108. if aVC.isClassRoom == true{
  109. eqment.isSchool = "1"
  110. }
  111. eqment.devData = aVC.dataSource
  112. eqment.navigationBarTitle = aVC.dataSource?.name
  113. self.vc?.navigationController?.pushViewController(eqment, animated: true)
  114. }
  115. @objc func mapAction1(){
  116. log.debug("点击了")
  117. let history = IHRoomLightHistoryVCtr()
  118. let lightVC = self.vc as! IHLampControlVCtr
  119. let lightId = lightVC.dataSource?.id!
  120. history.devId = lightId
  121. history.devType = lightVC.dataSource?.devType
  122. getLampHistory(lightVC.dataSource!)
  123. }
  124. func getLampHistory(_ devData: DevData) {
  125. let devId = devData.id
  126. HUD.show(.progress)
  127. HUD.hide(afterDelay: 20) { (isSuccess) in
  128. if isSuccess == true{
  129. g_showHUD("请求超时")
  130. }
  131. }
  132. var energy_consumption : ReportData?
  133. var lighting_use_schedule :ReportData?
  134. let queue = DispatchQueue(label: "com.custom.thread", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent)
  135. let group = DispatchGroup()
  136. queue.async(group: group, qos: .default, flags: []) {
  137. group.enter()
  138. IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "0", date: nil, requestSuccess: { (data) in
  139. group.leave()
  140. energy_consumption = data
  141. }) {
  142. group.leave()
  143. energy_consumption = nil
  144. }
  145. }
  146. queue.async(group: group, qos: .default, flags: []) {
  147. group.enter()
  148. IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "1", date: nil, requestSuccess: { (data) in
  149. group.leave()
  150. lighting_use_schedule = data
  151. }) {
  152. group.leave()
  153. lighting_use_schedule = nil
  154. }
  155. }
  156. //回到主线程
  157. group.notify(queue: DispatchQueue.main) {
  158. HUD.hide()
  159. if energy_consumption != nil || lighting_use_schedule != nil{
  160. let history = IHRoomLightHistoryVCtr()
  161. history.devId = devData.id
  162. history.devType = devData.devType
  163. let lampHistory = (energy_consumption:energy_consumption,lighting_use_schedule:lighting_use_schedule) as! (energy_consumption: ReportData, lighting_use_schedule: ReportData)
  164. history.lampHistory = lampHistory
  165. self.vc?.navigationController?.pushViewController(history, animated: true)
  166. }else{
  167. g_showHUD("未发现相关数据")
  168. }
  169. }
  170. }
  171. }
  172. extension IHLampControlManager: IHLampControlViewDelegate{
  173. func regulateLampControlSwitch(_ status: String) {
  174. let lightVC = self.vc as! IHLampControlVCtr
  175. let lightId = lightVC.dataSource?.id!
  176. IHRoomListService.share.setLampTurn(lightId: lightId!, status: status, requestSuccess: { (isSuccess) in
  177. self.requestData(lightId!)
  178. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  179. }) {
  180. }
  181. }
  182. func regulateLampControlBrightness(_ light: String) {
  183. let lightVC = self.vc as! IHLampControlVCtr
  184. let lightId = lightVC.dataSource?.id!
  185. IHRoomLightService.share.individualDimming(lightId: lightId!, light: light, requestSuccess: { (isSuccess) in
  186. self.requestData(lightId!)
  187. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  188. }) {
  189. }
  190. }
  191. func regulateLampControlColorTemperature(_ color: String) {
  192. let lightVC = self.vc as! IHLampControlVCtr
  193. let lightId = lightVC.dataSource?.id!
  194. IHRoomLightService.share.setLampColor(lightId: lightId!, color: color, requestSuccess: { (isSuccess) in
  195. self.requestData(lightId!)
  196. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  197. }) {
  198. }
  199. }
  200. func regulateLampControlColor(_ red: String, green: String, blue: String) {
  201. let lightVC = self.vc as! IHLampControlVCtr
  202. let lightId = lightVC.dataSource?.id!
  203. IHRoomLightService.share.setLampColorRGB(lightId!, colorR: red, colorG: green, colorB: blue, requestSuccess: { (isSuccess) in
  204. // self.requestLightInfo(lightId!)
  205. self.requestData(lightId ?? "0")
  206. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  207. }) {
  208. }
  209. }
  210. //学校灯的-亮度
  211. func schoolRegulateLampControlBrightness(_ light: String) {
  212. //color是调节的值
  213. HUD.show(.progress)
  214. let lightVC = self.vc as! IHLampControlVCtr
  215. guard let lightId = lightVC.shoolDevice!.id else { return }
  216. IHRoomLightService.share.lightsBright(lightId: lightId, light: light) { (isSuccessStr,code) in
  217. g_showHUD(isSuccessStr)
  218. HUD.hide(afterDelay: 1.5)
  219. if code != "0000"{
  220. let lightVC = self.vc as! IHLampControlVCtr
  221. self.setSchoolLightColorAndBright(color: nil, light: lightVC.shoolDevice?.light, status: nil)
  222. }else{
  223. //成功
  224. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateBright), object: nil)
  225. self.setSchoolLightColorAndBright(color: nil, light: light, status: nil)
  226. }
  227. }
  228. }
  229. //学校灯的-色度 - 调
  230. func schoolRegulateLampControlColorTemperature(_ color: String) {
  231. //color是调节的值
  232. HUD.show(.progress)
  233. let lightVC = self.vc as! IHLampControlVCtr
  234. guard let lightId = lightVC.shoolDevice!.id else { return }
  235. IHRoomLightService.share.lightsColor(lightId: lightId, color: color) { (isSuccessStr,code) in
  236. // if(isSuccessStr == "")
  237. g_showHUD(isSuccessStr)
  238. HUD.hide(afterDelay: 1.5)
  239. if code != "0000"{
  240. let lightVC = self.vc as! IHLampControlVCtr
  241. self.setSchoolLightColorAndBright(color: lightVC.shoolDevice?.color, light: nil, status: nil)
  242. }else{
  243. //成功
  244. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateTempColor), object: nil)
  245. self.setSchoolLightColorAndBright(color: color, light: nil, status: nil)
  246. }
  247. }
  248. }
  249. // schoolRegulateLampControlswitch
  250. //单个灯的开关
  251. func schoolRegulateLampControlSwitch(_ status: String) {
  252. //
  253. HUD.show(.progress)
  254. let lightVC = self.vc as! IHLampControlVCtr
  255. guard let lightId = lightVC.shoolDevice!.id else { return }
  256. IHRoomLightService.share.setSchoolLightOn_off(lightId: lightId, status: status) { (isSuccessStr,code) in
  257. g_showHUD(isSuccessStr)
  258. HUD.hide(afterDelay: 1.5)
  259. if(code == "0000"){
  260. //成功
  261. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionClassLightUpdateSwitch), object: nil)
  262. self.setSchoolLightColorAndBright(color: nil, light: nil, status: status)
  263. }else{
  264. //开/关灯失败 - 保持原来的状态
  265. let lightVC = self.vc as! IHLampControlVCtr
  266. self.setSchoolLightColorAndBright(color: nil, light: nil, status: lightVC.shoolDevice?.status)
  267. }
  268. }
  269. // IHRoomLightService.share.lightsColor(lightId: lightId, color: color) { (isSuccessStr) in
  270. //// if(isSuccessStr == "")
  271. //
  272. // self.setSchoolLightColorAndBright(color: color, light: nil)
  273. // }
  274. }
  275. }