IHClassRoomService.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // IHClassRoomService.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/6/15.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. class IHClassRoomService: NSObject {
  11. static let share : IHClassRoomService = IHClassRoomService()
  12. var clientId : String?
  13. let username = AppShare.username
  14. let client_key = AppShare.client_key
  15. let os = AppShare.os
  16. let version = AppShare.version
  17. let token = AppShare.token
  18. // 所有设备
  19. func getDeviceList( roomId:String,requstSuccess:@escaping (([ShoolDeviceList])->Void),requestFail:@escaping (()->Void)){
  20. g_school_dev_listHttpRequest(username, client_key: client_key, os: os, version: version, token: token, roomId: roomId, success: { (json) -> (Void) in
  21. let deviceList = Shool_Dev_List.fromJSON(json)
  22. let result = deviceList?.result
  23. if let roomlist = result?.list{
  24. requstSuccess(roomlist)
  25. }
  26. }, fail: requestFail)
  27. }
  28. //更换灯的位置
  29. // func chargeLight(infoList : [[String : String]],id : String, location : String,requstSuccess:@escaping ((Shool_Charge_Location)->Void),requestFail:@escaping (()->Void)) {
  30. // g_school_charge_locationHttpRequest(username, client_key: client_key, os: os, version: version, token: token, infoList: infoList, success: { (json) -> (Void) in
  31. // //要改
  32. //
  33. // }, fail: requestFail)
  34. // }
  35. //
  36. //更换灯的位置(_ json :JSON) ->(Void)
  37. func saveLight(infoList: [[String:String]],requstSuccess:@escaping ((_ data : String)->Void),requestFail:@escaping (()->Void)) {
  38. g_school_charge_locationHttpRequest(username, client_key: client_key, os: os, version: version, token: token, infoList: infoList, success: { (json) -> (Void) in
  39. //要改
  40. log.debug("位置\(json)")
  41. //json解释
  42. requstSuccess(json["code"].stringValue)
  43. }, fail: requestFail)
  44. }
  45. //窗帘的开关
  46. func curtainOnOff(id : String, status : String,requstSuccess:@escaping((_ data : String,String)->Void),requestFail:@escaping (()->Void)) {
  47. HUD.show(.progress)
  48. self.getWebSocketId(id : id, status : status, requestFail: requestFail, success: requstSuccess)
  49. }
  50. //获取websocket - clientId
  51. func getWebSocketId(id : String, status : String,requestFail:@escaping (()->Void),success:@escaping (_ msg : String,_ code : String)->()) {
  52. IHWebSocketManager.shareInstance().schoolDeviceOpation(isSchool: true) { (isSuccess, responeStr,code) in
  53. if isSuccess == 2 {
  54. log.debug("websocket窗帘返回的数据")
  55. g_showHUD(responeStr)
  56. HUD.hide(afterDelay: 1)
  57. success(responeStr,code)
  58. }else{
  59. // guard let clientId = responeStr else{return}
  60. //isSuccess == 1时,responeStr是clientId
  61. g_school_set_curtainHttpRequest(self.username, client_key: self.client_key, os: self.os
  62. , version: self.version, token: self.token, id: id, status: status, clientId:responeStr , success: { (json) -> (Void) in
  63. // log.debug("窗帘返回的数据\(json)")
  64. // success(json["msg"].stringValue)
  65. }, fail: requestFail)
  66. }
  67. }
  68. }
  69. //灯的全开/全关 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式,7 上课模式,8 下课模式,9 午休模式)
  70. func setLights_on_off(roomId : String, type : String,success:@escaping (_ msg : String,_ code: String)->(),requestFail:@escaping (()->Void)) {
  71. HUD.show(.progress)
  72. // HUD.hide(afterDelay: 15) { (isSuccess) in // 15s超时提醒
  73. // g_showHUD("Request timeout")
  74. // }
  75. IHWebSocketManager.shareInstance().schoolDeviceOpation(isSchool: true) { (isSuccess, responeStr,code ) in
  76. if isSuccess == 2 {
  77. log.debug("websocket-开关返回的数据\(responeStr)")
  78. g_showHUD(responeStr)
  79. HUD.hide(afterDelay: 1)
  80. success(responeStr,code)
  81. }else{
  82. //isSuccess == 1时返回的是clientId
  83. g_school_set_offHttpRequest(self.username, client_key: self.client_key, os: self.os, version: self.version, token: self.token, roomId: roomId, type: type, clientId: responeStr, success: { (json) -> (Void) in
  84. log.debug("开关返回的数据")
  85. }, fail: requestFail)
  86. }
  87. }
  88. }
  89. //教室灯的整体色温 -- 暂时不用做
  90. func lightsColor(classDevices : [ShoolDeviceList]) {
  91. //循环给每个教室灯发命令
  92. let count = classDevices.count
  93. var index = 0
  94. IHWebSocketManager.shareInstance().schoolDeviceOpation(isSchool: true) { (isSuccess, responeStr,code) in
  95. if isSuccess == 2 {
  96. log.debug("websocket-教室灯的色温")
  97. self.loopSetlightColor(index: index, count: count, responeStr: responeStr, classDevices: classDevices)
  98. // success(responeStr ?? "")
  99. }else{
  100. // guard let clientId = responeStr else{return}
  101. //issuccess == 1时responeStr是clientId
  102. guard let id = classDevices[index].id else {return}
  103. guard var color = classDevices[index].color else {return}
  104. //色温 : 3000 - 6000 ,间隔 300
  105. guard var colorValue = Int(color) else {return}
  106. if colorValue < 6000{
  107. colorValue = colorValue + 300
  108. }
  109. if colorValue == 6000{
  110. colorValue = 3000
  111. }
  112. color = ("\(colorValue)")
  113. g_school_set_colorHttpRequest(self.username, client_key: self.client_key, os: self.os, version: self.version, token: self.token, id: id, color: color, clientId: responeStr, success: { (json) -> (Void) in
  114. log.debug("----")
  115. }) { () -> (Void) in
  116. log.debug("----")
  117. }
  118. index = index + 1
  119. }
  120. }
  121. }
  122. func loopSetlightColor(index : Int ,count : Int,responeStr:String? ,classDevices : [ShoolDeviceList]) {
  123. var index = index
  124. if index < count {
  125. IHWebSocketManager.shareInstance().schoolDeviceOpation(isSchool: true) { (isSuccess, responeStr,code) in
  126. if isSuccess == 2 {
  127. log.debug("websocket-教室灯的色温")
  128. self.loopSetlightColor(index: index, count: count, responeStr: responeStr, classDevices: classDevices)
  129. // success(responeStr ?? "")
  130. }
  131. //self.loopSetlightColor(index: index, count: count, responeStr: responeStr, classDevices: classDevices)
  132. }
  133. guard let clientId = responeStr else{return}
  134. guard let id = classDevices[index].id else {return}
  135. guard var color = classDevices[index].color else {return}
  136. //色温 : 3000 - 6000 ,间隔 300
  137. guard var colorValue = Int(color) else {return}
  138. if colorValue < 6000{
  139. colorValue = colorValue + 300
  140. }
  141. if colorValue == 6000{
  142. colorValue = 3000
  143. }
  144. color = ("\(colorValue)")
  145. g_school_set_colorHttpRequest(self.username, client_key: self.client_key, os: self.os, version: self.version, token: self.token, id: id, color: color, clientId: clientId, success: { (json) -> (Void) in
  146. log.debug("----")
  147. }) { () -> (Void) in
  148. log.debug("----")
  149. }
  150. index = index + 1
  151. }
  152. }
  153. //
  154. }