ZigbeeApp/Shared/Phone/UserCenter/Device/Curtain/SiphonateDirectionAndLimitSettionForm.cs
New file
@@ -0,0 +1,568 @@
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.DeviceCurtain
{
    /// <summary>
    /// 卷帘的上下限位的配置界面
    /// </summary>
    public class SiphonateDirectionAndLimitSettionForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        ///窗帘的回路
        /// </summary>
        private Rollershade curtainDevice = null;
        /// <summary>
        /// 窗帘数据
        /// </summary>
        private CurtainData curtainData = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listdevice">窗帘的回路</param>
        public void ShowForm(Rollershade i_CurtainDevice)
        {
            this.curtainDevice = i_CurtainDevice;
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uDirectionAndLimit));
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化中部控件
                this.InitMiddleFrame();
            });
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //获取设备初始数据
            var result = this.InitCurtainDefultData();
            if (result == false)
            {
                //显示重新加载的界面
                this.ShowReLoadView();
                return;
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                //清空bodyFrame
                bodyFrameLayout.BackgroundColor = UserCenterColor.Current.White;
                this.ClearBodyFrame();
                this.listView = new VerticalListControl(29);
                listView.Y = Application.GetRealHeight(-6);
                listView.Height = bodyFrameLayout.Height;
                bodyFrameLayout.AddChidren(listView);
                //添加方向行
                this.AddDirectionRow();
                //添加限位行
                this.AddLimitRow();
                //添加重置电机行
                this.AddElectricalMachineryRow();
                //保存
                var btnSave = new BottomClickButton();
                btnSave.TextID = R.MyInternationalizationString.uSave;
                bodyFrameLayout.AddChidren(btnSave);
                btnSave.ButtonClickEvent += (sender, e) =>
                {
                    //这个保存没啥意义
                    this.CloseForm();
                };
            });
        }
        #endregion
        #region ■ 方向行_____________________________
        /// <summary>
        /// 添加方向行
        /// </summary>
        private void AddDirectionRow()
        {
            var rowDirection = new FrameRowControl(listView.rowSpace / 2);
            listView.AddChidren(rowDirection);
            //方向
            rowDirection.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uDirection), 600);
            //右箭头
            rowDirection.AddRightArrow();
            //状态
            var btnStatu = rowDirection.AddMostRightView("", 300);
            btnStatu.TextID = curtainData.Direction == false ? R.MyInternationalizationString.uForwardDirection : R.MyInternationalizationString.uReverseDirection;
            //底线
            rowDirection.AddBottomLine();
            int nowSelectNo = curtainData.Direction == false ? 0 : 1;
            rowDirection.ButtonClickEvent += (sender, e) =>
            {
                var listText = new List<string>();
                listText.Add(Language.StringByID(R.MyInternationalizationString.uForwardDirection));//正向
                listText.Add(Language.StringByID(R.MyInternationalizationString.uReverseDirection));//反向
                var form = new BottomItemSelectForm();
                form.AddForm(Language.StringByID(R.MyInternationalizationString.uDirectionSelect), listText, null, nowSelectNo);
                form.FinishSelectEvent += (index) =>
                {
                    //变更方向
                    var result = HdlDeviceCurtainLogic.Current.SetCurtainDirection(curtainDevice, index == 0 ? false : true);
                    if (result == false)
                    {
                        return;
                    }
                    nowSelectNo = index;
                    btnStatu.Text = listText[index];
                    curtainData.Direction = index == 0 ? false : true;
                };
            };
        }
        #endregion
        #region ■ 限位行_____________________________
        /// <summary>
        /// 添加限位行
        /// </summary>
        private void AddLimitRow()
        {
            var frameBack = new FrameLayout();
            frameBack.Height = Application.GetRealHeight(714);
            var rowLimit = new FrameRowControl(listView.rowSpace / 2);
            rowLimit.UseClickStatu = false;
            listView.AddChidren(rowLimit);
            //限位
            rowLimit.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uLimit), 600);
            //右箭头
            var btnRinght = rowLimit.AddMostRightEmptyIcon(58, 58);
            rowLimit.ChangedChidrenBindMode(btnRinght, ChidrenBindMode.NotBind);
            btnRinght.UseClickStatu = false;
            btnRinght.UnSelectedImagePath = "Item/RightNext.png";
            btnRinght.SelectedImagePath = "Item/Down.png";
            btnRinght.IsSelected = true;
            btnRinght.ButtonClickEvent += (sender, e) =>
            {
                btnRinght.IsSelected = !btnRinght.IsSelected;
                //展开折叠
                frameBack.Height = frameBack.Height > 10 ? 0 : Application.GetRealHeight(714);
            };
            //底线
            rowLimit.AddBottomLine();
            listView.AddChidren(frameBack);
            //添加下限位控件
            this.AddDownLimitControl(frameBack);
            //添加开限位控件
            this.AddUpLimitControl(frameBack);
        }
        /// <summary>
        /// 添加下限位控件
        /// </summary>
        /// <param name="frameBack"></param>
        private void AddDownLimitControl(FrameLayout frameBack)
        {
            var frameIcon = new FrameLayout();
            frameIcon.X = Application.GetRealWidth(228);
            frameIcon.Y = Application.GetRealHeight(121);
            frameIcon.Width = this.GetPictrueRealSize(265);
            frameIcon.Height= this.GetPictrueRealSize(495);
            frameIcon.BackgroundImagePath = "Item/DeviceLimitGround.png";
            frameBack.AddChidren(frameIcon);
            var frameIconback = new FrameLayoutStatuControl();
            frameIconback.Height = this.GetPictrueRealSize(144);
            frameIconback.Width = this.GetPictrueRealSize(144);
            frameIconback.Radius = (uint)this.GetPictrueRealSize(144) / 2;
            frameIconback.BackgroundColor = 0xffeff2fb;
            frameIconback.Gravity = Gravity.CenterHorizontal;
            frameIconback.Y = this.GetPictrueRealSize(35);
            frameIcon.AddChidren(frameIconback);
            var btnIcon = new NormalViewControl(this.GetPictrueRealSize(69), this.GetPictrueRealSize(35), false);
            btnIcon.UnSelectedImagePath = "Item/DownTriangle.png";
            btnIcon.Gravity = Gravity.Center;
            frameIconback.AddChidren(btnIcon, ChidrenBindMode.BindEvent);
            var frameText = new FrameLayoutStatuControl();
            frameText.Y= this.GetPictrueRealSize(265);
            frameText.Height = this.GetPictrueRealSize(120);
            frameIcon.AddChidren(frameText);
            //确定
            var btnOk = new NormalViewControl(frameIcon.Width, this.GetPictrueRealSize(60), false);
            btnOk.TextID = R.MyInternationalizationString.uConfirm1;
            btnOk.TextAlignment = TextAlignment.Center;
            btnOk.TextColor = UserCenterColor.Current.TextOrangeColor;
            frameText.AddChidren(btnOk, ChidrenBindMode.BindEvent);
            //底线
            var btnLine1 = new NormalViewControl(this.GetPictrueRealSize(100), ControlCommonResourse.BottomLineHeight, false);
            btnLine1.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            btnLine1.Gravity = Gravity.CenterHorizontal;
            btnLine1.Y = btnOk.Bottom - this.GetPictrueRealSize(8);
            frameText.AddChidren(btnLine1);
            //下限位
            var btnDown = new NormalViewControl(frameIcon.Width, this.GetPictrueRealSize(60), false);
            btnDown.TextID = R.MyInternationalizationString.uDownLimit;
            btnDown.Y = btnOk.Bottom;
            btnDown.TextAlignment = TextAlignment.Center;
            btnDown.TextColor = UserCenterColor.Current.TextOrangeColor;
            frameText.AddChidren(btnDown, ChidrenBindMode.BindEvent);
            //底线
            var btnLine2 = new NormalViewControl(this.GetPictrueRealSize(124), ControlCommonResourse.BottomLineHeight, false);
            btnLine2.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            btnLine2.Gravity = Gravity.CenterHorizontal;
            btnLine2.Y = btnDown.Bottom - this.GetPictrueRealSize(8);
            frameText.AddChidren(btnLine2);
            //图标点击
            frameIconback.ButtonClickEvent += (sender, e) =>
            {
                curtainDevice.CurtainUpDownStopControl(1);
            };
            //展示模板或者虚拟住宅,则无效
            if (Config.Instance.Home.IsShowTemplate == true || Config.Instance.Home.IsVirtually == true)
            {
                frameIconback.CanClick = false;
            }
            //重写控件点击效果
            frameIconback.SelectStatuEvent += (select) =>
            {
                if (select == true)
                {
                    frameIconback.BackgroundColor = 0xfffef1ed;
                }
                else
                {
                    frameIconback.BackgroundColor = 0xffeff2fb;
                }
            };
            //限位确定
            frameText.ButtonClickEvent += (sender, e) =>
            {
                //确认当前位置为下限位?
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainDownLimitMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
                {
                    //执行确定及覆盖
                    await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.DownLimit, -1, -1);
                });
            };
            //展示模板或者虚拟住宅,则无效
            if (Config.Instance.Home.IsShowTemplate == true || Config.Instance.Home.IsVirtually == true)
            {
                frameText.CanClick = false;
            }
            //重写控件点击效果
            frameText.SelectStatuEvent += (select) =>
            {
                if (select == true)
                {
                    frameIconback.BackgroundColor = UserCenterColor.Current.White;
                    btnOk.TextColor = UserCenterColor.Current.White;
                    btnDown.TextColor = UserCenterColor.Current.White;
                    frameIcon.BackgroundImagePath = "Item/DeviceLimitGroundSelected.png";
                }
                else
                {
                    frameIconback.BackgroundColor = 0xffeff2fb;
                    btnOk.TextColor = UserCenterColor.Current.TextOrangeColor;
                    btnDown.TextColor = UserCenterColor.Current.TextOrangeColor;
                    frameIcon.BackgroundImagePath = "Item/DeviceLimitGround.png";
                }
            };
        }
        /// <summary>
        /// 添加上限位控件
        /// </summary>
        /// <param name="frameBack"></param>
        private void AddUpLimitControl(FrameLayout frameBack)
        {
            var frameIcon = new FrameLayout();
            frameIcon.X = Application.GetRealWidth(619);
            frameIcon.Y = Application.GetRealHeight(121);
            frameIcon.Width = this.GetPictrueRealSize(265);
            frameIcon.Height = this.GetPictrueRealSize(495);
            frameIcon.BackgroundImagePath = "Item/DeviceLimitGround.png";
            frameBack.AddChidren(frameIcon);
            var frameIconback = new FrameLayoutStatuControl();
            frameIconback.Height = this.GetPictrueRealSize(144);
            frameIconback.Width = this.GetPictrueRealSize(144);
            frameIconback.Radius = (uint)this.GetPictrueRealSize(144) / 2;
            frameIconback.BackgroundColor = 0xffeff2fb;
            frameIconback.Gravity = Gravity.CenterHorizontal;
            frameIconback.Y = this.GetPictrueRealSize(35);
            frameIcon.AddChidren(frameIconback);
            var btnIcon = new NormalViewControl(this.GetPictrueRealSize(69), this.GetPictrueRealSize(35), false);
            btnIcon.UnSelectedImagePath = "Item/UpTriangle.png";
            btnIcon.Gravity = Gravity.Center;
            frameIconback.AddChidren(btnIcon, ChidrenBindMode.BindEvent);
            var frameText = new FrameLayoutStatuControl();
            frameText.Y = this.GetPictrueRealSize(265);
            frameText.Height = this.GetPictrueRealSize(120);
            frameIcon.AddChidren(frameText);
            //确定
            var btnOk = new NormalViewControl(frameIcon.Width, this.GetPictrueRealSize(60), false);
            btnOk.TextID = R.MyInternationalizationString.uConfirm1;
            btnOk.TextAlignment = TextAlignment.Center;
            btnOk.TextColor = UserCenterColor.Current.TextOrangeColor;
            frameText.AddChidren(btnOk, ChidrenBindMode.BindEvent);
            //底线
            var btnLine1 = new NormalViewControl(this.GetPictrueRealSize(100), ControlCommonResourse.BottomLineHeight, false);
            btnLine1.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            btnLine1.Gravity = Gravity.CenterHorizontal;
            btnLine1.Y = btnOk.Bottom - this.GetPictrueRealSize(8);
            frameText.AddChidren(btnLine1);
            //上限位
            var btnDown = new NormalViewControl(frameIcon.Width, this.GetPictrueRealSize(60), false);
            btnDown.TextID = R.MyInternationalizationString.uUpLimit;
            btnDown.Y = btnOk.Bottom;
            btnDown.TextAlignment = TextAlignment.Center;
            btnDown.TextColor = UserCenterColor.Current.TextOrangeColor;
            frameText.AddChidren(btnDown, ChidrenBindMode.BindEvent);
            //底线
            var btnLine2 = new NormalViewControl(this.GetPictrueRealSize(124), ControlCommonResourse.BottomLineHeight, false);
            btnLine2.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            btnLine2.Gravity = Gravity.CenterHorizontal;
            btnLine2.Y = btnDown.Bottom - this.GetPictrueRealSize(8);
            frameText.AddChidren(btnLine2);
            //图标点击
            frameIconback.ButtonClickEvent += (sender, e) =>
            {
                curtainDevice.CurtainUpDownStopControl(0);
            };
            //展示模板或者虚拟住宅,则无效
            if (Config.Instance.Home.IsShowTemplate == true || Config.Instance.Home.IsVirtually == true)
            {
                frameIconback.CanClick = false;
            }
            //重写控件点击效果
            frameIconback.SelectStatuEvent += (select) =>
            {
                if (select == true)
                {
                    frameIconback.BackgroundColor = 0xfffef1ed;
                }
                else
                {
                    frameIconback.BackgroundColor = 0xffeff2fb;
                }
            };
            //限位确定
            frameText.ButtonClickEvent += (sender, e) =>
            {
                //确认当前位置为上限位?
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainUpLimitMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
                {
                    //执行确定及覆盖
                    await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.UpLimit, -1, -1);
                });
            };
            //展示模板或者虚拟住宅,则无效
            if (Config.Instance.Home.IsShowTemplate == true || Config.Instance.Home.IsVirtually == true)
            {
                frameText.CanClick = false;
            }
            //重写控件点击效果
            frameText.SelectStatuEvent += (select) =>
            {
                if (select == true)
                {
                    frameIconback.BackgroundColor = UserCenterColor.Current.White;
                    btnOk.TextColor = UserCenterColor.Current.White;
                    btnDown.TextColor = UserCenterColor.Current.White;
                    frameIcon.BackgroundImagePath = "Item/DeviceLimitGroundSelected.png";
                }
                else
                {
                    frameIconback.BackgroundColor = 0xffeff2fb;
                    btnOk.TextColor = UserCenterColor.Current.TextOrangeColor;
                    btnDown.TextColor = UserCenterColor.Current.TextOrangeColor;
                    frameIcon.BackgroundImagePath = "Item/DeviceLimitGround.png";
                }
            };
        }
        #endregion
        #region ■ 重置电机___________________________
        /// <summary>
        /// 添加重置电机行
        /// </summary>
        private void AddElectricalMachineryRow()
        {
            var rowReset = new FrameRowControl(listView.rowSpace / 2);
            listView.AddChidren(rowReset);
            //重置电机
            rowReset.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uResetElectricalMachinery), 600);
            //底线
            rowReset.AddBottomLine();
            rowReset.ButtonClickEvent += (sender, e) =>
            {
                //重置电机将初始化{0}方向与限位设置,确认继续?
                string msg = Language.StringByID(R.MyInternationalizationString.uResetElectricalMachineryMsg);
                msg = msg.Replace("{0}", "\r\n");
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                {
                    HdlThreadLogic.Current.RunThread(async () =>
                    {
                        //重置窗帘
                        var result = await HdlDeviceCurtainLogic.Current.RestoreCurtain(curtainDevice);
                        if (result == false)
                        {
                            return;
                        }
                        //重新初始化界面
                        this.InitMiddleFrame();
                    });
                });
            };
            //展示模板或者虚拟住宅,则无效
            if (Config.Instance.Home.IsShowTemplate == true || Config.Instance.Home.IsVirtually == true)
            {
                rowReset.CanClick = false;
            }
        }
        #endregion
        #region ■ 初始化窗帘数据_____________________
        /// <summary>
        /// 初始化窗帘数据
        /// </summary>
        /// <returns></returns>
        private bool InitCurtainDefultData()
        {
            this.curtainData = new CurtainData();
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                var data = TemplateData.TemplateDeviceDataLogic.Current.GetCurtainLimitPoint(curtainDevice);
                curtainData.Direction = data.Direction;
                return true;
            }
            //开启进度条
            this.ShowProgressBar();
            bool receiptData = false;
            string mainkeys = LocalDevice.Current.GetDeviceMainKeys(curtainDevice);
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("CurtainDeviceAttribute", ReceiveComandDiv.A设备属性上报, (device) =>
            {
                string checkKey = LocalDevice.Current.GetDeviceMainKeys(device);
                if (mainkeys != checkKey || device.DeviceStatusReport.CluterID != 258)
                {
                    return;
                }
                foreach (var attriBute in device.DeviceStatusReport.AttriBute)
                {
                    if (attriBute.AttributeId == 23)
                    {
                        //开合帘方向
                        if (0 < (attriBute.AttriButeData & 0x01))
                        {
                            //反向
                            curtainData.Direction = true;
                        }
                        else
                        {
                            //正向
                            curtainData.Direction = false;
                        }
                        //数据接收结束
                        receiptData = true;
                    }
                }
            });
            //发送获取窗帘限位配置的命令
            HdlDeviceCurtainLogic.Current.SetGetCurtainLimitSettionComand(curtainDevice);
            int timeCount = 30;
            while (receiptData == false && timeCount >= 0)
            {
                System.Threading.Thread.Sleep(100);
                timeCount--;
            }
            //关闭进度条
            this.CloseProgressBar();
            if (timeCount <= 0)
            {
                //获取窗帘方向与限位设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetCurtainDirectionAndLimitSettionFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowMassage(ShowMsgType.Tip, msg);
                return false;
            }
            //移除监听
            HdlGatewayReceiveLogic.Current.RemoveEvent("CurtainDeviceAttribute");
            return true;
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            HdlGatewayReceiveLogic.Current.RemoveEvent("CurtainDeviceAttribute");
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 结构体_____________________________
        /// <summary>
        /// 窗帘数据
        /// </summary>
        private class CurtainData
        {
            /// <summary>
            /// false:电机方向正向;true:电机方向反向
            /// </summary>
            public bool Direction = false;
        }
        #endregion
    }
}