黄学彪
2020-09-01 dee21bf452a8979d0515d13e534fbb69ed9715dd
ZigbeeApp/Shared/Phone/MainPage/ControlForm/DeviceMiniLightDetailCardForm.cs
New file
@@ -0,0 +1,522 @@
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>
    /// mini夜灯的深度卡片界面
    /// </summary>
    public class DeviceMiniLightDetailCardForm : DeviceDetailCardCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 蜂鸣器的开关控件
        /// </summary>
        private IconBigViewControl btnBuzzerSwitch = null;
        /// <summary>
        /// mini夜灯的开关控件
        /// </summary>
        private IconBigViewControl btnMiniSwitch = null;
        /// <summary>
        /// 亮度的进度条控件
        /// </summary>
        private SeekBarControl seekBarLight = null;
        /// <summary>
        /// 色温的进度条控件
        /// </summary>
        private SeekBarControl seekBarColor = null;
        /// <summary>
        /// MaxLevel
        /// </summary>
        private const int MaxLevel = 254;
        /// <summary>
        /// 亮度进度值是否在改变中
        /// </summary>
        private bool isLightProgressing = false;
        /// <summary>
        /// 色温进度值是否在改变中
        /// </summary>
        private bool isColorProgressing = false;
        /// <summary>
        /// 1:发送的是小夜灯  2:发送的是蜂鸣器
        /// </summary>
        private int sendDiv = 0;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 底层初始化中部控件完成之后
        /// </summary>
        /// <param name="frameWhiteBack"></param>
        public override void InitMiddleFrameAfter(FrameLayout frameWhiteBack)
        {
            //添加接收蜂鸣器Ack主题
            this.AddReceiveBuzzerAckEvent();
            //左滑不能
            this.ScrollEnabled = false;
            //设置状态文字
            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));
            }
            //亮度
            var btnLightView = new NormalViewControl(300, 60, true);
            btnLightView.Text = Language.StringByID(R.MyInternationalizationString.uBrightness) + ":";
            btnLightView.X = Application.GetRealWidth(43);
            btnLightView.Y = Application.GetRealHeight(320);
            frameWhiteBack.AddChidren(btnLightView);
            //底线
            var btnLightLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
            btnLightLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            btnLightLine.X = btnLightView.X;
            btnLightLine.Y = btnLightView.Bottom + Application.GetRealHeight(37);
            frameWhiteBack.AddChidren(btnLightLine);
            //进度条(+60的偏移量)
            this.seekBarLight = new SeekBarControl(611 + 120);
            seekBarLight.Y = Application.GetRealHeight(280);
            seekBarLight.X = Application.GetRealWidth(266 - 60);
            seekBarLight.Height = Application.GetRealHeight(144);
            seekBarLight.SeekBarViewHeight = Application.GetRealHeight(20);
            seekBarLight.SeekBarPadding = Application.GetRealWidth(60);
            seekBarLight.IsProgressTextShow = true;
            seekBarLight.ProgressBarColor = 0xffff6c2c;
            seekBarLight.ProgressTextSize = 14;
            seekBarLight.ProgressTextColor = UserCenterColor.Current.TextGrayColor2;
            seekBarLight.Gravity = Gravity.Frame;
            seekBarLight.Progress = (int)(((ColorTemperatureLight)this.device).Level * 1.0 / MaxLevel * 100);
            frameWhiteBack.AddChidren(seekBarLight);
            //色温
            var btnColorView = new NormalViewControl(300, 60, true);
            btnColorView.Text = Language.StringByID(R.MyInternationalizationString.uColorTemperature) + ":";
            btnColorView.X = Application.GetRealWidth(43);
            btnColorView.Y = btnLightLine.Y + Application.GetRealHeight(107);
            frameWhiteBack.AddChidren(btnColorView);
            //底线
            var btnColorLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
            btnColorLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            btnColorLine.X = btnColorView.X;
            btnColorLine.Y = btnColorView.Bottom + Application.GetRealHeight(37);
            frameWhiteBack.AddChidren(btnColorLine);
            //色温的渐变色背景
            var btnColorBack = new PicViewControl(611, 20);
            btnColorBack.UnSelectedImagePath = "Item/ColorTemperatureBar.png";
            btnColorBack.X = Application.GetRealWidth(266);
            btnColorBack.Y = btnColorView.Y + btnColorView.Height / 2 - HdlControlLogic.Current.GetPictrueRealSize(20) / 2;
            frameWhiteBack.AddChidren(btnColorBack);
            //进度条(+60的偏移量,色温的范围是 3000~6000)
            this.seekBarColor = new SeekBarControl(611 + 120);
            seekBarColor.Y = btnLightLine.Bottom + Application.GetRealHeight(88);
            seekBarColor.X = Application.GetRealWidth(266 - 60);
            seekBarColor.SeekBarViewHeight = Application.GetRealHeight(20);
            seekBarColor.SeekBarPadding = Application.GetRealWidth(60);
            seekBarColor.ProgressBarColor = UserCenterColor.Current.Transparent;
            seekBarColor.ProgressBarUnEnableColor = UserCenterColor.Current.Transparent;
            seekBarColor.SeekBarBackgroundColor = UserCenterColor.Current.Transparent;
            seekBarColor.ProgressTextSize = 14;
            seekBarColor.ProgressTextColor = UserCenterColor.Current.TextGrayColor2;
            seekBarColor.Gravity = Gravity.Frame;
            seekBarColor.MinValue = 30;
            seekBarColor.MaxValue = 60;
            seekBarColor.ProgressChangeDelayTime = 0;
            frameWhiteBack.AddChidren(seekBarColor);
            //显示自定义文本
            seekBarColor.ShowCustomTextView(Application.GetRealWidth(200), 15, UserCenterColor.Current.TextGrayColor3);
            //设置初始值
            int colorValue = ((ColorTemperatureLight)this.device).ColorTemperature;
            if (colorValue == 0) { colorValue = 3000; }
            seekBarColor.Progress = colorValue / 100;
            seekBarColor.SetCustomText(seekBarColor.Progress * 100 + "K");
            //蜂鸣器开关:
            var btnBuzzerView = new NormalViewControl(300, 60, true);
            btnBuzzerView.Text = Language.StringByID(R.MyInternationalizationString.uBuzzerSwitch) + ":";
            btnBuzzerView.X = Application.GetRealWidth(43);
            btnBuzzerView.Y = btnColorLine.Y + Application.GetRealHeight(107);
            frameWhiteBack.AddChidren(btnBuzzerView);
            //底线
            var btnBuzzerLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
            btnBuzzerLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            btnBuzzerLine.X = btnBuzzerView.X;
            btnBuzzerLine.Y = btnBuzzerView.Bottom + Application.GetRealHeight(37);
            frameWhiteBack.AddChidren(btnBuzzerLine);
            //开关
            this.btnBuzzerSwitch = new IconBigViewControl(81, 81);
            btnBuzzerSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnBuzzerSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            btnBuzzerSwitch.IsSelected = ((ColorTemperatureLight)this.device).IsBuzzerRing;
            frameWhiteBack.AddChidren(btnBuzzerSwitch);
            btnBuzzerSwitch.InitControl();
            btnBuzzerSwitch.UseClickStatu = false;
            btnBuzzerSwitch.X = Application.GetRealWidth(795) - btnBuzzerSwitch.XOffset;
            btnBuzzerSwitch.Y = btnColorLine.Y + Application.GetRealHeight(98) - btnBuzzerSwitch.YOffset;
            btnBuzzerSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetBuzzerSwitchCommand(!btnBuzzerSwitch.IsSelected);
            };
            //小夜灯开关:
            var btnMiniView = new NormalViewControl(300, 60, true);
            btnMiniView.Text = Language.StringByID(R.MyInternationalizationString.uMiniLightSwitch) + ":";
            btnMiniView.X = Application.GetRealWidth(43);
            btnMiniView.Y = btnBuzzerLine.Y + Application.GetRealHeight(107);
            frameWhiteBack.AddChidren(btnMiniView);
            //底线
            var btnMiniLine = new NormalViewControl(Application.GetRealWidth(841), ControlCommonResourse.BottomLineHeight, false);
            btnMiniLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
            btnMiniLine.X = btnMiniView.X;
            btnMiniLine.Y = btnMiniView.Bottom + Application.GetRealHeight(37);
            frameWhiteBack.AddChidren(btnMiniLine);
            //开关
            this.btnMiniSwitch = new IconBigViewControl(81, 81);
            btnMiniSwitch.UnSelectedImagePath = "Item/Switch.png";
            btnMiniSwitch.SelectedImagePath = "Item/SwitchSelected.png";
            frameWhiteBack.AddChidren(btnMiniSwitch);
            btnMiniSwitch.InitControl();
            btnMiniSwitch.UseClickStatu = false;
            btnMiniSwitch.X = Application.GetRealWidth(795) - btnMiniSwitch.XOffset;
            btnMiniSwitch.Y = btnBuzzerLine.Y + Application.GetRealHeight(98) - btnMiniSwitch.YOffset;
            btnMiniSwitch.ButtonClickEvent += (sender, e) =>
            {
                //发送开关命令
                this.SetLightSwitchCommand(!btnMiniSwitch.IsSelected);
            };
            btnMiniSwitch.IsSelected = ((LightBase)this.device).OnOffStatus == 1;
            //亮度开始滑动的事件
            seekBarLight.OnStartTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始变更
                this.isLightProgressing = true;
            };
            //亮度结束滑动的事件
            seekBarLight.OnStopTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始结束
                this.isLightProgressing = false;
            };
            //亮度滑动过程中
            int oldLightValue = seekBarLight.Progress;//之前的值
            int nowLightValue = oldLightValue;//变更的值
            seekBarLight.OnProgressChangedEvent += (sender, value) =>
            {
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    //能够发送进度值的时候,才记录
                    nowLightValue = value;
                }
                else
                {
                    //如果住宅为虚拟住宅,直接改缓存
                    ((ColorTemperatureLight)this.device).Level = value * MaxLevel / 100;
                    //亮度 XX
                    this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
                }
            };
            //色温开始滑动的事件
            seekBarColor.OnStartTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始变更
                this.isColorProgressing = true;
            };
            //色温结束滑动的事件
            seekBarColor.OnStopTrackingTouchEvent += (sender, e) =>
            {
                //进度值开始结束
                this.isColorProgressing = false;
            };
            //色温滑动过程中
            int oldColorValue = seekBarColor.Progress;//之前的值
            int nowColorValue = oldColorValue;//变更的值
            seekBarColor.OnProgressChangedEvent += (sender, value) =>
            {
                //设置自定义的文本
                seekBarColor.SetCustomText(value * 100 + "K");
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    //能够发送进度值的时候,才记录
                    nowColorValue = value;
                }
                else
                {
                    //如果住宅为虚拟住宅,直接改缓存
                    ((ColorTemperatureLight)this.device).ColorTemperature = value * 100;
                }
            };
            if (Config.Instance.Home.IsVirtually == true)
            {
                //虚拟住宅
                return;
            }
                //开一个线程,监视是否滑动的滑动条,每秒检测一次
            HdlThreadLogic.Current.RunThread(() =>
            {
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    //发送亮度值
                    if (nowLightValue != oldLightValue)
                    {
                        oldLightValue = nowLightValue;
                        ((ColorTemperatureLight)this.device).SetLevel((int)(oldLightValue * MaxLevel / 100.0));
                    }
                    //发送色温值
                    if (nowColorValue != oldColorValue)
                    {
                        oldColorValue = nowColorValue;
                        int value = 1000000 / (oldColorValue * 100);
                        ((ColorTemperatureLight)this.device).SetColorTemperature(value);
                    }
                }
                //界面关闭时
                if (nowLightValue != oldLightValue)
                {
                    //发送亮度值
                    ((ColorTemperatureLight)this.device).SetLevel((int)(nowLightValue * MaxLevel / 100.0));
                }
                if (nowColorValue != oldColorValue)
                {
                    //发送色温值
                    int value = 1000000 / (nowColorValue * 100);
                    ((ColorTemperatureLight)this.device).SetColorTemperature(value);
                }
            });
        }
        #endregion
        #region ■ 发送夜灯开关命令___________________
        /// <summary>
        /// 发送夜灯开关命令
        /// </summary>
        /// <param name="isOpen"></param>
        private void SetLightSwitchCommand(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.StartCheckResponeResult(new List<ButtonBase> { this.btnMiniSwitch.btnIcon }, (result) =>
             {
                 HdlThreadLogic.Current.RunMain(() =>
                 {
                     //接收到网关回复
                     if (result == true)
                     {
                         bool statu = ((LightBase)this.device).OnOffStatus == 1;
                         //刷新开关状态
                         this.RefreshSwitchStatu(statu);
                     }
                 });
             });
            this.sendDiv = 1;
            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>
        /// 发送蜂鸣器开关命令
        /// </summary>
        /// <param name="isOpen"></param>
        private void SetBuzzerSwitchCommand(bool isOpen)
        {
            //如果住宅是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                ((ColorTemperatureLight)this.device).IsBuzzerRing = isOpen;
                //刷新开关状态
                this.RefreshSwitchStatu(isOpen);
                return;
            }
            //检测是否获取网关反馈的结果,如果网关没有回复,则会弹出消息
            this.btnBuzzerSwitch.CanClick = false;
            this.StartCheckResponeResult(new List<ButtonBase>(), (result) =>
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //接收到网关回复
                    this.btnBuzzerSwitch.CanClick = true;
                    if (result == true)
                    {
                        this.btnBuzzerSwitch.IsSelected = isOpen;
                    }
                });
            });
            //发送命令
            this.sendDiv = 2;
            ((ColorTemperatureLight)this.device).SendBuzzerSwitchControl(isOpen);
        }
        /// <summary>
        /// 添加接收蜂鸣器Ack主题
        /// </summary>
        private void AddReceiveBuzzerAckEvent()
        {
            //Ack主题上报
            string mainKeys = LocalDevice.Current.GetDeviceMainKeys(this.device);
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("DeviceBuzzerDetailCardACK", ReceiveComandDiv.A节点控制反馈, (report) =>
            {
                string mainKey2 = LocalDevice.Current.GetDeviceMainKeys(report);
                if (mainKeys != mainKey2)
                {
                    //不是同一个东西
                    return;
                }
                //这里只处理蜂鸣器的
                if (this.sendDiv == 2)
                {
                    //结果已经接收到
                    this.ResponeResult = 1;
                }
            });
        }
        #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)
        {
            //刷新蜂鸣器开关状态
            bool isBuzzerRing = ((ColorTemperatureLight)this.device).IsBuzzerRing;
            if (this.btnBuzzerSwitch.IsSelected != isBuzzerRing)
            {
                this.btnBuzzerSwitch.IsSelected = isBuzzerRing;
            }
            if (isOpen == true)
            {
                //亮度是必须要刷新的  亮度 XX
                this.SetStatuText(Language.StringByID(R.MyInternationalizationString.uBrightness) + "  " + HdlDeviceOtherLogic.Current.GetDeviceStatu(this.device));
                if (this.btnMiniSwitch.IsSelected == false)
                {
                    this.btnMiniSwitch.IsSelected = true;
                }
            }
            else
            {
                if (this.btnMiniSwitch.IsSelected == true)
                {
                    this.btnMiniSwitch.IsSelected = false;
                    //变更字样:关闭
                    this.SetStatuText(Language.StringByID(R.MyInternationalizationString.Close));
                }
            }
            if (this.isLightProgressing == false)
            {
                //当进度值在手动变更中时,不接收推送
                seekBarLight.Progress = (int)(((ColorTemperatureLight)this.device).Level * 1.0 / MaxLevel * 100);
            }
            if (this.isColorProgressing == false)
            {
                //当进度值在手动变更中时,不接收推送
                seekBarColor.Progress = (int)(((ColorTemperatureLight)this.device).ColorTemperature / 100);
                //设置自定义的文本
                seekBarColor.SetCustomText(seekBarColor.Progress * 100 + "K");
            }
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            base.CloseFormBefore();
            HdlGatewayReceiveLogic.Current.RemoveEvent("DeviceBuzzerDetailCardACK");
        }
        #endregion
    }
}