IHClassControlView.swift 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. //
  2. // IHClassControlView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/8/19.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. protocol IHClassControlViewDelegate : NSObjectProtocol{
  11. //调色温与亮度
  12. // func clickLight(_index : Int)
  13. // func clickLight(lightDevice : ShoolDeviceList)
  14. // //更换灯的位置
  15. // func chargeLight(lists: [ShoolDeviceList])
  16. // //窗帘的开关
  17. // func curtainSetOnOff(curtainDevice : ShoolDeviceList,respone:@escaping ((_ msg : String)->Void))
  18. // //色温 3000 - 6000 ,间隔是300
  19. // func setClassLightColor(classDevices : [ShoolDeviceList])
  20. //
  21. // //所有灯的开关 -- 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式)
  22. // func lights_set_off(type : String)
  23. //ShoolDevice : ShoolDeviceList
  24. //点击跳转
  25. func pushToLightCommandVC(ShoolDevice : (address : String ,type : String,location : String))
  26. //面板情景id绑定按键id
  27. func panelBindScenesAndKeyId(panel : (address : String ,type : String,location : String),curtain : (address : String ,type : String,location : String)?,lampLights : [(address : String ,type : String,location : String)]?,classLights : [(address : String ,type : String,location : String)]?,scenes : String,keyId : String )
  28. //操作情景模式 - 全关/全开
  29. func switchScenesMode(panelAddress : String,lampLights : [(address : String ,type : String,location : String)],classLights : [(address : String ,type : String,location : String)], scenes : String ,keyId: String)
  30. //窗帘的开关
  31. func controlCurtainTurnOn(panel : (address : String ,type : String,location : String),curtainDevice : (address : String ,type : String,location : String),scenes : String)
  32. //设置设备的位置
  33. func setDeviceLocation(device : (address : String ,type : String,location : String))
  34. //交换位置后再重新获取数据
  35. func getDeviceData()
  36. }
  37. class IHClassControlView: UIView {
  38. //上课模型数组
  39. var classModelArr = [UIView]()
  40. var lightsArr = [UIView]()
  41. var blackboardLights = [UIView]()
  42. var allLightsBtn : UIButton?
  43. var cancelBtn : UIButton?
  44. var sureBtn : UIButton?
  45. private var classImage : UIImageView?
  46. var blackboardLine : UIImageView?
  47. private var line : UIImageView?
  48. var moveLight : UIView?
  49. var isLongTap : Bool = false
  50. var isBlackboardLongTap : Bool = false
  51. var clickImage : UIView?
  52. var beginPoint = CGPoint.init(x: 0, y: 0)
  53. var originalPoint = CGPoint.init(x: 0, y: 0)
  54. var isChangeLight : Bool = false
  55. var recordLights = [[UIView : CGPoint]]()
  56. weak var delegate : IHClassControlViewDelegate?
  57. // private var classLightArr = [ShoolDeviceList]()
  58. // private var blackBoardLightArr = [ShoolDeviceList]()
  59. // private var curtainArr = [ShoolDeviceList]()
  60. private var classLightArr = [(address : String ,type : String,location : String)]()
  61. private var blackBoardLightArr = [(address : String ,type : String,location : String)]()
  62. private var curtainArr = [(address : String ,type : String,location : String)]()
  63. var selectBtnIndex : Int = 0
  64. //var oldSelectBtnIndex : Int = 0
  65. var isSuccess : Bool = false
  66. var isAllLightOpen : Bool = false
  67. var isSelect : Bool = false
  68. var blackBoardLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //黑板灯的位置
  69. var classRoomLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //教室灯的位置
  70. var deviceList : [(address : String ,type : String,location : String)]?
  71. var lampLights = [(address : String,type : String,location : String)]()
  72. var classLights = [(address : String,type : String,location : String)]()
  73. var curtainDevice = [(address : String,type : String,location : String)]()
  74. //添加成功还没有设置位置
  75. var unSetClassLightLocation : [(address : String ,type : String,location : String)]?
  76. var unSetLampLightLocation : [(address : String ,type : String,location : String)]?
  77. var unSetCurtainLocation : [(address : String ,type : String,location : String)]?
  78. private var isFinish : Bool = false
  79. private var changeLocationCout = 0
  80. // //数据 - 教室设备
  81. // var shoolDivListArr : [ShoolDeviceList]? {
  82. // didSet{
  83. // if classImage == nil {
  84. // createUI()
  85. // }else{
  86. // classModelArr.removeAll()
  87. // lightsArr.removeAll()
  88. // blackboardLights.removeAll()
  89. // blackBoardLightFrames?.removeAll()
  90. // classRoomLightFrames?.removeAll()
  91. // allLightsBtn = nil
  92. // cancelBtn = nil
  93. // sureBtn = nil
  94. // classImage = nil
  95. // blackboardLine = nil
  96. // line = nil
  97. // moveLight = nil
  98. // isLongTap = false
  99. // isBlackboardLongTap = false
  100. // clickImage = nil
  101. // beginPoint = CGPoint.init(x: 0, y: 0)
  102. // originalPoint = CGPoint.init(x: 0, y: 0)
  103. // isChangeLight = false
  104. // recordLights = [[UIView : CGPoint]]()
  105. //
  106. // self.removeSubviews()
  107. // classLightArr.removeAll()
  108. // blackBoardLightArr.removeAll()
  109. // curtainArr.removeAll()
  110. //
  111. // createUI()
  112. // }
  113. //
  114. // }
  115. // }
  116. //
  117. private func resetUI(){
  118. if classImage == nil {
  119. createUI()
  120. }else{
  121. classModelArr.removeAll()
  122. lightsArr.removeAll()
  123. blackboardLights.removeAll()
  124. blackBoardLightFrames?.removeAll()
  125. classRoomLightFrames?.removeAll()
  126. allLightsBtn = nil
  127. cancelBtn = nil
  128. sureBtn = nil
  129. classImage = nil
  130. blackboardLine = nil
  131. line = nil
  132. moveLight = nil
  133. isLongTap = false
  134. isBlackboardLongTap = false
  135. clickImage = nil
  136. beginPoint = CGPoint.init(x: 0, y: 0)
  137. originalPoint = CGPoint.init(x: 0, y: 0)
  138. isChangeLight = false
  139. recordLights = [[UIView : CGPoint]]()
  140. self.removeSubviews()
  141. classLightArr.removeAll()
  142. blackBoardLightArr.removeAll()
  143. curtainArr.removeAll()
  144. lampLights.removeAll()
  145. classLights.removeAll()
  146. curtainDevice.removeAll()
  147. unSetClassLightLocation?.removeAll()
  148. unSetLampLightLocation?.removeAll()
  149. unSetCurtainLocation?.removeAll()
  150. createUI()
  151. }
  152. }
  153. override init(frame: CGRect) {
  154. super.init(frame: frame)
  155. NotificationCenter.default.addObserver(self, selector: #selector(updateAllDevices), name: NSNotification.Name(KNotifiGetAllDeviceInforFromGateway), object: nil)
  156. NotificationCenter.default.addObserver(self, selector: #selector(updateSceneMode), name: NSNotification.Name(KNotifiSwitchScenseMode), object: nil)
  157. //交换位置后,再重新数据
  158. NotificationCenter.default.addObserver(self, selector: #selector(upadateDeviceLocation), name: NSNotification.Name(KNotifiSetLightLocation), object: nil)
  159. }
  160. override func layoutSubviews() {
  161. super.layoutSubviews()
  162. }
  163. @objc private func upadateDeviceLocation(){
  164. //交换位置后,再重新数据
  165. if isFinish == true {
  166. self.delegate?.getDeviceData()
  167. }
  168. }
  169. //模式换新 - 更新界面的状态
  170. @objc private func updateSceneMode(noti : Notification){
  171. let isfail = noti.userInfo?["isfail"] as? Bool
  172. let arr = noti.userInfo?["devicesInfor"] as? [(address : String ,type : String,location : String)]
  173. //更新模式的状态
  174. if isfail == true {
  175. DispatchQueue.main.async {
  176. if arr != nil{
  177. self.deviceList?.removeAll()
  178. self.deviceList = arr
  179. }
  180. self.resetUI()
  181. }
  182. }
  183. }
  184. //所有的设备
  185. @objc private func updateAllDevices(noti : Notification){
  186. let arr = noti.userInfo?["devicesInfor"] as! [(address : String ,type : String,location : String)]
  187. log.debug("---获取带地址的设备")
  188. self.deviceList?.removeAll()
  189. self.deviceList = arr
  190. DispatchQueue.main.async {
  191. if self.isFinish == true {
  192. self.changeLocationCout = self.changeLocationCout - 1
  193. if self.changeLocationCout == 0 {
  194. self.resetUI()
  195. self.isFinish = false
  196. }
  197. }else{
  198. //找出没有位置的
  199. var unLocationDevice = [(address : String ,type : String,location : String)]()
  200. if arr.count > 0{
  201. for device in arr{
  202. if device.type != "12" && device.location == "00"{
  203. unLocationDevice.append(device)
  204. }
  205. }
  206. }
  207. self.createUI()
  208. //设置所有设备的位置 - 如果有了就不设置
  209. if unLocationDevice.count > 0{
  210. for subDevice in unLocationDevice {
  211. for locationDevice in self.deviceList! {
  212. if subDevice.address == locationDevice.address {
  213. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  214. self.delegate?.setDeviceLocation(device: locationDevice)
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. //self.isFinish = false
  222. }
  223. }
  224. required init?(coder: NSCoder) {
  225. fatalError("init(coder:) has not been implemented")
  226. }
  227. deinit {
  228. log.debug("销毁--")
  229. NotificationCenter.default.removeObserver(self)
  230. }
  231. }
  232. extension IHClassControlView{
  233. func createUI() {
  234. //设置大图片
  235. // let backImageView = UIImageView.init(frame: self.bounds)
  236. let backImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH + 2, height: self.frame.height))
  237. backImageView.image = UIImage.init(named: "背景")
  238. self.addSubview(backImageView)
  239. //811.5 49.5
  240. //教室背景图片
  241. classImage = UIImageView.init()
  242. let percent = (10.5 * KSCREENWIDTH) / 376
  243. classImage!.frame = CGRect.init(x: percent, y: KSCREENHEIGHT * 49.1 / 811.5 , width: KSCREENWIDTH - 2 * percent, height: self.frame.height - KSCREENHEIGHT * 49.1 / 811.5)
  244. classImage!.image = UIImage.init(named: "教室背景")
  245. classImage!.isUserInteractionEnabled = true
  246. self.addSubview(classImage!)
  247. // guard let deviceLists = self.shoolDivListArr else {return }
  248. //
  249. // for light in deviceLists {
  250. // //设备类型(4 教室灯,5 黑板灯,6 窗帘)
  251. // guard let type = light.type else { return}
  252. // if type == "4" {
  253. // classLightArr.append(light)
  254. //
  255. // }
  256. // if type == "5" {
  257. // blackBoardLightArr.append(light)
  258. // }
  259. // if type == "6" {
  260. // curtainArr.append(light)
  261. // }
  262. // }
  263. // for i in 0..<9 {
  264. // var list = ShoolDeviceList()
  265. // list.id = "\(i + 1)"
  266. // list.type = "\(i + 1)"
  267. // list.location = "\(i + 1)"
  268. // list.status = "1"
  269. // list.light = "100"
  270. // list.color = "6000"
  271. // list.name = "教室灯\(i + 1)"
  272. // list.model = "0"
  273. // classLightArr.append(list)
  274. // }
  275. //
  276. // for i in 0..<3 {
  277. // var list = ShoolDeviceList()
  278. // list.id = "\(i + 1)"
  279. // list.type = "\(i + 1)"
  280. // list.location = "\(i + 1)"
  281. // list.status = "1"
  282. // list.light = "100"
  283. // list.color = "6000"
  284. // list.name = "黑板灯\(i + 1)"
  285. // list.model = "0"
  286. // blackBoardLightArr.append(list)
  287. // }
  288. //
  289. // for i in 0..<2 {
  290. // var list = ShoolDeviceList()
  291. // list.id = "\(i + 1)"
  292. // list.type = "\(i + 1)"
  293. // list.location = "\(i + 1)"
  294. // list.status = "1"
  295. // list.light = "100"
  296. // list.color = "6000"
  297. // list.name = "窗帘\(i + 1)"
  298. // list.model = "0"
  299. // curtainArr.append(list)
  300. // }
  301. // //教室灯
  302. // setClassLight(lights : classLightArr)
  303. // //黑板灯
  304. // setBlackBoardLightLocation(lights : blackBoardLightArr)
  305. // // 窗帘
  306. // setCurtain(curtains :curtainArr )
  307. //假数据
  308. // var devicesL = [(address : String ,type : String,location : String)]()
  309. // for i in 0..<3 {
  310. // let device = (address : "010\(i+1)",type : "04",location : "1\(i)")
  311. // devicesL.append(device)
  312. // }
  313. // for i in 0..<9 {
  314. // let device = (address : "010\(i+1)",type : "01",location : "2\(i)")
  315. // devicesL.append(device)
  316. // }
  317. // for i in 0..<2 {
  318. // let device = (address : "010\(i+1)",type : "40",location : "0\(i+1)")
  319. // devicesL.append(device)
  320. // }
  321. // self.deviceList = devicesL
  322. guard let devices = self.deviceList else {
  323. return
  324. }
  325. let allClassLightLocation = ["20","21","22","23","24","25","26","27","28"]
  326. let allLampLightLocation = ["10","11","12"]
  327. let allCurtainLocation = ["01","02"]
  328. //已用过的
  329. var setClassLightLocation = [String]()
  330. var setLampLightLocation = [String]()
  331. var setCurtainLocation = [String]()
  332. var otherDevice = [(address : String,type : String,location : String)]()
  333. for subDevice in devices {
  334. //04: Lamp device(黑板灯)
  335. //31: sensor device(485)(教室灯)
  336. //01: CW Lamp (教室灯)
  337. //40: Curtain device (窗帘)
  338. if subDevice.type == "04" {
  339. if subDevice.location != "00"{
  340. setLampLightLocation.append(subDevice.location)
  341. }
  342. lampLights.append(subDevice)
  343. self.deviceList?.removeAll(where: {$0 == subDevice})
  344. }else if subDevice.type == "31" || subDevice.type == "01" {
  345. if subDevice.location != "00"{
  346. setClassLightLocation.append(subDevice.location)
  347. }
  348. classLights.append(subDevice)
  349. self.deviceList?.removeAll(where: {$0 == subDevice})
  350. }else if subDevice.type == "40" {
  351. if subDevice.location != "00"{
  352. setCurtainLocation.append(subDevice.location)
  353. }
  354. curtainDevice.append(subDevice)
  355. //删除指定的元素
  356. self.deviceList?.removeAll(where: {$0 == subDevice})
  357. }else{
  358. otherDevice.append(subDevice)
  359. }
  360. }
  361. //设置位置
  362. if lampLights.count > 0 {
  363. setDeviceLocation(deviceList: lampLights, setDeviceLocation: setLampLightLocation, allDeviceLocation: allLampLightLocation, deviceType: "04")
  364. }
  365. if classLights.count > 0 {
  366. setDeviceLocation(deviceList: classLights, setDeviceLocation: setClassLightLocation, allDeviceLocation: allClassLightLocation, deviceType: "01")
  367. }
  368. if curtainDevice.count > 0{
  369. setDeviceLocation(deviceList: curtainDevice, setDeviceLocation: setCurtainLocation, allDeviceLocation: allCurtainLocation, deviceType: "40")
  370. }
  371. //改变位置后的数组
  372. self.deviceList = otherDevice + lampLights + classLights + curtainDevice
  373. //教室灯
  374. setClassLight(lights : classLights)
  375. //黑板灯
  376. setBlackBoardLightLocation(lights : lampLights)
  377. // 窗帘
  378. setCurtain(curtains :curtainDevice )
  379. //
  380. //开关与色温
  381. setOnOffAndColTep()
  382. //上课模式
  383. setClassModelUI()
  384. }
  385. //初始化位置
  386. private func setDeviceLocation(deviceList : [(address : String,type : String,location : String)],setDeviceLocation : [String],allDeviceLocation : [String],deviceType : String ){
  387. //设置位置
  388. var totalLights = [(address : String,type : String,location : String)]()
  389. if setDeviceLocation.count > 0{
  390. //未用过的
  391. var unSetLocaiont = [String]()
  392. for location in allDeviceLocation {
  393. //找出还没用过的位置
  394. var i = 0
  395. for setLocation in setDeviceLocation {
  396. i = i + 1
  397. if location != setLocation && i == setDeviceLocation.count {
  398. unSetLocaiont.append(location)
  399. }
  400. if location == setLocation {
  401. break
  402. }
  403. }
  404. }
  405. //设置位置
  406. var i = 0
  407. for light in deviceList {
  408. if light.location == "00"{
  409. let subLight = (address : light.address,type : light.type,location : unSetLocaiont[i])
  410. i = i + 1
  411. totalLights.append(subLight)
  412. }else{
  413. totalLights.append(light)
  414. }
  415. }
  416. }else{
  417. //全部没用过
  418. for ( index,light )in deviceList.enumerated() {
  419. var subLight : (address : String,type : String,location : String)?
  420. if deviceType == "04" {
  421. subLight = (address : light.address,type : light.type,location : "1\(index)")
  422. }else if deviceType == "40"{
  423. subLight = (address : light.address,type : light.type,location : "0\(index + 1)")
  424. }else{
  425. subLight = (address : light.address,type : light.type,location : "2\(index)")
  426. }
  427. totalLights.append(subLight!)
  428. }
  429. }
  430. // deviceList = totalLights
  431. if deviceType == "04"{
  432. //黑板灯
  433. lampLights = totalLights
  434. }else if(deviceType == "40"){
  435. curtainDevice = totalLights
  436. }else{
  437. classLights = totalLights
  438. }
  439. }
  440. //设置上课模式
  441. private func setClassModel(classModelView_super : UIView) {
  442. let offsetX = (40 * KSCREENWIDTH) / 750
  443. let width = (78 * KSCREENWIDTH) / 750
  444. let between = (classModelView_super.size.width - offsetX * 2 - width * 5) / 5
  445. // "午休",
  446. let arr = ["上课","休息","投影","自习","板书"]
  447. for i in (0..<5) {
  448. let backView = UIView.init(frame: CGRect.init(x: offsetX + CGFloat(i) * (width + between), y: 1, width: width, height: classModelView_super.size.height - 1))
  449. backView.tag = i + 1
  450. let line = UIView.init(frame: CGRect.init(x: 0, y: 0, width: width, height: 1))
  451. line.backgroundColor = UIColor.init(hexString: "#3E328B")
  452. let btn = UIButton.init(frame: CGRect.init(x: 0, y: 1, width: width, height: backView.size.height))
  453. //btn.setImage(UIImage.init(named: "勾选"), for: .normal)
  454. btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  455. btn.setTitle(arr[i], for: .normal)
  456. btn.setTitleColor(.black, for: .normal)
  457. btn.isUserInteractionEnabled = false
  458. line.tag = tag + 1
  459. btn.tag = tag + 1
  460. backView.addSubview(line)
  461. backView.addSubview(btn)
  462. let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeClassModel))
  463. backView.addGestureRecognizer(tapGR)
  464. classModelView_super.addSubview(backView)
  465. line.backgroundColor = .white
  466. //btn.setImage(UIImage.init(named: ""), for: .normal)
  467. if selectBtnIndex != 0 {
  468. if i + 1 == selectBtnIndex {
  469. line.backgroundColor = UIColor.init(hexString: "#3E328B")
  470. btn.setImage(UIImage.init(named: "勾选"), for: .normal)
  471. }
  472. }
  473. btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal)
  474. classModelArr.append(backView)
  475. }
  476. }
  477. //教室灯 ShoolDeviceList
  478. fileprivate func setLightUI(light : (address : String,type : String,location : String) ,offsetX : CGFloat,offsetY : CGFloat,backW : CGFloat,backH: CGFloat, lightW: CGFloat,lightH: CGFloat,light_superView: UIView){
  479. // guard let location = light.location else {
  480. // return
  481. // }
  482. // guard let locationValue = Int(location) else{return}
  483. var locationValue : Int = 0
  484. switch light.location {
  485. case "20":
  486. locationValue = 1
  487. case "21":
  488. locationValue = 2
  489. case "22":
  490. locationValue = 3
  491. case "23":
  492. locationValue = 4
  493. case "24":
  494. locationValue = 5
  495. case "25":
  496. locationValue = 6
  497. case "26":
  498. locationValue = 7
  499. case "27":
  500. locationValue = 8
  501. default:
  502. locationValue = 9
  503. }
  504. let row : Int = (locationValue - 1) % 3
  505. let col : Int = (locationValue - 1) / 3
  506. let marginX = offsetX + CGFloat(row) * backW
  507. let marginY = offsetY + CGFloat(col) * backH
  508. //let X = String.init(format: "%.3f",marginX).cgFloat()
  509. let backLightView = UIView.init(frame: CGRect.init(x: Double(marginX), y: Double(marginY), width: Double(backW), height: Double(backH)))
  510. //灯
  511. let x = (backW - lightW) * 0.5
  512. let lightImageView = UIImageView.init(frame: CGRect.init(x: x, y: 0, width: lightW, height: lightH))
  513. // guard let status = light.status else {
  514. // return
  515. // }
  516. // if status == "0" {
  517. // lightImageView.image = UIImage.init(named: "大灯_暗")
  518. // }else{
  519. // lightImageView.image = UIImage.init(named: "大灯_亮")
  520. // }
  521. //灯亮的状态
  522. let defaults = UserDefaults.standard
  523. let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
  524. if let arrDevice = arrDevice {
  525. if arrDevice.count > 0 {
  526. for subDic in arrDevice {
  527. let address = subDic["address"]!
  528. let bright = subDic["bright"]
  529. if light.address == address && bright != "0"{
  530. lightImageView.image = UIImage.init(named: "大灯_亮")
  531. break
  532. }
  533. if light.address == address && bright == "0"{
  534. lightImageView.image = UIImage.init(named: "大灯_暗")
  535. break
  536. }
  537. }
  538. }
  539. }else{
  540. //如果是别人先添加的设置,自己第一次运行
  541. lightImageView.image = UIImage.init(named: "大灯_暗")
  542. }
  543. lightImageView.isUserInteractionEnabled = true
  544. backLightView.tag = locationValue
  545. lightImageView.tag = locationValue
  546. //标签
  547. let lightLabel = UILabel.init(frame: CGRect.init(x: 0, y: lightH - 20, width: backW, height: backH - lightH + 10))
  548. // lightLabel.text = light.name
  549. lightLabel.text = light.address
  550. lightLabel.textAlignment = .center
  551. lightLabel.textColor = .white
  552. lightLabel.font = UIFont.systemFont(ofSize: 12)
  553. lightLabel.isHidden = true
  554. self.clickImage = backLightView
  555. //手势
  556. let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(tapImage))
  557. let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(longTapImage))
  558. // backLightView.addGestureRecognizer(tapGR)
  559. // backLightView.addGestureRecognizer(longGR)
  560. lightImageView.addGestureRecognizer(tapGR)
  561. lightImageView.addGestureRecognizer(longGR)
  562. // light_superView.isUserInteractionEnabled = true
  563. backLightView.addSubview(lightImageView)
  564. backLightView.addSubview(lightLabel)
  565. light_superView.addSubview(backLightView)
  566. lightsArr.append(backLightView)
  567. }
  568. //设置窗帘
  569. func setCurtainUI(curtain : (address : String,type : String,location : String), curtain_superView : UIView) {
  570. //
  571. // guard let location = curtain.location else {
  572. // return
  573. // }
  574. let curtain_offsetY = ((134 + 290) * KSCREENHEIGHT) / 1623
  575. var offsetY : CGFloat = 0
  576. if curtain.location == "02" {
  577. offsetY = curtain_offsetY
  578. }
  579. let curtainH = (290 * KSCREENHEIGHT) / 1623
  580. let curtainBtn = UIButton.init(frame: CGRect.init(x: 0, y: offsetY, width: 30, height: curtainH))
  581. // guard let status = curtain.status else {
  582. // return
  583. // }
  584. // if status == "1" {
  585. // //开
  586. // curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal)
  587. // }else{
  588. // //关
  589. // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
  590. // }
  591. // guard let tag : Int = Int(location) else {
  592. // return
  593. // }
  594. let defaults = UserDefaults.standard
  595. let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
  596. if let arrDevice = arrDevice {
  597. if arrDevice.count > 0 {
  598. for subDic in arrDevice {
  599. let address = subDic["address"]!
  600. let bright = subDic["bright"]
  601. if curtain.address == address && bright != "0"{
  602. curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal)
  603. break
  604. }
  605. if curtain.address == address && bright == "0"{
  606. curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
  607. break
  608. }
  609. }
  610. }
  611. }else{
  612. curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
  613. }
  614. // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
  615. var tag = 0
  616. if curtain.location == "01"{
  617. tag = 1
  618. }else{
  619. tag = 2
  620. }
  621. curtainBtn.tag = tag
  622. curtainBtn.addTarget(self, action: #selector(switchCurtain), for: .touchUpInside)
  623. curtain_superView.addSubview(curtainBtn)
  624. }
  625. }
  626. extension IHClassControlView{
  627. //黑板灯-点击
  628. @objc func blackboardTapImage(tapGR : UIGestureRecognizer){
  629. let imageV = tapGR.view!
  630. log.debug("黑板-开关\(imageV.tag)")
  631. //教室灯长按之后,这里不允许进来
  632. if isLongTap == true {
  633. return
  634. }
  635. guard let delegate = self.delegate else{
  636. return
  637. }
  638. // for light in blackBoardLightArr {
  639. // guard let location = light.location else {
  640. // return
  641. // }
  642. // if location == String.init(format: "%d", imageV.tag){
  643. // delegate.clickLight(lightDevice: light)
  644. // break
  645. // }
  646. // }
  647. for light in lampLights {
  648. var location = 0
  649. switch light.location {
  650. case "10":
  651. location = 1
  652. case "11":
  653. location = 2
  654. default:
  655. location = 3
  656. }
  657. if imageV.tag == location {
  658. delegate.pushToLightCommandVC(ShoolDevice: light)
  659. break
  660. }
  661. }
  662. //更改
  663. // delegate.clickLight(_index: imageV.tag)
  664. }
  665. //黑板灯-长按
  666. @objc func blackboardTLongTapImage(longTapGR : UIGestureRecognizer){
  667. log.debug("黑板灯-长按灯了")
  668. if longTapGR.state == .began {
  669. for lightV in blackboardLights {
  670. lightV.isUserInteractionEnabled = false
  671. let dic = [lightV : lightV.frame.origin]
  672. recordLights.append(dic)
  673. for label in lightV.subviews {
  674. if label.isKind(of: UILabel.self) {
  675. label.isHidden = false
  676. }
  677. }
  678. }
  679. setLightRotation(lights: blackboardLights)
  680. isBlackboardLongTap = true
  681. //在黑板上盖一个
  682. guard let coverView = line else {
  683. return
  684. }
  685. let blackview = UIView.init(frame: coverView.frame)
  686. blackview.tag = 100
  687. blackview.backgroundColor = .clear
  688. classImage?.addSubview(blackview)
  689. }
  690. }
  691. //窗帘开关
  692. @objc func switchCurtain(btn: UIButton){
  693. log.debug("窗帘开关\(btn.tag)")
  694. btn.isSelected = !btn.isSelected
  695. if isBlackboardLongTap == true || isLongTap == true{
  696. return
  697. }
  698. guard let delegate = delegate else {
  699. return
  700. }
  701. for device in self.deviceList! {
  702. //面板情景id绑定按键id
  703. //操作情景模式
  704. if device.type == "12" {
  705. HUD.flash(.progress)
  706. if btn.isSelected == true {
  707. //开
  708. delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5")
  709. }else{
  710. delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5")
  711. }
  712. break
  713. }
  714. }
  715. for device in self.deviceList! {
  716. //面板情景id绑定按键id
  717. //操作情景模式
  718. if device.type == "12" {
  719. delegate.controlCurtainTurnOn(panel: device, curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
  720. break
  721. }
  722. }
  723. // if btn.isSelected == true {
  724. // //开
  725. //
  726. // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
  727. //
  728. //// delegate.panelBindScenesAndKeyId(panel: device, scenes: type, keyId: keyId)
  729. // }else{
  730. // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
  731. // }
  732. // for curtain in curtainArr {
  733. // guard let location = curtain.location else {
  734. // return
  735. // }
  736. // if location == String.init(format: "%d", btn.tag){
  737. //
  738. // delegate.curtainSetOnOff(curtainDevice: curtain, respone: { msg in
  739. // if msg == "successful operation"{
  740. // //重新加载新的数据
  741. //
  742. // }
  743. // })
  744. // }
  745. // }
  746. }
  747. //面板灯开关
  748. @objc func tapImage(tapGR : UIGestureRecognizer){
  749. let imageV = tapGR.view!
  750. log.debug("面板灯-开关\(imageV.tag)")
  751. //黑板在移动里时,这里不允许点击
  752. if isBlackboardLongTap == true {
  753. return
  754. }
  755. guard let delegate = self.delegate else{
  756. return
  757. }
  758. // for light in classLightArr {
  759. // guard let location = light.location else {
  760. // return
  761. // }
  762. // if location == String.init(format: "%d", imageV.tag){
  763. //// delegate.clickLight(lightDevice: light)
  764. // break
  765. // }
  766. // }
  767. for light in classLights {
  768. var location = 0
  769. switch light.location {
  770. case "20":
  771. location = 1
  772. case "21":
  773. location = 2
  774. case "22":
  775. location = 3
  776. case "23":
  777. location = 4
  778. case "24":
  779. location = 5
  780. case "25":
  781. location = 6
  782. case "26":
  783. location = 7
  784. case "27":
  785. location = 8
  786. default:
  787. location = 9
  788. }
  789. if imageV.tag == location {
  790. delegate.pushToLightCommandVC(ShoolDevice: light)
  791. break
  792. }
  793. }
  794. // delegate.pushToLightCommandVC(ShoolDevice: classLights[imageV.tag - 1])
  795. // delegate.clickLight(_index: imageV.tag)
  796. }
  797. //面板灯长按
  798. @objc func longTapImage(longGR : UIGestureRecognizer){
  799. if longGR.state == .began {
  800. for lightV in lightsArr {
  801. lightV.isUserInteractionEnabled = false
  802. let dic = [lightV : lightV.frame.origin]
  803. recordLights.append(dic)
  804. for label in lightV.subviews {
  805. if label.isKind(of: UILabel.self) {
  806. label.isHidden = false
  807. }
  808. }
  809. }
  810. setLightRotation(lights: lightsArr)
  811. isLongTap = true
  812. //在黑板上盖一个
  813. guard let coverView = blackboardLine else {
  814. return
  815. }
  816. let blackview = UIView.init(frame: coverView.frame)
  817. blackview.tag = 100
  818. blackview.backgroundColor = .clear
  819. classImage?.addSubview(blackview)
  820. }
  821. }
  822. //设置抖动
  823. func setLightRotation(lights : [UIView]) {
  824. let anim = CAKeyframeAnimation.init()
  825. anim.keyPath = "transform.rotation"
  826. //弧度转角度
  827. let s = 4 / 180.0 * Double.pi
  828. // anim.values = [-s,0,s,0,-s,0,s,0]
  829. anim.values = [-s,s,-s]
  830. anim.repeatCount = MAXFLOAT
  831. anim.duration = 0.2
  832. for lightView in lights {
  833. let views = lightView.subviews
  834. for subV in views {
  835. if subV.isKind(of: UIImageView.self) && subV.tag > 0 {
  836. subV.isUserInteractionEnabled = false
  837. subV.layer.add(anim, forKey: nil)
  838. }
  839. }
  840. }
  841. //隐藏-开关
  842. allLightsBtn?.isHidden = true
  843. classModelArr[0].superview?.isHidden = true
  844. //显示--选中与取消 -- 注意
  845. if blackboardLine != nil {
  846. blackboardLine!.isHidden = false
  847. }
  848. if line != nil{
  849. line!.isHidden = false
  850. }
  851. cancelBtn?.isHidden = false
  852. sureBtn?.isHidden = false
  853. }
  854. //色温
  855. @objc func changeColorTemp(tapGR : UIGestureRecognizer){
  856. log.debug("色温")
  857. guard let delegate = delegate else {
  858. return
  859. }
  860. //教室灯循环请求,全部返回后再点击发命令
  861. // delegate.setClassLightColor(classDevices: classLightArr)
  862. }
  863. //所有灯的开关
  864. @objc func switchAll(btn : UIButton){
  865. log.debug("所有灯的开关")
  866. btn.isSelected = !btn.isSelected
  867. //先要判断灯的状态
  868. guard let delegate = delegate else {
  869. return
  870. }
  871. guard let lists = self.deviceList else {
  872. return
  873. }
  874. //灯的全开/全关
  875. if btn.isSelected == true{
  876. for device in lists {
  877. if device.type == "12"{
  878. //配置情景id与按键id成功后再发送情景模式
  879. HUD.flash(.progress)
  880. delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "1", keyId: "0")
  881. break
  882. }
  883. }
  884. }else{
  885. for device in lists {
  886. if device.type == "12"{
  887. //配置情景id与按键id成功后再发送情景模式
  888. HUD.flash(.progress)
  889. delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "2", keyId: "1")
  890. break
  891. }
  892. }
  893. }
  894. // isSelect = !isSelect
  895. // if isSelect == true {
  896. //// delegate.lights_set_off(type: "1")
  897. // for device in lists {
  898. // if device.type == "12"{
  899. // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "0")
  900. // //delegate.switchScenesMode(address : device.address, scenes : "2" )
  901. // break
  902. // }
  903. // }
  904. // }else{
  905. //// delegate.lights_set_off(type: "2")
  906. //
  907. // for device in lists {
  908. // if device.type == "12"{
  909. // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "1")
  910. // //delegate.switchScenesMode(address : device.address, scenes : "2" )
  911. // break
  912. // }
  913. // }
  914. // }
  915. }
  916. func setLightModel(blackboardLight : UIView, lightName : String) {
  917. for imageV in blackboardLight.subviews {
  918. if imageV.isKind(of: UIImageView.self) && imageV.tag > 0{
  919. // imageV.isHighlighted = true
  920. let imageV = imageV as! UIImageView
  921. imageV.image = UIImage.init(named: lightName)
  922. }
  923. }
  924. }
  925. //改变上课模式
  926. @objc func changeClassModel(tapGR : UIGestureRecognizer){
  927. let view = tapGR.view!
  928. log.debug("模式:\(view.tag)")
  929. guard let lists = self.deviceList else {
  930. return
  931. }
  932. var count = 0
  933. for device in lists {
  934. if device.type == "12"{
  935. count = count + 1
  936. }
  937. }
  938. if count == 0 {
  939. HUD.flash(.label("还没添加面板"), delay: 0.7)
  940. return
  941. }
  942. UIView.animate(withDuration: 0.3) {
  943. for subView in self.classModelArr {
  944. if view.tag == subView.tag {
  945. for v in subView.subviews {
  946. if(v.tag > 0){
  947. if v.isMember(of: UIButton.self) {
  948. let btn :UIButton = v as! UIButton
  949. btn.setImage(UIImage.init(named: "勾选"), for: .normal)
  950. btn.setTitleColor(.black, for: .normal)
  951. }else{
  952. v.backgroundColor = UIColor.init(hexString: "#3E328B")
  953. }
  954. }
  955. }
  956. }else if subView.tag > 0 && view.tag != subView.tag {
  957. for v in subView.subviews {
  958. if(v.tag > 0){
  959. if v.isMember(of: UIButton.self) {
  960. let btn :UIButton = v as! UIButton
  961. //btn.setImage(UIImage.init(named: ""), for: .normal)
  962. btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal)
  963. }else{
  964. v.backgroundColor = .white
  965. }
  966. }
  967. }
  968. }
  969. }
  970. }
  971. guard let delegate = delegate else {
  972. return
  973. }
  974. // 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式,7 上课模式,8 下课模式,9 午休模式)
  975. //1-7 2-8 3-9 4-3 5-4 6-5
  976. // var type = "1"
  977. // if view.tag == 1 {
  978. // type = "7"
  979. // }
  980. // if view.tag == 2 {
  981. // type = "8"
  982. // }
  983. // if view.tag == 3 {
  984. // type = "9"
  985. // }
  986. // if view.tag == 4 {
  987. // type = "3"
  988. // }
  989. // if view.tag == 5 {
  990. // type = "4"
  991. // }
  992. // if view.tag == 6 {
  993. // type = "5"
  994. // }
  995. // 全开模式 1,全关模式 2,投影模式 3,自习模式 4,板书模式 5,窗帘开/关 6,上课模式 7,下课模式 8
  996. var type = "1"
  997. var keyId = "1"
  998. if view.tag == 1 {
  999. type = "7"
  1000. keyId = "0"
  1001. }
  1002. if view.tag == 2 {
  1003. type = "8"
  1004. keyId = "4"
  1005. }
  1006. if view.tag == 3 {
  1007. type = "2"
  1008. keyId = "1"
  1009. }
  1010. if view.tag == 4 {
  1011. type = "3"
  1012. keyId = "3"
  1013. }
  1014. if view.tag == 5 {
  1015. type = "4"
  1016. keyId = "2"
  1017. }
  1018. selectBtnIndex = view.tag
  1019. /**
  1020. 按键1 : 上课模式
  1021. 按键2 : 投影模式
  1022. 按键3 : 板书模式
  1023. 按键4 : 自习模式
  1024. 按键5 : 休息模式
  1025. 按键6 : 窗帘开关
  1026. */
  1027. for device in lists {
  1028. if device.type == "12"{
  1029. //配置情景id与按键id成功后再发送情景模式
  1030. HUD.flash(.progress)
  1031. delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: type, keyId: keyId)
  1032. break
  1033. }
  1034. }
  1035. // delegate.lights_set_off(type: type)
  1036. }
  1037. @objc func cancelChooseClass(btn: UIButton){
  1038. //取消选中教室 - 面板灯又回到原来的位置
  1039. if isLongTap == true{
  1040. var arr = [UIView]()
  1041. for dic in recordLights {
  1042. let key = dic.keys.first
  1043. let value = dic.values.first
  1044. key!.frame.origin = value!
  1045. arr.append(key!)
  1046. }
  1047. lightsArr = arr
  1048. //取消黑板灯的蒙板
  1049. let coverView = classImage?.viewWithTag(100)
  1050. coverView?.removeFromSuperview()
  1051. }
  1052. if isBlackboardLongTap == true {
  1053. var arr = [UIView]()
  1054. for dic in recordLights {
  1055. let key = dic.keys.first
  1056. let value = dic.values.first
  1057. key!.frame.origin = value!
  1058. arr.append(key!)
  1059. }
  1060. blackboardLights = arr
  1061. //取消教室灯的蒙板
  1062. let coverView = classImage?.viewWithTag(100)
  1063. coverView?.removeFromSuperview()
  1064. }
  1065. setBottomState()
  1066. }
  1067. @objc func chooseClass(btn: UIButton){
  1068. guard let delegate = delegate else { return }
  1069. //更换 -- 灯的位置
  1070. var lights = [(address : String, type : String, location : String)]()
  1071. if isBlackboardLongTap == true {
  1072. //黑板灯
  1073. //原来的位置与改变后的位置相比
  1074. for (idx,changeLocation) in blackboardLights.enumerated() {
  1075. let light = lampLights[idx]
  1076. //位置 : 0x10,0x11,0x12
  1077. var locationValue = 0
  1078. switch light.location {
  1079. case "10":
  1080. locationValue = 1
  1081. case "11":
  1082. locationValue = 2
  1083. default:
  1084. locationValue = 3
  1085. }
  1086. if changeLocation.tag != locationValue {
  1087. var lastLocation = "0"
  1088. switch changeLocation.tag {
  1089. case 1:
  1090. lastLocation = "10"
  1091. case 2:
  1092. lastLocation = "11"
  1093. default:
  1094. lastLocation = "12"
  1095. }
  1096. lights.append((address : light.address,type : light.type,location : lastLocation))
  1097. }
  1098. // guard let loc = light.location else {
  1099. // return
  1100. // }
  1101. // guard let locValue = Int(loc) else {
  1102. // return
  1103. // }
  1104. // if changeLocation.tag != locValue{
  1105. // light.location = String.init(format: "%d", changeLocation.tag)
  1106. // lights.append(light)
  1107. // }
  1108. }
  1109. }
  1110. if isLongTap == true {
  1111. //教室灯
  1112. //原来的位置与改变后的位置相比
  1113. for (idx,changeLocation) in lightsArr.enumerated() {
  1114. let light = classLights[idx]
  1115. //位置 : 0x10,0x11,0x12
  1116. var locationValue = 0
  1117. switch light.location {
  1118. case "20":
  1119. locationValue = 1
  1120. case "21":
  1121. locationValue = 2
  1122. case "22":
  1123. locationValue = 3
  1124. case "23":
  1125. locationValue = 4
  1126. case "24":
  1127. locationValue = 5
  1128. case "25":
  1129. locationValue = 6
  1130. case "26":
  1131. locationValue = 7
  1132. case "27":
  1133. locationValue = 8
  1134. default:
  1135. locationValue = 9
  1136. }
  1137. if changeLocation.tag != locationValue {
  1138. var lastLocation = "0"
  1139. switch changeLocation.tag {
  1140. case 1:
  1141. lastLocation = "20"
  1142. case 2:
  1143. lastLocation = "21"
  1144. case 3:
  1145. lastLocation = "22"
  1146. case 4:
  1147. lastLocation = "23"
  1148. case 5:
  1149. lastLocation = "24"
  1150. case 6:
  1151. lastLocation = "25"
  1152. case 7:
  1153. lastLocation = "26"
  1154. case 8:
  1155. lastLocation = "27"
  1156. default:
  1157. lastLocation = "28"
  1158. }
  1159. lights.append((address : light.address,type : light.type,location : lastLocation))
  1160. }
  1161. // guard let loc = light.location else {
  1162. // return
  1163. // }
  1164. // guard let locValue = Int(loc) else {
  1165. // return
  1166. // }
  1167. //// if changeLocation.tag == locValue{
  1168. //// return
  1169. //// }
  1170. // if changeLocation.tag != locValue{
  1171. // light.location = String.init(format: "%d", changeLocation.tag)
  1172. // lights.append(light)
  1173. // }
  1174. }
  1175. }
  1176. var i = 0
  1177. for subLight in lights {
  1178. i = i + 1
  1179. if i == lights.count{
  1180. isFinish = true
  1181. changeLocationCout = i
  1182. }
  1183. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  1184. delegate.setDeviceLocation(device: subLight)
  1185. }
  1186. }
  1187. // delegate.chargeLight(lists: lights)
  1188. //选中教室
  1189. setBottomState()
  1190. }
  1191. func setBottomState(){
  1192. //取消教室灯的蒙板
  1193. let coverView = classImage?.viewWithTag(100)
  1194. coverView?.removeFromSuperview()
  1195. if(isLongTap == true){
  1196. for lightV in lightsArr {
  1197. lightV.isUserInteractionEnabled = true
  1198. for imageView in lightV.subviews {
  1199. if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 {
  1200. imageView.isUserInteractionEnabled = true
  1201. imageView.layer.removeAllAnimations()
  1202. }
  1203. if imageView.isKind(of: UILabel.self) {
  1204. imageView.isHidden = true
  1205. }
  1206. }
  1207. }
  1208. }
  1209. if(isBlackboardLongTap == true){
  1210. for lightV in blackboardLights {
  1211. lightV.isUserInteractionEnabled = true
  1212. for imageView in lightV.subviews {
  1213. if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 {
  1214. imageView.isUserInteractionEnabled = true
  1215. imageView.layer.removeAllAnimations()
  1216. }
  1217. if imageView.isKind(of: UILabel.self) {
  1218. imageView.isHidden = true
  1219. }
  1220. }
  1221. }
  1222. }
  1223. // --- 注意
  1224. if blackboardLine != nil {
  1225. blackboardLine!.isHidden = true
  1226. }
  1227. if line != nil {
  1228. line!.isHidden = true
  1229. }
  1230. cancelBtn?.isHidden = true
  1231. sureBtn?.isHidden = true
  1232. allLightsBtn?.isHidden = false
  1233. classModelArr[0].superview?.isHidden = false
  1234. isLongTap = false
  1235. isBlackboardLongTap = false
  1236. }
  1237. }
  1238. //上课模式
  1239. extension IHClassControlView{
  1240. func setClassModelUI() {
  1241. //上课模式
  1242. // let classY = (1321 * KSCREENHEIGHT) / 811.5
  1243. // let classH = (82 * KSCREENHEIGHT) / 811.5
  1244. let classY = (632 * KSCREENHEIGHT) / 811.5
  1245. let classH = (41 * KSCREENHEIGHT) / 811.5
  1246. let classModelView = UIView.init(frame: CGRect.init(x: 0, y: classY , width: classImage!.size.width, height: classH))
  1247. classModelView.backgroundColor = .white
  1248. classModelView.cornerRadius = 8
  1249. classModelView.layer.masksToBounds = true
  1250. classImage!.addSubview(classModelView)
  1251. setClassModel(classModelView_super: classModelView)
  1252. //选中界面下的 x和v
  1253. let btnX = (254 * KSCREENWIDTH) / 750
  1254. let btnY = (1311 * KSCREENHEIGHT) / 1623
  1255. let btnWH = (60 * KSCREENWIDTH) / 750
  1256. cancelBtn = UIButton.init(frame: CGRect.init(x: btnX, y: btnY - 5, width: btnWH, height: btnWH))
  1257. cancelBtn?.setImage(UIImage.init(named: "取消"), for: .normal)
  1258. cancelBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7)
  1259. cancelBtn?.addTarget(self, action: #selector(cancelChooseClass), for: .touchUpInside)
  1260. cancelBtn?.cornerRadius = btnWH / 2
  1261. cancelBtn?.layer.masksToBounds = true
  1262. cancelBtn?.isHidden = true
  1263. classImage!.addSubview(cancelBtn!)
  1264. let sbtnX = (394 * KSCREENWIDTH) / 750
  1265. sureBtn = UIButton.init(frame: CGRect.init(x: sbtnX, y: btnY - 5, width: btnWH, height: btnWH))
  1266. sureBtn?.setImage(UIImage.init(named: "确认"), for: .normal)
  1267. sureBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7)
  1268. sureBtn?.addTarget(self, action: #selector(chooseClass), for: .touchUpInside)
  1269. sureBtn?.isHidden = true
  1270. sureBtn?.cornerRadius = btnWH / 2
  1271. sureBtn?.layer.masksToBounds = true
  1272. classImage!.addSubview(sureBtn!)
  1273. }
  1274. }
  1275. //开关与色温
  1276. extension IHClassControlView{
  1277. func setOnOffAndColTep() {
  1278. //所有灯的开关
  1279. let lightsX = (9.5 * KSCREENWIDTH) / 376
  1280. let lightsY = (582 * KSCREENHEIGHT) / 811.5
  1281. let lightsW = (35 * KSCREENWIDTH) / 376
  1282. allLightsBtn = UIButton.init(frame: CGRect.init(x: lightsX, y: lightsY, width: lightsW, height: lightsW))
  1283. allLightsBtn?.isSelected = isSelect
  1284. if isSelect == false{
  1285. allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .normal)
  1286. allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .selected)
  1287. }else{
  1288. allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .normal)
  1289. allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .selected)
  1290. }
  1291. allLightsBtn?.addTarget(self, action: #selector(switchAll), for: .touchUpInside)
  1292. classImage!.addSubview(allLightsBtn!)
  1293. //色温开关
  1294. let tempX = (309 * KSCREENWIDTH) / 376
  1295. let tempImageV = UIImageView.init(frame: CGRect.init(x: tempX, y: lightsY, width: lightsW, height: lightsW))
  1296. tempImageV.image = UIImage.init(named: "色温")
  1297. let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeColorTemp))
  1298. tempImageV.addGestureRecognizer(tapGR)
  1299. tempImageV.isUserInteractionEnabled = true
  1300. //暂时取消
  1301. //classImage!.addSubview(tempImageV)
  1302. }
  1303. }
  1304. //教室灯
  1305. extension IHClassControlView{
  1306. //[ShoolDeviceList]
  1307. fileprivate func setClassLight(lights :[(address : String,type : String,location : String)]){
  1308. if lights.count == 0 {
  1309. return
  1310. }
  1311. //面板灯的网格线
  1312. let lineX = (49 * KSCREENWIDTH) / 750
  1313. let lineY = (222 * KSCREENHEIGHT) / 1623
  1314. let lineH = (1029 * KSCREENHEIGHT) / 1623
  1315. line = UIImageView.init(frame: CGRect.init(x: lineX, y: lineY, width: classImage!.size.width - 2 * lineX, height: lineH))
  1316. line!.image = UIImage.init(named: "3-3网格")
  1317. line!.isHidden = true
  1318. classImage!.addSubview(line!)
  1319. //面板灯222
  1320. let offsetY = (202 * KSCREENHEIGHT) / 1623
  1321. let offsetX = (49 * KSCREENWIDTH) / 750
  1322. let backW = ((708 - 49 * 2) / 3 * KSCREENWIDTH) / 750
  1323. let backH = ((1029 * KSCREENHEIGHT) / 1623) / 3
  1324. let lightW : CGFloat = (120 * KSCREENWIDTH) / 750
  1325. let lightH = (306 * KSCREENHEIGHT) / 1623
  1326. //计算所有按键的位置
  1327. setAllClassRoomLightFrame(offsetX: offsetX, offsetY: offsetY, backW: backW, backH: backH)
  1328. for light in lights {
  1329. setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!)
  1330. }
  1331. // for light in lights {
  1332. //
  1333. // setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!)
  1334. // }
  1335. }
  1336. }
  1337. //计算所有的灯的位置
  1338. extension IHClassControlView {
  1339. //设置教室灯的位置
  1340. private func setAllClassRoomLightFrame (offsetX : CGFloat, offsetY : CGFloat, backW : CGFloat,backH: CGFloat){
  1341. var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]()
  1342. for i in 0..<9 {
  1343. let row : Int = i % 3
  1344. let col : Int = i / 3
  1345. let marginX = offsetX + CGFloat(row) * backW
  1346. let marginY = offsetY + CGFloat(col) * backH
  1347. let X = String.init(format: "%.3f", marginX).cgFloat()
  1348. frames.append((x: X!, y: marginY, w: backW, h: backH))
  1349. }
  1350. classRoomLightFrames = frames
  1351. }
  1352. private func setBlackBoardLightFrames(width: CGFloat,height : CGFloat) {
  1353. var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]()
  1354. for i in 0..<3 {
  1355. var offsetX : CGFloat = 102
  1356. offsetX = offsetX + width * CGFloat(i)
  1357. let light_percent = (offsetX * KSCREENWIDTH) / 750
  1358. let width = (width * KSCREENWIDTH) / 750
  1359. let X = String.init(format: "%.3f", light_percent).cgFloat()
  1360. frames.append((x : X!, y: 0, w : width, h : height))
  1361. }
  1362. blackBoardLightFrames = frames
  1363. }
  1364. }
  1365. //窗帘
  1366. extension IHClassControlView{
  1367. //[ShoolDeviceList]
  1368. func setCurtain(curtains : [(address : String,type : String,location : String)]) {
  1369. if curtains.count > 0 {
  1370. //窗帘 360
  1371. let curtain_percent = (360 * KSCREENHEIGHT) / 1623
  1372. let height = ((290 * 2 + 134) * KSCREENHEIGHT) / 1623
  1373. let curtainView = UIView.init(frame: CGRect.init(x: 0, y: curtain_percent, width: 44, height: height))
  1374. curtainView.backgroundColor = .clear
  1375. classImage!.addSubview(curtainView)
  1376. for curtain in curtains {
  1377. setCurtainUI(curtain : curtain, curtain_superView: curtainView)
  1378. }
  1379. }
  1380. }
  1381. }
  1382. //黑板灯
  1383. extension IHClassControlView {
  1384. //[ShoolDeviceList]
  1385. fileprivate func setBlackBoardLightLocation(lights : [(address : String,type : String,location : String)]) {
  1386. if lights.count == 0 {
  1387. return
  1388. }
  1389. //黑板灯的网格线
  1390. let blackboardLineX = (102 * KSCREENWIDTH) / 750
  1391. let blackboardLineH = (136 * KSCREENHEIGHT) / 1623
  1392. blackboardLine = UIImageView.init(frame: CGRect.init(x: blackboardLineX, y: 0, width: classImage!.size.width - 2 * blackboardLineX, height: blackboardLineH))
  1393. blackboardLine!.image = UIImage.init(named: "1-3网格")
  1394. blackboardLine!.isHidden = true
  1395. classImage!.addSubview(blackboardLine!)
  1396. let w : CGFloat = (708 - 102 * 2) / 3
  1397. //计算所有的黑板灯的位置
  1398. setBlackBoardLightFrames(width: w,height : blackboardLineH)
  1399. for light in lights {
  1400. setBlackBoardLight(light : light,width: w,height : blackboardLineH,blackBoardLight_superView: classImage!)
  1401. }
  1402. }
  1403. //设置黑板灯
  1404. //ShoolDeviceList
  1405. fileprivate func setBlackBoardLight(light : (address : String,type : String,location : String),width: CGFloat,height : CGFloat, blackBoardLight_superView : UIView) {
  1406. // guard let location = light.location else {
  1407. // return
  1408. // }
  1409. var offsetX : CGFloat = 102
  1410. if light.location == "11" {
  1411. offsetX = offsetX + width
  1412. }
  1413. if light.location == "12" {
  1414. offsetX = offsetX + width * 2
  1415. }
  1416. let light_percent = (offsetX * KSCREENWIDTH) / 750
  1417. let width = (width * KSCREENWIDTH) / 750
  1418. //let blackboardView = UIView.init(frame: CGRect.init(x: light_percent, y: 0, width: width, height: height))
  1419. let X = String.init(format: "%.3f",light_percent).cgFloat()
  1420. let blackboardView = UIView.init(frame: CGRect.init(x: X!, y: 0, width: width, height: height))
  1421. //灯
  1422. // guard let status = light.status else {
  1423. // return
  1424. // }
  1425. // let blackboard_light = UIImageView.init()
  1426. // if status == "0" {
  1427. // blackboard_light.image = UIImage.init(named: "黑板灯_暗")
  1428. // }else{
  1429. // blackboard_light.image = UIImage.init(named: "黑板灯_亮")
  1430. // }
  1431. let blackboard_light = UIImageView.init()
  1432. //灯亮的状态
  1433. let defaults = UserDefaults.standard
  1434. let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
  1435. if let arrDevice = arrDevice {
  1436. if arrDevice.count > 0 {
  1437. for subDic in arrDevice {
  1438. let address = subDic["address"]!
  1439. let bright = subDic["bright"]
  1440. if light.address == address && bright != "0"{
  1441. blackboard_light.image = UIImage.init(named: "黑板灯_亮")
  1442. break
  1443. }
  1444. if light.address == address && bright == "0"{
  1445. blackboard_light.image = UIImage.init(named: "黑板灯_暗")
  1446. break
  1447. }
  1448. }
  1449. }
  1450. }else{
  1451. blackboard_light.image = UIImage.init(named: "黑板灯_暗")
  1452. }
  1453. blackboard_light.sizeToFit()
  1454. blackboard_light.frame.origin = CGPoint.init(x: (blackboardView.frame.size.width - blackboard_light.frame.size.width) * 0.5, y: -10)
  1455. // guard let tag = Int(location) else {
  1456. // return
  1457. // }
  1458. var tag = 0
  1459. if light.location == "10"{
  1460. tag = 1
  1461. }else if light.location == "11"{
  1462. tag = 2
  1463. }else{
  1464. tag = 3
  1465. }
  1466. blackboardView.tag = tag
  1467. blackboard_light.tag = tag
  1468. //标签
  1469. let blackboardLabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: width, height: 34))
  1470. // blackboardLabel.text = light.name
  1471. blackboardLabel.text = light.address
  1472. blackboardLabel.textColor = .white
  1473. blackboardLabel.font = UIFont.systemFont(ofSize: 12)
  1474. blackboardLabel.textAlignment = .center
  1475. blackboardLabel.isHidden = true
  1476. blackboardView.addSubview(blackboard_light)
  1477. blackboardView.addSubview(blackboardLabel)
  1478. blackBoardLight_superView.addSubview(blackboardView)
  1479. //手势
  1480. let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(blackboardTapImage))
  1481. let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(blackboardTLongTapImage))
  1482. blackboardView.addGestureRecognizer(tapGR)
  1483. blackboardView.addGestureRecognizer(longGR)
  1484. self.clickImage = blackboardView
  1485. blackboardLights.append(blackboardView)
  1486. }
  1487. }
  1488. extension IHClassControlView {
  1489. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  1490. //长按之后
  1491. if isLongTap == true {
  1492. lightTouch(touches: touches, lights: lightsArr)
  1493. }
  1494. if isBlackboardLongTap == true {
  1495. lightTouch(touches: touches, lights: blackboardLights)
  1496. }
  1497. }
  1498. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  1499. if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)) {
  1500. if isLongTap == true{
  1501. lightMove(touches: touches)
  1502. }
  1503. if isBlackboardLongTap == true {
  1504. lightMove(touches: touches)
  1505. }
  1506. }
  1507. }
  1508. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  1509. log.debug("结束--")
  1510. if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)){
  1511. if isLongTap == true{
  1512. lightEnd(touches: touches, lights: lightsArr)
  1513. }
  1514. if isBlackboardLongTap == true {
  1515. lightEnd(touches: touches, lights: blackboardLights)
  1516. }
  1517. }
  1518. }
  1519. //面板灯的 - 点击
  1520. func lightTouch(touches: Set<UITouch>,lights : [UIView]){
  1521. //遍历所有的面板灯的坐标
  1522. for touch:AnyObject in touches {
  1523. let touche : UITouch = touch as! UITouch
  1524. let point = touche.location(in: classImage)
  1525. for lightView in lights {
  1526. let bool = lightView.frame.contains(point)
  1527. if bool == true {
  1528. classImage!.bringSubviewToFront(lightView)
  1529. beginPoint = point
  1530. moveLight = lightView
  1531. originalPoint = lightView.frame.origin
  1532. }
  1533. }
  1534. }
  1535. }
  1536. //面板灯 - 移动
  1537. func lightMove(touches: Set<UITouch>) {
  1538. for touch:AnyObject in touches {
  1539. let touche : UITouch = touch as! UITouch
  1540. let point = touche.location(in: classImage)
  1541. let x = point.x - beginPoint.x
  1542. let y = point.y - beginPoint.y
  1543. guard let moveLight = moveLight else {
  1544. return
  1545. }
  1546. moveLight.center.x = moveLight.center.x + x
  1547. moveLight.center.y = moveLight.center.y + y
  1548. beginPoint = point
  1549. }
  1550. }
  1551. //灯 - 停止
  1552. func lightEnd(touches: Set<UITouch>,lights : [UIView]) {
  1553. for touch:AnyObject in touches {
  1554. let touche : UITouch = touch as! UITouch
  1555. let endPoint = touche.location(in: classImage)
  1556. //1.先遍历点是否包含在frame的数组中
  1557. //2.1. 如果移动的终点是没有灯的,就直接修改该灯的位置
  1558. //2.2. 如果移动的终点是有灯的,就与此灯交换位置
  1559. var isContain = false
  1560. var contained = false
  1561. var lightFrames = [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]()
  1562. if isLongTap == true {
  1563. lightFrames = self.classRoomLightFrames!
  1564. }
  1565. if isBlackboardLongTap == true {
  1566. lightFrames = self.blackBoardLightFrames!
  1567. }
  1568. for (index,frameTupe) in lightFrames.enumerated() {
  1569. let frame = CGRect.init(x: frameTupe.x, y: frameTupe.y, width: frameTupe.w, height: frameTupe.h)
  1570. isContain = frame.contains(endPoint)
  1571. let frameX = String.init(format: "%.3f", frame.origin.x)
  1572. let frameY = String.init(format: "%.3f", frame.origin.y)
  1573. //再看是否有可以交换的灯
  1574. if isContain == true{
  1575. contained = isContain
  1576. var ischange = 0
  1577. for light in lights {
  1578. //&& light.frame != moveLight!.frame
  1579. let lightFrameX = String.init(format: "%.3f", light.frame.origin.x)
  1580. let lightFrameY = String.init(format: "%.3f", light.frame.origin.y)
  1581. //创建view精度会改变,所以把origin取二位做比较
  1582. if frameX == lightFrameX && frameY == lightFrameY {
  1583. //有交换的灯,但不能交换自己
  1584. log.debug("有交换的\(frame),\(light.frame)")
  1585. ischange = 1
  1586. light.frame.origin = originalPoint
  1587. light.tag = moveLight!.tag
  1588. moveLight!.frame = frame
  1589. moveLight!.tag = index + 1
  1590. beginPoint = CGPoint.init(x: 0, y: 0)
  1591. break
  1592. }
  1593. }
  1594. if ischange == 0 {
  1595. //没有交换的,直接修改位置
  1596. log.debug("直接修改位置\(frame)")
  1597. moveLight?.tag = index + 1
  1598. moveLight?.frame = frame
  1599. beginPoint = CGPoint.init(x: 0, y: 0)
  1600. break
  1601. }
  1602. }
  1603. }
  1604. if isContain == false && contained == false{
  1605. //结束点是否在 这些
  1606. log.debug("不在classRoomFrame范围内\(frame)")
  1607. moveLight?.frame.origin = originalPoint
  1608. beginPoint = CGPoint.init(x: 0, y: 0)
  1609. break
  1610. }
  1611. // for lightView in lights{
  1612. // let bool = lightView.frame.contains(endPoint)
  1613. // log.debug("包含:\(bool),\(lightView)" )
  1614. // if bool == true {
  1615. //
  1616. // if lightView.isEqual(moveLight) {
  1617. // //2.如果不挡住就回原来的位置
  1618. // lightView.frame.origin = originalPoint
  1619. // beginPoint = CGPoint.init(x: 0, y: 0)
  1620. // }else{
  1621. //
  1622. // //1.如果挡住就置换
  1623. // let end = lightView.frame.origin
  1624. // lightView.frame.origin = originalPoint
  1625. // moveLight?.frame.origin = end
  1626. //
  1627. // //更换location
  1628. // let location = lightView.tag
  1629. // guard let moveLight = moveLight else {
  1630. // return
  1631. // }
  1632. // lightView.tag = moveLight.tag
  1633. // moveLight.tag = location
  1634. // beginPoint = CGPoint.init(x: 0, y: 0)
  1635. //
  1636. // break
  1637. // }
  1638. // }
  1639. // }
  1640. }
  1641. }
  1642. }