using System; using System.Collections.Generic; using Shared.Common; using Shared.Phone.Device.Category; using Shared.Phone.UserCenter; namespace Shared.Phone.Device.CommonForm { public class SceneCategoryView : FrameLayout { /// /// scene /// public SceneUI scene; /// /// room /// public Shared.Common.Room room; /// /// delayTimeBtn /// private Button delayTimeBtn; /// /// delayImgBtn /// private Button delayImgBtn; /// /// sceneImg /// private ImageView sceneImg; /// /// SceneCategoryView /// /// /// public SceneCategoryView(int x, int y) { X = Application.GetRealWidth(x); Y = Application.GetRealHeight(y); Width = Application.GetRealWidth(1080); Height = Application.GetRealHeight(397 + 46); BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor; } /// /// init /// public void Init(SceneUI sceneUI,Common.Room curRoom) { this.scene = sceneUI; this.room = curRoom; InitFrame(); } /// /// InitFrame /// private void InitFrame() { RemoveAll(); var sceneRowLayout = new RowLayout { Height = Application.GetRealHeight(440), LineColor = ZigbeeColor.Current.GXCGrayBackgroundColor, Tag = scene }; AddChidren(sceneRowLayout); sceneImg = new ImageView() { X = Application.GetRealWidth(179), Width = Application.GetRealWidth(844), Height = Application.GetRealHeight(397), ImagePath = scene.IconPathType == 0 ? scene.IconPath : System.IO.Path.Combine(Config.Instance.FullPath, scene.IconPath), Radius = (uint)Application.GetRealHeight(17) }; sceneRowLayout.AddChidren(sceneImg); //阴影 var btnShadow = new PicViewControl(sceneImg.Width + Application.GetRealWidth(14 * 2), sceneImg.Height + Application.GetRealHeight(43), false); btnShadow.X = sceneImg.X - Application.GetRealWidth(14); btnShadow.UnSelectedImagePath = "Scene/SceneShadow.png"; sceneRowLayout.AddChidren(btnShadow); btnShadow.ButtonClickEvent += this.SceneUpHandler; var leftFL = new FrameLayout { X = Application.GetRealWidth(CommonFormResouce.X_Left), Y = Application.GetRealHeight(58), Width = Application.GetRealWidth(251), Height = Application.GetRealHeight(282), BackgroundColor = 0xd6333333, Radius = (uint)Application.GetRealHeight(17) }; sceneRowLayout.AddChidren(leftFL); var collectionBtn = new Button { X = Application.GetRealWidth(23), Y = Application.GetRealHeight(23), Width = Application.GetMinRealAverage(65), Height = Application.GetMinRealAverage(65), UnSelectedImagePath = "Item/Collection.png", SelectedImagePath = "Item/CollectionSelected.png", IsSelected = HdlRoomLogic.Current.IsCollectInRoom(scene), Tag = scene }; leftFL.AddChidren(collectionBtn); var sceneNameBtn = new Button() { Width = Application.GetRealWidth(176), Height = Application.GetRealHeight(150), TextColor = ZigbeeColor.Current.GXCTextWhiteColor, Text = scene.Name, Gravity = Gravity.Center, IsMoreLines = true }; leftFL.AddChidren(sceneNameBtn); var collection = new Button { Tag = scene }; leftFL.AddChidren(collection); collection.MouseUpEventHandler += (sender, e) => { if (room.IsLove) { HdlSceneLogic.Current.DeleteScene(HdlRoomLogic.Current.GetLoveRoom(), scene); RemoveViewByTag(collectionBtn.Tag); } else { collectionBtn.IsSelected = !collectionBtn.IsSelected; if (collectionBtn.IsSelected) { HdlSceneLogic.Current.AddScene(HdlRoomLogic.Current.GetLoveRoom(), scene); } else { HdlSceneLogic.Current.DeleteScene(HdlRoomLogic.Current.GetLoveRoom(), scene); } } }; delayTimeBtn = new Button { X = Application.GetRealWidth(780), Y = Application.GetRealHeight(317), Width = Application.GetRealWidth(200), Height = Application.GetRealHeight(58), Text = CommonFormResouce.GetTimeString(scene.SceneDelayTime), TextAlignment = TextAlignment.CenterRight, TextColor = ZigbeeColor.Current.GXCTextSelectedColor3 }; sceneRowLayout.AddChidren(delayTimeBtn); delayImgBtn = new Button { X = Application.GetRealWidth(900), Y = Application.GetRealHeight(30), Width = Application.GetMinRealAverage(52), Height = Application.GetMinRealAverage(52), UnSelectedImagePath = "Scene/Delaying.png", Visible=false }; sceneRowLayout.AddChidren(delayImgBtn); sceneRowLayout.SubViewWidth = Application.GetRealWidth(199); //定时 var btnDelay = new NormalViewControl(Application.GetRealWidth(199), sceneRowLayout.Height, false); btnDelay.BackgroundColor = ZigbeeColor.Current.GXCTextSelectedColor; btnDelay.TextID = R.MyInternationalizationString.Delay; btnDelay.TextColor = ZigbeeColor.Current.GXCTextWhiteColor; btnDelay.TextAlignment = TextAlignment.Center; btnDelay.Tag = scene.SceneDelayTime; btnDelay.Radius = (uint)Application.GetRealHeight(17); sceneRowLayout.AddLeftView(btnDelay); //变更按钮的高度 btnDelay.Height = sceneImg.Height; //编辑 var btnEditor = new NormalViewControl(Application.GetRealWidth(199), sceneImg.Height, false); btnEditor.BackgroundColor = ZigbeeColor.Current.GXCEditBackGroundColor; btnEditor.TextID = R.MyInternationalizationString.Edit; btnEditor.TextColor = ZigbeeColor.Current.GXCTextWhiteColor; btnEditor.TextAlignment = TextAlignment.Center; btnEditor.Tag = scene.SceneDelayTime; btnEditor.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerBottomLeft | HDLUtils.RectCornerTopLeft); //删除 var btnDelete = new NormalViewControl(Application.GetRealWidth(199), sceneImg.Height, false); btnDelete.Tag = scene; btnDelete.BackgroundColor = ZigbeeColor.Current.GXCRedColor; btnDelete.TextID = R.MyInternationalizationString.Delete; btnDelete.TextColor = ZigbeeColor.Current.GXCTextWhiteColor; btnDelete.TextAlignment = TextAlignment.Center; btnDelete.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerTopRight | HDLUtils.RectCornerBottomRight); if (room.IsSharedRoom == false && room.IsLove == false) { //不是分享,并且不是收藏 sceneRowLayout.AddRightView(btnEditor); sceneRowLayout.AddRightView(btnDelete); //变更按钮的高度 btnEditor.Height = sceneImg.Height; btnDelete.Height = sceneImg.Height; } //删除场景 btnDelete.ButtonClickEvent += (sender, e) => { var alert = new ShowMsgControl(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.ConfirmDelete)); alert.Show(); alert.ConfirmClickEvent += async () => { //0 移除失败 1 移除成功 2 没有该场景 var removeSceneAllData = await ZigBee.Device.Scene.DeleteSceneAsync(scene.Id); if (removeSceneAllData == null || removeSceneAllData.removeSceneData == null) { CommonPage.Instance.FailureToServer(); return; } //1成功 if (removeSceneAllData.removeSceneData.Result == 1) { HdlSceneLogic.Current.RemoveScene(scene); //RefreshBodyView(); RemoveFromParent(); } //0 移除失败 else if (removeSceneAllData.removeSceneData.Result == 0) { CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.FailedPleaseTryAgain)); return; } //2 没有该场景 else if (removeSceneAllData.removeSceneData.Result == 2) { HdlSceneLogic.Current.RemoveScene(scene); //RefreshBodyView(); RemoveFromParent(); CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheSceneIsNull)); return; } }; }; //编辑场景 btnEditor.ButtonClickEvent += async (sender, e) => { try { var targetList = new List { }; CommonPage.Loading.Start(); //从网关中反序列化出对应的房间设备 var sceneDeviceList = await ZigBee.Device.Scene.GetSceneDeviceListAsync(scene.Id); if (sceneDeviceList == null) { CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheMainGatewayIsNotOnLine)); return; } if (sceneDeviceList.getSceneDeviceListInfo != null) { var deviceList = sceneDeviceList.getSceneDeviceListInfo.DeviceList; var allDevice = HdlRoomLogic.Current.GetAllRoomListDevice(); var allScene = HdlSceneLogic.Current.GetAllRoomSceneList(); if (deviceList != null && allDevice.Count != 0) { foreach (var sceneDev in deviceList) { if (sceneDev.Type == 0) { var dev = allDevice.Find((obj) => obj.DeviceEpoint == sceneDev.Epoint && obj.DeviceAddr == sceneDev.DeviceAddr); if (dev != null) { var sceneTargetDevice = new SceneTargetDeviceUI { Type = 0, DeviceUI = dev, TaskList = sceneDev.TaskList, DelayTimeSerialNumber = deviceList.IndexOf(sceneDev) + 1 }; targetList.Add(sceneTargetDevice); } } else if (sceneDev.Type == 1) { var localScene = allScene.Find((obj) => obj.Id == sceneDev.ElseScenesId); if (localScene != null) { var sceneTargetDeviceScene = new SceneTargetDeviceUI { Type = 1, ElseScenesId = sceneDev.ElseScenesId, SceneName = localScene.Name, SceneUI = localScene, DelayTimeSerialNumber = deviceList.IndexOf(sceneDev) + 1 }; targetList.Add(sceneTargetDeviceScene); } } else { var sceneTargetDeviceScene = new SceneTargetDeviceUI { Type = 2, DelayTime = sceneDev.DelayTime, DelayTimeSerialNumber = deviceList.IndexOf(sceneDev) + 1 }; targetList.Add(sceneTargetDeviceScene); } } } } else { CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.FailedPleaseTryAgain)); } var sceneView = new CategoryAddScene(); Phone.UserView.HomePage.Instance.AddChidren(sceneView); Phone.UserView.HomePage.Instance.PageIndex += 1; sceneView.isModify = true; sceneView.modifyRoom = room; sceneView.modifySceneUI = scene; sceneView.modifySceneTargetDevicesList = targetList; sceneView.Show(); sceneView.EditorAction = (s, r) => { if (r.Id != room.Id) { RemoveFromParent(); } else { Init(s, r); } }; } catch (Exception ex) { System.Console.WriteLine($"编辑场景出错---{ex.Message}"); } finally { CommonPage.Loading.Hide(); } }; //编辑延时 btnDelay.ButtonClickEvent += (sender, e) => { if (scene.RemainTime > 0) { CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying)); return; } var timeSelect = new SelectTime(); CommonPage.Instance.AddChidren(timeSelect); timeSelect.TempTime = scene.SceneDelayTime; timeSelect.Init(); timeSelect.TimeAction = (t) => { scene.SceneDelayTime = t; delayTimeBtn.Text = CommonFormResouce.GetTimeString(t); }; }; } /// /// SceneUpHandler /// /// /// private async void SceneUpHandler(object sender, MouseEventArgs args) { if (scene.RemainTime > 0 && scene.SceneDelayTime == 0) { CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying)); return; } //1成功 0失败 //后面补上相关提示 // Error参数含义 //1:网关无法解析命令数据。 //2:协调器正在升级或备份/恢复数据 //3:操作设备/组/场景不存在 //4:其他错误 //5:数据传输错误(在某次客户端向网关发送数据的过程中,网关在合理时间范围内接收客户端数据不完整导致该错误发生。如客户端向网关一次发送100个字节的数据,但网关等待接收了一秒只接收了80个字节。发生该错误,网关将主动关闭客户端连接) var sceneOpenAllData = await ZigBee.Device.Scene.ControlSceneAsync(scene.Id, scene.SceneDelayTime); if (sceneOpenAllData == null || sceneOpenAllData.sceneOpenData == null) { return; } if (sceneOpenAllData.sceneOpenData.Result == 0) { if (sceneOpenAllData.errorResponData == null) { return; } CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.ControlSceneFail)); } scene.RemainTime = scene.SceneDelayTime; scene.SceneDelayTime = 0; new System.Threading.Thread(() => { while (scene.RemainTime > 0) { System.Threading.Thread.Sleep(1000); Application.RunOnMainThread(() => { SetTimeText(CommonFormResouce.GetTimeString(scene.RemainTime)); }); scene.RemainTime -= 1; } Application.RunOnMainThread(() => { SetTimeImage(); }); }) { IsBackground = true }.Start(); } /// /// SetTimeText /// /// public void SetTimeText(string name) { delayImgBtn.Visible = true; delayTimeBtn.Text = name; } /// /// SetTimeImage /// public void SetTimeImage() { delayImgBtn.Visible = false; delayTimeBtn.Text = string.Empty; } } }