IHNewEquipmentManager.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // IHNewEquipmentManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/14.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Photos
  10. import AVFoundation
  11. import PKHUD
  12. let kNotificationIHNewEquipmentManagerEditEquipmentReload = "kNotificationIHNewEquipmentManagerEditEquipmentReload"
  13. let kNotifactionIHNewEquipmentManagerscanFinished = "kNotifactionIHNewEquipmentManagerscanFinished"
  14. let kNotificationReloadEquipmentInfo = "kNotificationReloadEquipmentInfo"
  15. class IHNewEquipmentManager: NSObject ,IHViewManagerProtocolDelegate{
  16. var style:NewEquipmentRoomStyle? = .newRoom
  17. var floorId:String?
  18. var roomId:String?
  19. lazy var mainView: IHNewEquipmentView = {
  20. let mainView = Bundle.main.loadNibNamed("IHNewEquipmentView", owner: nil, options: nil)?.last as! IHNewEquipmentView
  21. mainView.delegate = self
  22. return mainView
  23. }()
  24. let imagePicker = UIImagePickerController()
  25. // private var vc = UIViewController()
  26. private weak var vc : UIViewController?
  27. func bindController(_ vc: UIViewController) {
  28. self.vc = vc
  29. //self.vc.navigationBarTitle = "New Equipment"
  30. createUI()
  31. let eq = self.vc as! IHNewEquipmentVCtr
  32. //区分是否学校
  33. self.mainView.isSchool = eq.isSchool
  34. if let gatewayList = eq.gatewayList{
  35. //学校时
  36. self.mainView.gatewayList = gatewayList
  37. }
  38. self.mainView.equipmentStyle = eq.roomStyle
  39. self.roomId = eq.roomId
  40. self.floorId = eq.floorId
  41. if eq.roomStyle == .gatewayEdit {
  42. self.mainView.gateway = eq.gateway
  43. }else{
  44. self.mainView.devData = eq.devData
  45. }
  46. imagePicker.delegate = self
  47. // imagePicker.allowsEditing = true
  48. self.imagePicker.modalPresentationStyle = .fullScreen
  49. if eq.roomStyle != .newRoom {
  50. //新建房间
  51. getFloorList(Intermediate.buildId)
  52. }
  53. }
  54. func shouldPopViewController() -> Bool {
  55. if mainView.isBeginEdit == true {
  56. let alert = UIAlertController(title: "", message: "是否放弃编辑", preferredStyle: .alert)
  57. let save = UIAlertAction(title: "留下", style: .default) { (action) in
  58. }
  59. let cancel = UIAlertAction(title: "放弃", style: .destructive) { (action) in
  60. self.vc?.navigationController?.popViewController(animated: true)
  61. }
  62. alert.addAction(cancel)
  63. alert.addAction(save)
  64. self.vc?.present(alert, animated: true, completion: nil)
  65. return false
  66. }
  67. return true
  68. }
  69. func createUI() {
  70. mainView.frame = self.vc!.view.bounds
  71. //在小屏上显示不完整,加一个scrollview
  72. if KNavBarHeight == 64{
  73. let scrollView = UIScrollView.init(frame: mainView.frame)
  74. scrollView.contentSize = CGSize.init(width: KSCREENWIDTH , height: KSCREENHEIGHT + 160)
  75. scrollView.bounces = true
  76. scrollView.alwaysBounceVertical = true
  77. self.mainView.size.height = self.mainView.size.height + 60
  78. scrollView.addSubview(mainView)
  79. self.vc?.view.addSubview(scrollView)
  80. }else{
  81. self.vc?.view.addSubview(mainView)
  82. }
  83. }
  84. func getFloorList(_ buildId:String) {
  85. if buildId == "" {
  86. g_showHUD("请选择酒店")
  87. return
  88. }
  89. IHAreaService.share.getRoomListData(buildId: buildId, keyworkds: nil, requestSuccess: { (roomlist) in
  90. self.mainView.floorList = roomlist
  91. }) {
  92. }
  93. }
  94. }
  95. extension IHNewEquipmentManager : IHNewEquipmentViewDelegate{
  96. func scanQRCode() {
  97. //设置扫码区域参数
  98. var style = LBXScanViewStyle()
  99. style.centerUpOffset = 44
  100. style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.On
  101. style.photoframeLineW = 6
  102. style.photoframeAngleW = 24
  103. style.photoframeAngleH = 24
  104. style.isNeedShowRetangle = true
  105. style.anmiationStyle = LBXScanViewAnimationStyle.LineMove
  106. //使用的支付宝里面网格图片
  107. style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_part_net")
  108. //4个角的颜色
  109. // #05CFAB
  110. style.colorAngle = UIColor(hexString: "#573F95")
  111. //矩形框颜色
  112. // #05CFAB
  113. style.colorRetangleLine = UIColor(hexString: "#573F95")
  114. //非矩形框区域颜色
  115. style.color_NotRecoginitonArea = UIColor(red: 247.0/255.0, green: 202.0/255.0, blue: 15.0/255.0, alpha: 0.2)
  116. let scanvc = LBXScanViewController()
  117. scanvc.scanStyle = style
  118. scanvc.readyString = "loading..."
  119. scanvc.scanResultDelegate = self
  120. self.vc?.navigationController?.pushViewController(scanvc, animated: true)
  121. }
  122. //保存
  123. func storageDeviceInformation(_ infomation: EquipmentInfo?, error: EquipmentError) {
  124. log.debug("infomation - \(infomation) -error - \(error)")
  125. switch error {
  126. case .success:
  127. let devType = infomation?.deviceType
  128. let deviceId = infomation?.deviceID
  129. let name = infomation?.deviceName
  130. let image = infomation?.imagePath ?? ""
  131. let lightMode = infomation?.lightMode
  132. if self.mainView.equipmentStyle == .newRoom{
  133. //保存到数组中
  134. Intermediate.EquipmentArr?.append(infomation!)
  135. self.vc?.navigationController?.popViewController(animated: true)
  136. }else if self.mainView.equipmentStyle == .oldRoom{
  137. IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: floorId!, roomId: roomId!, devId: nil, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) {
  138. self.vc?.navigationController?.popViewController(animated: true)
  139. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  140. }
  141. }else if self.mainView.equipmentStyle == .equipmentList{
  142. //设备列表新建设备
  143. if infomation?.type != nil{
  144. //学校
  145. 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)!) {
  146. HUD.flash(.label("成功"), onView: nil, delay: 0.3) { (_) in
  147. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name )
  148. self.vc?.navigationController?.popViewController(animated: true)
  149. }
  150. }
  151. }else{
  152. IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: nil, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) {
  153. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name )
  154. self.vc?.navigationController?.popViewController(animated: true)
  155. }
  156. }
  157. }else if self.mainView.equipmentStyle == .equipmentEdit {
  158. //编辑设备
  159. if infomation?.type != nil{
  160. //学校
  161. 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)!) {
  162. //更新房间,也要更新列表
  163. HUD.flash(.label("更新成功"), onView: nil, delay: 0.3) { (_) in
  164. NotificationCenter.default.post(name: NSNotification.Name(kNotificationReloadEquipmentInfo), object: nil , userInfo: ["name":name!])
  165. self.vc?.navigationController?.popViewController(animated: true)
  166. }
  167. }
  168. }else{
  169. let data = self.mainView.devData
  170. let id = data?.id
  171. IHNewEquipmentService.share.saveEquipmentInfo(devType: devType!, floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: id, name: name!, deviceId: deviceId!, image: image,model:lightMode, gatewayId: (infomation?.gatewayId)!) {
  172. self.vc?.navigationController?.popViewController(animated: true)
  173. //刷新房间列表
  174. NotificationCenter.default.post(name: NSNotification.Name(kIHRoomListManagerNoticationReloadData), object: nil)
  175. //刷新设备列表
  176. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name )
  177. }
  178. }
  179. }else if self.mainView.equipmentStyle == .gatewayEdit{
  180. let data = self.mainView.gateway
  181. let id = data?.id
  182. IHNewEquipmentService.share.editGateway(floorId: (infomation?.floorId)!, roomId: (infomation?.roomId)!, devId: id, name: name!, deviceId: deviceId!, inspecTime: (infomation?.inspecTime)) {
  183. self.vc?.navigationController?.popViewController(animated: true)
  184. NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHNewEquipmentManagerEditEquipmentReload), object: name )
  185. }
  186. }
  187. print(error)
  188. case .lackImage:
  189. g_showHUD("缺少设备的图片")
  190. print(error)
  191. case .lackDeviceName:
  192. g_showHUD("缺少设备的名称")
  193. print(error)
  194. case .lackDeviceID:
  195. g_showHUD("缺少设备的ID")
  196. print(error)
  197. case .lackFloorId:
  198. g_showHUD("缺少楼层")
  199. print(error)
  200. case .lackRoomId:
  201. g_showHUD("缺少房间号")
  202. print(error)
  203. case .lackInspecTime:
  204. g_showHUD("缺少周期时间")
  205. print(error)
  206. }
  207. }
  208. func selectedPhoto() {
  209. let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  210. alert.addAction(UIAlertAction(title: "相册", style: .default, handler: { (button) in
  211. if self.checkAlbum() == true{
  212. self.presentToImagePickerController(.photoLibrary)
  213. }else{
  214. self.showAlart(title: "提示", message: "相册权限未启用,请进入系统[设置]>[隐私]>[照片]打开开关启用相册功能")
  215. }
  216. }))
  217. alert.addAction(UIAlertAction(title: "相机", style: .default, handler: { (button) in
  218. //判断是否支持使用相册
  219. if UIImagePickerController.isSourceTypeAvailable(.camera){
  220. if self.checkCamera() == true {
  221. self.presentToImagePickerController(.camera)
  222. }else{
  223. self.showAlart(title: "提示", message: "相机权限未开启,请进入系统[设置]>[隐私]>[相机]打开开关,开启相机功能")
  224. return
  225. }
  226. }else{
  227. g_showHUD("当前设备不支持照相功能")
  228. }
  229. }))
  230. alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
  231. self.vc?.present(alert, animated: true, completion: nil)
  232. }
  233. func presentToImagePickerController(_ type : UIImagePickerController.SourceType) {
  234. self.imagePicker.sourceType = type
  235. self.vc?.present(self.imagePicker, animated: true, completion: nil)
  236. }
  237. func showAlart(title:String,message:String) {
  238. let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
  239. let action = UIAlertAction(title: "I know", style: .default) { (action) in
  240. }
  241. alert.addAction(action)
  242. self.vc?.present(alert, animated: true, completion: nil)
  243. }
  244. func checkAlbum() -> Bool {
  245. let status = PHPhotoLibrary.authorizationStatus()
  246. if status == .denied || status == .restricted {
  247. return false
  248. }
  249. return true
  250. }
  251. func checkCamera() -> Bool {
  252. let status = AVCaptureDevice.authorizationStatus(for: .video)
  253. if status == .restricted || status == .denied {
  254. return false
  255. }
  256. return true
  257. }
  258. }
  259. extension IHNewEquipmentManager : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  260. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  261. guard let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }
  262. //imageView.image = pickedImage
  263. log.debug("pickedImage - \(pickedImage)")
  264. NotificationCenter.default.post(name: NSNotification.Name(kIHNewEquipmentViewNotificationGetDeviceImage), object: pickedImage)
  265. self.vc?.dismiss(animated: true, completion: nil)
  266. //上传图片
  267. //let imageData = pickedImage.pngData()
  268. HUD.show(.progress)
  269. IHNewEquipmentService.share.uploadImage(file: pickedImage, requestSuccess: { (path) in
  270. log.debug("图片地址 - \(path)")
  271. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewEquipmentViewUploadImageFinished), object: path)
  272. HUD.flash(.success, delay: 1)
  273. }) {
  274. log.debug("上传失败")
  275. HUD.flash(.error, delay: 1)
  276. }
  277. }
  278. }
  279. extension IHNewEquipmentManager:LBXScanViewControllerDelegate{
  280. func scanFinished(scanResult: LBXScanResult, error: String?) {
  281. log.debug("scanResult -- \(scanResult.strScanned) ---")
  282. if error == nil {
  283. log.debug("scanResult - \(scanResult.strScanned)")
  284. if let strScanned = scanResult.strScanned {
  285. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHNewEquipmentManagerscanFinished), object: strScanned)
  286. }
  287. }else{
  288. g_showHUD(error!)
  289. }
  290. }
  291. }