123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- //
- // 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!
- }
-
- }
|