IHNewEquipmentService.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // IHNewEquipmentService.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/30.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import AFNetworking
  10. class IHNewEquipmentService: NSObject {
  11. static let share = IHNewEquipmentService()
  12. var completionCallback :(()->Void)?
  13. func uploadImage(file:UIImage,requestSuccess:@escaping (String)->Void,requestFail:@escaping (()->Void)) {
  14. //let urlstr = "http://www.iotena-web.com/common/update_file"
  15. let imageData = file.jpegData(compressionQuality: 0.1)
  16. let manager = AFHTTPSessionManager()
  17. manager.requestSerializer = AFJSONRequestSerializer()
  18. manager.responseSerializer = AFHTTPResponseSerializer()
  19. manager.requestSerializer.setValue("application/json,text/html", forHTTPHeaderField: "Accept")
  20. manager.requestSerializer.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
  21. //let url = "http://www.iotena-web.com/api/common/update_file"
  22. let url = "http://elinker.lampmind.com/api/common/update_file"
  23. manager.post(url, parameters: nil, constructingBodyWith: { (fordata) in
  24. fordata.appendPart(withFileData: imageData!, name: "file", fileName: "test.jpg", mimeType: "image/jpeg")
  25. }, progress: nil, success: { (task, respond) in
  26. print(respond)
  27. let dict = try? JSONSerialization.jsonObject(with: respond as! Data, options: .mutableLeaves) as! [String:Any]
  28. print("dict --\(dict)")
  29. let msg = dict!["msg"] as! String
  30. print("msg -- \(msg)")
  31. let status = dict!["code"] as! String
  32. log.debug("code --\(status)")
  33. if status == "0000"{
  34. let data = dict!["data"] as! [String:String]
  35. let path = data["path"]
  36. log.debug("path - \(path)")
  37. requestSuccess(path!)
  38. }else{
  39. requestFail()
  40. }
  41. }) { (task, error) in
  42. print("error - \(error)")
  43. requestFail()
  44. }
  45. }
  46. //devType: 0 light , 1 circadian light , 2 sensor, 3 air puirfier 4,gateway
  47. func saveEquipmentInfo(devType:String,floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String?,model:String?,gatewayId:String?,completion:@escaping (()->Void)) {
  48. self.completionCallback = completion
  49. if devType == "2" {
  50. saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!,gatewayId:gatewayId!)
  51. }else if devType == "3" {
  52. saveAirPuirfier(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model: model!,gatewayId: gatewayId!)
  53. }else if devType == "4"{
  54. saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: nil)
  55. }else {
  56. saveLight(devType: devType, floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!,gatewayId : gatewayId!)
  57. }
  58. }
  59. func editGateway(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,inspecTime:String?,completion:@escaping (()->Void)){
  60. self.completionCallback = completion
  61. saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: inspecTime)
  62. }
  63. fileprivate func saveLight(devType:String,floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model:String,gatewayId : String){
  64. let username = AppShare.username
  65. let client_key = AppShare.client_key
  66. let os = AppShare.os
  67. let version = AppShare.version
  68. let token = AppShare.token
  69. g_lamp_save_lightHttpRequest(username, client_key: client_key, os: os, version: version, token: token, lightId: devId, name: name, deviceId: deviceId, floorId: floorId, roomId: roomId, type: devType, image: image, model: model,gatewayId : gatewayId, success: { (json) -> (Void) in
  70. let info = Operation_Info.fromJSON(json)
  71. let result = info?.result
  72. if result?.code == .Success{
  73. g_showHUD(result?.msg ?? "")
  74. self.completionCallback!()
  75. }else{
  76. g_showFail(result?.msg ?? "")
  77. }
  78. }) { () -> (Void) in
  79. g_showFail("Network error!")
  80. }
  81. }
  82. fileprivate func saveSensor(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model:String,gatewayId:String){
  83. let username = AppShare.username
  84. let client_key = AppShare.client_key
  85. let os = AppShare.os
  86. let version = AppShare.version
  87. let token = AppShare.token
  88. g_sensor_save_sensorHttpRequest(username, client_key: client_key, os: os, version: version, token: token, sensorId: devId, name: name, deviceId: deviceId, floorId: floorId, roomId: roomId, image: image,model:model,gatewayId:gatewayId, success: { (json) -> (Void) in
  89. let info = Operation_Info.fromJSON(json)
  90. let result = info?.result
  91. if result?.code == .Success{
  92. g_showHUD(result?.msg ?? "")
  93. self.completionCallback!()
  94. }else{
  95. g_showFail(result?.msg ?? "")
  96. }
  97. }) { () -> (Void) in
  98. g_showFail("Network error!")
  99. }
  100. }
  101. fileprivate func saveAirPuirfier(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model: String,gatewayId: String){
  102. let username = AppShare.username
  103. let client_key = AppShare.client_key
  104. let os = AppShare.os
  105. let version = AppShare.version
  106. let token = AppShare.token
  107. g_purifier_save_purifierHttpRequest(username, client_key: client_key, os: os, version: version, token: token, purifierId: devId, name: name, deviceId: deviceId, floorId: floorId, roomId: roomId, image: image,model: model,gatewayId: gatewayId, success: { (json) -> (Void) in
  108. let info = Operation_Info.fromJSON(json)
  109. let result = info?.result
  110. if result?.code == .Success{
  111. g_showHUD(result?.msg ?? "")
  112. self.completionCallback!()
  113. }else{
  114. g_showFail(result?.msg ?? "")
  115. }
  116. }) { () -> (Void) in
  117. g_showFail("Network error!")
  118. }
  119. }
  120. fileprivate func saveGateway(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,inspecTime:String?){
  121. let username = AppShare.username
  122. let client_key = AppShare.client_key
  123. let os = AppShare.os
  124. let version = AppShare.version
  125. let token = AppShare.token
  126. g_network_addRoomNetHttpRequest(username, client_key: client_key, os: os, version: version, token: token, id: devId, gatewayId: deviceId, network_name: name, floorId: floorId, roomId: roomId, inspecTime: inspecTime, success: { (json) -> (Void) in
  127. let info = Operation_Info.fromJSON(json)
  128. let result = info?.result
  129. if result?.code == .Success{
  130. g_showHUD(result?.msg ?? "")
  131. self.completionCallback!()
  132. }else{
  133. g_showFail(result?.msg ?? "")
  134. }
  135. }) { () -> (Void) in
  136. g_showFail("Network error!")
  137. }
  138. }
  139. // 学校 添加/编辑设备
  140. func schoolSaveEquipmentInfo(lightId:String?,name:String,deviceId:String,floorId:String,roomId:String,image:String?,type:String,model:String?,gatewayId: String,completion:@escaping (()->Void)) {
  141. self.completionCallback = completion
  142. let username = AppShare.username
  143. let client_key = AppShare.client_key
  144. let os = AppShare.os
  145. let version = AppShare.version
  146. let token = AppShare.token
  147. //添加
  148. g_school_save_lightHttpRequest(username, client_key: client_key, os: os, version: version, token: token, lightId: lightId, name: name, deviceId: deviceId, floorId: floorId, roomId: roomId, image: image, type: type, model: model, gatewayId: gatewayId, success: { (json) -> (Void) in
  149. let info = Operation_Info.fromJSON(json)
  150. let result = info?.result
  151. if result?.code == .Success{
  152. g_showHUD(result?.msg ?? "")
  153. self.completionCallback!()
  154. }else{
  155. g_showFail(result?.msg ?? "")
  156. }
  157. }) { () -> (Void) in
  158. g_showFail("Network error!")
  159. }
  160. // self.completionCallback = completion
  161. // if devType == "2" {
  162. // saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!)
  163. // }else if devType == "3" {
  164. // saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!)
  165. // }else if devType == "4"{
  166. // saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: nil)
  167. // }else {
  168. // saveLight(devType: devType, floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!)
  169. // }
  170. }
  171. }
  172. extension UIImage {
  173. func resizableImage() -> UIImage {
  174. var normal = self
  175. let imageWidth = normal.size.width * 0.5
  176. let imageHeight = normal.size.height * 0.5
  177. normal = resizableImage(withCapInsets: UIEdgeInsets(top: imageHeight, left: imageWidth, bottom: imageHeight, right: imageWidth))
  178. return normal
  179. }
  180. ///对指定图片进行拉伸
  181. func resizableImage(name: String) -> UIImage {
  182. var normal = UIImage(named: name)!
  183. let imageWidth = normal.size.width * 0.5
  184. let imageHeight = normal.size.height * 0.5
  185. normal = resizableImage(withCapInsets: UIEdgeInsets(top: imageHeight, left: imageWidth, bottom: imageHeight, right: imageWidth))
  186. return normal
  187. }
  188. /**
  189. * 压缩上传图片到指定字节
  190. *
  191. * image 压缩的图片
  192. * maxLength 压缩后最大字节大小
  193. *
  194. * return 压缩后图片的二进制
  195. */
  196. func compressImage(image: UIImage, maxLength: Int) -> NSData? {
  197. let newSize = self.scaleImage(image: image, imageLength: 300)
  198. let newImage = self.resizeImage(image: image, newSize: newSize)
  199. var compress:CGFloat = 0.9
  200. var data = newImage.jpegData(compressionQuality: compress)
  201. while data!.count > maxLength && compress > 0.01 {
  202. compress -= 0.02
  203. data = newImage.jpegData(compressionQuality: compress)
  204. }
  205. return data as NSData?
  206. }
  207. /**
  208. * 通过指定图片最长边,获得等比例的图片size
  209. *
  210. * image 原始图片
  211. * imageLength 图片允许的最长宽度(高度)
  212. *
  213. * return 获得等比例的size
  214. */
  215. func scaleImage(image: UIImage, imageLength: CGFloat) -> CGSize {
  216. var newWidth:CGFloat = 0.0
  217. var newHeight:CGFloat = 0.0
  218. let width = image.size.width
  219. let height = image.size.height
  220. if (width > imageLength || height > imageLength){
  221. if (width > height) {
  222. newWidth = imageLength;
  223. newHeight = newWidth * height / width;
  224. }else if(height > width){
  225. newHeight = imageLength;
  226. newWidth = newHeight * width / height;
  227. }else{
  228. newWidth = imageLength;
  229. newHeight = imageLength;
  230. }
  231. }
  232. return CGSize(width: newWidth, height: newHeight)
  233. }
  234. /**
  235. * 获得指定size的图片
  236. *
  237. * image 原始图片
  238. * newSize 指定的size
  239. *
  240. * return 调整后的图片
  241. */
  242. func resizeImage(image: UIImage, newSize: CGSize) -> UIImage {
  243. UIGraphicsBeginImageContext(newSize)
  244. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  245. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  246. UIGraphicsEndImageContext()
  247. return newImage!
  248. }
  249. }