wxr
2022-05-17 4c586bb23cdcae56ae4e4c62e90e148dc10b9d90
HDL_ON/UI/UI2/FuntionControlView/1ContorlPage/CacControlPage.cs
New file
@@ -0,0 +1,657 @@
using System;
using System.Collections.Generic;
using HDL_ON.DriverLayer;
using HDL_ON.Entity;
using HDL_ON.Stan;
using HDL_ON.UI.CSS;
using Shared;
namespace HDL_ON.UI
{
    public class CacControlPage : DeviceFunctionCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 空调温度控制控件
        /// </summary>
        DiyArcSeekBar arcBar;
        /// <summary>
        /// 减号按钮
        /// </summary>
        Button btnMinus;
        /// <summary>
        /// 加号按钮
        /// </summary>
        Button btnPlus;
        /// <summary>
        /// 温度按钮
        /// </summary>
        Button btnTemp;
        Button btnTempUint;
        /// <summary>
        /// 室内温度按钮
        /// </summary>
        Button btnIndoorTemp;
        /// <summary>
        /// 模式按钮
        /// </summary>
        Button btnMode;
        /// <summary>
        /// 风速按钮
        /// </summary>
        Button btnWindSpeed;
        /// <summary>
        /// 开关按钮
        /// </summary>
        Button btnSwitch;
        AC acFunction = new AC();
        /// <summary>
        /// 控制时间
        /// </summary>
        DateTime controlTime = DateTime.MinValue.AddDays(10);
        /// <summary>
        /// 控制温度
        /// </summary>
        int temp = 16;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 初始化白色区域的内容
        /// </summary>
        public override void InitFrameWhiteContent()
        {
            var sk = device.GetAttribute(FunctionAttributeKey.SetTemp);
            if (sk != null)
            {
                if (sk.min == 0)
                {
                    sk.min = 16;
                }
                if (sk.max == 0)
                {
                    sk.max = 32;
                }
            }
            base.ShowColltionButton = false;
            base.SetTitleText(Language.StringByID(StringId.Electric));
            //初始化第一个索引页的内容
            this.InitFrameWhiteContent1();
            //刷新当前设备的状态缓存
            this.RefreshNowDeviceStatuMemory(this.device);
            //刷新界面状态
            this.RefreshFormStatu();
            //读取状态
            new System.Threading.Thread(() =>
            {
                while (true)
                {
                    if (controlTime == DateTime.MinValue)
                    {
                    }
                    else if (controlTime.AddSeconds(2) < DateTime.Now)
                    {
                        Control.Ins.SendReadCommand(device);
                        controlTime = DateTime.MinValue;
                    }
                    System.Threading.Thread.Sleep(1000);
                }
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
        /// 初始化第一个索引页的内容
        /// </summary>
        private void InitFrameWhiteContent1()
        {
            Button btnJinmaoIcon = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(94),
                Width = Application.GetRealWidth(164),
                Height = Application.GetRealHeight(43),
                UnSelectedImagePath = "FunctionIcon/CAC/JinMaoIcon.png"
            };
            FrameWhiteCentet1.AddChidren(btnJinmaoIcon);
            temp = (int)Convert.ToDouble(device.GetAttrState(FunctionAttributeKey.SetTemp).Replace(",", "."));
            arcBar = new DiyArcSeekBar()
            {
                Gravity = Gravity.CenterHorizontal,
                OpenAngle = 160,
                ThumbImageHeight = Application.GetRealWidth(50),
                ProgressBarColor = CSS_Color.MainColor,
                OfflineProgressBarColor = CSS_Color.PromptingColor2,
                ArcColor = CSS_Color.BackgroundColor,
#if __IOS__
                Y = Application.GetRealHeight(120 + 25),
                Width = Application.GetRealWidth(260 - 40),
                Height = Application.GetRealWidth(260 - 40),
                SeekBarPadding = Application.GetRealWidth(8),
#else
                Y = Application.GetRealHeight(120 + 40),
                Width = Application.GetRealWidth(260 - 40),
                Height = Application.GetRealWidth(260 - 40),
                SeekBarPadding = Application.GetRealWidth(7),
#endif
            };
            FrameWhiteCentet1.AddChidren(arcBar);
            arcBar.ThumbImagePath = device.GetAttrState(FunctionAttributeKey.OnOff) == "on" ? "FunctionIcon/AC/DiyThumbIconOn.png" : "FunctionIcon/AC/DiyThumbIcon.png";
            arcBar.IsOffline = device.GetAttrState(FunctionAttributeKey.OnOff) == "off";
            arcBar.MinValue = device.GetAttribute(FunctionAttributeKey.SetTemp).min;
            arcBar.MaxValue = device.GetAttribute(FunctionAttributeKey.SetTemp).max;
            arcBar.Progress = (int)Convert.ToDouble(device.GetAttrState(FunctionAttributeKey.SetTemp).Replace(",", "."));
            btnTemp = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(206),
                Width = Application.GetRealWidth(71),
                Height = Application.GetRealWidth(60),
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = 50,
                IsBold = true,
                Text = Convert.ToDouble(device.GetAttrState(FunctionAttributeKey.SetTemp).Replace(",", ".")).ToString(),
                TextAlignment = TextAlignment.Center,
            };
            FrameWhiteCentet1.AddChidren(btnTemp);
            btnTempUint = new Button()
            {
                X = btnTemp.Right,
                Y = btnTemp.Y,
                Width = Application.GetRealWidth(30),
                Height = Application.GetRealHeight(30),
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.EmphasisFontSize_Secondary,
                IsBold = true,
                TextAlignment = TextAlignment.CenterLeft,
                Text = "°C",
            };
            FrameWhiteCentet1.AddChidren(btnTempUint);
            btnIndoorTemp = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = btnTemp.Bottom,
                Width = Application.GetRealWidth(120),
                Height = Application.GetRealHeight(20),
                Text = Language.StringByID(StringId.IndoorTemp) + "20°C/" + Language.StringByID(StringId.Humidity) +"45%",
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
            };
            FrameWhiteCentet1.AddChidren(btnIndoorTemp);
            btnMinus = new Button()
            {
                X = Application.GetRealWidth(21),
                Y = Application.GetRealHeight(263),
                Width = Application.GetRealWidth(32),
                Height = Application.GetRealWidth(32),
                UnSelectedImagePath = "Public/MinusSignIcon.png",
            };
            FrameWhiteCentet1.AddChidren(btnMinus);
            btnPlus = new Button()
            {
                X = Application.GetRealWidth(275),
                Y = Application.GetRealHeight(263),
                Width = Application.GetRealWidth(32),
                Height = Application.GetRealWidth(32),
                UnSelectedImagePath = "Public/PlusSignIcon.png",
            };
            FrameWhiteCentet1.AddChidren(btnPlus);
            btnMode = new Button()
            {
                X = Application.GetRealWidth(60),
                Y = Application.GetRealHeight(334),
                Width = Application.GetRealWidth(30),
                Height = Application.GetRealWidth(30),
                UnSelectedImagePath = acFunction.GetModeIconPath(device.GetAttrState(FunctionAttributeKey.Mode),true),
            };
            FrameWhiteCentet1.AddChidren(btnMode);
            Button btnModeText = new Button()
            {
                X = Application.GetRealWidth(30),
                Y = btnMode.Bottom,
                Width = Application.GetRealWidth(90),
                Height = Application.GetRealHeight(20),
                TextAlignment = TextAlignment.Center,
                TextSize = 12,
                TextColor = CSS_Color.TextualColor,
                TextID = StringId.Mode
            };
            FrameWhiteCentet1.AddChidren(btnModeText);
            btnWindSpeed = new Button()
            {
                X = Application.GetRealWidth(238),
                Y = Application.GetRealHeight(333),
                Width = Application.GetRealWidth(38),
                Height = Application.GetRealWidth(38),
                UnSelectedImagePath = "FunctionIcon/CAC/DataIcon.png",
            };
            FrameWhiteCentet1.AddChidren(btnWindSpeed);
            Button btnDataText = new Button()
            {
                X = Application.GetRealWidth(212),
                Y = btnWindSpeed.Bottom,
                Width = Application.GetRealWidth(90),
                Height = Application.GetRealHeight(20),
                TextAlignment = TextAlignment.Center,
                TextSize = 12,
                TextColor = CSS_Color.TextualColor,
                TextID = StringId.Data
            };
            FrameWhiteCentet1.AddChidren(btnDataText);
            btnSwitch = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(466),
                Width = Application.GetRealWidth(32),
                Height = Application.GetRealWidth(32),
                UnSelectedImagePath = "Public/PowerClose.png",
                SelectedImagePath = "Public/PowerOpen.png",
                IsSelected = device.GetAttrState(FunctionAttributeKey.OnOff) == "on"
            };
            FrameWhiteCentet1.AddChidren(btnSwitch);
            LoadEvent_TempChange();
            LoadEvent_AcStatesChange();
        }
        /// <summary>
        /// 加载修改模式区域
        /// </summary>
        void LoadDiv_ChangeModeView()
        {
            Dialog dialog = new Dialog();
            FrameLayout dialogView = new FrameLayout()
            {
                BackgroundColor = CSS_Color.DialogTransparentColor1,
            };
            dialog.AddChidren(dialogView);
            FrameLayout modeChangeView;
            modeChangeView = new FrameLayout()
            {
                X = Application.GetRealWidth(30),
                Y = Application.GetRealHeight(128 + 135),
                Width = Application.GetRealWidth(160),
                Height = Application.GetRealHeight(152),
                BackgroundImagePath = "Public/dialogBg2.png",
            };
            dialogView.AddChidren(modeChangeView);
            Button btnTitle;
            btnTitle = new Button()
            {
                X = Application.GetRealWidth(8 + 16),
                Y = Application.GetRealHeight(8),
                Width = Application.GetRealWidth(112),
                Height = Application.GetRealHeight(44),
                TextID = StringId.ChooseMode,
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.EmphasisFontSize_Secondary,
            };
            modeChangeView.AddChidren(btnTitle);
            Button btnLine = new Button()
            {
                X = btnTitle.X,
                Y = btnTitle.Bottom,
                Width = Application.GetRealWidth(112),
                Height = Application.GetRealHeight(1),
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            modeChangeView.AddChidren(btnLine);
            var modeList = device.GetAttribute(FunctionAttributeKey.Mode).value;
            foreach (var m in modeList)
            {
                Button btnModeIcon = new Button()
                {
                    X = btnTitle.X,
                    Y = Application.GetRealHeight(44 * (modeList.IndexOf(m) + 1) + 10 + 8),
                    Width = Application.GetRealWidth(24),
                    Height = Application.GetRealWidth(24),
                    IsSelected = device.GetAttrState(FunctionAttributeKey.Mode) == m,
                };
                modeChangeView.AddChidren(btnModeIcon);
                Button btnModeText = new Button()
                {
                    X = Application.GetRealWidth(12) + btnModeIcon.Right,
                    Y = Application.GetRealHeight(44 * (modeList.IndexOf(m) + 1) + 8),
                    Height = Application.GetRealHeight(44),
                    Width = Application.GetRealWidth(90),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = CSS_Color.FirstLevelTitleColor,
                    SelectedTextColor = CSS_Color.MainColor,
                    IsSelected = device.GetAttrState(FunctionAttributeKey.Mode) == m,
                    TextSize = CSS_FontSize.TextFontSize,
                };
                modeChangeView.AddChidren(btnModeText);
                btnModeIcon.UnSelectedImagePath = acFunction.GetModeIconPath(m, false);
                btnModeIcon.SelectedImagePath = acFunction.GetModeIconPath(m);
                btnModeText.Text = acFunction.GetModeAttrText(m);
                if (modeList.IndexOf(m) < modeList.Count - 1)
                {
                    modeChangeView.AddChidren(new Button()
                    {
                        X = btnTitle.X,
                        Y = btnModeText.Bottom,
                        Width = Application.GetRealWidth(112),
                        Height = Application.GetRealHeight(1),
                        BackgroundColor = CSS_Color.BackgroundColor,
                    });
                }
                EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
                {
                    dialog.Close();
                };
                EventHandler<MouseEventArgs> eventHandler1 = (sender, e) =>
                {
                    //if (!device.online)
                    //{
                    //    new Tip()
                    //    {
                    //        CloseTime = 1,
                    //        Text = Language.StringByID(StringId.DeviceOfflineCannotOption),
                    //        Direction = AMPopTipDirection.None,
                    //    }.Show(MainPage.BaseView);
                    //}
                    btnModeIcon.IsSelected = btnModeText.IsSelected = true;
                    device.SetAttrState(FunctionAttributeKey.Mode, m);
                    btnMode.UnSelectedImagePath = acFunction.GetModeIconPath(m);
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.Mode, m);
                    Control.Ins.SendWriteCommand(device, d);
                    dialog.Close();
                    if (device.GetAttrState(FunctionAttributeKey.Mode) == "fan")
                    {
                        arcBar.IsClickable = false;
                        arcBar.IsOffline = true;
                    }
                    else
                    {
                        arcBar.IsOffline = false;
                        arcBar.IsClickable = true;
                    }
                };
                btnModeIcon.MouseUpEventHandler = eventHandler1;
                btnModeText.MouseUpEventHandler = eventHandler1;
                dialogView.MouseUpEventHandler = eventHandler;
            }
            dialogView.MouseUpEventHandler = (sender, e) => {
                dialog.Close();
            };
            dialog.Show();
        }
        #endregion
        #region ■ 设备状态反馈_______________________
        /// <summary>
        /// 设备状态反馈
        /// </summary>
        /// <param name="i_LocalDevice"></param>
        public override void DeviceStatuPush(Function i_LocalDevice)
        {
            //不是同一个东西
            if (this.device.sid != i_LocalDevice.sid) { return; }
            //刷新当前设备的状态缓存
            this.RefreshNowDeviceStatuMemory(i_LocalDevice);
            //刷新界面状态
            this.RefreshFormStatu();
        }
        #endregion
        #region ■ 发送各种命令_______________________
        /// <summary>
        /// 温度改变模式
        /// </summary>
        void LoadEvent_TempChange()
        {
            btnMinus.MouseUpEventHandler = (sender, e) =>
            {
                //if (!device.online)
                //{
                //    new Tip()
                //    {
                //        CloseTime = 1,
                //        Text = Language.StringByID(StringId.DeviceOfflineCannotOption),
                //        Direction = AMPopTipDirection.None,
                //    }.Show(MainPage.BaseView);
                //}
                if (device.trait_on_off.curValue.ToString() == "off" || device.GetAttrState(FunctionAttributeKey.Mode) == "fan")
                {
                    return;
                }
                if (temp <= device.GetAttribute(FunctionAttributeKey.SetTemp).min)
                {
                    return;
                }
                --temp;
                Console.WriteLine($"temp == {temp}");
                controlTime = DateTime.Now;
                arcBar.Progress = temp;
                btnTemp.Text = temp.ToString();
                device.SetAttrState(FunctionAttributeKey.SetTemp, temp.ToString());
                System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string, string>();
                d.Add(FunctionAttributeKey.SetTemp, temp.ToString());
                Control.Ins.SendWriteCommand(device, d);
            };
            btnPlus.MouseUpEventHandler = (sender, e) =>
            {
                //if (!device.online)
                //{
                //    new Tip()
                //    {
                //        CloseTime = 1,
                //        Text = Language.StringByID(StringId.DeviceOfflineCannotOption),
                //        Direction = AMPopTipDirection.None,
                //    }.Show(MainPage.BaseView);
                //}
                if (device.trait_on_off.curValue.ToString() == "off" || device.GetAttrState(FunctionAttributeKey.Mode) == "fan")
                {
                    return;
                }
                if (temp >= device.GetAttribute(FunctionAttributeKey.SetTemp).max)
                {
                    return;
                }
                ++temp;
                controlTime = DateTime.Now;
                arcBar.Progress = temp;
                btnTemp.Text = temp.ToString();
                device.SetAttrState(FunctionAttributeKey.SetTemp, temp.ToString());
                System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string, string>();
                d.Add(FunctionAttributeKey.SetTemp, temp.ToString());
                Control.Ins.SendWriteCommand(device, d);
            };
            //if (device.online)
            {
                arcBar.OnStopTrackingTouchEvent = (sender, e) =>
                {
                    temp = arcBar.Progress;
                    device.SetAttrState(FunctionAttributeKey.SetTemp, arcBar.Progress.ToString());
                    btnTemp.Text = arcBar.Progress.ToString();
                    controlTime = DateTime.Now;
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.SetTemp, arcBar.Progress.ToString());
                    Control.Ins.SendWriteCommand(device, d);
                };
                arcBar.OnProgressChangedEvent = (sender, e) =>
                {
                    device.SetAttrState(FunctionAttributeKey.SetTemp, e.ToString());
                    btnTemp.Text = e.ToString();
                };
                //arcBar.MouseDownEventHandler = (sender, e) => {
                //    Console.WriteLine("ddd");
                //    MainPage.BasePageView.ScrollEnabled =false;
                //};
                //arcBar.MouseUpEventHandler = (sender, e) => {
                //    Console.WriteLine("ddd2");
                //    MainPage.BasePageView.ScrollEnabled = true;
                //};
            }
        }
        /// <summary>
        /// 控制模式事件
        /// </summary>
        void LoadEvent_AcStatesChange()
        {
            btnMode.MouseUpEventHandler = (sender, e) =>
            {
                if (device.trait_on_off.curValue.ToString() == "off")
                {
                    return;
                }
                LoadDiv_ChangeModeView();
            };
            btnWindSpeed.MouseUpEventHandler = (sender, e) =>
            {
                var page = new CacHistoryChoosePage(device);
                MainPage.BasePageView.AddChidren(page);
                page.InitView();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
            btnSwitch.MouseUpEventHandler = (sender, e) =>
            {
                //if (!device.online)
                //{
                //    new Tip()
                //    {
                //        CloseTime = 1,
                //        Text = Language.StringByID(StringId.DeviceOfflineCannotOption),
                //        Direction = AMPopTipDirection.None,
                //    }.Show(MainPage.BaseView);
                //}
                btnSwitch.IsSelected = !btnSwitch.IsSelected;
                device.trait_on_off.curValue = btnSwitch.IsSelected ? "on" : "off";
                if (device.trait_on_off.curValue.ToString() == "on")
                {
                    arcBar.ThumbImagePath = "FunctionIcon/AC/DiyThumbIconOn.png";
                    btnSwitch.IsSelected = true;
                    arcBar.IsOffline = false;
                }
                else
                {
                    arcBar.ThumbImagePath = "FunctionIcon/AC/DiyThumbIcon.png";
                    btnSwitch.IsBold = false;
                    arcBar.IsOffline = true;
                }
                System.Collections.Generic.Dictionary<string, string> d = new System.Collections.Generic.Dictionary<string, string>();
                d.Add(FunctionAttributeKey.OnOff, device.trait_on_off.curValue.ToString());
                Control.Ins.SendWriteCommand(device, d);
            };
        }
        #endregion
        #region ■ 刷新界面状态_______________________
        /// <summary>
        /// 刷新界面状态
        /// </summary>
        private void RefreshFormStatu()
        {
            Application.RunOnMainThread(() =>
            {
                //app自己控制的不用更新,会造成跳动
                if (controlTime.AddSeconds(2) > DateTime.Now)
                {
                    return;
                }
                else
                {
                    btnIndoorTemp.Text = Language.StringByID(StringId.IndoorTemp) + Convert.ToInt32(Convert.ToDouble(device.GetAttrState(FunctionAttributeKey.RoomTemp).Replace(",", "."))) + "°C"
                                        + Language.StringByID(StringId.Humidity) + Convert.ToInt32(Convert.ToDouble(device.GetAttrState("room_humidity").Replace(", ", "."))) + "%";
                    btnMode.UnSelectedImagePath = acFunction.GetModeIconPath(device.GetAttrState(FunctionAttributeKey.Mode));
                    temp = Convert.ToInt32(Convert.ToDouble(device.GetAttrState(FunctionAttributeKey.SetTemp).Replace(",", ".")));
                    arcBar.Progress = temp;
                    btnTemp.Text = temp.ToString();
                    if (device.trait_on_off.curValue.ToString() == "on")
                    {
                        arcBar.IsOffline = false;
                        btnSwitch.IsSelected = true;
                        arcBar.ThumbImagePath = "FunctionIcon/AC/DiyThumbIconOn.png";
                        if (device.GetAttrState(FunctionAttributeKey.Mode) == "fan")
                        {
                            arcBar.IsClickable = false;
                        }
                        else
                        {
                            arcBar.IsClickable = true;
                        }
                    }
                    else
                    {
                        arcBar.IsOffline = true;
                        btnSwitch.IsSelected = false;
                        arcBar.IsClickable = false;
                        arcBar.ThumbImagePath = "FunctionIcon/AC/DiyThumbIcon.png";
                    }
                    if (device.GetAttrState(FunctionAttributeKey.Mode) == "dry")
                    {
                        arcBar.IsOffline = true;
                        arcBar.IsClickable = false;
                    }
                }
            });
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 刷新当前设备的状态缓存
        /// </summary>
        private void RefreshNowDeviceStatuMemory(Function i_LocalDevice)
        {
        }
        #endregion
    }
}