using System; using System.Collections.Generic; using Microsoft.AppCenter.Crashes; using Shared.Common; using Shared.Phone.Device.Light; using Shared.Phone.UserCenter.Device; using ZigBee.Device; using static ZigBee.Device.BindObj; using static ZigBee.Device.Panel; namespace Shared.Phone.UserCenter.DeviceBind { public class AddScenePage : BindCommonLayout, ZigBee.Common.IStatus { /// /// 按键模式接收 /// /// Common. public void Changed(CommonDevice common) { Shared.Application.RunOnMainThread(() => { //var dev = common as Panel; }); } /// 构造函数 /// /// public AddScenePage(ZigBee.Device.Panel key) { this.currentKey = key; } #region ◆ 变量申明__________________________ ZigBee.Device.Panel currentKey; //显示被绑定设备或场景的view VerticalScrolViewLayout midVerticalScrolViewLayout; HorizontalScrolViewLayout btnHorizontalScrolViewLayout; /// /// 按键配置的目标列表 /// List targetList = new List(); List roomList = new List(); public Action> action; Button btnFinifh; Button btnFloorText; Dictionary dicFloor; #endregion public void Show() { this.TopFrameLayout(this, Language.StringByID(R.MyInternationalizationString.AddScene)); EventHandler eHandlerBack = (sender, e) => { RemoveFromParent(); }; this.btnTitle.Width = Application.GetRealWidth(1080 - 161 - 200 - 58); this.btnBack.MouseUpEventHandler += eHandlerBack; this.btnBackFrameLayout.MouseUpEventHandler += eHandlerBack; this.MidFrameLayout(this); var btnAddFrameLayout = new FrameLayout { X = Application.GetRealWidth(830), Width = Application.GetRealWidth(200), }; this.titleFrameLayout.AddChidren(btnAddFrameLayout); btnFloorText = new Button { Width = Application.GetRealWidth(110), X = Application.GetRealWidth(15), Text = Language.StringByID(R.MyInternationalizationString.FirstFloor), TextColor = Shared.Common.ZigbeeColor.Current.XMBlack, TextSize = 15, TextAlignment = TextAlignment.CenterRight, }; btnAddFrameLayout.AddChidren(btnFloorText); var btnBindDown = new Button { Height = Application.GetRealHeight(69), Width = Application.GetRealWidth(69), X = Application.GetRealWidth(200 - 77), UnSelectedImagePath = "BindPic/BindDown.png", Gravity = Gravity.CenterVertical, }; btnAddFrameLayout.AddChidren(btnBindDown); btnBindDown.MouseDownEventHandler += (sender, e) => { SideslipFramelayout(); }; MidFrameLayoutContent(btnFloorText); } async void MidFrameLayoutContent(Button btnFloorText) { #region UI var btnMidTopLayout = new FrameLayout { Height = Application.GetRealHeight(389 - 184), }; this.midFrameLayout.AddChidren(btnMidTopLayout); btnHorizontalScrolViewLayout = new HorizontalScrolViewLayout { X = Application.GetRealWidth(58), }; btnMidTopLayout.AddChidren(btnHorizontalScrolViewLayout); midVerticalScrolViewLayout = new VerticalScrolViewLayout() { Y = btnMidTopLayout.Bottom, }; this.midFrameLayout.AddChidren(midVerticalScrolViewLayout); var bottomFrameLayout = new FrameLayout() { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight(1737 - 1472), Y = Application.GetRealHeight(1472), }; this.midFrameLayout.AddChidren(bottomFrameLayout); btnFinifh = new Button() { Width = Application.GetRealWidth(907), Height = Application.GetRealHeight(127), Gravity = Gravity.CenterHorizontal, Radius = (uint)Application.GetRealHeight(120) / 2, TextID = R.MyInternationalizationString.Save, BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack, TextColor = Shared.Common.ZigbeeColor.Current.XMWhite, TextSize = 16, }; bottomFrameLayout.AddChidren(btnFinifh); #endregion #region 数据处理 //获取楼层 dicFloor = Common.Room.CurrentRoom.GetFloorSortList(); currentKey.currentSelectFloorId = BindInfo.GetCurrentSelectFloorId(); btnFloorText.Text = BindInfo.GetCurrentSelectFloorIdName(); // 获取楼层对应对房间 foreach (var room in Shared.Common.Room.Lists) { if (room.FloorId == currentKey.currentSelectFloorId) { roomList.Add(room); } } Shared.Common.Room curRoom = roomList[0]; int index = 0; RefreshRoomList(curRoom, btnFinifh, ref index); RefreshSceneList(roomList[index]); if (curRoom.SceneUIList.Count == 0) { btnFinifh.Enable = false; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMUnSelect; } else { btnFinifh.Enable = true; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; } btnFinifh.MouseUpEventHandler += async (sender, e) => { bool isFinish = false; btnFinifh.Enable = false; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMUnSelect; try { Application.RunOnMainThread(() => { CommonPage.Loading.Start(""); }); if (currentKey.bindList.Count != 0) { var bindResult = await currentKey.ClearBindInfoAsync(); if (bindResult != null && bindResult.clearBindInfoResponseData != null) { if (bindResult.clearBindInfoResponseData.Result != 0) { Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); CommonPage.Loading.Hide(); }); this.RemoveFromParent(); btnFinifh.Enable = true; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; return; } else { currentKey.bindList.Clear(); } } else { var btn = new Button() { Gravity = Gravity.Center, }; this.midFrameLayout.AddChidren(btn); Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btn); }); } } var temp = await currentKey.ConfigureHdlKeyValueAsync(Panel.KeyMode.Scene_Trigger); if (temp.setWritableValueResponData?.Status != 0) { Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindMode), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnFinifh); btnFinifh.Enable = true; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; }); return; } else { currentKey.panelMode = 1; var addBindeDev = new AddBindData(); addBindeDev.DeviceAddr = currentKey.DeviceAddr; addBindeDev.Epoint = currentKey.DeviceEpoint; foreach (var de in targetList) { if (de.DeviceAddr != null) { var addBindInfo = new AddBindListObj(); addBindInfo.BindType = 0; addBindInfo.BindCluster = 6; addBindInfo.BindMacAddr = de.DeviceAddr; addBindInfo.BindEpoint = de.DeviceEpoint; addBindeDev.BindList.Add(addBindInfo); } else { var addBindInfo = new AddBindListObj(); addBindInfo.BindType = 1; addBindInfo.BindCluster = 6; var sc1 = (SceneUI)de; addBindInfo.BindScenesId = sc1.Id; addBindeDev.BindList.Add(addBindInfo); } } var dev = new AddedDeviceBindResponseAllData(); dev = await currentKey.AddDeviceBindAsync(addBindeDev); foreach (var ab in dev.addedDeviceBindResponseData?.BindList) { switch (ab.Result) { case 0: currentKey.bindList.Add((BindListResponseObj)ab); for (int i = 0; i < targetList.Count; i++) { var de = targetList[i]; var sc1 = new SceneUI(); if (de.DeviceAddr == null) { sc1 = (SceneUI)de; if (sc1.Id == ab.BindScenesId) { targetList.RemoveAt(i); i--; } } } isFinish = true; break; case 1: Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorAbsence), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); }); break; case 2: switch (dev.addBindResultResponseData?.Result) { case 0: currentKey.bindList.Add((BindListResponseObj)ab); for (int i = 0; i < targetList.Count; i++) { var de = targetList[i]; if (de.DeviceAddr == ab.BindMacAddr && de.DeviceEpoint == ab.BindEpoint) { targetList.RemoveAt(i); i--; } } isFinish = true; break; case 140: Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFull), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); }); break; default: Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); }); break; } break; case 3: Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindErrorExisted), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); }); break; case 4: Application.RunOnMainThread(() => { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh); }); break; default: break; } } } } catch { } finally { if (isFinish) { if (action != null) { action(currentKey.bindList); } this.RemoveFromParent(); } else { this.RemoveFromParent(); } Application.RunOnMainThread(() => { CommonPage.Loading.Hide(); btnFinifh.Enable = true; btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; }); } }; #endregion } // 按键绑定场景表显示 void RefreshRoomList(Shared.Common.Room curRoom, Button btnFinish, ref int index2) { if (roomList.Count == 0) { return; } btnHorizontalScrolViewLayout.RemoveAll(); Button curentOldRoom = null; FrameLayout curentOldRoomFrameLayout = null; int index = 0; var roomTempList = new List(); for (int i = 0; i < roomList.Count; i++) { var room = roomList[i]; //如果房间为喜爱,则不显示 if (room.IsLove == true) { continue; } //如果房间中没有设备,则不显示 if (room.SceneUIList.Count == 0) { continue; } else { roomTempList.Add(room); } if (roomTempList.Count != 0) { curRoom = roomTempList[0]; } var btnRoomFrameLayout = new FrameLayout { Height = Application.GetRealHeight(78), Width = Application.GetRealWidth(187), Y = Application.GetRealHeight(58), Radius = (uint)Application.GetMinRealAverage(78 / 2), BorderColor = Shared.Common.ZigbeeColor.Current.XMOrange, BorderWidth = 1, }; btnHorizontalScrolViewLayout.AddChidren(btnRoomFrameLayout); var btnRoom = new Button { Height = Application.GetRealHeight(58), Width = Application.GetRealWidth(127), Y = Application.GetRealHeight(58), X = Application.GetRealWidth(14), Text = room.Name, TextSize = 12, TextColor = Shared.Common.ZigbeeColor.Current.XMGray3, Gravity = Gravity.Center, //SelectedImagePath = "BindPic/BindRoomColorOn.png", //UnSelectedImagePath = "BindPic/BindRoomColor.png", }; btnRoomFrameLayout.AddChidren(btnRoom); btnRoom.IsSelected = false; //默认选中每个楼层的第一个房间 if (index == 0) { btnRoom.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; btnRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; btnRoomFrameLayout.BorderWidth = 0; btnRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite; curentOldRoom = btnRoom; curentOldRoomFrameLayout = btnRoomFrameLayout; } //记录之前选中的房间 //if (currentKey.RoomId == "") //{ // if (i == index) // { // btnRoom.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; // btnRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; // btnRoomFrameLayout.BorderWidth = 0; // btnRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite; // curentOldRoom = btnRoom; // curentOldRoomFrameLayout = btnRoomFrameLayout; // } // else // { // btnRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMGray3; // btnRoom.IsSelected = false; // btnRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMMidFrameLayout; // btnRoomFrameLayout.BorderWidth = 1; // } //} //else //{ // if (currentKey.RoomId == room.Id) // { // btnRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite; // btnRoom.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; // btnRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; // btnRoomFrameLayout.BorderWidth = 0; // curentOldRoom = btnRoom; // curentOldRoomFrameLayout = btnRoomFrameLayout; // curRoom = room; // index2 = i; // } // else // { // btnRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMGray3; // btnRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMMidFrameLayout; // btnRoomFrameLayout.BorderWidth = 1; // } //} EventHandler eHandlerRoom = (sender, e) => { if (!btnRoom.IsSelected) { if (curentOldRoom != null) { curentOldRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMGray3; curentOldRoom.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMMidFrameLayout; } curentOldRoom = btnRoom; curentOldRoom.TextColor = Shared.Common.ZigbeeColor.Current.XMWhite; curentOldRoom.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; curRoom = room; currentKey.RoomId = room.Id; if (curRoom.SceneUIList.Count == 0) { btnFinish.Enable = false; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMUnSelect; } else { btnFinish.Enable = true; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; } RefreshSceneList(curRoom); } if (btnRoomFrameLayout.BorderWidth == 1) { if (curentOldRoomFrameLayout != null) { curentOldRoomFrameLayout.BorderWidth = 1; curentOldRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMMidFrameLayout; } curentOldRoomFrameLayout = btnRoomFrameLayout; curentOldRoomFrameLayout.BorderWidth = 0; curentOldRoomFrameLayout.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMOrange; curRoom = room; currentKey.RoomId = room.Id; if (curRoom.SceneUIList.Count == 0) { btnFinish.Enable = false; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMUnSelect; } else { btnFinish.Enable = true; btnFinish.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack; } RefreshSceneList(curRoom); } }; btnRoom.MouseUpEventHandler += eHandlerRoom; btnRoomFrameLayout.MouseUpEventHandler += eHandlerRoom; var btnEmpty = new Button { Width = Application.GetRealWidth(58), X = btnRoom.Right, }; btnHorizontalScrolViewLayout.AddChidren(btnEmpty); } BindInfo.FinishDisplay(roomTempList, btnFinish); index++; } //按键绑定场景表显示 void RefreshSceneList(Shared.Common.Room curRoom) { if (curRoom.SceneUIList.Count == 0) { return; } midVerticalScrolViewLayout.RemoveAll(); Button oldScene = null; int curIndex = 0; foreach (var scene in curRoom.SceneUIList) { #region 绑定表UI显(无数据处理) var rowLayout = new FrameLayout() { Height = Application.GetRealHeight(23 + 127), BackgroundColor = Shared.Common.ZigbeeColor.Current.XMWhite, }; midVerticalScrolViewLayout.AddChidren(rowLayout); var devicePic = new Button() { X = Application.GetMinRealAverage(58), Y = Application.GetMinRealAverage(46), Width = Application.GetMinRealAverage(81), Height = Application.GetMinRealAverage(81), UnSelectedImagePath = scene.IconPath, }; rowLayout.AddChidren(devicePic); var btnBindName = new Button() { Width = Application.GetRealWidth(500 - 120), X = devicePic.Right + Application.GetRealWidth(20), TextColor = Shared.Common.ZigbeeColor.Current.TextBlack, TextAlignment = TextAlignment.CenterLeft, }; rowLayout.AddChidren(btnBindName); var btnChoose = new Button() { Width = Application.GetRealWidth(60), Height = Application.GetRealHeight(60), X = Application.GetRealWidth(861 + 81), Y = Application.GetRealHeight(35), SelectedImagePath = "DoorLock/SelectedIcon.png", Visible = false, }; rowLayout.AddChidren(btnChoose); var line2 = new Button() { Y = rowLayout.Height - 1, X = devicePic.Right + Application.GetRealWidth(20), Width = Application.GetRealWidth(965 - 116), Height = 1, BackgroundColor = Shared.Common.ZigbeeColor.Current.XMRowLine, }; rowLayout.AddChidren(line2); if (curIndex == curRoom.SceneUIList.Count - 1) { line2.Visible = false; } #endregion if (scene.Name == null) { btnBindName.Text = Language.StringByID(R.MyInternationalizationString.Unknown); } else { btnBindName.Text = scene.Name; } foreach (var bindedSc in currentKey.bindList) { if (scene.Id == bindedSc.BindScenesId) { btnChoose.IsSelected = true; btnChoose.Visible = true; oldScene = btnChoose; targetList.Add(scene); } } bool IsTip = false; EventHandler hander = (sender, e) => { if (currentKey.bindList.Count != 0) { if (IsTip) { return; } IsTip = true; if (btnChoose.IsSelected) { new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindExist), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnBindName); return; } } if (!btnChoose.IsSelected) { if (oldScene != null) { oldScene.IsSelected = false; oldScene.Visible = false; targetList.Clear(); } oldScene = btnChoose; oldScene.IsSelected = true; oldScene.Visible = true; currentKey.curSelectSceneID = scene.Id.ToString(); targetList.Clear(); targetList.Add(scene); } }; rowLayout.MouseUpEventHandler += hander; devicePic.MouseUpEventHandler += hander; btnBindName.MouseUpEventHandler += hander; btnChoose.MouseUpEventHandler += hander; curIndex++; } } // 侧边导航栏 void SideslipFramelayout() { var dialog = new Dialog { }; dialog.Show(); var flMain = new FrameLayout { BackgroundColor = 0x00000000 }; dialog.AddChidren(flMain); flMain.MouseUpEventHandler += (sender11, e11) => { dialog.Close(); }; var sidelipFrameLayout = new FrameLayout() { Height = Application.GetRealHeight(783), Width = Application.GetRealWidth(449), Y = Application.GetRealHeight(161), X = Application.GetRealWidth(596), BackgroundImagePath = "DoorLock/SideslipPic.png", }; flMain.AddChidren(sidelipFrameLayout); var btnSelectFloor = new Button() { Width = Application.GetRealWidth(200), Height = Application.GetRealHeight(58), X = Application.GetRealWidth(81), Y = Application.GetRealHeight(81), TextColor = Shared.Common.ZigbeeColor.Current.XMGray3, TextSize = 14, TextAlignment = TextAlignment.CenterLeft, TextID = R.MyInternationalizationString.SelectFloor, }; sidelipFrameLayout.AddChidren(btnSelectFloor); var sidelipVerticalScrolViewLayout = new VerticalScrolViewLayout() { Height = Application.GetRealHeight(600), Y = btnSelectFloor.Bottom, }; sidelipFrameLayout.AddChidren(sidelipVerticalScrolViewLayout); Button oldbutton = null; Button oldbuttonText = null; foreach (var floorId in dicFloor.Keys) { var rowFrameLayout = new RowLayout() { Height = Application.GetRealHeight(150), LineColor = Shared.Common.ZigbeeColor.Current.XMRowLine, X = Application.GetRealWidth(81), }; sidelipVerticalScrolViewLayout.AddChidren(rowFrameLayout); var btnAllMethod = new Button() { Width = Application.GetRealWidth(81), Height = Application.GetRealHeight(81), Y = Application.GetRealHeight(55), UnSelectedImagePath = "Floor/Floor.png", SelectedImagePath = "Floor/FloorSelected.png", }; rowFrameLayout.AddChidren(btnAllMethod); var btnMethodText = new Button() { Width = Application.GetRealWidth(311), Height = Application.GetRealHeight(58), X = Application.GetRealWidth(92), Y = Application.GetRealHeight(69), TextColor = Shared.Common.ZigbeeColor.Current.XMGray3, TextSize = 14, TextAlignment = TextAlignment.CenterLeft, Text = dicFloor[floorId], }; rowFrameLayout.AddChidren(btnMethodText); if (currentKey.currentSelectFloorId == floorId) { btnAllMethod.IsSelected = true; btnMethodText.TextColor = Shared.Common.ZigbeeColor.Current.XMBlack; } string curFloorId = floorId; EventHandler hander = (sender, e) => { if (currentKey.currentSelectFloorId == curFloorId) { return; } currentKey.currentSelectFloorId = curFloorId; btnFloorText.Text = dicFloor[floorId]; roomList.Clear(); if (!btnMethodText.IsSelected) { if (oldbutton != null) { oldbutton.IsSelected = false; } if (oldbuttonText != null) { oldbuttonText.TextColor = Shared.Common.ZigbeeColor.Current.XMGray3; } oldbutton = btnMethodText; oldbuttonText = btnMethodText; btnMethodText.IsSelected = true; oldbuttonText.TextColor = Shared.Common.ZigbeeColor.Current.XMBlack; } //获取楼层对应的房间 foreach (var room in Shared.Common.Room.Lists) { if (room.FloorId == currentKey.currentSelectFloorId) { roomList.Add(room); } } Shared.Common.Room curRoom = new Shared.Common.Room(); if (roomList.Count != 0) { curRoom = roomList[0]; } else { curRoom = new Shared.Common.Room(); roomList.Add(curRoom); } int index = 0; RefreshRoomList(curRoom, btnFinifh, ref index); RefreshSceneList(roomList[0]); dialog.Close(); }; btnAllMethod.MouseUpEventHandler += hander; rowFrameLayout.MouseUpEventHandler += hander; btnMethodText.MouseUpEventHandler += hander; } } public void DeviceInfoChange(CommonDevice common, string typeTag) { } public void ChangedILogicStatus(ZigBee.Device.Logic logic) { } public void ChangedISceneStatus(Scene scene) { } } }