ZigbeeApp/Shared/Phone/MainPage/ControlForm/DeviceColorTemperatureLightDetailCardForm.cs
New file
@@ -0,0 +1,367 @@
using Shared.Common;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.MainPage.ControlForm
{
    /// <summary>
    /// 色温灯类型的深度卡片界面
    /// </summary>
    public class DeviceColorTemperatureLightDetailCardForm : DeviceDetailCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 界面上可以操作的控件
        /// </summary>
        private List<ButtonBase> listControl = new List<ButtonBase>();
        /// <summary>
        /// MaxLevel
        /// </summary>
        private const int MaxLevel = 254;
        /// <summary>
        /// 彩灯控件
        /// </summary>
        private WaveSeekBar waveSeekBar = null;
        /// <summary>
        /// 进度值是否在改变中
        /// </summary>
        private bool isProgressing = false;
        /// <summary>
        /// 能否发送进度值
        /// </summary>
        private bool canSetProgressValue = true;
        /// <summary>
        /// 彩灯是否是打开状态(不能用控件的Select来判断,太坑)
        /// </summary>
        private bool IsLightOpen = false;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 底层初始化中部控件完成之后
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        public override void InitMiddleFrameAfter(FrameLayout frameWhiteBack)
        {
            //左滑不能
            this.ScrollEnabled = false;
            //先清空
            this.listControl = new List<ButtonBase>();
            //设置状态文字
            if (((LightBase)this.device).OnOffStatus == 1)
            {
                //亮度 XX
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
            }
            else
            {
                //关闭
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.Close));
            }
            //彩灯控件
            this.waveSeekBar = new WaveSeekBar();
            waveSeekBar.Y = Application.GetRealHeight(233);
            waveSeekBar.Width = this.GetPictrueRealSize(253);
            waveSeekBar.Height = this.GetPictrueRealSize(516);
            waveSeekBar.Gravity = Gravity.CenterHorizontal;
            waveSeekBar.WavePadding = Application.GetRealWidth(8);
            waveSeekBar.MaxValue = 100;
            waveSeekBar.Progress = (int)(((ColorTemperatureLight)this.device).Level * 1.0 / MaxLevel * 100);
            waveSeekBar.CornerRadius = Application.GetRealHeight(58);
            waveSeekBar.SetProgressBarColors(ZigbeeColor.Current.GXCWaveSeekBarColor_Start, ZigbeeColor.Current.GXCWaveSeekBarColor_End);
            frameWhiteBack.AddChidren(waveSeekBar);
            //开关
            var btnSwitch = new IconViewControl(81);
            btnSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            btnSwitch.Y = waveSeekBar.Bottom + Application.GetRealHeight(418);
            btnSwitch.Gravity = Gravity.CenterHorizontal;
            frameWhiteBack.AddChidren(btnSwitch);
            listControl.Add(btnSwitch);
            btnSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetSwitchCommand(!btnSwitch.IsSelected);
            };
            //设置初始状态
            this.IsLightOpen = ((LightBase)this.device).OnOffStatus == 1;
            this.canSetProgressValue = this.IsLightOpen;
            if (IsLightOpen == true)
            {
                btnSwitch.IsSelected = true;
            }
            //彩灯控件里面的那个显示百分比的控件
            int progressY = waveSeekBar.Y - Application.GetMinReal(154);
            var btnProgress = new NormalViewControl(Application.GetMinReal(135), Application.GetMinReal(104), false);
            btnProgress.Y = progressY;
            btnProgress.UnSelectedImagePath = "Item/ProgressBubbles.png";
            btnProgress.IsBold = true;
            btnProgress.TextColor = UserCenterColor.Current.White;
            btnProgress.Gravity = Gravity.CenterHorizontal;
            btnProgress.TextAlignment = TextAlignment.Center;
            frameWhiteBack.AddChidren(btnProgress);
            btnProgress.Visible = false;
            //开始滑动的事件
            waveSeekBar.OnStartTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始变更
                this.isProgressing = true;
                //变更进度百分比的显示
                btnProgress.Y = progressY + waveSeekBar.NowProgressY;
                btnProgress.Text = waveSeekBar.Progress + "%";
                waveSeekBar.IsProgressTextShow = false;
                if (btnProgress.Visible == false)
                {
                    btnProgress.Visible = true;
                }
            };
            //结束滑动的事件
            waveSeekBar.OnStopTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始结束
                this.isProgressing = false;
                btnProgress.Visible = false;
                waveSeekBar.IsProgressTextShow = true;
            };
            //滑动过程中
            int oldProgressValue = waveSeekBar.Progress;//之前的值
            int nowProgressValue = oldProgressValue;//变更的值
            waveSeekBar.OnProgressChangedEvent += (sender, value) =>
            {
                //变更进度百分比的显示
                btnProgress.Y = progressY + waveSeekBar.NowProgressY;
                btnProgress.Text = value + "%";
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    nowProgressValue = value;
                }
                else
                {
                    //如果住宅为虚拟住宅,直接改缓存
                    ((ColorTemperatureLight)this.device).Level = value * MaxLevel / 100;
                    //亮度 XX
                    this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
                }
            };
            //色温
            var btnColor = new NormalViewControl(300, 50, true);
            btnColor.X = Application.GetRealWidth(132);
            btnColor.Y = Application.GetRealHeight(829);
            btnColor.TextSize = 12;
            btnColor.Text = Language.StringByID(R.MyInternationalizationString.uColorTemperature) + ":";
            btnColor.TextColor = UserCenterColor.Current.TextGrayColor3;
            frameWhiteBack.AddChidren(btnColor);
            //上部显示文本
            var btnColorView = new NormalViewControl(150, 60, true);
            btnColorView.Y = btnColor.Bottom;
            btnColorView.TextAlignment = TextAlignment.Center;
            btnColorView.TextSize = 15;
            btnColorView.Gravity = Gravity.CenterHorizontal;
            btnColorView.TextColor = UserCenterColor.Current.TextGrayColor3;
            frameWhiteBack.AddChidren(btnColorView);
            //进度条(色温的范围是 3400~6000)
            var seekBar1 = new SeekBarControl(683);
            seekBar1.Y = btnColorView.Bottom;
            seekBar1.MinValue = 34;
            seekBar1.MaxValue = 60;
            seekBar1.ProgressBarColor = 0xff707070;
            seekBar1.SeekBarViewHeight = Application.GetRealHeight(19);
            frameWhiteBack.AddChidren(seekBar1);
            int oldColorValue = 0;
            int nowColorValue = 0;
            seekBar1.ProgressChangedEvent += (div, value) =>
            {
                //数据变更
                btnColorView.Text = value * 100 + "K";
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    nowColorValue = value;
                }
            };
            //设置初始值
            int colorValue = ((ColorTemperatureLight)this.device).ColorTemperature;
            if (colorValue == 0) { colorValue = 3400; }
            seekBar1.Progress = colorValue / 100;
            btnColorView.Text = colorValue + "K";
            //开一个线程,监视是否滑动的滑动条,每秒检测一次
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    //能够发送
                    if (this.canSetProgressValue == true)
                    {
                        //发送亮度值
                        if (nowProgressValue != oldProgressValue)
                        {
                            oldProgressValue = nowProgressValue;
                            ((ColorTemperatureLight)this.device).SetLevel((int)(oldProgressValue * MaxLevel / 100.0));
                        }
                        //发送色温值
                        if (nowColorValue != oldColorValue)
                        {
                            oldColorValue = nowColorValue;
                            ((ColorTemperatureLight)this.device).SetColorTemperature(oldColorValue * 100);
                        }
                    }
                }
                //界面关闭时
                if (nowProgressValue != oldProgressValue)
                {
                    //发送亮度值
                    ((ColorTemperatureLight)this.device).SetLevel((int)(nowProgressValue * MaxLevel / 100.0));
                }
                if (nowColorValue != oldColorValue)
                {
                    //发送色温值
                    ((ColorTemperatureLight)this.device).SetColorTemperature(nowColorValue * 100);
                }
            });
        }
        #endregion
        #region ■ 发送开关命令_______________________
        /// <summary>
        /// 发送开关命令
        /// </summary>
        /// <param name="isOpen"></param>
        private void SetSwitchCommand(bool isOpen)
        {
            //获取当前亮度
            int level = Convert.ToInt32(this.device.GetType().InvokeMember("Level", System.Reflection.BindingFlags.GetField, null, this.device, null));
            //如果住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                ((LightBase)this.device).OnOffStatus = isOpen == true ? 1 : 0;
                if (((LightBase)this.device).OnOffStatus == 1 && level == 0)
                {
                    //如果当前是打开状态,并且亮度为0的话,则需要变成100%亮度
                    this.device.GetType().InvokeMember("Level", System.Reflection.BindingFlags.SetField, null, this.device, new object[] { 100 });
                }
                //刷新开关状态
                this.RefreshSwitchStatu(isOpen);
                return;
            }
            //当按下开关按钮时,不能再发送进度值
            this.canSetProgressValue = false;
            //检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息
            this.StartCheckResponeResult(this.listControl, (result) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    bool statu = ((LightBase)this.device).OnOffStatus == 1;
                    //接收到网关回复
                    if (result == true)
                    {
                        //刷新开关状态
                        this.RefreshSwitchStatu(statu);
                        //状态取反
                        listControl[0].IsSelected = statu;
                    }
                });
            });
            if (isOpen == true)
            {
                //打开
                if (level == 0)
                {
                    //如果当前是打开状态,并且亮度为0的话,则需要变成100%亮度
                    this.device.GetType().InvokeMember("SetLevel", System.Reflection.BindingFlags.InvokeMethod, null, this.device, new object[] { 254 });
                }
                else
                {
                    this.device.SwitchControl(1);
                }
            }
            else
            {
                //关闭
                this.device.SwitchControl(0);
            }
        }
        #endregion
        #region ■ 是否获取网关反馈的结果_____________
        /// <summary>
        /// 检测网关的反馈结果(属性上报的对象:device.DeviceStatusReport)
        /// </summary>
        /// <param name="comandDiv">命令区分</param>
        /// <param name="report">上报数据</param>
        /// <returns></returns>
        public override bool CheckResponeResultStatu(ReceiveComandDiv comandDiv, CommonDevice report)
        {
            if (comandDiv == ReceiveComandDiv.A设备属性上报)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //刷新开关状态
                    this.RefreshSwitchStatu(((LightBase)this.device).OnOffStatus == 1);
                });
                return true;
            }
            return false;
        }
        #endregion
        #region ■ 刷新开关状态_______________________
        /// <summary>
        /// 刷新开关状态
        /// </summary>
        /// <param name="isOpen">打开状态</param>
        private void RefreshSwitchStatu(bool isOpen)
        {
            if (isOpen == true)
            {
                //亮度是必须要刷新的  亮度 XX
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
            }
            if (isOpen == false && this.IsLightOpen == true)
            {
                //状态不一样,才变更字样:关闭
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.Close));
            }
            if (listControl[0].IsSelected != isOpen)
            {
                //开关状态变更
                listControl[0].IsSelected = isOpen;
            }
            if (this.isProgressing == false)
            {
                //当进度值在手动变更中时,不接收推送
                waveSeekBar.Progress = (int)(((ColorTemperatureLight)this.device).Level * 1.0 / MaxLevel * 100);
            }
            this.IsLightOpen = isOpen;
            //回复的结果说,处于打开状态才能发送
            this.canSetProgressValue = this.IsLightOpen;
        }
        #endregion
    }
}