1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894 |
- //
- // IHClassControlView.swift
- // Inhealth
- //
- // Created by weclouds on 2020/8/19.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- import PKHUD
- protocol IHClassControlViewDelegate : NSObjectProtocol{
- //调色温与亮度
- // func clickLight(_index : Int)
- // func clickLight(lightDevice : ShoolDeviceList)
- // //更换灯的位置
- // func chargeLight(lists: [ShoolDeviceList])
- // //窗帘的开关
- // func curtainSetOnOff(curtainDevice : ShoolDeviceList,respone:@escaping ((_ msg : String)->Void))
- // //色温 3000 - 6000 ,间隔是300
- // func setClassLightColor(classDevices : [ShoolDeviceList])
- //
- // //所有灯的开关 -- 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式)
- // func lights_set_off(type : String)
-
- //ShoolDevice : ShoolDeviceList
- //点击跳转
- func pushToLightCommandVC(ShoolDevice : (address : String ,type : String,location : String))
-
- //面板情景id绑定按键id
- 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 )
-
- //操作情景模式 - 全关/全开
- func switchScenesMode(panelAddress : String,lampLights : [(address : String ,type : String,location : String)],classLights : [(address : String ,type : String,location : String)], scenes : String ,keyId: String)
-
- //窗帘的开关
- func controlCurtainTurnOn(panel : (address : String ,type : String,location : String),curtainDevice : (address : String ,type : String,location : String),scenes : String)
-
- //设置设备的位置
- func setDeviceLocation(device : (address : String ,type : String,location : String))
- //交换位置后再重新获取数据
- func getDeviceData()
- }
- class IHClassControlView: UIView {
- //上课模型数组
- var classModelArr = [UIView]()
- var lightsArr = [UIView]()
- var blackboardLights = [UIView]()
- var allLightsBtn : UIButton?
- var cancelBtn : UIButton?
- var sureBtn : UIButton?
- private var classImage : UIImageView?
- var blackboardLine : UIImageView?
- private var line : UIImageView?
- var moveLight : UIView?
- var isLongTap : Bool = false
- var isBlackboardLongTap : Bool = false
- var clickImage : UIView?
- var beginPoint = CGPoint.init(x: 0, y: 0)
- var originalPoint = CGPoint.init(x: 0, y: 0)
- var isChangeLight : Bool = false
- var recordLights = [[UIView : CGPoint]]()
- weak var delegate : IHClassControlViewDelegate?
- // private var classLightArr = [ShoolDeviceList]()
- // private var blackBoardLightArr = [ShoolDeviceList]()
- // private var curtainArr = [ShoolDeviceList]()
-
- private var classLightArr = [(address : String ,type : String,location : String)]()
- private var blackBoardLightArr = [(address : String ,type : String,location : String)]()
- private var curtainArr = [(address : String ,type : String,location : String)]()
-
- var selectBtnIndex : Int = 0
- //var oldSelectBtnIndex : Int = 0
- var isSuccess : Bool = false
- var isAllLightOpen : Bool = false
- var isSelect : Bool = false
- var blackBoardLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //黑板灯的位置
- var classRoomLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //教室灯的位置
-
-
- var deviceList : [(address : String ,type : String,location : String)]?
- var lampLights = [(address : String,type : String,location : String)]()
- var classLights = [(address : String,type : String,location : String)]()
- var curtainDevice = [(address : String,type : String,location : String)]()
-
- //添加成功还没有设置位置
- var unSetClassLightLocation : [(address : String ,type : String,location : String)]?
- var unSetLampLightLocation : [(address : String ,type : String,location : String)]?
- var unSetCurtainLocation : [(address : String ,type : String,location : String)]?
- private var isFinish : Bool = false
- private var changeLocationCout = 0
- // //数据 - 教室设备
- // var shoolDivListArr : [ShoolDeviceList]? {
- // didSet{
- // if classImage == nil {
- // createUI()
- // }else{
- // classModelArr.removeAll()
- // lightsArr.removeAll()
- // blackboardLights.removeAll()
- // blackBoardLightFrames?.removeAll()
- // classRoomLightFrames?.removeAll()
- // allLightsBtn = nil
- // cancelBtn = nil
- // sureBtn = nil
- // classImage = nil
- // blackboardLine = nil
- // line = nil
- // moveLight = nil
- // isLongTap = false
- // isBlackboardLongTap = false
- // clickImage = nil
- // beginPoint = CGPoint.init(x: 0, y: 0)
- // originalPoint = CGPoint.init(x: 0, y: 0)
- // isChangeLight = false
- // recordLights = [[UIView : CGPoint]]()
- //
- // self.removeSubviews()
- // classLightArr.removeAll()
- // blackBoardLightArr.removeAll()
- // curtainArr.removeAll()
- //
- // createUI()
- // }
- //
- // }
- // }
- //
- private func resetUI(){
- if classImage == nil {
- createUI()
- }else{
- classModelArr.removeAll()
- lightsArr.removeAll()
- blackboardLights.removeAll()
- blackBoardLightFrames?.removeAll()
- classRoomLightFrames?.removeAll()
- allLightsBtn = nil
- cancelBtn = nil
- sureBtn = nil
- classImage = nil
- blackboardLine = nil
- line = nil
- moveLight = nil
- isLongTap = false
- isBlackboardLongTap = false
- clickImage = nil
- beginPoint = CGPoint.init(x: 0, y: 0)
- originalPoint = CGPoint.init(x: 0, y: 0)
- isChangeLight = false
- recordLights = [[UIView : CGPoint]]()
-
- self.removeSubviews()
- classLightArr.removeAll()
- blackBoardLightArr.removeAll()
- curtainArr.removeAll()
-
-
- lampLights.removeAll()
- classLights.removeAll()
- curtainDevice.removeAll()
- unSetClassLightLocation?.removeAll()
- unSetLampLightLocation?.removeAll()
- unSetCurtainLocation?.removeAll()
-
-
- createUI()
- }
-
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- NotificationCenter.default.addObserver(self, selector: #selector(updateAllDevices), name: NSNotification.Name(KNotifiGetAllDeviceInforFromGateway), object: nil)
-
- NotificationCenter.default.addObserver(self, selector: #selector(updateSceneMode), name: NSNotification.Name(KNotifiSwitchScenseMode), object: nil)
- //交换位置后,再重新数据
- NotificationCenter.default.addObserver(self, selector: #selector(upadateDeviceLocation), name: NSNotification.Name(KNotifiSetLightLocation), object: nil)
-
- }
- override func layoutSubviews() {
- super.layoutSubviews()
-
- }
-
- @objc private func upadateDeviceLocation(){
- //交换位置后,再重新数据
- if isFinish == true {
- self.delegate?.getDeviceData()
-
- }
- }
-
- //模式换新 - 更新界面的状态
- @objc private func updateSceneMode(noti : Notification){
- let isfail = noti.userInfo?["isfail"] as? Bool
- let arr = noti.userInfo?["devicesInfor"] as? [(address : String ,type : String,location : String)]
- //更新模式的状态
- if isfail == true {
- DispatchQueue.main.async {
- if arr != nil{
- self.deviceList?.removeAll()
- self.deviceList = arr
- }
- self.resetUI()
- }
-
- }
-
- }
- //所有的设备
- @objc private func updateAllDevices(noti : Notification){
-
- let arr = noti.userInfo?["devicesInfor"] as! [(address : String ,type : String,location : String)]
- log.debug("---获取带地址的设备")
- self.deviceList?.removeAll()
- self.deviceList = arr
-
- DispatchQueue.main.async {
- if self.isFinish == true {
- self.changeLocationCout = self.changeLocationCout - 1
- if self.changeLocationCout == 0 {
- self.resetUI()
- self.isFinish = false
- }
-
-
- }else{
- //找出没有位置的
- var unLocationDevice = [(address : String ,type : String,location : String)]()
- if arr.count > 0{
- for device in arr{
- if device.type != "12" && device.location == "00"{
- unLocationDevice.append(device)
- }
- }
- }
-
-
- self.createUI()
-
- //设置所有设备的位置 - 如果有了就不设置
- if unLocationDevice.count > 0{
- for subDevice in unLocationDevice {
- for locationDevice in self.deviceList! {
- if subDevice.address == locationDevice.address {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
- self.delegate?.setDeviceLocation(device: locationDevice)
- }
- }
- }
- }
- }
-
- }
- //self.isFinish = false
- }
-
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- deinit {
- log.debug("销毁--")
- NotificationCenter.default.removeObserver(self)
- }
-
- }
- extension IHClassControlView{
- func createUI() {
-
- //设置大图片
- // let backImageView = UIImageView.init(frame: self.bounds)
- let backImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH + 2, height: self.frame.height))
- backImageView.image = UIImage.init(named: "背景")
- self.addSubview(backImageView)
- //811.5 49.5
- //教室背景图片
- classImage = UIImageView.init()
- let percent = (10.5 * KSCREENWIDTH) / 376
- 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)
- classImage!.image = UIImage.init(named: "教室背景")
- classImage!.isUserInteractionEnabled = true
- self.addSubview(classImage!)
-
- // guard let deviceLists = self.shoolDivListArr else {return }
- //
- // for light in deviceLists {
- // //设备类型(4 教室灯,5 黑板灯,6 窗帘)
- // guard let type = light.type else { return}
- // if type == "4" {
- // classLightArr.append(light)
- //
- // }
- // if type == "5" {
- // blackBoardLightArr.append(light)
- // }
- // if type == "6" {
- // curtainArr.append(light)
- // }
- // }
-
-
- // for i in 0..<9 {
- // var list = ShoolDeviceList()
- // list.id = "\(i + 1)"
- // list.type = "\(i + 1)"
- // list.location = "\(i + 1)"
- // list.status = "1"
- // list.light = "100"
- // list.color = "6000"
- // list.name = "教室灯\(i + 1)"
- // list.model = "0"
- // classLightArr.append(list)
- // }
- //
- // for i in 0..<3 {
- // var list = ShoolDeviceList()
- // list.id = "\(i + 1)"
- // list.type = "\(i + 1)"
- // list.location = "\(i + 1)"
- // list.status = "1"
- // list.light = "100"
- // list.color = "6000"
- // list.name = "黑板灯\(i + 1)"
- // list.model = "0"
- // blackBoardLightArr.append(list)
- // }
- //
- // for i in 0..<2 {
- // var list = ShoolDeviceList()
- // list.id = "\(i + 1)"
- // list.type = "\(i + 1)"
- // list.location = "\(i + 1)"
- // list.status = "1"
- // list.light = "100"
- // list.color = "6000"
- // list.name = "窗帘\(i + 1)"
- // list.model = "0"
- // curtainArr.append(list)
- // }
- // //教室灯
- // setClassLight(lights : classLightArr)
- // //黑板灯
- // setBlackBoardLightLocation(lights : blackBoardLightArr)
- // // 窗帘
- // setCurtain(curtains :curtainArr )
-
- //假数据
- // var devicesL = [(address : String ,type : String,location : String)]()
- // for i in 0..<3 {
- // let device = (address : "010\(i+1)",type : "04",location : "1\(i)")
- // devicesL.append(device)
- // }
- // for i in 0..<9 {
- // let device = (address : "010\(i+1)",type : "01",location : "2\(i)")
- // devicesL.append(device)
- // }
- // for i in 0..<2 {
- // let device = (address : "010\(i+1)",type : "40",location : "0\(i+1)")
- // devicesL.append(device)
- // }
- // self.deviceList = devicesL
-
-
-
- guard let devices = self.deviceList else {
- return
- }
- let allClassLightLocation = ["20","21","22","23","24","25","26","27","28"]
- let allLampLightLocation = ["10","11","12"]
- let allCurtainLocation = ["01","02"]
- //已用过的
- var setClassLightLocation = [String]()
- var setLampLightLocation = [String]()
- var setCurtainLocation = [String]()
-
- var otherDevice = [(address : String,type : String,location : String)]()
- for subDevice in devices {
- //04: Lamp device(黑板灯)
- //31: sensor device(485)(教室灯)
- //01: CW Lamp (教室灯)
- //40: Curtain device (窗帘)
- if subDevice.type == "04" {
- if subDevice.location != "00"{
- setLampLightLocation.append(subDevice.location)
- }
- lampLights.append(subDevice)
- self.deviceList?.removeAll(where: {$0 == subDevice})
- }else if subDevice.type == "31" || subDevice.type == "01" {
- if subDevice.location != "00"{
- setClassLightLocation.append(subDevice.location)
- }
- classLights.append(subDevice)
- self.deviceList?.removeAll(where: {$0 == subDevice})
- }else if subDevice.type == "40" {
- if subDevice.location != "00"{
- setCurtainLocation.append(subDevice.location)
- }
- curtainDevice.append(subDevice)
- //删除指定的元素
- self.deviceList?.removeAll(where: {$0 == subDevice})
- }else{
- otherDevice.append(subDevice)
- }
-
- }
-
- //设置位置
- if lampLights.count > 0 {
- setDeviceLocation(deviceList: lampLights, setDeviceLocation: setLampLightLocation, allDeviceLocation: allLampLightLocation, deviceType: "04")
- }
- if classLights.count > 0 {
- setDeviceLocation(deviceList: classLights, setDeviceLocation: setClassLightLocation, allDeviceLocation: allClassLightLocation, deviceType: "01")
- }
- if curtainDevice.count > 0{
- setDeviceLocation(deviceList: curtainDevice, setDeviceLocation: setCurtainLocation, allDeviceLocation: allCurtainLocation, deviceType: "40")
- }
- //改变位置后的数组
- self.deviceList = otherDevice + lampLights + classLights + curtainDevice
-
- //教室灯
- setClassLight(lights : classLights)
- //黑板灯
- setBlackBoardLightLocation(lights : lampLights)
- // 窗帘
- setCurtain(curtains :curtainDevice )
-
- //
-
- //开关与色温
- setOnOffAndColTep()
- //上课模式
- setClassModelUI()
-
- }
-
- //初始化位置
- private func setDeviceLocation(deviceList : [(address : String,type : String,location : String)],setDeviceLocation : [String],allDeviceLocation : [String],deviceType : String ){
- //设置位置
-
- var totalLights = [(address : String,type : String,location : String)]()
-
- if setDeviceLocation.count > 0{
- //未用过的
- var unSetLocaiont = [String]()
- for location in allDeviceLocation {
- //找出还没用过的位置
- var i = 0
- for setLocation in setDeviceLocation {
- i = i + 1
- if location != setLocation && i == setDeviceLocation.count {
- unSetLocaiont.append(location)
- }
- if location == setLocation {
- break
- }
- }
- }
-
- //设置位置
- var i = 0
- for light in deviceList {
- if light.location == "00"{
- let subLight = (address : light.address,type : light.type,location : unSetLocaiont[i])
- i = i + 1
- totalLights.append(subLight)
- }else{
- totalLights.append(light)
- }
- }
-
- }else{
- //全部没用过
- for ( index,light )in deviceList.enumerated() {
- var subLight : (address : String,type : String,location : String)?
- if deviceType == "04" {
- subLight = (address : light.address,type : light.type,location : "1\(index)")
- }else if deviceType == "40"{
- subLight = (address : light.address,type : light.type,location : "0\(index + 1)")
- }else{
- subLight = (address : light.address,type : light.type,location : "2\(index)")
- }
-
- totalLights.append(subLight!)
- }
- }
-
- // deviceList = totalLights
- if deviceType == "04"{
- //黑板灯
- lampLights = totalLights
- }else if(deviceType == "40"){
- curtainDevice = totalLights
- }else{
- classLights = totalLights
- }
-
- }
-
- //设置上课模式
- private func setClassModel(classModelView_super : UIView) {
- let offsetX = (40 * KSCREENWIDTH) / 750
- let width = (78 * KSCREENWIDTH) / 750
- let between = (classModelView_super.size.width - offsetX * 2 - width * 5) / 5
- // "午休",
- let arr = ["上课","休息","投影","自习","板书"]
- for i in (0..<5) {
- let backView = UIView.init(frame: CGRect.init(x: offsetX + CGFloat(i) * (width + between), y: 1, width: width, height: classModelView_super.size.height - 1))
- backView.tag = i + 1
- let line = UIView.init(frame: CGRect.init(x: 0, y: 0, width: width, height: 1))
- line.backgroundColor = UIColor.init(hexString: "#3E328B")
- let btn = UIButton.init(frame: CGRect.init(x: 0, y: 1, width: width, height: backView.size.height))
- //btn.setImage(UIImage.init(named: "勾选"), for: .normal)
- btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
- btn.setTitle(arr[i], for: .normal)
- btn.setTitleColor(.black, for: .normal)
- btn.isUserInteractionEnabled = false
- line.tag = tag + 1
- btn.tag = tag + 1
- backView.addSubview(line)
- backView.addSubview(btn)
- let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeClassModel))
- backView.addGestureRecognizer(tapGR)
- classModelView_super.addSubview(backView)
-
- line.backgroundColor = .white
- //btn.setImage(UIImage.init(named: ""), for: .normal)
- if selectBtnIndex != 0 {
- if i + 1 == selectBtnIndex {
- line.backgroundColor = UIColor.init(hexString: "#3E328B")
- btn.setImage(UIImage.init(named: "勾选"), for: .normal)
- }
- }
- btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal)
-
- classModelArr.append(backView)
- }
-
- }
-
- //教室灯 ShoolDeviceList
- 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){
-
- // guard let location = light.location else {
- // return
- // }
- // guard let locationValue = Int(location) else{return}
- var locationValue : Int = 0
- switch light.location {
- case "20":
- locationValue = 1
- case "21":
- locationValue = 2
- case "22":
- locationValue = 3
- case "23":
- locationValue = 4
- case "24":
- locationValue = 5
- case "25":
- locationValue = 6
- case "26":
- locationValue = 7
- case "27":
- locationValue = 8
- default:
- locationValue = 9
- }
- let row : Int = (locationValue - 1) % 3
- let col : Int = (locationValue - 1) / 3
- let marginX = offsetX + CGFloat(row) * backW
- let marginY = offsetY + CGFloat(col) * backH
- //let X = String.init(format: "%.3f",marginX).cgFloat()
-
- let backLightView = UIView.init(frame: CGRect.init(x: Double(marginX), y: Double(marginY), width: Double(backW), height: Double(backH)))
-
- //灯
-
- let x = (backW - lightW) * 0.5
- let lightImageView = UIImageView.init(frame: CGRect.init(x: x, y: 0, width: lightW, height: lightH))
- // guard let status = light.status else {
- // return
- // }
- // if status == "0" {
- // lightImageView.image = UIImage.init(named: "大灯_暗")
- // }else{
- // lightImageView.image = UIImage.init(named: "大灯_亮")
- // }
-
- //灯亮的状态
- let defaults = UserDefaults.standard
- let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
- if let arrDevice = arrDevice {
- if arrDevice.count > 0 {
- for subDic in arrDevice {
- let address = subDic["address"]!
- let bright = subDic["bright"]
- if light.address == address && bright != "0"{
- lightImageView.image = UIImage.init(named: "大灯_亮")
- break
- }
- if light.address == address && bright == "0"{
- lightImageView.image = UIImage.init(named: "大灯_暗")
- break
- }
- }
- }
-
- }else{
- //如果是别人先添加的设置,自己第一次运行
- lightImageView.image = UIImage.init(named: "大灯_暗")
- }
-
-
-
- lightImageView.isUserInteractionEnabled = true
- backLightView.tag = locationValue
- lightImageView.tag = locationValue
- //标签
- let lightLabel = UILabel.init(frame: CGRect.init(x: 0, y: lightH - 20, width: backW, height: backH - lightH + 10))
-
- // lightLabel.text = light.name
- lightLabel.text = light.address
- lightLabel.textAlignment = .center
- lightLabel.textColor = .white
- lightLabel.font = UIFont.systemFont(ofSize: 12)
- lightLabel.isHidden = true
- self.clickImage = backLightView
- //手势
- let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(tapImage))
- let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(longTapImage))
- // backLightView.addGestureRecognizer(tapGR)
- // backLightView.addGestureRecognizer(longGR)
-
- lightImageView.addGestureRecognizer(tapGR)
- lightImageView.addGestureRecognizer(longGR)
-
- // light_superView.isUserInteractionEnabled = true
- backLightView.addSubview(lightImageView)
- backLightView.addSubview(lightLabel)
- light_superView.addSubview(backLightView)
- lightsArr.append(backLightView)
- }
-
- //设置窗帘
- func setCurtainUI(curtain : (address : String,type : String,location : String), curtain_superView : UIView) {
- //
- // guard let location = curtain.location else {
- // return
- // }
- let curtain_offsetY = ((134 + 290) * KSCREENHEIGHT) / 1623
- var offsetY : CGFloat = 0
- if curtain.location == "02" {
- offsetY = curtain_offsetY
- }
- let curtainH = (290 * KSCREENHEIGHT) / 1623
- let curtainBtn = UIButton.init(frame: CGRect.init(x: 0, y: offsetY, width: 30, height: curtainH))
- // guard let status = curtain.status else {
- // return
- // }
- // if status == "1" {
- // //开
- // curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal)
- // }else{
- // //关
- // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
- // }
- // guard let tag : Int = Int(location) else {
- // return
- // }
-
- let defaults = UserDefaults.standard
- let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
- if let arrDevice = arrDevice {
- if arrDevice.count > 0 {
- for subDic in arrDevice {
- let address = subDic["address"]!
- let bright = subDic["bright"]
- if curtain.address == address && bright != "0"{
- curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal)
- break
- }
- if curtain.address == address && bright == "0"{
- curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
- break
- }
- }
- }
- }else{
- curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
- }
-
- // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal)
- var tag = 0
- if curtain.location == "01"{
- tag = 1
- }else{
- tag = 2
- }
- curtainBtn.tag = tag
- curtainBtn.addTarget(self, action: #selector(switchCurtain), for: .touchUpInside)
-
- curtain_superView.addSubview(curtainBtn)
- }
- }
- extension IHClassControlView{
-
- //黑板灯-点击
- @objc func blackboardTapImage(tapGR : UIGestureRecognizer){
- let imageV = tapGR.view!
- log.debug("黑板-开关\(imageV.tag)")
- //教室灯长按之后,这里不允许进来
- if isLongTap == true {
- return
- }
- guard let delegate = self.delegate else{
- return
- }
- // for light in blackBoardLightArr {
- // guard let location = light.location else {
- // return
- // }
- // if location == String.init(format: "%d", imageV.tag){
- // delegate.clickLight(lightDevice: light)
- // break
- // }
- // }
-
- for light in lampLights {
- var location = 0
- switch light.location {
- case "10":
- location = 1
- case "11":
- location = 2
- default:
- location = 3
- }
- if imageV.tag == location {
- delegate.pushToLightCommandVC(ShoolDevice: light)
- break
- }
- }
-
- //更改
- // delegate.clickLight(_index: imageV.tag)
- }
- //黑板灯-长按
- @objc func blackboardTLongTapImage(longTapGR : UIGestureRecognizer){
- log.debug("黑板灯-长按灯了")
-
- if longTapGR.state == .began {
- for lightV in blackboardLights {
- lightV.isUserInteractionEnabled = false
- let dic = [lightV : lightV.frame.origin]
- recordLights.append(dic)
-
- for label in lightV.subviews {
- if label.isKind(of: UILabel.self) {
- label.isHidden = false
- }
- }
- }
- setLightRotation(lights: blackboardLights)
- isBlackboardLongTap = true
-
- //在黑板上盖一个
- guard let coverView = line else {
- return
- }
- let blackview = UIView.init(frame: coverView.frame)
- blackview.tag = 100
- blackview.backgroundColor = .clear
- classImage?.addSubview(blackview)
- }
- }
- //窗帘开关
- @objc func switchCurtain(btn: UIButton){
- log.debug("窗帘开关\(btn.tag)")
- btn.isSelected = !btn.isSelected
-
-
- if isBlackboardLongTap == true || isLongTap == true{
- return
- }
-
- guard let delegate = delegate else {
- return
- }
-
- for device in self.deviceList! {
- //面板情景id绑定按键id
- //操作情景模式
- if device.type == "12" {
- HUD.flash(.progress)
- if btn.isSelected == true {
- //开
- delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5")
- }else{
- delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5")
-
- }
- break
- }
-
- }
- for device in self.deviceList! {
- //面板情景id绑定按键id
- //操作情景模式
- if device.type == "12" {
- delegate.controlCurtainTurnOn(panel: device, curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
- break
- }
- }
-
-
- // if btn.isSelected == true {
- // //开
- //
- // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
- //
- //// delegate.panelBindScenesAndKeyId(panel: device, scenes: type, keyId: keyId)
- // }else{
- // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6")
- // }
-
-
-
- // for curtain in curtainArr {
- // guard let location = curtain.location else {
- // return
- // }
- // if location == String.init(format: "%d", btn.tag){
- //
- // delegate.curtainSetOnOff(curtainDevice: curtain, respone: { msg in
- // if msg == "successful operation"{
- // //重新加载新的数据
- //
- // }
- // })
- // }
- // }
-
-
- }
- //面板灯开关
- @objc func tapImage(tapGR : UIGestureRecognizer){
- let imageV = tapGR.view!
- log.debug("面板灯-开关\(imageV.tag)")
- //黑板在移动里时,这里不允许点击
- if isBlackboardLongTap == true {
- return
- }
-
- guard let delegate = self.delegate else{
- return
- }
-
- // for light in classLightArr {
- // guard let location = light.location else {
- // return
- // }
- // if location == String.init(format: "%d", imageV.tag){
- //// delegate.clickLight(lightDevice: light)
- // break
- // }
- // }
- for light in classLights {
- var location = 0
- switch light.location {
- case "20":
- location = 1
- case "21":
- location = 2
- case "22":
- location = 3
- case "23":
- location = 4
- case "24":
- location = 5
- case "25":
- location = 6
- case "26":
- location = 7
- case "27":
- location = 8
- default:
- location = 9
- }
-
- if imageV.tag == location {
- delegate.pushToLightCommandVC(ShoolDevice: light)
- break
- }
- }
-
- // delegate.pushToLightCommandVC(ShoolDevice: classLights[imageV.tag - 1])
-
- // delegate.clickLight(_index: imageV.tag)
- }
- //面板灯长按
- @objc func longTapImage(longGR : UIGestureRecognizer){
-
- if longGR.state == .began {
-
- for lightV in lightsArr {
- lightV.isUserInteractionEnabled = false
- let dic = [lightV : lightV.frame.origin]
- recordLights.append(dic)
-
- for label in lightV.subviews {
- if label.isKind(of: UILabel.self) {
- label.isHidden = false
- }
- }
- }
- setLightRotation(lights: lightsArr)
- isLongTap = true
-
- //在黑板上盖一个
- guard let coverView = blackboardLine else {
- return
- }
- let blackview = UIView.init(frame: coverView.frame)
- blackview.tag = 100
- blackview.backgroundColor = .clear
- classImage?.addSubview(blackview)
-
- }
- }
-
- //设置抖动
- func setLightRotation(lights : [UIView]) {
- let anim = CAKeyframeAnimation.init()
- anim.keyPath = "transform.rotation"
- //弧度转角度
- let s = 4 / 180.0 * Double.pi
- // anim.values = [-s,0,s,0,-s,0,s,0]
- anim.values = [-s,s,-s]
- anim.repeatCount = MAXFLOAT
- anim.duration = 0.2
- for lightView in lights {
- let views = lightView.subviews
- for subV in views {
- if subV.isKind(of: UIImageView.self) && subV.tag > 0 {
- subV.isUserInteractionEnabled = false
- subV.layer.add(anim, forKey: nil)
- }
- }
-
- }
- //隐藏-开关
- allLightsBtn?.isHidden = true
- classModelArr[0].superview?.isHidden = true
-
- //显示--选中与取消 -- 注意
- if blackboardLine != nil {
- blackboardLine!.isHidden = false
- }
- if line != nil{
- line!.isHidden = false
- }
-
- cancelBtn?.isHidden = false
- sureBtn?.isHidden = false
-
- }
-
- //色温
- @objc func changeColorTemp(tapGR : UIGestureRecognizer){
- log.debug("色温")
-
- guard let delegate = delegate else {
- return
- }
- //教室灯循环请求,全部返回后再点击发命令
- // delegate.setClassLightColor(classDevices: classLightArr)
-
- }
-
- //所有灯的开关
- @objc func switchAll(btn : UIButton){
- log.debug("所有灯的开关")
- btn.isSelected = !btn.isSelected
-
- //先要判断灯的状态
- guard let delegate = delegate else {
- return
- }
- guard let lists = self.deviceList else {
- return
- }
- //灯的全开/全关
- if btn.isSelected == true{
- for device in lists {
- if device.type == "12"{
- //配置情景id与按键id成功后再发送情景模式
- HUD.flash(.progress)
-
- delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "1", keyId: "0")
-
- break
- }
- }
- }else{
- for device in lists {
- if device.type == "12"{
- //配置情景id与按键id成功后再发送情景模式
- HUD.flash(.progress)
-
- delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "2", keyId: "1")
-
- break
- }
- }
- }
-
-
-
-
-
-
- // isSelect = !isSelect
- // if isSelect == true {
- //// delegate.lights_set_off(type: "1")
- // for device in lists {
- // if device.type == "12"{
- // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "0")
- // //delegate.switchScenesMode(address : device.address, scenes : "2" )
- // break
- // }
- // }
- // }else{
- //// delegate.lights_set_off(type: "2")
- //
- // for device in lists {
- // if device.type == "12"{
- // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "1")
- // //delegate.switchScenesMode(address : device.address, scenes : "2" )
- // break
- // }
- // }
- // }
-
- }
-
- func setLightModel(blackboardLight : UIView, lightName : String) {
- for imageV in blackboardLight.subviews {
- if imageV.isKind(of: UIImageView.self) && imageV.tag > 0{
- // imageV.isHighlighted = true
- let imageV = imageV as! UIImageView
- imageV.image = UIImage.init(named: lightName)
- }
- }
- }
-
- //改变上课模式
- @objc func changeClassModel(tapGR : UIGestureRecognizer){
- let view = tapGR.view!
- log.debug("模式:\(view.tag)")
- guard let lists = self.deviceList else {
- return
- }
- var count = 0
- for device in lists {
- if device.type == "12"{
- count = count + 1
- }
- }
- if count == 0 {
- HUD.flash(.label("还没添加面板"), delay: 0.7)
- return
- }
-
- UIView.animate(withDuration: 0.3) {
- for subView in self.classModelArr {
- if view.tag == subView.tag {
- for v in subView.subviews {
- if(v.tag > 0){
- if v.isMember(of: UIButton.self) {
- let btn :UIButton = v as! UIButton
- btn.setImage(UIImage.init(named: "勾选"), for: .normal)
- btn.setTitleColor(.black, for: .normal)
- }else{
- v.backgroundColor = UIColor.init(hexString: "#3E328B")
- }
- }
- }
- }else if subView.tag > 0 && view.tag != subView.tag {
- for v in subView.subviews {
- if(v.tag > 0){
- if v.isMember(of: UIButton.self) {
- let btn :UIButton = v as! UIButton
- //btn.setImage(UIImage.init(named: ""), for: .normal)
- btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal)
- }else{
- v.backgroundColor = .white
- }
- }
- }
- }
- }
- }
-
- guard let delegate = delegate else {
- return
- }
- // 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式,7 上课模式,8 下课模式,9 午休模式)
- //1-7 2-8 3-9 4-3 5-4 6-5
-
- // var type = "1"
- // if view.tag == 1 {
- // type = "7"
- // }
- // if view.tag == 2 {
- // type = "8"
- // }
- // if view.tag == 3 {
- // type = "9"
- // }
- // if view.tag == 4 {
- // type = "3"
- // }
- // if view.tag == 5 {
- // type = "4"
- // }
- // if view.tag == 6 {
- // type = "5"
- // }
-
- // 全开模式 1,全关模式 2,投影模式 3,自习模式 4,板书模式 5,窗帘开/关 6,上课模式 7,下课模式 8
-
- var type = "1"
- var keyId = "1"
- if view.tag == 1 {
- type = "7"
- keyId = "0"
- }
- if view.tag == 2 {
- type = "8"
- keyId = "4"
- }
- if view.tag == 3 {
- type = "2"
- keyId = "1"
- }
- if view.tag == 4 {
- type = "3"
- keyId = "3"
- }
- if view.tag == 5 {
- type = "4"
- keyId = "2"
- }
- selectBtnIndex = view.tag
- /**
- 按键1 : 上课模式
- 按键2 : 投影模式
- 按键3 : 板书模式
- 按键4 : 自习模式
- 按键5 : 休息模式
- 按键6 : 窗帘开关
- */
- for device in lists {
- if device.type == "12"{
- //配置情景id与按键id成功后再发送情景模式
- HUD.flash(.progress)
-
- delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: type, keyId: keyId)
-
- break
- }
- }
-
- // delegate.lights_set_off(type: type)
- }
-
-
- @objc func cancelChooseClass(btn: UIButton){
- //取消选中教室 - 面板灯又回到原来的位置
- if isLongTap == true{
- var arr = [UIView]()
- for dic in recordLights {
- let key = dic.keys.first
- let value = dic.values.first
- key!.frame.origin = value!
- arr.append(key!)
- }
- lightsArr = arr
-
- //取消黑板灯的蒙板
- let coverView = classImage?.viewWithTag(100)
- coverView?.removeFromSuperview()
- }
-
- if isBlackboardLongTap == true {
- var arr = [UIView]()
- for dic in recordLights {
- let key = dic.keys.first
- let value = dic.values.first
- key!.frame.origin = value!
- arr.append(key!)
- }
- blackboardLights = arr
- //取消教室灯的蒙板
- let coverView = classImage?.viewWithTag(100)
- coverView?.removeFromSuperview()
- }
- setBottomState()
- }
-
- @objc func chooseClass(btn: UIButton){
-
-
- guard let delegate = delegate else { return }
- //更换 -- 灯的位置
-
- var lights = [(address : String, type : String, location : String)]()
- if isBlackboardLongTap == true {
- //黑板灯
-
- //原来的位置与改变后的位置相比
- for (idx,changeLocation) in blackboardLights.enumerated() {
-
- let light = lampLights[idx]
- //位置 : 0x10,0x11,0x12
- var locationValue = 0
- switch light.location {
- case "10":
- locationValue = 1
- case "11":
- locationValue = 2
- default:
- locationValue = 3
- }
- if changeLocation.tag != locationValue {
- var lastLocation = "0"
- switch changeLocation.tag {
- case 1:
- lastLocation = "10"
- case 2:
- lastLocation = "11"
- default:
- lastLocation = "12"
- }
- lights.append((address : light.address,type : light.type,location : lastLocation))
- }
-
-
- // guard let loc = light.location else {
- // return
- // }
- // guard let locValue = Int(loc) else {
- // return
- // }
- // if changeLocation.tag != locValue{
- // light.location = String.init(format: "%d", changeLocation.tag)
- // lights.append(light)
- // }
-
- }
- }
- if isLongTap == true {
- //教室灯
- //原来的位置与改变后的位置相比
- for (idx,changeLocation) in lightsArr.enumerated() {
-
- let light = classLights[idx]
- //位置 : 0x10,0x11,0x12
- var locationValue = 0
- switch light.location {
- case "20":
- locationValue = 1
- case "21":
- locationValue = 2
- case "22":
- locationValue = 3
- case "23":
- locationValue = 4
- case "24":
- locationValue = 5
- case "25":
- locationValue = 6
- case "26":
- locationValue = 7
- case "27":
- locationValue = 8
- default:
- locationValue = 9
- }
- if changeLocation.tag != locationValue {
- var lastLocation = "0"
- switch changeLocation.tag {
- case 1:
- lastLocation = "20"
- case 2:
- lastLocation = "21"
- case 3:
- lastLocation = "22"
- case 4:
- lastLocation = "23"
- case 5:
- lastLocation = "24"
- case 6:
- lastLocation = "25"
- case 7:
- lastLocation = "26"
- case 8:
- lastLocation = "27"
- default:
- lastLocation = "28"
- }
- lights.append((address : light.address,type : light.type,location : lastLocation))
- }
-
-
- // guard let loc = light.location else {
- // return
- // }
- // guard let locValue = Int(loc) else {
- // return
- // }
- //// if changeLocation.tag == locValue{
- //// return
- //// }
- // if changeLocation.tag != locValue{
- // light.location = String.init(format: "%d", changeLocation.tag)
- // lights.append(light)
- // }
-
- }
-
- }
- var i = 0
- for subLight in lights {
- i = i + 1
- if i == lights.count{
- isFinish = true
- changeLocationCout = i
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
- delegate.setDeviceLocation(device: subLight)
- }
-
- }
-
-
- // delegate.chargeLight(lists: lights)
-
- //选中教室
- setBottomState()
- }
-
- func setBottomState(){
- //取消教室灯的蒙板
- let coverView = classImage?.viewWithTag(100)
- coverView?.removeFromSuperview()
- if(isLongTap == true){
- for lightV in lightsArr {
- lightV.isUserInteractionEnabled = true
- for imageView in lightV.subviews {
- if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 {
- imageView.isUserInteractionEnabled = true
- imageView.layer.removeAllAnimations()
- }
- if imageView.isKind(of: UILabel.self) {
- imageView.isHidden = true
- }
- }
-
- }
- }
- if(isBlackboardLongTap == true){
- for lightV in blackboardLights {
- lightV.isUserInteractionEnabled = true
- for imageView in lightV.subviews {
- if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 {
- imageView.isUserInteractionEnabled = true
- imageView.layer.removeAllAnimations()
- }
- if imageView.isKind(of: UILabel.self) {
- imageView.isHidden = true
- }
-
- }
- }
- }
-
- // --- 注意
- if blackboardLine != nil {
- blackboardLine!.isHidden = true
- }
- if line != nil {
- line!.isHidden = true
- }
-
- cancelBtn?.isHidden = true
- sureBtn?.isHidden = true
- allLightsBtn?.isHidden = false
- classModelArr[0].superview?.isHidden = false
- isLongTap = false
- isBlackboardLongTap = false
- }
- }
- //上课模式
- extension IHClassControlView{
- func setClassModelUI() {
- //上课模式
- // let classY = (1321 * KSCREENHEIGHT) / 811.5
- // let classH = (82 * KSCREENHEIGHT) / 811.5
- let classY = (632 * KSCREENHEIGHT) / 811.5
- let classH = (41 * KSCREENHEIGHT) / 811.5
- let classModelView = UIView.init(frame: CGRect.init(x: 0, y: classY , width: classImage!.size.width, height: classH))
- classModelView.backgroundColor = .white
- classModelView.cornerRadius = 8
- classModelView.layer.masksToBounds = true
- classImage!.addSubview(classModelView)
-
- setClassModel(classModelView_super: classModelView)
-
- //选中界面下的 x和v
- let btnX = (254 * KSCREENWIDTH) / 750
- let btnY = (1311 * KSCREENHEIGHT) / 1623
- let btnWH = (60 * KSCREENWIDTH) / 750
-
- cancelBtn = UIButton.init(frame: CGRect.init(x: btnX, y: btnY - 5, width: btnWH, height: btnWH))
- cancelBtn?.setImage(UIImage.init(named: "取消"), for: .normal)
- cancelBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7)
- cancelBtn?.addTarget(self, action: #selector(cancelChooseClass), for: .touchUpInside)
- cancelBtn?.cornerRadius = btnWH / 2
- cancelBtn?.layer.masksToBounds = true
- cancelBtn?.isHidden = true
- classImage!.addSubview(cancelBtn!)
-
- let sbtnX = (394 * KSCREENWIDTH) / 750
- sureBtn = UIButton.init(frame: CGRect.init(x: sbtnX, y: btnY - 5, width: btnWH, height: btnWH))
- sureBtn?.setImage(UIImage.init(named: "确认"), for: .normal)
- sureBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7)
- sureBtn?.addTarget(self, action: #selector(chooseClass), for: .touchUpInside)
- sureBtn?.isHidden = true
- sureBtn?.cornerRadius = btnWH / 2
- sureBtn?.layer.masksToBounds = true
- classImage!.addSubview(sureBtn!)
- }
- }
- //开关与色温
- extension IHClassControlView{
- func setOnOffAndColTep() {
- //所有灯的开关
- let lightsX = (9.5 * KSCREENWIDTH) / 376
- let lightsY = (582 * KSCREENHEIGHT) / 811.5
- let lightsW = (35 * KSCREENWIDTH) / 376
- allLightsBtn = UIButton.init(frame: CGRect.init(x: lightsX, y: lightsY, width: lightsW, height: lightsW))
-
- allLightsBtn?.isSelected = isSelect
- if isSelect == false{
- allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .normal)
- allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .selected)
- }else{
- allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .normal)
- allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .selected)
- }
-
-
- allLightsBtn?.addTarget(self, action: #selector(switchAll), for: .touchUpInside)
- classImage!.addSubview(allLightsBtn!)
-
- //色温开关
- let tempX = (309 * KSCREENWIDTH) / 376
-
- let tempImageV = UIImageView.init(frame: CGRect.init(x: tempX, y: lightsY, width: lightsW, height: lightsW))
- tempImageV.image = UIImage.init(named: "色温")
- let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeColorTemp))
- tempImageV.addGestureRecognizer(tapGR)
- tempImageV.isUserInteractionEnabled = true
- //暂时取消
- //classImage!.addSubview(tempImageV)
- }
- }
- //教室灯
- extension IHClassControlView{
- //[ShoolDeviceList]
- fileprivate func setClassLight(lights :[(address : String,type : String,location : String)]){
- if lights.count == 0 {
- return
- }
- //面板灯的网格线
- let lineX = (49 * KSCREENWIDTH) / 750
- let lineY = (222 * KSCREENHEIGHT) / 1623
- let lineH = (1029 * KSCREENHEIGHT) / 1623
- line = UIImageView.init(frame: CGRect.init(x: lineX, y: lineY, width: classImage!.size.width - 2 * lineX, height: lineH))
- line!.image = UIImage.init(named: "3-3网格")
- line!.isHidden = true
- classImage!.addSubview(line!)
-
-
- //面板灯222
- let offsetY = (202 * KSCREENHEIGHT) / 1623
- let offsetX = (49 * KSCREENWIDTH) / 750
- let backW = ((708 - 49 * 2) / 3 * KSCREENWIDTH) / 750
- let backH = ((1029 * KSCREENHEIGHT) / 1623) / 3
- let lightW : CGFloat = (120 * KSCREENWIDTH) / 750
- let lightH = (306 * KSCREENHEIGHT) / 1623
- //计算所有按键的位置
- setAllClassRoomLightFrame(offsetX: offsetX, offsetY: offsetY, backW: backW, backH: backH)
- for light in lights {
-
- setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!)
- }
-
-
- // for light in lights {
- //
- // setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!)
- // }
- }
- }
- //计算所有的灯的位置
- extension IHClassControlView {
- //设置教室灯的位置
- private func setAllClassRoomLightFrame (offsetX : CGFloat, offsetY : CGFloat, backW : CGFloat,backH: CGFloat){
- var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]()
- for i in 0..<9 {
- let row : Int = i % 3
- let col : Int = i / 3
- let marginX = offsetX + CGFloat(row) * backW
- let marginY = offsetY + CGFloat(col) * backH
- let X = String.init(format: "%.3f", marginX).cgFloat()
- frames.append((x: X!, y: marginY, w: backW, h: backH))
- }
- classRoomLightFrames = frames
- }
-
- private func setBlackBoardLightFrames(width: CGFloat,height : CGFloat) {
- var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]()
- for i in 0..<3 {
- var offsetX : CGFloat = 102
- offsetX = offsetX + width * CGFloat(i)
- let light_percent = (offsetX * KSCREENWIDTH) / 750
- let width = (width * KSCREENWIDTH) / 750
- let X = String.init(format: "%.3f", light_percent).cgFloat()
- frames.append((x : X!, y: 0, w : width, h : height))
-
- }
- blackBoardLightFrames = frames
- }
- }
- //窗帘
- extension IHClassControlView{
- //[ShoolDeviceList]
- func setCurtain(curtains : [(address : String,type : String,location : String)]) {
-
- if curtains.count > 0 {
- //窗帘 360
- let curtain_percent = (360 * KSCREENHEIGHT) / 1623
- let height = ((290 * 2 + 134) * KSCREENHEIGHT) / 1623
- let curtainView = UIView.init(frame: CGRect.init(x: 0, y: curtain_percent, width: 44, height: height))
- curtainView.backgroundColor = .clear
- classImage!.addSubview(curtainView)
- for curtain in curtains {
- setCurtainUI(curtain : curtain, curtain_superView: curtainView)
- }
- }
- }
- }
- //黑板灯
- extension IHClassControlView {
- //[ShoolDeviceList]
- fileprivate func setBlackBoardLightLocation(lights : [(address : String,type : String,location : String)]) {
- if lights.count == 0 {
- return
- }
- //黑板灯的网格线
- let blackboardLineX = (102 * KSCREENWIDTH) / 750
- let blackboardLineH = (136 * KSCREENHEIGHT) / 1623
- blackboardLine = UIImageView.init(frame: CGRect.init(x: blackboardLineX, y: 0, width: classImage!.size.width - 2 * blackboardLineX, height: blackboardLineH))
- blackboardLine!.image = UIImage.init(named: "1-3网格")
- blackboardLine!.isHidden = true
- classImage!.addSubview(blackboardLine!)
- let w : CGFloat = (708 - 102 * 2) / 3
-
- //计算所有的黑板灯的位置
- setBlackBoardLightFrames(width: w,height : blackboardLineH)
-
- for light in lights {
-
- setBlackBoardLight(light : light,width: w,height : blackboardLineH,blackBoardLight_superView: classImage!)
- }
- }
-
-
- //设置黑板灯
- //ShoolDeviceList
- fileprivate func setBlackBoardLight(light : (address : String,type : String,location : String),width: CGFloat,height : CGFloat, blackBoardLight_superView : UIView) {
- // guard let location = light.location else {
- // return
- // }
-
-
- var offsetX : CGFloat = 102
- if light.location == "11" {
- offsetX = offsetX + width
- }
- if light.location == "12" {
- offsetX = offsetX + width * 2
- }
- let light_percent = (offsetX * KSCREENWIDTH) / 750
- let width = (width * KSCREENWIDTH) / 750
- //let blackboardView = UIView.init(frame: CGRect.init(x: light_percent, y: 0, width: width, height: height))
- let X = String.init(format: "%.3f",light_percent).cgFloat()
- let blackboardView = UIView.init(frame: CGRect.init(x: X!, y: 0, width: width, height: height))
-
- //灯
- // guard let status = light.status else {
- // return
- // }
- // let blackboard_light = UIImageView.init()
- // if status == "0" {
- // blackboard_light.image = UIImage.init(named: "黑板灯_暗")
- // }else{
- // blackboard_light.image = UIImage.init(named: "黑板灯_亮")
- // }
-
- let blackboard_light = UIImageView.init()
- //灯亮的状态
- let defaults = UserDefaults.standard
- let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]]
- if let arrDevice = arrDevice {
- if arrDevice.count > 0 {
- for subDic in arrDevice {
- let address = subDic["address"]!
- let bright = subDic["bright"]
- if light.address == address && bright != "0"{
- blackboard_light.image = UIImage.init(named: "黑板灯_亮")
- break
- }
- if light.address == address && bright == "0"{
- blackboard_light.image = UIImage.init(named: "黑板灯_暗")
- break
- }
- }
- }
-
- }else{
- blackboard_light.image = UIImage.init(named: "黑板灯_暗")
- }
-
-
-
-
- blackboard_light.sizeToFit()
- blackboard_light.frame.origin = CGPoint.init(x: (blackboardView.frame.size.width - blackboard_light.frame.size.width) * 0.5, y: -10)
- // guard let tag = Int(location) else {
- // return
- // }
- var tag = 0
- if light.location == "10"{
- tag = 1
- }else if light.location == "11"{
- tag = 2
- }else{
- tag = 3
- }
- blackboardView.tag = tag
- blackboard_light.tag = tag
- //标签
- let blackboardLabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: width, height: 34))
- // blackboardLabel.text = light.name
- blackboardLabel.text = light.address
- blackboardLabel.textColor = .white
- blackboardLabel.font = UIFont.systemFont(ofSize: 12)
- blackboardLabel.textAlignment = .center
- blackboardLabel.isHidden = true
- blackboardView.addSubview(blackboard_light)
- blackboardView.addSubview(blackboardLabel)
- blackBoardLight_superView.addSubview(blackboardView)
-
- //手势
- let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(blackboardTapImage))
- let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(blackboardTLongTapImage))
- blackboardView.addGestureRecognizer(tapGR)
- blackboardView.addGestureRecognizer(longGR)
- self.clickImage = blackboardView
- blackboardLights.append(blackboardView)
- }
- }
- extension IHClassControlView {
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- //长按之后
- if isLongTap == true {
- lightTouch(touches: touches, lights: lightsArr)
- }
- if isBlackboardLongTap == true {
- lightTouch(touches: touches, lights: blackboardLights)
- }
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)) {
- if isLongTap == true{
- lightMove(touches: touches)
- }
- if isBlackboardLongTap == true {
- lightMove(touches: touches)
- }
- }
-
- }
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- log.debug("结束--")
- if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)){
- if isLongTap == true{
- lightEnd(touches: touches, lights: lightsArr)
- }
- if isBlackboardLongTap == true {
- lightEnd(touches: touches, lights: blackboardLights)
- }
- }
-
- }
-
- //面板灯的 - 点击
- func lightTouch(touches: Set<UITouch>,lights : [UIView]){
-
- //遍历所有的面板灯的坐标
- for touch:AnyObject in touches {
- let touche : UITouch = touch as! UITouch
- let point = touche.location(in: classImage)
- for lightView in lights {
- let bool = lightView.frame.contains(point)
- if bool == true {
- classImage!.bringSubviewToFront(lightView)
- beginPoint = point
- moveLight = lightView
- originalPoint = lightView.frame.origin
-
- }
- }
- }
-
- }
-
- //面板灯 - 移动
- func lightMove(touches: Set<UITouch>) {
- for touch:AnyObject in touches {
- let touche : UITouch = touch as! UITouch
- let point = touche.location(in: classImage)
- let x = point.x - beginPoint.x
- let y = point.y - beginPoint.y
- guard let moveLight = moveLight else {
- return
- }
- moveLight.center.x = moveLight.center.x + x
- moveLight.center.y = moveLight.center.y + y
- beginPoint = point
- }
-
- }
- //灯 - 停止
- func lightEnd(touches: Set<UITouch>,lights : [UIView]) {
-
- for touch:AnyObject in touches {
- let touche : UITouch = touch as! UITouch
- let endPoint = touche.location(in: classImage)
-
- //1.先遍历点是否包含在frame的数组中
- //2.1. 如果移动的终点是没有灯的,就直接修改该灯的位置
- //2.2. 如果移动的终点是有灯的,就与此灯交换位置
- var isContain = false
- var contained = false
- var lightFrames = [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]()
- if isLongTap == true {
- lightFrames = self.classRoomLightFrames!
- }
- if isBlackboardLongTap == true {
- lightFrames = self.blackBoardLightFrames!
- }
- for (index,frameTupe) in lightFrames.enumerated() {
-
- let frame = CGRect.init(x: frameTupe.x, y: frameTupe.y, width: frameTupe.w, height: frameTupe.h)
-
- isContain = frame.contains(endPoint)
- let frameX = String.init(format: "%.3f", frame.origin.x)
- let frameY = String.init(format: "%.3f", frame.origin.y)
- //再看是否有可以交换的灯
- if isContain == true{
- contained = isContain
- var ischange = 0
- for light in lights {
- //&& light.frame != moveLight!.frame
- let lightFrameX = String.init(format: "%.3f", light.frame.origin.x)
- let lightFrameY = String.init(format: "%.3f", light.frame.origin.y)
- //创建view精度会改变,所以把origin取二位做比较
- if frameX == lightFrameX && frameY == lightFrameY {
- //有交换的灯,但不能交换自己
- log.debug("有交换的\(frame),\(light.frame)")
- ischange = 1
- light.frame.origin = originalPoint
- light.tag = moveLight!.tag
- moveLight!.frame = frame
- moveLight!.tag = index + 1
-
- beginPoint = CGPoint.init(x: 0, y: 0)
-
- break
- }
- }
-
- if ischange == 0 {
- //没有交换的,直接修改位置
- log.debug("直接修改位置\(frame)")
- moveLight?.tag = index + 1
- moveLight?.frame = frame
- beginPoint = CGPoint.init(x: 0, y: 0)
- break
- }
- }
-
- }
-
- if isContain == false && contained == false{
- //结束点是否在 这些
- log.debug("不在classRoomFrame范围内\(frame)")
- moveLight?.frame.origin = originalPoint
- beginPoint = CGPoint.init(x: 0, y: 0)
- break
- }
-
-
-
-
-
- // for lightView in lights{
- // let bool = lightView.frame.contains(endPoint)
- // log.debug("包含:\(bool),\(lightView)" )
- // if bool == true {
- //
- // if lightView.isEqual(moveLight) {
- // //2.如果不挡住就回原来的位置
- // lightView.frame.origin = originalPoint
- // beginPoint = CGPoint.init(x: 0, y: 0)
- // }else{
- //
- // //1.如果挡住就置换
- // let end = lightView.frame.origin
- // lightView.frame.origin = originalPoint
- // moveLight?.frame.origin = end
- //
- // //更换location
- // let location = lightView.tag
- // guard let moveLight = moveLight else {
- // return
- // }
- // lightView.tag = moveLight.tag
- // moveLight.tag = location
- // beginPoint = CGPoint.init(x: 0, y: 0)
- //
- // break
- // }
- // }
- // }
- }
-
- }
- }
|