gxc
2019-11-07 a4924de3136289d10cabbf2f61a228387d44ded7
ZigbeeApp/Shared/Phone/UserCenter/Device/PirSensor/PirSensorBindTargetSettionForm.cs
New file
@@ -0,0 +1,467 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.DevicePirSensor
{
    /// <summary>
    /// PIR传感器绑定界面★
    /// </summary>
    public class PirSensorBindTargetSettionForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 传感器设备
        /// </summary>
        private IASZone deviceIASZone = null;
        /// <summary>
        /// 已经存在的绑定设备
        /// </summary>
        private List<string> listEsixtDevice = new List<string>();
        /// <summary>
        /// 需要保存的设备对象
        /// </summary>
        private List<string> listSaveDevice = null;
        /// <summary>
        /// PIR传感器光照等级总刻度
        /// </summary>
        private int lightLevelCount = -1;
        /// <summary>
        /// 灯光的配置
        /// </summary>
        private IASZone.ConfigureParamates Lightconfigure = null;
        /// <summary>
        /// 旧数据
        /// </summary>
        private List<int> listOldData = new List<int>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_iasZone">传感器设备</param>
        public void ShowForm(IASZone i_iasZone)
        {
            deviceIASZone = i_iasZone;
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uBindTargets));
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化中部信息
                this.InitMiddleFrame();
            });
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private async void InitMiddleFrame()
        {
            this.listEsixtDevice.Clear();
            var result = await this.GetDeviceAllDefultData();
            if (result == false)
            {
                //显示重新加载的界面
                this.ShowReLoadView();
                return;
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                //初始化控件
                this.InitMiddleControl();
            });
        }
        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitMiddleControl()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
            var listView = new VerticalListControl(23);
            listView.Height = bodyFrameLayout.Height;
            listView.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(listView);
            //照明等级Frame(因展开折叠,优先声明)
            var frameLight = new FrameLayout();
            frameLight.Height = Application.GetRealHeight(363);
            //如果满足-----------------------------------------------------------
            var frameIfRow = new FrameRowControl(listView.rowSpace / 2);
            frameIfRow.UseClickStatu = false;
            listView.AddChidren(frameIfRow);
            frameIfRow.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uIfSatisfied), 600);
            //右箭头
            var btnIfRinght = frameIfRow.AddMostRightEmptyIcon(58, 58);
            frameIfRow.ChangedChidrenBindMode(btnIfRinght, ChidrenBindMode.NotBind);
            btnIfRinght.UseClickStatu = false;
            btnIfRinght.UnSelectedImagePath = "Item/Next.png";
            btnIfRinght.SelectedImagePath = "Item/Down.png";
            btnIfRinght.IsSelected = true;
            btnIfRinght.ButtonClickEvent += (sender, e) =>
            {
                btnIfRinght.IsSelected = !btnIfRinght.IsSelected;
                //展开折叠
                frameLight.Height = frameLight.Height > 10 ? 0 : Application.GetRealHeight(363);
            };
            //底线
            frameIfRow.AddBottomLine();
            listView.AddChidren(frameLight);
            //照明等级
            var btnLight = new NormalViewControl(500, 50, true);
            btnLight.TextID = R.MyInternationalizationString.uLightLevel;
            btnLight.TextSize = 12;
            btnLight.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnLight.X = Application.GetRealWidth(138);
            btnLight.Y = Application.GetRealHeight(58);
            frameLight.AddChidren(btnLight);
            //进度条
            var seekBar = new HorizontalSeekBar();
            seekBar.X = Application.GetRealWidth(138);
            seekBar.Y = Application.GetRealHeight(152);
            seekBar.Width = Application.GetRealWidth(884);
            seekBar.Height = Application.GetRealHeight(84);
            seekBar.Max = lightLevelCount - 1;
            seekBar.BackgroundColor = 0xfff5f5f5;
            seekBar.ThumbColor = Common.ZigbeeColor.Current.GXCButtonBlueColor;
            seekBar.ProgressColor = 0xfffdb500;
            seekBar.Progress = lightLevelCount - Lightconfigure.levelSize;
            frameLight.AddChidren(seekBar);
            seekBar.ProgressChanged += (sender, value) =>
            {
                //因为它的等级刻度从左往右是从大到小的
                this.Lightconfigure.levelSize = lightLevelCount - value;
            };
            //低
            var btnDown = new NormalViewControl(300, 50, true);
            btnDown.TextID = R.MyInternationalizationString.uLow;
            btnDown.TextSize = 12;
            btnDown.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnDown.X = Application.GetRealWidth(138);
            btnDown.Y = btnLight.Bottom + Application.GetRealHeight(115);
            frameLight.AddChidren(btnDown);
            //高
            var btnUp = new NormalViewControl(300, 50, true);
            btnUp.TextID = R.MyInternationalizationString.uHigh;
            btnUp.TextSize = 12;
            btnUp.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnUp.TextAlignment = TextAlignment.CenterRight;
            btnUp.X = frameLight.Width - ControlCommonResourse.XXLeft - Application.GetRealWidth(300);
            btnUp.Y = btnDown.Y;
            frameLight.AddChidren(btnUp);
            //底线
            var btnIfLine = new NormalViewControl(seekBar.Width, ControlCommonResourse.BottomLineHeight, false);
            btnIfLine.X = btnDown.X;
            btnIfLine.Y = btnDown.Bottom + Application.GetRealHeight(69);
            btnIfLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            frameLight.AddChidren(btnIfLine);
            //执行对象的Frame(因展开折叠,优先声明)
            var frameResult = new FrameListControl(23);
            frameResult.Height = Application.GetRealHeight(310);
            //就会--------------------------------------------------
            var frameDo = new FrameRowControl(listView.rowSpace / 2);
            frameDo.UseClickStatu = false;
            listView.AddChidren(frameDo);
            frameDo.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uResultDo), 600);
            //右箭头
            var btnResultRight = frameDo.AddMostRightEmptyIcon(58, 58);
            frameDo.ChangedChidrenBindMode(btnResultRight, ChidrenBindMode.NotBind);
            btnResultRight.UseClickStatu = false;
            btnResultRight.UnSelectedImagePath = "Item/Next.png";
            btnResultRight.SelectedImagePath = "Item/Down.png";
            btnResultRight.IsSelected = true;
            btnResultRight.ButtonClickEvent += (sender, e) =>
            {
                btnResultRight.IsSelected = !btnResultRight.IsSelected;
                //展开折叠
                frameResult.Height = frameResult.Height > 10 ? 0 : Application.GetRealHeight(310);
            };
            //底线
            frameDo.AddBottomLine();
            listView.AddChidren(frameResult);
            //触发目标
            var rowTarget = new FrameRowControl(listView.rowSpace / 2);
            rowTarget.LeftOffset = Application.GetRealWidth(138) - ControlCommonResourse.XXLeft;
            frameResult.AddChidren(rowTarget);
            rowTarget.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uTriggerTarget), 600);
            rowTarget.AddRightArrow();
            var btnTargetView = rowTarget.AddMostRightView("", 700);
            rowTarget.ButtonClickEvent += (sender, e) =>
            {
                var form = new PirSensorTargetSelectForm();
                form.AddForm(listSaveDevice == null ? listEsixtDevice : listSaveDevice);
                form.FinishSelectEvent += (listSelect) =>
                {
                    listSaveDevice = new List<string>();
                    listSaveDevice.AddRange(listSelect);
                    if (listSaveDevice.Count > 0)
                    {
                        //随便丢一个名字上去
                        var device = Common.LocalDevice.Current.GetDevice(listSaveDevice[0]);
                        btnTargetView.Text = Common.LocalDevice.Current.GetDeviceEpointName(device);
                    }
                    else
                    {
                        btnTargetView.Text = string.Empty;
                    }
                };
            };
            foreach (string mainkey in this.listEsixtDevice)
            {
                var device = Common.LocalDevice.Current.GetDevice(mainkey);
                if (device != null)
                {
                    //随便丢一个名字上去
                    btnTargetView.Text = Common.LocalDevice.Current.GetDeviceEpointName(device);
                    break;
                }
            }
            //延时
            string strMinute = Lightconfigure.transitionTime / 60 + Language.StringByID(R.MyInternationalizationString.uMinute);
            string strSecond = Lightconfigure.transitionTime % 60 + Language.StringByID(R.MyInternationalizationString.uSecond);
            var rowDelay = new FrameRowControl(listView.rowSpace / 2);
            rowDelay.LeftOffset = Application.GetRealWidth(138) - ControlCommonResourse.XXLeft;
            frameResult.AddChidren(rowDelay);
            rowDelay.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uDelayed), 300);
            rowDelay.AddRightArrow();
            var btnDelayView = rowDelay.AddMostRightView(strMinute + strSecond, 700);
            rowDelay.ButtonClickEvent += (sender, e) =>
            {
                this.ShowDelayListTime(btnDelayView);
            };
            //保存
            var btnSave = new BottomClickButton();
            btnSave.TextID = R.MyInternationalizationString.uSave;
            bodyFrameLayout.AddChidren(btnSave);
            btnSave.ButtonClickEvent += (sender, e) =>
            {
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //保存Pir传感器数据
                    this.SavePirSensorData();
                });
            };
        }
        #endregion
        #region ■ 获取初始数据_______________________
        /// <summary>
        /// 获取初始数据
        /// </summary>
        /// <returns></returns>
        private async Task<bool> GetDeviceAllDefultData()
        {
            //打开进度条
            this.ShowProgressBar();
            this.listOldData.Clear();
            //获取绑定目标设备
            List<CommonDevice> listDevice = await HdlDeviceBindLogic.Current.GetBindTargetDevice(this.deviceIASZone);
            if (listDevice == null)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return false;
            }
            foreach (var device in listDevice)
            {
                this.listEsixtDevice.Add(Common.LocalDevice.Current.GetDeviceMainKeys(device));
            }
            //获取PIR传感器的【光感等级总刻度】
            this.lightLevelCount = await HdlDevicePirSensorLogic.Current.GetPirLightAbilitySize(this.deviceIASZone);
            if (lightLevelCount == -1)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return false;
            }
            //获取灯光的配置信息
            this.Lightconfigure = await HdlDevicePirSensorLogic.Current.GetPirSensorLightSettion(deviceIASZone);
            if (Lightconfigure == null)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return false;
            }
            //记录旧数据
            this.listOldData.Add(Lightconfigure.levelSize);
            this.listOldData.Add(Lightconfigure.transitionTime);
            //关闭进度条
            this.CloseProgressBar();
            return true;
        }
        #endregion
        #region ■ 显示延时时间_______________________
        /// <summary>
        /// 显示延时时间列表
        /// </summary>
        /// <param name="btnTime"></param>
        private void ShowDelayListTime(NormalViewControl btnTime)
        {
            //分
            string strMinute = Language.StringByID(R.MyInternationalizationString.uMinute);
            //秒
            string strSecond = Language.StringByID(R.MyInternationalizationString.uSecond);
            var listfirst = new List<string>();
            var listSecond = new List<List<string>>();
            for (int i = 0; i <= 29; i++)
            {
                listfirst.Add(i.ToString().PadLeft(2, '0') + strMinute);
                var listTemp = new List<string>();
                for (int j = 0; j <= 59; j++)
                {
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }
                    listTemp.Add(j.ToString().PadLeft(2, '0') + strSecond);
                }
                listSecond.Add(listTemp);
            }
            //加一个30分钟吧
            listfirst.Add("30" + strMinute);
            var listTemp2 = new List<string>() { "00" + strSecond };
            listSecond.Add(listTemp2);
            string value = (this.Lightconfigure.transitionTime / 60).ToString().PadLeft(2, '0') + strMinute;
            int index1 = listfirst.IndexOf(value);
            if (index1 == -1)
            {
                index1 = 0;
            }
            value = (this.Lightconfigure.transitionTime % 60).ToString().PadLeft(2, '0') + strSecond;
            int index2 = listSecond[index1].IndexOf(value);
            if (index2 == -1)
            {
                index2 = 0;
            }
            PickerView.ShowSecondary(listfirst, listSecond, (value1, value2) =>
             {
                 btnTime.Text = listfirst[value1] + listSecond[value1][value2];
                 int minute = Convert.ToInt32(listfirst[value1].Substring(0, 2));
                 int second = Convert.ToInt32(listSecond[value1][value2].Substring(0, 2));
                 int delaySecond = minute * 60 + second;
                 //更改数值
                 this.Lightconfigure.transitionTime = delaySecond;
             },
             index1, index2,
             Language.StringByID(R.MyInternationalizationString.uDelayed),
             Language.StringByID(R.MyInternationalizationString.uFinish),
             Language.StringByID(R.MyInternationalizationString.uCancel));
        }
        #endregion
        #region ■ 数据保存___________________________
        /// <summary>
        /// 保存Pir传感器数据
        /// </summary>
        private async void SavePirSensorData()
        {
            if (listOldData[0] == Lightconfigure.levelSize && listOldData[1] == Lightconfigure.transitionTime
                && listSaveDevice == null)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //数据没有变更过,关闭界面
                    this.CloseForm();
                });
                return;
            }
            //打开进度条
            this.ShowProgressBar();
            if (listSaveDevice != null)
            {
                //删除绑定目标
                var listDelDevice = new List<CommonDevice>();
                foreach (string mainKey in listEsixtDevice)
                {
                    if (this.listSaveDevice.Contains(mainKey) == false)
                    {
                        listDelDevice.Add(Common.LocalDevice.Current.GetDevice(mainKey));
                    }
                }
                if (listDelDevice.Count > 0)
                {
                    //删除目标
                    var listSucess = await HdlDeviceBindLogic.Current.DeleteDeviceTarget(deviceIASZone, listDelDevice);
                    if (listSucess == null || listSucess.Count == 0)
                    {
                        //关闭进度条
                        this.CloseProgressBar();
                        return;
                    }
                }
                //绑定目标
                var listBind = new List<CommonDevice>();
                foreach (var mainKey in listSaveDevice)
                {
                    listBind.Add(Common.LocalDevice.Current.GetDevice(mainKey));
                }
                if (listSaveDevice.Count > 0)
                {
                    //绑定目标
                    var listSucess = await HdlDeviceBindLogic.Current.BindDeviceTarget(deviceIASZone, listBind);
                    if (listSucess == null || listSucess.Count == 0)
                    {
                        //关闭进度条
                        this.CloseProgressBar();
                        return;
                    }
                }
            }
            if (listOldData[0] != Lightconfigure.levelSize || listOldData[1] != Lightconfigure.transitionTime)
            {
                //保存灯光配置
                var result = await HdlDevicePirSensorLogic.Current.SetPirSensorLightSettion(this.deviceIASZone, this.Lightconfigure);
                if (result == false)
                {
                    //关闭进度条
                    this.CloseProgressBar();
                    return;
                }
            }
            //关闭进度条
            this.CloseProgressBar();
            HdlThreadLogic.Current.RunMain(() =>
            {
                //界面关闭
                this.CloseForm();
            });
        }
        #endregion
    }
}