IHLightCommandView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //
  2. // IHLightCommandView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/8/21.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. protocol IHLightCommandViewDelegate : NSObjectProtocol {
  11. // func regulateLampControlSwitch(_ status: String)
  12. //
  13. // func regulateLampControlBrightness(_ light: String)
  14. //
  15. // func regulateLampControlColorTemperature(_ color: String)
  16. // //学校的 - 亮度
  17. // func schoolRegulateLampControlBrightness(_ light: String)
  18. //
  19. // //学校的 - 色温
  20. // func schoolRegulateLampControlColorTemperature(_ color: String)
  21. // //学校的单个灯 - 开关
  22. // func schoolRegulateLampControlSwitch(_ status: String)
  23. //
  24. // func regulateLampControlColor(_ red: String, green: String, blue: String)
  25. //不是485的灯
  26. func setLampCWBrightnessAndColorValue(device : (address : String, type : String,location: String),brightness : String, color : String,isColor : Bool)
  27. //开关灯
  28. func setDeviceTurnOn(_ isTurnOn : Bool, _ device : (address: String, type: String,location : String))
  29. }
  30. class IHLightCommandView: UIView {
  31. //加---
  32. // var isClassRoom :Bool? {
  33. // didSet{
  34. // if let isClassRoom = isClassRoom{
  35. // if isClassRoom == true {
  36. // self.colorBtn.isHidden = true
  37. // self.whiteBtn.isHidden = true
  38. // let x = (KSCREENWIDTH - 50) * 0.5
  39. // self.closeBtn.frame = CGRect(x: x, y: 0, width: 50, height: 75)
  40. // }
  41. // }
  42. //
  43. // }
  44. // }
  45. // var schoolLight : ShoolDeviceList?{
  46. // didSet{
  47. // guard let schoolLight = schoolLight else {
  48. // return
  49. // }
  50. //
  51. // if let colorTemp = schoolLight.color,let bright = schoolLight.light ,let status = schoolLight.status{
  52. //
  53. // whiteView.lightMode = "5"
  54. // whiteView.model = schoolLight.model
  55. // whiteView.currentBrightness = "\(bright)"
  56. // whiteView.currentColorTemp = "\(colorTemp)"
  57. // colorView.currentColor = ColorStruct(Red: 0 , Green: 0, Blue: 0)
  58. // if status == "0" {
  59. // closeBtn.isSelected = false
  60. // closeView.isHidden = false
  61. // colorView.isHidden = true
  62. // whiteView.isHidden = true
  63. // whiteBtn.isSelected = false
  64. // colorBtn.isSelected = false
  65. // }else if status == "1"{
  66. //
  67. // if Intermediate.isRegulateColor == "0" {
  68. // closeBtn.isSelected = true
  69. // closeView.isHidden = true
  70. // colorView.isHidden = true
  71. // whiteView.isHidden = false
  72. // whiteBtn.isSelected = true
  73. // colorBtn.isSelected = false
  74. // }else if Intermediate.isRegulateColor == "1"{
  75. // closeBtn.isSelected = true
  76. // closeView.isHidden = true
  77. // colorView.isHidden = false
  78. // whiteView.isHidden = true
  79. // whiteBtn.isSelected = false
  80. // colorBtn.isSelected = true
  81. // }
  82. //
  83. //
  84. // }
  85. // }
  86. //
  87. // }
  88. // }
  89. // var delegate : IHLampControlViewDelegate?
  90. // var lightData : LightInfodata?{
  91. // didSet{
  92. // if let lightData = self.lightData {
  93. // if let model = lightData.model ,let colorTemp = lightData.color,let r = lightData.colorR,let g = lightData.colorG,let b = lightData.colorB,let bright = lightData.light ,let status = lightData.status{
  94. // setButtonFrame(model)
  95. // whiteView.lightMode = model
  96. // whiteView.currentBrightness = "\(bright)"
  97. // whiteView.currentColorTemp = "\(colorTemp)"
  98. // colorView.currentColor = ColorStruct(Red: r , Green: g, Blue: b)
  99. // if status == "0" {
  100. // closeBtn.isSelected = false
  101. // closeView.isHidden = false
  102. // colorView.isHidden = true
  103. // whiteView.isHidden = true
  104. // whiteBtn.isSelected = false
  105. // colorBtn.isSelected = false
  106. // }else if status == "1"{
  107. //
  108. // if Intermediate.isRegulateColor == "0" {
  109. // closeBtn.isSelected = true
  110. // closeView.isHidden = true
  111. // colorView.isHidden = true
  112. // whiteView.isHidden = false
  113. // whiteBtn.isSelected = true
  114. // colorBtn.isSelected = false
  115. // }else if Intermediate.isRegulateColor == "1"{
  116. // closeBtn.isSelected = true
  117. // closeView.isHidden = true
  118. // colorView.isHidden = false
  119. // whiteView.isHidden = true
  120. // whiteBtn.isSelected = false
  121. // colorBtn.isSelected = true
  122. // }
  123. //
  124. //
  125. // }
  126. // }
  127. // }
  128. // }
  129. // }
  130. var schollDeviceInfor : (address: String, type: String, location: String)?{
  131. didSet{
  132. if let deviceInfor = schollDeviceInfor{
  133. setLightAtribut(deviceInfor: deviceInfor)
  134. // HUD.flash(.label("还没有亮度与色温值"), delay: 0.6)
  135. // if let status = deviceInfor.status,let light = deviceInfor.light,let color = deviceInfor.color{
  136. // if status == "0"{
  137. // //关灯
  138. // colorView.isHidden = false
  139. // whiteView.isHidden = true
  140. // colorView.isHidden = true
  141. // }else{
  142. // //whiteView,colorView二个都可以亮度与色温
  143. // colorView.isHidden = true
  144. // whiteView.isHidden = false
  145. // colorView.isHidden = true
  146. //
  147. // whiteView.currentBrightness = "\(light)"
  148. // whiteView.currentColorTemp = "\(color)"
  149. // }
  150. // }
  151. }
  152. }
  153. }
  154. //初始化灯的值
  155. private func setLightAtribut(deviceInfor : (address: String, type: String, location: String)){
  156. //获取当前灯的状态
  157. let defaults = UserDefaults.standard
  158. let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
  159. if let arrDevice = arrDevice {
  160. if arrDevice.count > 0 {
  161. for subDic in arrDevice {
  162. let address = subDic["address"]!
  163. let bright = subDic["bright"]
  164. let color = subDic["color"]
  165. if deviceInfor.address == address {
  166. if bright == "0" {
  167. //关灯
  168. colorView.isHidden = true
  169. whiteView.isHidden = true
  170. closeView.isHidden = false
  171. closeBtn.isSelected = false
  172. }else{
  173. colorView.isHidden = true
  174. whiteView.isHidden = false
  175. closeView.isHidden = true
  176. closeBtn.isSelected = true
  177. //0-100
  178. //亮度cw : 0-255, 485 : 0-50
  179. let brightI = Int(bright!)!
  180. let colorI = Int(color!)!
  181. var lastBright = 0
  182. var lastColor = 0
  183. if deviceInfor.type == "31" {
  184. //485
  185. lastBright = brightI * 100 / 50
  186. lastColor = colorI * 6000 / 10
  187. }else{
  188. lastBright = brightI * 100 / 255
  189. lastColor = colorI * 6000 / 255
  190. }
  191. whiteView.currentBrightness = "\(lastBright)"
  192. //3000-6000
  193. //色温cw : 0-255, 485 : 0-10
  194. whiteView.currentColorTemp = "\(lastColor)"
  195. }
  196. break
  197. }
  198. }
  199. }
  200. }
  201. }
  202. private let gradientLayer = IHRadialGradientLayer()
  203. weak var delegate : IHLightCommandViewDelegate?
  204. // @IBOutlet weak var buttonBar: UIView!
  205. // @IBOutlet weak var whiteBtn: UIButton!
  206. @IBOutlet weak var closeBtn: UIButton!
  207. // @IBOutlet weak var colorBtn: UIButton!
  208. lazy var closeView: IHLightCloseView = {
  209. let closeView = Bundle.main.loadNibNamed("IHLightCloseView", owner: nil, options: nil)?.first as! IHLightCloseView
  210. closeView.isHidden = true
  211. return closeView
  212. }()
  213. lazy var whiteView: IHLightBrightView = {
  214. let whiteView = Bundle.main.loadNibNamed("IHLightBrightView", owner: nil, options: nil)?.first as! IHLightBrightView
  215. whiteView.isHidden = true
  216. return whiteView
  217. }()
  218. lazy var colorView: IHLightColorView = {
  219. let colorView = Bundle.main.loadNibNamed("IHLightColorView", owner: nil, options: nil)?.first as! IHLightColorView
  220. colorView.isHidden = true
  221. return colorView
  222. }()
  223. override func awakeFromNib() {
  224. super.awakeFromNib()
  225. //设置灯的亮度与色温后返回更新
  226. NotificationCenter.default.addObserver(self, selector: #selector(updateLightBrightAndColor), name: NSNotification.Name(KNotifiSetLightBrightAndColor), object: nil)
  227. //灯的开关
  228. NotificationCenter.default.addObserver(self, selector: #selector(updateLightState), name: NSNotification.Name(KNotifiDeviceTurnOn), object: nil)
  229. setupUI()
  230. }
  231. deinit {
  232. log.debug("IHLightCommandView销毁")
  233. NotificationCenter.default.removeObserver(self)
  234. }
  235. //开灯后更新界面
  236. @objc private func updateLightState(noti : Notification){
  237. let deviceState = noti.userInfo?["turnOn"] as? (oldDevice :(address: String, type: String,location : String),state : Bool,turnOn : Bool)
  238. //state是成功还是失败
  239. if deviceState?.state == true{
  240. //turnOn == true 为开,否则是关
  241. turnOnLightState(operationLightState: deviceState!.turnOn)
  242. //刷新
  243. self.schollDeviceInfor = deviceState?.oldDevice
  244. }
  245. }
  246. //关开灯
  247. private func turnOnLightState(operationLightState : Bool){
  248. //更改此light的状态
  249. let defaults = UserDefaults.standard
  250. let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
  251. var newDevice = [[String : String]]()
  252. if let arrDevice = arrDevice {
  253. newDevice = arrDevice
  254. for (index , subDic) in arrDevice.enumerated() {
  255. let address = subDic["address"]!
  256. //let bright = subDic["bright"]
  257. let color = subDic["color"]
  258. if self.schollDeviceInfor!.address == address && operationLightState == true && self.schollDeviceInfor!.type == "31"{
  259. newDevice.remove(at: index)
  260. newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "50","color" : color!])
  261. break
  262. }
  263. if self.schollDeviceInfor!.address == address && operationLightState == false && self.schollDeviceInfor!.type == "31"{
  264. newDevice.remove(at: index)
  265. newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "0","color" : color!])
  266. break
  267. }
  268. if self.schollDeviceInfor!.address == address && operationLightState == true && (self.schollDeviceInfor!.type == "04" || self.schollDeviceInfor!.type == "01"){
  269. newDevice.remove(at: index)
  270. newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "255","color" : color!])
  271. break
  272. }
  273. if self.schollDeviceInfor!.address == address && operationLightState == false && (self.schollDeviceInfor!.type == "04" || self.schollDeviceInfor!.type == "01"){
  274. newDevice.remove(at: index)
  275. newDevice.append(["address" : self.schollDeviceInfor!.address,"type" : self.schollDeviceInfor!.type,"bright" : "0","color" : color!])
  276. break
  277. }
  278. }
  279. defaults.set(newDevice, forKey: "saveDeviceList")
  280. defaults.synchronize()
  281. //刷新列表
  282. // DispatchQueue.main.async {
  283. // self.segmentedView.reloadData()
  284. // }
  285. }
  286. }
  287. @objc private func updateLightBrightAndColor(){
  288. if let deviceInfor = schollDeviceInfor{
  289. setLightAtribut(deviceInfor: deviceInfor)
  290. }
  291. }
  292. override func layoutSubviews() {
  293. super.layoutSubviews()
  294. self.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  295. closeView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  296. whiteView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  297. colorView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  298. }
  299. func setupUI() {
  300. // whiteBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  301. closeBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  302. //colorBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  303. insertSubview(closeView, at: 0)
  304. insertSubview(whiteView, at: 0)
  305. insertSubview(colorView, at: 0)
  306. callbackAction()
  307. }
  308. @IBAction func closeAction(_ sender: UIButton) {
  309. sender.isSelected = !sender.isSelected
  310. //这里不需要立即处理 刷新之后才处理
  311. if sender.isSelected == true {
  312. closeView.isHidden = true
  313. colorView.isHidden = true
  314. whiteView.isHidden = false
  315. // whiteBtn.isSelected = true
  316. // colorBtn.isSelected = false
  317. }else{
  318. closeView.isHidden = false
  319. colorView.isHidden = true
  320. whiteView.isHidden = true
  321. // whiteBtn.isSelected = false
  322. // colorBtn.isSelected = false
  323. }
  324. //let device = (address : self.schollDeviceInfor!.address, type : self.schollDeviceInfor!.type)
  325. delegate?.setDeviceTurnOn(sender.isSelected, self.schollDeviceInfor!)
  326. // if let delegate = self.delegate {
  327. // if isClassRoom == true {
  328. // delegate.schoolRegulateLampControlSwitch(sender.isSelected == true ? "1" : "0")
  329. // }else{
  330. // delegate.regulateLampControlSwitch(sender.isSelected == true ? "1" : "0")
  331. // }
  332. //
  333. // }
  334. }
  335. @IBAction func whiteAction(_ sender: UIButton) {
  336. if closeBtn.isSelected == false {
  337. g_showHUD("设备已离线")
  338. return
  339. }
  340. //sender.isSelected = !sender.isSelected
  341. closeView.isHidden = true
  342. colorView.isHidden = true
  343. whiteView.isHidden = false
  344. // whiteBtn.isSelected = true
  345. // colorBtn.isSelected = false
  346. closeBtn.isSelected = true
  347. }
  348. @IBAction func colorAction(_ sender: UIButton) {
  349. // sender.isSelected = !sender.isSelected
  350. if closeBtn.isSelected == false {
  351. g_showHUD("设备已离线")
  352. return
  353. }
  354. closeView.isHidden = true
  355. colorView.isHidden = false
  356. whiteView.isHidden = true
  357. // whiteBtn.isSelected = false
  358. // colorBtn.isSelected = true
  359. closeBtn.isSelected = true
  360. }
  361. func callbackAction() {
  362. whiteView.lampControlTrunoff = {[unowned self] in
  363. self.closeView.isHidden = false
  364. self.colorView.isHidden = true
  365. self.whiteView.isHidden = true
  366. // self.whiteBtn.isSelected = false
  367. // self.colorBtn.isSelected = false
  368. }
  369. whiteView.brightnessCallback = { [unowned self] (bright) in
  370. //100-0
  371. log.debug("----亮度")
  372. guard let deviceInfor = self.schollDeviceInfor else {
  373. return
  374. }
  375. //转换
  376. //亮度cw : 0-255, 485 : 0-50
  377. var lastBright = 0
  378. let brightI = Int(bright)!
  379. var lastColor = 0
  380. let colorI = Int(self.whiteView.currentColorTemp!)!
  381. if deviceInfor.type == "31" {
  382. //485
  383. lastBright = brightI * 50 / 100
  384. lastColor = colorI * 10 / 6000
  385. }else{
  386. lastBright = brightI * 255 / 100
  387. lastColor = colorI * 255 / 6000
  388. }
  389. self.delegate?.setLampCWBrightnessAndColorValue(device: deviceInfor , brightness: "\(lastBright)", color: "\(lastColor)",isColor: false)
  390. // if let delegate = self.delegate {
  391. // if self.isClassRoom == true {
  392. // //学校的
  393. // delegate.schoolRegulateLampControlBrightness(bright)
  394. // }else{
  395. // //酒店的
  396. // delegate.regulateLampControlBrightness(bright)
  397. // }
  398. //
  399. // }
  400. }
  401. whiteView.colorTempCallback = {[unowned self] (color) in
  402. //6000-3000 0-255
  403. log.debug("---色温")
  404. guard let deviceInfor = self.schollDeviceInfor else {
  405. return
  406. }
  407. //转换
  408. //色温cw : 0-255, 485 : 0-10
  409. var lastBright = 0
  410. let brightI = Int(self.whiteView.currentBrightness!)!
  411. var lastColor = 0
  412. let colorI = Int(color)!
  413. if deviceInfor.type == "31" {
  414. //485
  415. lastBright = brightI * 50 / 100
  416. lastColor = colorI * 10 / 6000
  417. }else{
  418. lastBright = brightI * 255 / 100
  419. lastColor = colorI * 255 / 6000
  420. }
  421. // var lastColor = 0
  422. // let colorI = Int(color)!
  423. // if deviceInfor.type == "31" {
  424. // //485
  425. // lastColor = colorI * 10 / 6000
  426. // }else{
  427. // lastColor = colorI * 255 / 6000
  428. // }
  429. self.delegate?.setLampCWBrightnessAndColorValue(device: deviceInfor, brightness: "\(lastBright)", color: "\(lastColor)",isColor : true)
  430. // if let delegate = self.delegate {
  431. // if self.isClassRoom == true{
  432. // //学校的
  433. // delegate.schoolRegulateLampControlColorTemperature(color)
  434. // }else{
  435. // //酒店的
  436. // if self.colorBtn.isHidden == false{
  437. // delegate.regulateLampControlColorTemperature(color)
  438. // }
  439. // }
  440. // }
  441. }
  442. }
  443. // colorView.paletteComplition = {(rgb) in
  444. // if let delegate = self.delegate {
  445. // delegate.regulateLampControlColor("\(rgb.Red!)", green: "\(rgb.Green!)", blue: "\(rgb.Blue!)")
  446. // }
  447. // }
  448. // }
  449. }
  450. extension IHLightCommandView {
  451. func setButtonFrame(_ model:String) {
  452. if model == "0" || model == "1" || model == "4" || model == "5" {
  453. //self.colorBtn.isHidden = true
  454. let beginX = (KSCREENWIDTH - 50 * 2 - 90) / 2
  455. self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75)
  456. // self.whiteBtn.frame = CGRect(x: beginX + 50 + 90, y: 0, width: 50, height: 75)
  457. }else {
  458. // self.colorBtn.isHidden = false
  459. let beginX = (KSCREENWIDTH - 50 * 3 - 50 * 2) / 2
  460. self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75)
  461. // self.whiteBtn.frame = CGRect(x: beginX + 50 * 2, y: 0, width: 50, height: 75)
  462. // self.colorBtn.frame = CGRect(x: beginX + 50 * 4, y: 0, width: 50, height: 75)
  463. }
  464. }
  465. }