wxr
2022-11-23 5b77d9d57a6cddb474a66ca54401ad5ab0892db3
HDL_ON/UI/UI2/FuntionControlView/Light/LightScene/AddLightScene.cs
@@ -342,6 +342,14 @@
    /// </summary>
    public class LightSceneEditDialog :Dialog
    {
        Dictionary<string, string> d = new Dictionary<string, string>();
        List<Function> listSwitch = new List<Function>();
        List<Function> lightDimming = new List<Function>();
        List<Function> lightCCT = new List<Function>();
        List<Function> lightRGB = new List<Function>();
        /// <summary>
        /// 传入的灯光列表
        /// 加入场景控制功能列表
@@ -367,11 +375,39 @@
        public void ShowDialog()
        {
            commandDic.Clear();
            commandDic.Add(FunctionAttributeKey.OnOff, "off");
            var hadDimming = lights.Find((obj) => obj.spk == SPK.LightDimming) != null;
            var hadCCT = lights.Find((obj) => obj.spk == SPK.LightCCT) != null;
            var hadRGB = lights.Find((obj) => obj.spk == SPK.LightRGB) != null;
            listSwitch = lights.FindAll((obj) => obj.spk == SPK.LightSwitch);
            lightDimming = lights.FindAll((obj) => obj.spk == SPK.LightDimming);
            lightCCT = lights.FindAll((obj) => obj.spk == SPK.LightCCT);
            lightRGB = lights.FindAll((obj) => obj.spk == SPK.LightRGB);
            var hadDimming = lightDimming.Count > 0;
            var hadCCT = lightCCT.Count > 0;
            var hadRGB = lightRGB.Count > 0;
            if ( hadRGB)
            {
                commandDic.Add(FunctionAttributeKey.RGB, lightRGB[0].GetAttrState(FunctionAttributeKey.RGB));
                commandDic.Add(FunctionAttributeKey.Brightness, lightRGB[0].GetAttrState(FunctionAttributeKey.Brightness));
            }
            if (hadCCT)
            {
                commandDic.Add(FunctionAttributeKey.CCT, lightCCT[0].GetAttrState(FunctionAttributeKey.CCT));
                if (!commandDic.ContainsKey(FunctionAttributeKey.Brightness))
                {
                    commandDic.Add(FunctionAttributeKey.Brightness, lightCCT[0].GetAttrState(FunctionAttributeKey.Brightness));
                }
            }
            if (hadDimming)
            {
                if (!commandDic.ContainsKey(FunctionAttributeKey.Brightness))
                {
                    commandDic.Add(FunctionAttributeKey.Brightness, lightCCT[0].GetAttrState(FunctionAttributeKey.Brightness));
                }
            }
            var bodyView = new FrameLayout();
            this.AddChidren(bodyView);
@@ -422,11 +458,19 @@
                X = Application.GetRealWidth(12) + btnTitle.Right,
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Public/Edit.png",
                //SelectedImagePath = "Public/HookIcon.png"
            };
            titleView.AddChidren(btnEditIcon);
            if (string.IsNullOrEmpty(scene.userSceneId))
            {
                titleView.AddChidren(btnEditIcon);
            }
            else
            {
                btnTitle.Text = scene.name;
                btnTitle.Width = btnTitle.GetTextWidth()+ Application.GetRealWidth(10);
                btnTitle.Gravity = Gravity.Center;
            }
                Action<string> callBack = (str) =>
            Action<string> callBack = (str) =>
                {
                    //名称不能为空
                    if (string.IsNullOrEmpty(str))
@@ -479,19 +523,19 @@
            var brightnessValue = 0;
            var cctValue = 27;
            if(rgbLight!= null)
            {
                int.TryParse(rgbLight.status.Find((obj) => obj.key == FunctionAttributeKey.Brightness).value, out brightnessValue);
            }
            if (cctLight != null)
            {
                int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.Brightness).value, out brightnessValue);
                int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.CCT).value, out cctValue);
            }
            if(dimmingLight!=null)
            {
                int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.CCT).value, out cctValue);
            }
            //if(rgbLight!= null)
            //{
            //    int.TryParse(rgbLight.status.Find((obj) => obj.key == FunctionAttributeKey.Brightness).value, out brightnessValue);
            //}
            //if (cctLight != null)
            //{
            //    int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.Brightness).value, out brightnessValue);
            //    int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.CCT).value, out cctValue);
            //}
            //if(dimmingLight!=null)
            //{
            //    int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.CCT).value, out brightnessValue);
            //}
            //属性设置区域
@@ -561,12 +605,24 @@
                    btnClose.IsSelected = true;
                    btnOpen.IsSelected = false;
                    commandDic[FunctionAttributeKey.OnOff] = "off";
                    d.Clear();
                    d.Add(FunctionAttributeKey.OnOff, "off");
                    foreach (var light in lights)
                    {
                        Control.Ins.SendWriteCommand(light, d);
                    }
                };
                btnOpen.MouseUpEventHandler = (sender, e) =>
                {
                    btnClose.IsSelected = false;
                    btnOpen.IsSelected = true;
                    commandDic[FunctionAttributeKey.OnOff] = "on";
                    d.Clear();
                    d.Add(FunctionAttributeKey.OnOff, "on");
                    foreach (var light in lights)
                    {
                        Control.Ins.SendWriteCommand(light, d);
                    }
                };
            }
@@ -621,172 +677,198 @@
            contentView.AddChidren(bottomView);
            bottomView.AddChidren(new Button() { Height = 1, BackgroundColor = CSS_Color.DividingLineColor });
            var btnSave = new Button()
            if (string.IsNullOrEmpty(scene.userSceneId))
            {
                Width = Application.GetRealWidth(172),
                Height = Application.GetRealHeight(44),
                TextAlignment = TextAlignment.Center,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.TextualColor,
                TextID = StringId.Save,
            };
            bottomView.AddChidren(btnSave);
            var btnComplete = new Button()
            {
                X = Application.GetRealWidth(172),
                Width = Application.GetRealWidth(172),
                Height = Application.GetRealHeight(46),
                BackgroundColor = CSS_Color.MainColor,
                TextColor = CSS_Color.MainBackgroundColor,
                TextAlignment = TextAlignment.Center,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.Complete
            };
            bottomView.AddChidren(btnComplete);
            //例:右下圆角 大小为50
            int mRectCornerID = HDLUtils.RectCornerBottomRight;
            btnComplete.SetCornerWithSameRadius((uint)Application.GetRealWidth(14), mRectCornerID);
            btnComplete.MouseUpEventHandler = (sender, e) => {
                this.Close();
                var waitPage = new Loading();
                MainPage.BaseView.AddChidren(waitPage);
                waitPage.Start(Language.StringByID(StringId.PleaseWait));
                new Thread(() =>
                var btnCacel = new Button()
                {
                    try
                    Width = Application.GetRealWidth(172),
                    Height = Application.GetRealHeight(44),
                    TextAlignment = TextAlignment.Center,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                    TextColor = CSS_Color.TextualColor,
                    TextID = StringId.Cancel,
                };
                bottomView.AddChidren(btnCacel);
                var btnSave = new Button()
                {
                    X = Application.GetRealWidth(172),
                    Width = Application.GetRealWidth(172),
                    Height = Application.GetRealHeight(46),
                    BackgroundColor = CSS_Color.MainColor,
                    TextColor = CSS_Color.MainBackgroundColor,
                    TextAlignment = TextAlignment.Center,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                    TextID = StringId.Save
                };
                bottomView.AddChidren(btnSave);
                //例:右下圆角 大小为50
                int mRectCornerID = HDLUtils.RectCornerBottomRight;
                btnSave.SetCornerWithSameRadius((uint)Application.GetRealWidth(14), mRectCornerID);
                btnCacel.MouseUpEventHandler = (sender, e) =>
                {
                    this.Close();
                    //var waitPage = new Loading();
                    //MainPage.BaseView.AddChidren(waitPage);
                    //waitPage.Start(Language.StringByID(StringId.PleaseWait));
                    //new Thread(() =>
                    //{
                    //    try
                    //    {
                    //        foreach (var light in lights)
                    //        {
                    //            Dictionary<string, string> sendDate = new Dictionary<string, string>();
                    //            //赋值场景功能数据
                    //            foreach (var attr in light.GetAttributes())
                    //            {
                    //                if (commandDic.ContainsKey(attr))
                    //                {
                    //                    try
                    //                    {
                    //                        sendDate.Add(attr, commandDic[attr]);
                    //                    }
                    //                    catch { }
                    //                }
                    //            }
                    //                Control.Ins.SendWriteCommand(light, sendDate);
                    //            Thread.Sleep(50);
                    //        }
                    //    }
                    //    catch
                    //    { }
                    //    finally
                    //    {
                    //        Application.RunOnMainThread(() =>
                    //        {
                    //            waitPage.Hide();
                    //            new PublicAssmebly().TipMsgAutoClose("组合控制已执行", true);
                    //            if (waitPage != null)
                    //            {
                    //                //backAction(null);
                    //                waitPage.RemoveFromParent();
                    //            }
                    //        });
                    //    }
                    //})
                    //{ IsBackground = true }.Start();
                };
                btnSave.MouseUpEventHandler = (sender, e) =>
                {
                    if (string.IsNullOrEmpty(scene.name))
                    {
                        foreach (var light in lights)
                        new Tip()
                        {
                            Dictionary<string, string> sendDate = new Dictionary<string, string>();
                            //赋值场景功能数据
                            foreach (var attr in light.GetAttributes())
                            {
                                if (commandDic.ContainsKey(attr))
                                {
                                    try
                                    {
                                        sendDate.Add(attr, commandDic[attr]);
                                    }
                                    catch { }
                                }
                            }
                                Control.Ins.SendWriteCommand(light, sendDate);
                            Thread.Sleep(50);
                        }
                    }
                    catch
                    { }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            waitPage.Hide();
                            if (waitPage != null)
                            {
                                backAction(null);
                                waitPage.RemoveFromParent();
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            };
            btnSave.MouseUpEventHandler += (sender, e) =>
            {
                if (string.IsNullOrEmpty(scene.name))
                {
                    new Tip()
                    {
                        CloseTime = 1,
                        Text = Language.StringByID(StringId.SceneNameCannotBeEmpty),
                        Direction = AMPopTipDirection.None,
                    }.Show(bodyView);
                    return;
                }
                foreach (var tempRoom in FunctionList.List.scenes)
                {
                    if (scene.name == tempRoom.name)
                    {
                        new PublicAssmebly().TipMsg(StringId.Tip, StringId.SceneNameAlreadyExists);
                            CloseTime = 1,
                            Text = Language.StringByID(StringId.NameCannotBeEmpty),
                            Direction = AMPopTipDirection.None,
                        }.Show(bodyView);
                        return;
                    }
                }
                var waitPage = new Loading();
                MainPage.BaseView.AddChidren(waitPage);
                waitPage.Start(Language.StringByID(StringId.PleaseWait));
                new Thread(() =>
                {
                    try
                    foreach (var tempRoom in FunctionList.List.scenes)
                    {
                        var serverScene = new Scene();
                        for (int i = 0; i < lights.Count; i++)
                        if (scene.name == tempRoom.name)
                        {
                            var light = lights[i];
                            new PublicAssmebly().TipMsg(StringId.Tip, StringId.NameAlreadyExists);
                            return;
                        }
                    }
                    var waitPage = new Loading();
                    MainPage.BaseView.AddChidren(waitPage);
                    waitPage.Start(Language.StringByID(StringId.PleaseWait));
                    new Thread(() =>
                    {
                        try
                        {
                            var serverScene = new Scene();
                            for (int i = 0; i < lights.Count; i++)
                            {
                                var light = lights[i];
                            //赋值场景功能数据
                            foreach (var attr in light.GetAttributes())
                            {
                                if (commandDic.ContainsKey(attr))
                                {
                                    try
                                    if (commandDic.ContainsKey(attr))
                                    {
                                        light.SetAttrState(attr, commandDic[attr]);
                                        try
                                        {
                                            light.SetAttrState(attr, commandDic[attr]);
                                        }
                                        catch { }
                                    }
                                    catch { }
                                }
                            }
                            //转换场景的功能对象
                            var sceneFunction = light.ConvertSceneFunction();
                            //查询有没有存在
                            var temp = scene.functions.Find((obj) => obj.sid == sceneFunction.sid);
                            //赋值
                            if (temp != null)
                            {
                                temp = sceneFunction;
                                {
                                    temp = sceneFunction;
                                }
                                else
                                {
                                    scene.functions.Add(sceneFunction);
                                }
                            }
                            else
                            var result = FunctionList.List.AddScene(scene, out serverScene);
                            Application.RunOnMainThread(() =>
                            {
                                scene.functions.Add(sceneFunction);
                            }
                                if (result == StateCode.SUCCESS)
                                {
                                    scene.userSceneId = serverScene.userSceneId;
                                    FunctionList.List.scenes.Add(scene);
                                    backAction?.Invoke(scene);
                                    this.Close();
                                }
                                else
                                {
                                    if (result == "124005")
                                        IMessageCommon.Current.ShowErrorInfoAlter(Language.StringByID(StringId.GatewayOffline));
                                    else
                                        IMessageCommon.Current.ShowErrorInfoAlter(result);
                                }
                            });
                        }
                        var result = FunctionList.List.AddScene(scene, out serverScene);
                        Application.RunOnMainThread(() =>
                        catch { }
                        finally
                        {
                            if (result == StateCode.SUCCESS)
                            Application.RunOnMainThread(() =>
                            {
                                scene = serverScene;
                                FunctionList.List.scenes.Add(scene);
                                backAction(scene);
                                this.Close();
                            }
                            else
                            {
                                IMessageCommon.Current.ShowErrorInfoAlter(result);
                            }
                        });
                    }
                    catch { }
                    finally
                    {
                        Application.RunOnMainThread(() => {
                            waitPage.Hide();
                            waitPage.RemoveFromParent();
                        });
                    }
                })
                { IsBackground = true, Priority = ThreadPriority.AboveNormal }.Start();
            };
                                waitPage.Hide();
                                waitPage.RemoveFromParent();
                            });
                        }
                    })
                    { IsBackground = true, Priority = ThreadPriority.AboveNormal }.Start();
                };
            }
            else
            {
                var btnCacel = new Button()
                {
                    Height = Application.GetRealHeight(44),
                    TextAlignment = TextAlignment.Center,
                    TextSize = CSS_FontSize.SubheadingFontSize,
                    TextColor = CSS_Color.TextualColor,
                    TextID = StringId.Close,
                };
                bottomView.AddChidren(btnCacel);
                //例:右下圆角 大小为50
                //int mRectCornerID = HDLUtils.RectCornerBottomRight;
                //btnSave.SetCornerWithSameRadius((uint)Application.GetRealWidth(14), mRectCornerID);
                btnCacel.MouseUpEventHandler = (sender, e) =>
                {
                    this.Close();
                };
            }
@@ -805,8 +887,8 @@
        /// <param name="attrView"></param>
        void LoadDimmingAttrView(VerticalScrolViewLayout attrView)
        {
            commandDic.TryAdd(FunctionAttributeKey.Percent, "0");
            string briValue = "";
            commandDic.TryGetValue(FunctionAttributeKey.Brightness,out briValue);
            var dimmingView = new FrameLayout()
            {
@@ -826,7 +908,7 @@
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                TextID = StringId.Brightness,
            };
            btnBrightnessText.Text = Language.StringByID(StringId.Brightness) + " " + commandDic[FunctionAttributeKey.Percent] + "%";
            btnBrightnessText.Text = Language.StringByID(StringId.Brightness) + " " + commandDic[FunctionAttributeKey.Brightness] + "%";
            dimmingView.AddChidren(btnBrightnessText);
@@ -856,7 +938,7 @@
                ProgressTextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                ProgressBarColor = CSS_Color.AuxiliaryColor1,
                MaxValue = 100,
                Progress = Convert.ToInt32(commandDic[FunctionAttributeKey.Percent]),
                Progress = Convert.ToInt32(commandDic[FunctionAttributeKey.Brightness]),
                SeekBarPadding = Application.GetRealWidth(20),
                IsProgressTextShow = false,
                ProgressChangeDelayTime = 0,
@@ -866,8 +948,24 @@
                btnBrightnessText.Text = Language.StringByID(StringId.Brightness) + " " + e + "%";
            };
            dimmerBar.OnStopTrackingTouchEvent = (sender, e) => {
                commandDic[FunctionAttributeKey.Percent] = dimmerBar.Progress.ToString();
                commandDic[FunctionAttributeKey.Brightness] = dimmerBar.Progress.ToString();
                btnBrightnessText.Text = Language.StringByID(StringId.Brightness) + " " + e + "%";
                foreach (var light in lights)
                {
                    if (light.spk == SPK.LightSwitch)
                    {
                        d.Clear();
                        d.Add(FunctionAttributeKey.OnOff, e > 0 ? "on" : "off");
                        Control.Ins.SendWriteCommand(light, d);
                    }
                    else
                    {
                        d.Clear();
                        d.Add(FunctionAttributeKey.Brightness, e.ToString());
                        Control.Ins.SendWriteCommand(light, d);
                    }
                }
            };
@@ -894,6 +992,10 @@
        void LoadCctAttrView(VerticalScrolViewLayout attrView)
        {
            #region 色温
            string valueStr = "2700";
            commandDic.TryGetValue(FunctionAttributeKey.CCT, out valueStr);
            int value = Convert.ToInt32(valueStr) / 100;
            var cctView = new FrameLayout()
            {
@@ -957,9 +1059,7 @@
            barColorTemplatrue.SeekBarBackgroundColor = 0x00000000;
            cctView.AddChidren(barColorTemplatrue);
            barColorTemplatrue.Y = btnTempClolorMin.Y - (barColorTemplatrue.Height - btnTempClolorMin.Height) / 2;
            var cct = 27;
            //int.TryParse(cctLight.status.Find((obj) => obj.key == FunctionAttributeKey.CCT).value, out cct);
            barColorTemplatrue.Progress = cct;
            barColorTemplatrue.Progress = value;
            //设置初始值
            btnTempClolor.Text = Language.StringByID(StringId.ColorTemperature) + " " + (barColorTemplatrue.Progress * 100 + "K");
            barColorTemplatrue.OnProgressChangedEvent = (sender, e) =>
@@ -969,6 +1069,16 @@
            barColorTemplatrue.OnStopTrackingTouchEvent = (sender, e) =>
            {
                commandDic[FunctionAttributeKey.CCT] = (barColorTemplatrue.Progress*100).ToString();
                d.Clear();
                d.Add(FunctionAttributeKey.CCT, (barColorTemplatrue.Progress * 100).ToString());
                foreach (var light in lightCCT)
                {
                    if (light.spk == SPK.LightCCT)
                    {
                        Control.Ins.SendWriteCommand(light, d);
                    }
                }
            };
            //变更背景图的Y轴坐标
            btnColorTemplatrueBack.Y = barColorTemplatrue.Y + (barColorTemplatrue.Height - btnColorTemplatrueBack.Height) / 2;
@@ -998,7 +1108,9 @@
        {
            #region RGB
            commandDic.TryAdd(FunctionAttributeKey.RGB, "255,255,255");
            string value = "255,255,255";
            commandDic.TryGetValue(FunctionAttributeKey.RGB, out value);
            var rgbView = new FrameLayout()
            {
@@ -1053,11 +1165,25 @@
            //圆的半径(考虑边界,需要设置它的半径比较小一点)
            int circleR = colorPicker.Width / 2 - Application.GetRealWidth(12);
            colorPicker.MouseUpEventHandler = (sender2, e) => {
                d.Clear();
                d.Add(FunctionAttributeKey.RGB, commandDic[FunctionAttributeKey.RGB]);
                foreach (var light in lightCCT)
                {
                    if (light.spk == SPK.LightCCT)
                    {
                        Control.Ins.SendWriteCommand(light, d);
                    }
                }
            };
            colorPicker.ColorChaged += (sender2, e2) => {
                string rgbString = (e2[0] + "," + e2[1] + "," + e2[2]).ToString();
                commandDic[FunctionAttributeKey.RGB] = rgbString;
                btnCurColor.BackgroundColor = (uint)(0xFF000000 + tempLight.GetRGBcolor(rgbString));
                foreach (var rgbTemp in lightRGB)
                {
                    rgbTemp.SetAttrState(FunctionAttributeKey.RGB, rgbString);
                }
            };
            colorPicker.MouseDownEventHandler += (sender, e) =>
            {
@@ -1075,6 +1201,14 @@
                    btnWhiteRound.Visible = true;
                }
            };
            colorPicker.MouseUpEventHandler = (sender, e) => {
                foreach (var rgbTemp in lightRGB)
                {
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.RGB, tempLight.GetRGBcolorString(rgbTemp));
                    Control.Ins.SendWriteCommand(rgbTemp, d, false, 0);
                }
            };
            colorPicker.MouseMoveEventHandler += (sender, e) =>
            {