// // IHNewEquipmentManager.swift // Inhealth // // Created by weclouds on 2019/12/14. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import Photos import AVFoundation import PKHUD let kNotificationIHNewEquipmentManagerEditEquipmentReload = "kNotificationIHNewEquipmentManagerEditEquipmentReload" let kNotifactionIHNewEquipmentManagerscanFinished = "kNotifactionIHNewEquipmentManagerscanFinished" let kNotificationReloadEquipmentInfo = "kNotificationReloadEquipmentInfo" class IHNewEquipmentManager: NSObject ,IHViewManagerProtocolDelegate{ var style:NewEquipmentRoomStyle? = .newRoom var floorId:String? var roomId:String? lazy var mainView: IHNewEquipmentView = { let mainView = Bundle.main.loadNibNamed("IHNewEquipmentView", owner: nil, options: nil)?.last as! IHNewEquipmentView mainView.delegate = self return mainView }() let imagePicker = UIImagePickerController() // private var vc = UIViewController() private weak var vc : UIViewController? func bindController(_ vc: UIViewController) { self.vc = vc //self.vc.navigationBarTitle = "New Equipment" createUI() let eq = self.vc as! IHNewEquipmentVCtr //区分是否学校 self.mainView.isSchool = eq.isSchool if let gatewayList = eq.gatewayList{ //学校时 self.mainView.gatewayList = gatewayList } self.mainView.equipmentStyle = eq.roomStyle self.roomId = eq.roomId self.floorId = eq.floorId if eq.roomStyle == .gatewayEdit { self.mainView.gateway = eq.gateway }else{ self.mainView.devData = eq.devData } imagePicker.delegate = self // imagePicker.allowsEditing = true self.imagePicker.modalPresentationStyle = .fullScreen if eq.roomStyle != .newRoom { //新建房间 getFloorList(Intermediate.buildId) } } func shouldPopViewController() -> Bool { if mainView.isBeginEdit == true { let alert = UIAlertController(title: "", message: "是否放弃编辑", preferredStyle: .alert) let save = UIAlertAction(title: "留下", style: .default) { (action) in } let cancel = UIAlertAction(title: "放弃", style: .destructive) { (action) in self.vc?.navigationController?.popViewController(animated: true) } alert.addAction(cancel) alert.addAction(save) self.vc?.present(alert, animated: true, completion: nil) return false } return true } func createUI() { mainView.frame = self.vc!.view.bounds //在小屏上显示不完整,加一个scrollview if KNavBarHeight == 64{ let scrollView = UIScrollView.init(frame: mainView.frame) scrollView.contentSize = CGSize.init(width: KSCREENWIDTH , height: KSCREENHEIGHT + 160) scrollView.bounces = true scrollView.alwaysBounceVertical = true self.mainView.size.height = self.mainView.size.height + 60 scrollView.addSubview(mainView) self.vc?.view.addSubview(scrollView) }else{ self.vc?.view.addSubview(mainView) } } func getFloorList(_ buildId:String) { if buildId == "" { g_showHUD("请选择酒店") return } IHAreaService.share.getRoomListData(buildId: buildId, keyworkds: nil, requestSuccess: { (roomlist) in self.mainView.floorList = roomlist }) { } } } extension IHNewEquipmentManager : IHNewEquipmentViewDelegate{ func scanQRCode() { //设置扫码区域参数 var style = LBXScanViewStyle() style.centerUpOffset = 44 style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.On style.photoframeLineW = 6 style.photoframeAngleW = 24 style.photoframeAngleH = 24 style.isNeedShowRetangle = true style.anmiationStyle = LBXScanViewAnimationStyle.LineMove //使用的支付宝里面网格图片 style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net") //4个角的颜色 // #05CFAB style.colorAngle = UIColor(hexString: "#573F95") //矩形框颜色 // #05CFAB style.colorRetangleLine = UIColor(hexString: "#573F95") //非矩形框区域颜色 style.color_NotRecoginitonArea = UIColor(red: 247.0/255.0, green: 202.0/255.0, blue: 15.0/255.0, alpha: 0.2) let scanvc = LBXScanViewController() scanvc.scanStyle = style scanvc.readyString = "loading..." scanvc.scanResultDelegate = self self.vc?.navigationController?.pushViewController(scanvc, animated: true) } //保存 func storageDeviceInformation(_ infomation: EquipmentInfo?, error: EquipmentError) { log.debug("infomation - \(infomation) -error - \(error)") switch error { case .success: let devType = infomation?.deviceType let deviceId = infomation?.deviceID let name = infomation?.deviceName let image = infomation?.imagePath ?? "" let lightMode = infomation?.lightMode if self.mainView.equipmentStyle == .newRoom{ //保存到数组中 Intermediate.EquipmentArr?.append(infomation!) self.vc?.navigationController?.popViewController(animated: true) }else if self.mainView.equipmentStyle == .oldRoom{ IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: floorId!, roomId: roomId!, devId: nil, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) { self.vc?.navigationController?.popViewController(animated: true) NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) } }else if self.mainView.equipmentStyle == .equipmentList{ //设备列表新建设备 if infomation?.type != nil{ //学校 IHNewEquipmentService.share.schoolSaveEquipmentInfo(lightId: nil, name: name!, deviceId: deviceId!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, image: image, type: (infomation?.type)!, model: lightMode,gatewayId:(infomation?.gatewayId)!) { HUD.flash(.label("成功"), onView: nil, delay: 0.3) { (_) in NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name ) self.vc?.navigationController?.popViewController(animated: true) } } }else{ IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: nil, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) { NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name ) self.vc?.navigationController?.popViewController(animated: true) } } }else if self.mainView.equipmentStyle == .equipmentEdit { //编辑设备 if infomation?.type != nil{ //学校 IHNewEquipmentService.share.schoolSaveEquipmentInfo(lightId: (infomation?.lightId)!, name: name!, deviceId: deviceId!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, image: image, type: (infomation?.type)!, model: lightMode, gatewayId: (infomation?.gatewayId)!) { //更新房间,也要更新列表 HUD.flash(.label("更新成功"), onView: nil, delay: 0.3) { (_) in NotificationCenter.default.post(name: NSNotification.Name(kNotificationReloadEquipmentInfo), object: nil , userInfo: ["name":name!]) self.vc?.navigationController?.popViewController(animated: true) } } }else{ let data = self.mainView.devData let id = data?.id IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: id, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) { self.vc?.navigationController?.popViewController(animated: true) //刷新房间列表 NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil) //刷新设备列表 NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name ) } } }else if self.mainView.equipmentStyle == .gatewayEdit{ let data = self.mainView.gateway let id = data?.id IHNewEquipmentService.share.editGateway(floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: id, name: name!, deviceId: deviceId!, inspecTime: (infomation?.inspecTime)) { self.vc?.navigationController?.popViewController(animated: true) NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name ) } } print(error) case .lackImage: g_showHUD("缺少设备的图片") print(error) case .lackDeviceName: g_showHUD("缺少设备的名称") print(error) case .lackDeviceID: g_showHUD("缺少设备的ID") print(error) case .lackFloorId: g_showHUD("缺少楼层") print(error) case .lackRoomId: g_showHUD("缺少房间号") print(error) case .lackInspecTime: g_showHUD("缺少周期时间") print(error) } } func selectedPhoto() { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) alert.addAction(UIAlertAction(title: "相册", style: .default, handler: { (button) in if self.checkAlbum() == true{ self.presentToImagePickerController(.photoLibrary) }else{ self.showAlart(title: "提示", message: "相册权限未启用,请进入系统[设置]>[隐私]>[照片]打开开关启用相册功能") } })) alert.addAction(UIAlertAction(title: "相机", style: .default, handler: { (button) in //判断是否支持使用相册 if UIImagePickerController.isSourceTypeAvailable(.camera){ if self.checkCamera() == true { self.presentToImagePickerController(.camera) }else{ self.showAlart(title: "提示", message: "相机权限未开启,请进入系统[设置]>[隐私]>[相机]打开开关,开启相机功能") return } }else{ g_showHUD("当前设备不支持照相功能") } })) alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) self.vc?.present(alert, animated: true, completion: nil) } func presentToImagePickerController(_ type : UIImagePickerController.SourceType) { self.imagePicker.sourceType = type self.vc?.present(self.imagePicker, animated: true, completion: nil) } func showAlart(title:String,message:String) { let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) let action = UIAlertAction(title: "I know", style: .default) { (action) in } alert.addAction(action) self.vc?.present(alert, animated: true, completion: nil) } func checkAlbum() -> Bool { let status = PHPhotoLibrary.authorizationStatus() if status == .denied || status == .restricted { return false } return true } func checkCamera() -> Bool { let status = AVCaptureDevice.authorizationStatus(for: .video) if status == .restricted || status == .denied { return false } return true } } extension IHNewEquipmentManager : UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return } //imageView.image = pickedImage log.debug("pickedImage - \(pickedImage)") NotificationCenter.default.post(name: NSNotification.Name(kIHNewEquipmentViewNotificationGetDeviceImage), object: pickedImage) self.vc?.dismiss(animated: true, completion: nil) //上传图片 //let imageData = pickedImage.pngData() HUD.show(.progress) IHNewEquipmentService.share.uploadImage(file: pickedImage, requestSuccess: { (path) in log.debug("图片地址 - \(path)") NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewEquipmentViewUploadImageFinished), object: path) HUD.flash(.success, delay: 1) }) { log.debug("上传失败") HUD.flash(.error, delay: 1) } } } extension IHNewEquipmentManager:LBXScanViewControllerDelegate{ func scanFinished(scanResult: LBXScanResult, error: String?) { log.debug("scanResult -- \(scanResult.strScanned) ---") if error == nil { log.debug("scanResult - \(scanResult.strScanned)") if let strScanned = scanResult.strScanned { NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewEquipmentManagerscanFinished), object: strScanned) } }else{ g_showHUD(error!) } } }