// // IHNewEquipmentService.swift // Inhealth // // Created by weclouds on 2019/12/30. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import AFNetworking class IHNewEquipmentService: NSObject { static let share = IHNewEquipmentService() var completionCallback :(()->Void)? func uploadImage(file:UIImage,requestSuccess:@escaping (String)->Void,requestFail:@escaping (()->Void)) { //let urlstr = "http://www.iotena-web.com/common/update_file" let imageData = file.jpegData(compressionQuality: 0.1) let manager = AFHTTPSessionManager() manager.requestSerializer = AFJSONRequestSerializer() manager.responseSerializer = AFHTTPResponseSerializer() manager.requestSerializer.setValue("application/json,text/html", forHTTPHeaderField: "Accept") manager.requestSerializer.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") //let url = "http://www.iotena-web.com/api/common/update_file" let url = "http://elinker.lampmind.com/api/common/update_file" manager.post(url, parameters: nil, constructingBodyWith: { (fordata) in fordata.appendPart(withFileData: imageData!, name: "file", fileName: "test.jpg", mimeType: "image/jpeg") }, progress: nil, success: { (task, respond) in print(respond) let dict = try? JSONSerialization.jsonObject(with: respond as! Data, options: .mutableLeaves) as! [String:Any] print("dict --\(dict)") let msg = dict!["msg"] as! String print("msg -- \(msg)") let status = dict!["code"] as! String log.debug("code --\(status)") if status == "0000"{ let data = dict!["data"] as! [String:String] let path = data["path"] log.debug("path - \(path)") requestSuccess(path!) }else{ requestFail() } }) { (task, error) in print("error - \(error)") requestFail() } } //devType: 0 light , 1 circadian light , 2 sensor, 3 air puirfier 4,gateway func saveEquipmentInfo(devType:String,floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String?,model:String?,gatewayId:String?,completion:@escaping (()->Void)) { self.completionCallback = completion if devType == "2" { saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!,gatewayId:gatewayId!) }else if devType == "3" { saveAirPuirfier(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model: model!,gatewayId: gatewayId!) }else if devType == "4"{ saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: nil) }else { saveLight(devType: devType, floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!,gatewayId : gatewayId!) } } func editGateway(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,inspecTime:String?,completion:@escaping (()->Void)){ self.completionCallback = completion saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: inspecTime) } fileprivate func saveLight(devType:String,floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model:String,gatewayId : String){ let username = AppShare.username let client_key = AppShare.client_key let os = AppShare.os let version = AppShare.version let token = AppShare.token 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 let info = Operation_Info.fromJSON(json) let result = info?.result if result?.code == .Success{ g_showHUD(result?.msg ?? "") self.completionCallback!() }else{ g_showFail(result?.msg ?? "") } }) { () -> (Void) in g_showFail("Network error!") } } fileprivate func saveSensor(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model:String,gatewayId:String){ let username = AppShare.username let client_key = AppShare.client_key let os = AppShare.os let version = AppShare.version let token = AppShare.token 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 let info = Operation_Info.fromJSON(json) let result = info?.result if result?.code == .Success{ g_showHUD(result?.msg ?? "") self.completionCallback!() }else{ g_showFail(result?.msg ?? "") } }) { () -> (Void) in g_showFail("Network error!") } } fileprivate func saveAirPuirfier(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,image:String,model: String,gatewayId: String){ let username = AppShare.username let client_key = AppShare.client_key let os = AppShare.os let version = AppShare.version let token = AppShare.token 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 let info = Operation_Info.fromJSON(json) let result = info?.result if result?.code == .Success{ g_showHUD(result?.msg ?? "") self.completionCallback!() }else{ g_showFail(result?.msg ?? "") } }) { () -> (Void) in g_showFail("Network error!") } } fileprivate func saveGateway(floorId:String,roomId:String,devId:String?,name:String,deviceId:String,inspecTime:String?){ let username = AppShare.username let client_key = AppShare.client_key let os = AppShare.os let version = AppShare.version let token = AppShare.token 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 let info = Operation_Info.fromJSON(json) let result = info?.result if result?.code == .Success{ g_showHUD(result?.msg ?? "") self.completionCallback!() }else{ g_showFail(result?.msg ?? "") } }) { () -> (Void) in g_showFail("Network error!") } } // 学校 添加/编辑设备 func schoolSaveEquipmentInfo(lightId:String?,name:String,deviceId:String,floorId:String,roomId:String,image:String?,type:String,model:String?,gatewayId: String,completion:@escaping (()->Void)) { self.completionCallback = completion let username = AppShare.username let client_key = AppShare.client_key let os = AppShare.os let version = AppShare.version let token = AppShare.token //添加 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 let info = Operation_Info.fromJSON(json) let result = info?.result if result?.code == .Success{ g_showHUD(result?.msg ?? "") self.completionCallback!() }else{ g_showFail(result?.msg ?? "") } }) { () -> (Void) in g_showFail("Network error!") } // self.completionCallback = completion // if devType == "2" { // saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!) // }else if devType == "3" { // saveSensor(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!) // }else if devType == "4"{ // saveGateway(floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, inspecTime: nil) // }else { // saveLight(devType: devType, floorId: floorId, roomId: roomId, devId: devId, name: name, deviceId: deviceId, image: image!,model:model!) // } } } extension UIImage { func resizableImage() -> UIImage { var normal = self let imageWidth = normal.size.width * 0.5 let imageHeight = normal.size.height * 0.5 normal = resizableImage(withCapInsets: UIEdgeInsets(top: imageHeight, left: imageWidth, bottom: imageHeight, right: imageWidth)) return normal } ///对指定图片进行拉伸 func resizableImage(name: String) -> UIImage { var normal = UIImage(named: name)! let imageWidth = normal.size.width * 0.5 let imageHeight = normal.size.height * 0.5 normal = resizableImage(withCapInsets: UIEdgeInsets(top: imageHeight, left: imageWidth, bottom: imageHeight, right: imageWidth)) return normal } /** * 压缩上传图片到指定字节 * * image 压缩的图片 * maxLength 压缩后最大字节大小 * * return 压缩后图片的二进制 */ func compressImage(image: UIImage, maxLength: Int) -> NSData? { let newSize = self.scaleImage(image: image, imageLength: 300) let newImage = self.resizeImage(image: image, newSize: newSize) var compress:CGFloat = 0.9 var data = newImage.jpegData(compressionQuality: compress) while data!.count > maxLength && compress > 0.01 { compress -= 0.02 data = newImage.jpegData(compressionQuality: compress) } return data as NSData? } /** * 通过指定图片最长边,获得等比例的图片size * * image 原始图片 * imageLength 图片允许的最长宽度(高度) * * return 获得等比例的size */ func scaleImage(image: UIImage, imageLength: CGFloat) -> CGSize { var newWidth:CGFloat = 0.0 var newHeight:CGFloat = 0.0 let width = image.size.width let height = image.size.height if (width > imageLength || height > imageLength){ if (width > height) { newWidth = imageLength; newHeight = newWidth * height / width; }else if(height > width){ newHeight = imageLength; newWidth = newHeight * width / height; }else{ newWidth = imageLength; newHeight = imageLength; } } return CGSize(width: newWidth, height: newHeight) } /** * 获得指定size的图片 * * image 原始图片 * newSize 指定的size * * return 调整后的图片 */ func resizeImage(image: UIImage, newSize: CGSize) -> UIImage { UIGraphicsBeginImageContext(newSize) image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }