IHClassRoomManager.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // IHClassRoomManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/6/12.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. //亮度
  11. let kNotifactionClassLightUpdateBright = "kNotifactionClassLightUpdateBright"
  12. //色温
  13. let kNotifactionClassLightUpdateTempColor = "kNotifactionClassLightUpdateTempColor"
  14. //开关
  15. let kNotifactionClassLightUpdateSwitch = "kNotifactionClassLightUpdateSwitch"
  16. class IHClassRoomManager: NSObject {
  17. lazy var mainView: IHClassRoomView = {
  18. let mainView = IHClassRoomView.init(frame: self.vc!.view.bounds )
  19. mainView.delegate = self
  20. return mainView
  21. }()
  22. // private var vc = UIViewController()
  23. private weak var vc : UIViewController?
  24. func bindController(_ vc: UIViewController){
  25. self.vc = vc
  26. self.vc?.view.addSubview(mainView)
  27. let classRoomVC = vc as! IHClassRoomCtr
  28. // createUI()
  29. //
  30. // let arealistVC = vc as! IHAreaListVCtr
  31. // mainView.roomList = arealistVC.roomList
  32. //亮度
  33. NotificationCenter.default.addObserver(self, selector: #selector(notiUpdateLightBright), name: Notification.Name(kNotifactionClassLightUpdateBright), object: nil)
  34. //色温
  35. NotificationCenter.default.addObserver(self, selector: #selector(notiUpdateTempColor), name: Notification.Name(kNotifactionClassLightUpdateTempColor), object: nil)
  36. //开关
  37. NotificationCenter.default.addObserver(self, selector: #selector(notiUpdateSwitch), name: Notification.Name(kNotifactionClassLightUpdateSwitch), object: nil)
  38. getDeviceList(roomId: classRoomVC.roomId)
  39. }
  40. func createUI() {
  41. //按8的尺寸
  42. // mainView.frame = self.vc.view.bounds
  43. // //设置图片
  44. // let backImageView = UIImageView.init(frame: mainView.frame)
  45. // backImageView.image = UIImage.init(named: "关灯背景图")
  46. // mainView.addSubview(backImageView)
  47. // self.vc.view.addSubview(mainView)
  48. }
  49. deinit {
  50. NotificationCenter.default.removeObserver(self)
  51. }
  52. }
  53. extension IHClassRoomManager : IHClassRoomViewDelegate{
  54. //调灯的色温
  55. // func clickLight(_index: Int) {
  56. // //IHLampControlVCtr
  57. // let vc = IHLampControlVCtr()
  58. // var d = DevData.init()
  59. //// 2033
  60. // d.id = "2033"
  61. // vc.dataSource = d
  62. // vc.isClassRoom = true
  63. // self.vc.navigationController?.pushViewController(vc, animated: true)
  64. // }
  65. //调单个灯的色温与亮度
  66. func clickLight(lightDevice: ShoolDeviceList) {
  67. let vc = IHLampControlVCtr()
  68. vc.shoolDevice = lightDevice
  69. var dev = DevData.init()
  70. dev.id = lightDevice.id
  71. vc.dataSource = dev
  72. vc.isClassRoom = true
  73. self.vc?.navigationController?.pushViewController(vc, animated: true)
  74. }
  75. //更换灯的位置
  76. func chargeLight(lists: [ShoolDeviceList]) {
  77. changeLightLocation(lights :lists )
  78. }
  79. //窗帘的开关
  80. func curtainSetOnOff(curtainDevice: ShoolDeviceList,respone:@escaping ((_ msg : String)->Void)) {
  81. changeCurtainState(curtainDevice: curtainDevice, respone: respone)
  82. }
  83. }
  84. //网络请求
  85. extension IHClassRoomManager {
  86. //所有设备
  87. func getDeviceList(roomId : String) {
  88. IHClassRoomService.share.getDeviceList(roomId: roomId, requstSuccess: { (shoolDivListArr) in
  89. log.debug("-网络请求-教室设备\(shoolDivListArr)")
  90. //return shoolDivListArr
  91. self.mainView.shoolDivListArr = shoolDivListArr
  92. }) {
  93. HUD.flash(.error, delay: 1.5)
  94. }
  95. }
  96. //更换设备位置
  97. func changeLightLocation(lights : [ShoolDeviceList]) {
  98. var locations = [[String:String]]()
  99. for light in lights {
  100. let dic = ["id":light.id!,"location":light.location!]
  101. locations.append(dic)
  102. }
  103. IHClassRoomService.share.saveLight(infoList: locations, requstSuccess: { (msg) in
  104. log.debug("----\(msg)")
  105. if(msg == "0000"){
  106. HUD.flash(.success, delay: 1)
  107. }
  108. }) {
  109. HUD.flash(.error, delay: 1.5)
  110. }
  111. }
  112. //窗帘的开关
  113. func changeCurtainState(curtainDevice : ShoolDeviceList,respone:@escaping (_ msg : String)->()) {
  114. guard let id = curtainDevice.id else {
  115. return
  116. }
  117. guard var status = curtainDevice.status else {
  118. return
  119. }
  120. if status == "1" {
  121. status = "0"
  122. }else{
  123. status = "1"
  124. }
  125. IHClassRoomService.share.curtainOnOff(id: id, status: status, requstSuccess: { (msg,code) in
  126. log.debug("窗帘的开关")
  127. //respone(msg)
  128. // HUD.flash(.label(msg), delay: 1.5)
  129. if(code == "0000"){
  130. //如果成功的话,改变现在的状态
  131. let classRoomVC = self.vc as! IHClassRoomCtr
  132. self.getDeviceList(roomId: classRoomVC.roomId)
  133. }else{
  134. //如果没有成功的话,就恢复到没有的样子
  135. self.mainView.shoolDivListArr = self.mainView.shoolDivListArr
  136. }
  137. }) {
  138. HUD.flash(.error, delay: 1.5)
  139. }
  140. }
  141. //灯的全开与全关 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式,7 上课模式,8 下课模式,9 午休模式)
  142. func lights_set_off(type: String) {
  143. let classRoomVC = vc as! IHClassRoomCtr
  144. IHClassRoomService.share.setLights_on_off(roomId: classRoomVC.roomId, type: type, success: { (msg,code) in
  145. log.debug("----")
  146. if(code == "0000"){
  147. //如果成功的话,改变现在的状态
  148. let classRoomVC = self.vc as! IHClassRoomCtr
  149. self.mainView.isSuccess = true
  150. self.getDeviceList(roomId: classRoomVC.roomId)
  151. }else{
  152. //如果没有成功的话,就恢复到没有的样子
  153. //self.mainView.shoolDivListArr = self.mainView.shoolDivListArr
  154. self.mainView.isSuccess = false
  155. }
  156. }) {
  157. HUD.flash(.error, delay: 1.5)
  158. }
  159. //IHClassRoomService.share.setLights_on_off(roomId: classRoomVC.roomId, type: type,requestFail:@escaping (()->Void),success:@escaping (_ msg : String)->())
  160. }
  161. func setClassLightColor(classDevices: [ShoolDeviceList]) {
  162. IHClassRoomService.share.lightsColor(classDevices: classDevices)
  163. }
  164. }
  165. //通知的方法
  166. extension IHClassRoomManager{
  167. //亮度
  168. @objc func notiUpdateLightBright(infor : Notification) {
  169. log.debug("-通知的方法--")
  170. let classRoomVC = self.vc as! IHClassRoomCtr
  171. self.getDeviceList(roomId: classRoomVC.roomId)
  172. }
  173. //色温
  174. @objc func notiUpdateTempColor(infor : Notification) {
  175. log.debug("-通知的方法--")
  176. let classRoomVC = self.vc as! IHClassRoomCtr
  177. self.getDeviceList(roomId: classRoomVC.roomId)
  178. }
  179. //开关
  180. @objc func notiUpdateSwitch(infor : Notification) {
  181. log.debug("-通知的方法--")
  182. let classRoomVC = self.vc as! IHClassRoomCtr
  183. self.getDeviceList(roomId: classRoomVC.roomId)
  184. }
  185. }