IHEquipmentSeachManager.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //
  2. // IHEquipmentSeachManager.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/13.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. let kNotifactionIHEquipmentSeachManagerAfterDeleteEquipment = "kNotifactionIHEquipmentSeachManagerAfterDeleteEquipment"
  11. let kNotifactionIHEquipmentSeachManagerReloadSeachListData = "kNotifactionIHEquipmentSeachManagerReloadSeachListData"
  12. class IHEquipmentSeachManager:
  13. NSObject,IHViewManagerProtocolDelegate {
  14. var currentIndex :Int? = 0
  15. var hotelId : String?
  16. var devType: String? = "0"
  17. private var page = 1
  18. lazy var mainView: IHEquipmentSearchView = {[unowned self] in
  19. let mainView = IHEquipmentSearchView(frame: self.vc!.view.bounds)
  20. mainView.delegate = self
  21. return mainView
  22. }()
  23. // private var vc = UIViewController()
  24. private weak var vc : UIViewController?
  25. func bindController(_ vc: UIViewController) {
  26. self.vc = vc
  27. createUI()
  28. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(kNotifactionIHEquipmentSeachManagerReloadSeachListData), object: nil)
  29. }
  30. @objc func reloadData(_ notif:Notification){
  31. let dict = notif.object as! [String:Any]
  32. let devicelist = dict["devDataList"] as? [DevData]
  33. let gatewaylist = dict["gatewayList"] as? [GatewayData]
  34. if let list = devicelist {
  35. mainView.devDataList = list
  36. }
  37. if let list1 = gatewaylist {
  38. mainView.gatewayList = list1
  39. }
  40. }
  41. func createUI() {
  42. mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT )
  43. mainView.target = self.vc as? IHEquipmentSeachVCtr
  44. self.vc?.view.addSubview(mainView)
  45. self.vc?.navigationBarTitle = "查找设备"
  46. let searchVC = self.vc as! IHEquipmentSeachVCtr
  47. mainView.devDataList = searchVC.devDataList
  48. mainView.gatewayList = searchVC.gatewayList
  49. mainView.currentIndex = searchVC.currentIndex
  50. }
  51. }
  52. extension IHEquipmentSeachManager{
  53. //查找学校的设备(包括网关)
  54. private func searchSchoolDevice(keywords : String?){
  55. var i = 0
  56. HUD.flash(.progress, onView: nil, delay: 8) { (_) in
  57. HUD.flash(.label("请求超时"), delay: 0.8)
  58. }
  59. IHEquipmentService.share.getSchoolDevList(hotelId: Intermediate.hotelId, buildId: Intermediate.buildId, floorId:nil,roomId: nil, page:self.page , type: "4", keywords: keywords, requestSuccess: { (devList) in
  60. log.debug("---获取整个学校---\(devList)")
  61. if self.page == 1{
  62. self.mainView.searchSchoolAndHotelList = devList
  63. }else{
  64. self.mainView.searchSchoolAndHotelList = self.mainView.searchSchoolAndHotelList! + devList
  65. }
  66. i += 1
  67. if i == 2{
  68. HUD.hide()
  69. self.mainView.endReflesh()
  70. self.mainView.reloadAll = true
  71. }
  72. }) {
  73. HUD.hide()
  74. }
  75. IHGatewayService.share.getGatewayList(buildId: Intermediate.buildId,page: "\(self.page)", keywords: keywords, requestSuccess: { (gatewayList) in
  76. log.debug("---获取所有的网关---\(gatewayList)")
  77. if self.page == 1{
  78. self.mainView.searchGatewayList = gatewayList
  79. }else{
  80. self.mainView.searchGatewayList = self.mainView.searchGatewayList! + gatewayList
  81. }
  82. i += 1
  83. if i == 2{
  84. HUD.hide()
  85. self.mainView.endReflesh()
  86. self.mainView.reloadAll = true
  87. }
  88. }) {
  89. HUD.hide()
  90. }
  91. }
  92. //查找酒店的设备(包括网关)
  93. private func searchHotelDevice(keywords : String?){
  94. HUD.flash(.progress, onView: nil, delay: 8) { (_) in
  95. HUD.flash(.label("请求超时"), delay: 0.8)
  96. }
  97. var i = 0
  98. IHRoomListService.share.getDeviceList(Intermediate.hotelId, buildId: Intermediate.buildId, floorId: nil, roomId: nil, devType: "0", keywords: keywords, requestSuccess: { (deviceList) in
  99. log.debug("---获取酒店所有的设备---\(deviceList)")
  100. if self.page == 1{
  101. self.mainView.searchSchoolAndHotelList = deviceList
  102. }else{
  103. self.mainView.searchSchoolAndHotelList = self.mainView.searchSchoolAndHotelList! + deviceList
  104. }
  105. i += 1
  106. if i == 2{
  107. HUD.hide()
  108. self.mainView.endReflesh()
  109. self.mainView.reloadAll = true
  110. }
  111. }) {
  112. HUD.hide()
  113. }
  114. IHGatewayService.share.getGatewayList(buildId: Intermediate.buildId, page: "\(self.page)", keywords: keywords, requestSuccess: { (gatewayList) in
  115. log.debug("---获取所有的网关---\(gatewayList)")
  116. if self.page == 1{
  117. self.mainView.searchGatewayList = gatewayList
  118. }else{
  119. self.mainView.searchGatewayList = self.mainView.searchGatewayList! + gatewayList
  120. }
  121. i += 1
  122. if i == 2{
  123. HUD.hide()
  124. self.mainView.endReflesh()
  125. self.mainView.reloadAll = true
  126. }
  127. }) {
  128. HUD.hide()
  129. }
  130. }
  131. }
  132. //extension IHEquipmentSeachManager : IHEquipmentSearchViewDelegate{
  133. // func searchDevice(_ keyWords: String?) {
  134. // let searchVC = self.vc as? IHEquipmentSeachVCtr
  135. // if searchVC?.isSchool == "1"{
  136. // //学校
  137. // self.searchSchoolDevice(keywords: keyWords)
  138. // }else{
  139. // //酒店
  140. // self.searchHotelDevice(keywords: keyWords)
  141. // }
  142. // }
  143. //}
  144. //IHEquipmentSearchViewDelegate IHEquipmentlistViewDelegate
  145. extension IHEquipmentSeachManager: IHEquipmentSearchViewDelegate{
  146. //查找设备(包括网关)
  147. func searchDevice(_ keyWords: String?) {
  148. let searchVC = self.vc as? IHEquipmentSeachVCtr
  149. if searchVC?.isSchool == "1"{
  150. //学校
  151. self.searchSchoolDevice(keywords: keyWords)
  152. }else{
  153. //酒店
  154. self.searchHotelDevice(keywords: keyWords)
  155. }
  156. }
  157. //下拉刷新
  158. func tableviewHeaderEsReloadData(keywords: String?) {
  159. self.page = 1
  160. searchDevice(keywords)
  161. }
  162. //上拉刷新
  163. func tableviewBottonEsgetMoreData(keywords: String?) {
  164. self.page += 1
  165. searchDevice(keywords)
  166. }
  167. // //选择楼层
  168. // func selectFloorRoom(_ index : Int,floorId: String?, roomId: String?) {
  169. //
  170. // }
  171. // //选择房间
  172. // func selectFloorRoom(floorId: String) {
  173. //
  174. // }
  175. // func tableviewHeaderEsReloadData() {
  176. // mainView.endReflesh()
  177. // }
  178. //
  179. // func tableviewBottonEsgetMoreData(_index: Int) {
  180. // log.debug("查找:\(_index)")
  181. // mainView.endReflesh()
  182. // }
  183. func gatewayDelegate(_ gateway: GatewayData, segmentIndex: Int) {
  184. //删除网关
  185. IHGatewayService.share.delegateGateway(gateway.id!, requestSuccess: {
  186. // if isSuccess == true {
  187. //删除成功 刷新数据
  188. let dict = ["devId":gateway.id,"segmentIndex":"\(segmentIndex)"]
  189. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHEquipmentSeachManagerAfterDeleteEquipment), object: dict)
  190. //self.requiest(self.hotelId, devType: "\(segmentIndex)")
  191. self.vc?.navigationController?.popViewController(animated: false)
  192. //}
  193. }) {
  194. }
  195. }
  196. func equipmentListSelected(_ gateway: GatewayData?) {
  197. //点击网关
  198. let gatewayVC = IHGatewayDetailVCtr()
  199. gatewayVC.gateway = gateway
  200. self.vc?.navigationController?.pushViewController(gatewayVC, animated: true)
  201. }
  202. func equipmentListSelected(_ devData: DevData?){
  203. //点击设备
  204. let searchVC = self.vc as? IHEquipmentSeachVCtr
  205. if let data = devData {
  206. if searchVC?.isSchool == "1"{
  207. //学校
  208. // 4 教室灯,5 黑板灯,6 窗帘,7 面板
  209. if data.type == "4" || data.type == "5"{
  210. //灯
  211. let roomLight = IHLampControlVCtr()
  212. roomLight.dataSource = data
  213. roomLight.isClassRoom = true
  214. var light = ShoolDeviceList()
  215. light.id = data.id
  216. light.type = data.type
  217. light.status = data.status
  218. light.light = data.brightness
  219. light.color = data.color
  220. light.model = data.mode!
  221. light.gatewayId = data.gatewayId
  222. roomLight.shoolDevice = light
  223. self.vc?.navigationController?.pushViewController(roomLight, animated: true)
  224. }
  225. if data.type == "6"{
  226. HUD.flash(.label("还没开放--窗帘"), delay: 0.5)
  227. }
  228. if data.type == "7"{
  229. HUD.flash(.label("还没开放--面板"), delay: 0.5)
  230. }
  231. }else{
  232. //酒店
  233. if data.devType == "3" {
  234. let sensor = IHSensorVCtr()
  235. sensor.devId = data.id
  236. sensor.dataSource = data
  237. sensor.navigationBarTitle = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Sensor"
  238. self.vc?.navigationController?.pushViewController(sensor, animated: true)
  239. }else if data.devType == "4"{
  240. let air = IHAirVCtr()
  241. air.navigationBarTitle = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Air purifier"
  242. air.purifierId = data.id
  243. air.dataSource = data
  244. self.vc?.navigationController?.pushViewController(air, animated: true)
  245. }else{
  246. let roomLight = IHLampControlVCtr()
  247. if data.devType == "1" {
  248. roomLight.lampName = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Light"
  249. }else if data.devType == "2"{
  250. roomLight.lampName = (data.name?.isBlanck == false && data.name != nil) ? data.name : "HCL light"
  251. }
  252. roomLight.dataSource = data
  253. self.vc?.navigationController?.pushViewController(roomLight, animated: true)
  254. }
  255. }
  256. }
  257. }
  258. // func equipmentListSelected(_ devData: DevData?) {
  259. // //返回页面页面之后再跳转
  260. // self.vc?.navigationController?.popViewController(animated: false, {
  261. // if let data = devData {
  262. // self.gotoDetail(data)
  263. // }
  264. // })
  265. //
  266. // }
  267. func gotoDetail(_ data:DevData) {
  268. let searchVC = self.vc as! IHEquipmentSeachVCtr
  269. let lastVC = searchVC.lastVC
  270. // self.mainView.searchController?.isActive = false
  271. if data.devType == "3" {
  272. let sensor = IHSensorVCtr()
  273. sensor.devId = data.id
  274. sensor.dataSource = data
  275. sensor.navigationBarTitle = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Sensor"
  276. lastVC?.navigationController?.pushViewController(sensor, animated: true)
  277. }else if data.devType == "4"{
  278. let air = IHAirVCtr()
  279. air.navigationBarTitle = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Air purifier"
  280. air.purifierId = data.id
  281. air.dataSource = data
  282. lastVC?.navigationController?.pushViewController(air, animated: true)
  283. }else{
  284. let roomLight = IHLampControlVCtr()
  285. if data.devType == "1" {
  286. roomLight.lampName = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Light"
  287. }else if data.devType == "2"{
  288. roomLight.lampName = (data.name?.isBlanck == false && data.name != nil) ? data.name : "Circadian light"
  289. }
  290. roomLight.dataSource = data
  291. lastVC?.navigationController?.pushViewController(roomLight, animated: true)
  292. }
  293. }
  294. func gatewayUpdate(_ gateway: GatewayData,segmentIndex: Int) {
  295. //网关升级
  296. let updateVc = IHGatewayUPdateVCtr()
  297. updateVc.gateway = gateway
  298. self.vc?.navigationController?.pushViewController(updateVc, animated: true)
  299. }
  300. func gatewaySetting(_ gateway: GatewayData, segmentIndex: Int) {
  301. log.debug("网关设置")
  302. let eqment = IHNewEquipmentVCtr()
  303. eqment.roomStyle = .gatewayEdit
  304. eqment.gateway = gateway
  305. eqment.navigationBarTitle = gateway.network_name
  306. self.vc?.navigationController?.pushViewController(eqment, animated: true)
  307. }
  308. //左滑设置 - 设备
  309. func equipmentSetting(_ devData: DevData, segmentIndex: Int) {
  310. let searchVC = self.vc as? IHEquipmentSeachVCtr
  311. if searchVC?.isSchool == "0" {
  312. //原来的网关
  313. let eqment = IHNewEquipmentVCtr()
  314. eqment.roomStyle = .equipmentEdit
  315. eqment.devData = devData
  316. eqment.navigationBarTitle = devData.name
  317. self.vc?.navigationController?.pushViewController(eqment, animated: true)
  318. }else{
  319. //4 教室灯,5 黑板灯,6 窗帘,7 面板
  320. if devData.type == "4" || devData.type == "5" {
  321. let classLightVC = IHClassLightSettingCtr()
  322. classLightVC.roomStyle = .equipmentEdit
  323. classLightVC.devData = devData
  324. classLightVC.navigationBarTitle = devData.name
  325. self.vc?.navigationController?.pushViewController(classLightVC, animated: true)
  326. }else if devData.type == "6" {
  327. let classCurtainVC = IHCurtainSettingCtr()
  328. classCurtainVC.roomStyle = .equipmentEdit
  329. classCurtainVC.devData = devData
  330. classCurtainVC.navigationBarTitle = devData.name
  331. self.vc?.navigationController?.pushViewController(classCurtainVC, animated: true)
  332. }else {
  333. let classPanelVC = IHPanelSettingCtr()
  334. classPanelVC.roomStyle = .equipmentEdit
  335. classPanelVC.devData = devData
  336. classPanelVC.navigationBarTitle = devData.name
  337. self.vc?.navigationController?.pushViewController(classPanelVC, animated: true)
  338. }
  339. }
  340. }
  341. func equipmentHistory(_ devData: DevData, segmentIndex: Int) {
  342. //左滑 - 设备(历史)
  343. let searchVC = self.vc as? IHEquipmentSeachVCtr
  344. if searchVC?.isSchool == "0"{
  345. //酒店
  346. if devData.devType == "1" || devData.devType == "2" {
  347. getLampHistory(devData)
  348. }else if devData.devType == "3"{
  349. getSensorHistory(devData)
  350. }else if devData.devType == "4"{
  351. getPurifierHistory(devData)
  352. }
  353. }else{
  354. //学校
  355. HUD.flash(.label("还未开放"), delay: 0.8)
  356. }
  357. }
  358. func equipmentListDeleteDevice(_ devType: String, devId: String,segmentIndex: Int) {
  359. //删除-设备
  360. let searchVC = self.vc as? IHEquipmentSeachVCtr
  361. if searchVC?.isSchool == "0"{
  362. //灯,senor,air
  363. IHEquipmentService.share.deleteEquimemnt(devType, devId: devId) { (isSuccess) in
  364. if isSuccess == true {
  365. //删除成功 刷新数据
  366. let dict = ["devId":devId,"segmentIndex":"\(segmentIndex)"]
  367. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHEquipmentSeachManagerAfterDeleteEquipment), object: dict)
  368. //self.requiest(self.hotelId, devType: "\(segmentIndex)")
  369. self.vc?.navigationController?.popViewController(animated: false)
  370. }else{
  371. }
  372. }
  373. }else{
  374. //学校的
  375. IHEquipmentService.share.delSchoolDevice(lightId: devId) { (msg, code) in
  376. if let code = code{
  377. if(code == "0000"){
  378. HUD.flash(.label("删除成功"), onView: nil, delay: 0.2) { (_) in
  379. let dict = ["devId":devId,"segmentIndex":"\(segmentIndex)"]
  380. NotificationCenter.default.post(name: NSNotification.Name(kNotifactionIHEquipmentSeachManagerAfterDeleteEquipment), object: dict)
  381. //self.requiest(self.hotelId, devType: "\(segmentIndex)")
  382. self.vc?.navigationController?.popViewController(animated: false)
  383. }
  384. }else{
  385. HUD.flash(.label(msg!), delay: 0.2)
  386. }
  387. }
  388. }
  389. }
  390. }
  391. func equipmentlistViewdidSelectedItemAt(_ index: Int) {
  392. currentIndex = index
  393. }
  394. func getLampHistory(_ devData: DevData) {
  395. let devId = devData.id
  396. HUD.show(.progress)
  397. HUD.hide(afterDelay: 20) { (isSuccess) in
  398. if isSuccess == true{
  399. g_showHUD("请求超时")
  400. }
  401. }
  402. var energy_consumption : ReportData?
  403. var lighting_use_schedule :ReportData?
  404. let queue = DispatchQueue(label: "com.custom.thread", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent)
  405. let group = DispatchGroup()
  406. queue.async(group: group, qos: .default, flags: []) {
  407. group.enter()
  408. IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "0", date: nil, requestSuccess: { (data) in
  409. group.leave()
  410. energy_consumption = data
  411. }) {
  412. group.leave()
  413. energy_consumption = nil
  414. }
  415. }
  416. queue.async(group: group, qos: .default, flags: []) {
  417. group.enter()
  418. IHRoomLightService.share.getLampReport(devId!, dateType: "0", dataType: "1", date: nil, requestSuccess: { (data) in
  419. group.leave()
  420. lighting_use_schedule = data
  421. }) {
  422. group.leave()
  423. lighting_use_schedule = nil
  424. }
  425. }
  426. //回到主线程
  427. group.notify(queue: DispatchQueue.main) {
  428. HUD.hide()
  429. if energy_consumption != nil || lighting_use_schedule != nil{
  430. let history = IHRoomLightHistoryVCtr()
  431. history.devId = devData.id
  432. history.devType = devData.devType
  433. let lampHistory = (energy_consumption:energy_consumption,lighting_use_schedule:lighting_use_schedule) as! (energy_consumption: ReportData, lighting_use_schedule: ReportData)
  434. history.lampHistory = lampHistory
  435. self.vc?.navigationController?.pushViewController(history, animated: true)
  436. }else{
  437. g_showHUD("无数据")
  438. }
  439. }
  440. }
  441. func getSensorHistory( _ devData: DevData){
  442. log.debug("点击了")
  443. HUD.show(.progress)
  444. HUD.hide(afterDelay: 20) { (isSuccess) in
  445. if isSuccess == true{
  446. g_showHUD("请求超时")
  447. }
  448. }
  449. let historyvc = IHRoomLightHistoryVCtr()
  450. historyvc.devId = devData.id
  451. historyvc.devType = "3"
  452. IHSensorService.share.getSensorHistory(devData.id!, date_type: "0", requestSuccess: { (history) in
  453. HUD.hide()
  454. historyvc.sensorHistory = history
  455. self.vc?.navigationController?.pushViewController(historyvc, animated: true)
  456. }) {
  457. g_showHUD("感应器无历史数据")
  458. }
  459. }
  460. func getPurifierHistory(_ devData:DevData) {
  461. HUD.show(.progress)
  462. HUD.hide(afterDelay: 20) { (isSuccess) in
  463. if isSuccess == true{
  464. g_showHUD("请求超时")
  465. }
  466. }
  467. let historyvc = IHRoomLightHistoryVCtr()
  468. historyvc.devId = devData.id
  469. historyvc.devType = "4"
  470. IHAirService.share.getPurifierReport(devData.id!, dateType: "0", date: nil, requestSuccess: { (history) in
  471. HUD.hide()
  472. historyvc.purifierHistroy = history
  473. self.vc?.navigationController?.pushViewController(historyvc, animated: true)
  474. }) {
  475. g_showHUD("空气净化器无历史数据")
  476. }
  477. }
  478. }