陈嘉乐
2021-03-16 902f19750cbf3e0e3dba7139947421ecb461f93f
HDL_ON/UI/UI2/4-PersonalCenter/AddDevice/AddMiniRemoteControlDirection2Page.cs
New file
@@ -0,0 +1,308 @@
using HDL_ON.Stan;
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.UI
{
    /// <summary>
    /// 添加Evoyo的Mini智能遥控器步骤2界面
    /// </summary>
    public class AddMiniRemoteControlDirection2Page : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// wifi名
        /// </summary>
        private string wifiName = string.Empty;
        /// <summary>
        /// wifi密码
        /// </summary>
        private string wifiPassword = string.Empty;
        /// <summary>
        /// 蓝牙设备
        /// </summary>
        private List<HdlBluetoothLogic.BluetoothInfo> listDevice = new List<HdlBluetoothLogic.BluetoothInfo>();
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_wifiName">wifi名(别的界面传过来的)</param>
        /// <param name="i_wifiPassword">(别的界面传过来的)</param>
        public void ShowForm(string i_wifiName, string i_wifiPassword)
        {
            this.wifiName = i_wifiName;
            this.wifiPassword = i_wifiPassword;
            //设置头部信息
            base.SetTitleText(Language.StringByID(StringId.AddInfraredRemoteControl));
            //这个界面的背景需要白色
            bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
            //初始化中部信息
            this.InitMiddleFrame();
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空body
            this.ClearBodyFrame();
            //进度条
            var btnProgress = new ProgressRowBar(180, 6);
            btnProgress.Y = Application.GetRealHeight(218);
            btnProgress.Gravity = Gravity.CenterHorizontal;
            bodyFrameLayout.AddChidren(btnProgress);
            btnProgress.StartMode1(true);
            //设备搜索中...
            var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(20), false);
            btnSearch.Y = btnProgress.Bottom + Application.GetRealHeight(40);
            btnSearch.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnSearch.TextID = StringId.SearchingDevice;
            btnSearch.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnSearch);
            //请确保您的蓝牙已开启并处于可以被搜索状态
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg2);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.FirstLevelTitleColor, Application.GetRealHeight(20), btnSearch.Bottom + Application.GetRealHeight(4));
            //开启进度条特效
            this.StartProgressSpecialEffects(btnProgress);
        }
        #endregion
        #region ■ 搜索蓝牙___________________________
        /// <summary>
        /// 搜索蓝牙
        /// </summary>
        private void StartSearchBluetooth()
        {
            //开始搜索蓝牙
            HdlBluetoothLogic.Current.ScanBluetooth(2, (listBluetooth) =>
            {
                foreach (var device in listBluetooth)
                {
                    //只有这个标识,才是红外宝
                    if (device.Name.StartsWith("MIR01R-LK.10") == true)
                    {
                        //是指定的红外宝设备
                        listDevice.Add(device);
                    }
                }
                if (listDevice.Count == 0)
                {
                    //摧毁蓝牙缓存
                    HdlBluetoothLogic.Current.Dispone();
                }
                else if (listDevice.Count == 1)
                {
                    //如果只检测到一个,则直接连接
                    HdlBluetoothLogic.Current.ContectBluetooth(listDevice[0], (result) =>
                    {
                        if (result == true && this.Parent != null)
                        {
                            //连接成功,则跳转到下一个界面
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                this.CloseForm();
                                var form = new AddMiniRemoteControlDirection3Page();
                                form.AddForm(this.wifiName, this.wifiPassword);
                            });
                        }
                        else
                        {
                            HdlBluetoothLogic.Current.Dispone();
                        }
                    });
                }
                else
                {
                    var listName = new List<string>();
                    foreach (var device in listDevice)
                    {
                        listName.Add(device.Name);
                    }
                    //显示选择蓝牙列表的界面(当匹配到多个蓝牙的时候使用)
                    this.ShowBluetoothListView(listName, (index) =>
                    {
                        //如果只检测到一个,则直接连接
                        HdlBluetoothLogic.Current.ContectBluetooth(listDevice[index], (result) =>
                        {
                            if (result == true)
                            {
                                //连接成功,则跳转到下一个界面
                                HdlThreadLogic.Current.RunMain(() =>
                                {
                                    this.CloseForm();
                                    var form = new AddMiniRemoteControlDirection3Page();
                                    form.AddForm(this.wifiName, this.wifiPassword);
                                });
                            }
                            else
                            {
                                HdlBluetoothLogic.Current.Dispone();
                            }
                        });
                    });
                }
            });
        }
        /// <summary>
        /// 开启进度条特效
        /// </summary>
        private void StartProgressSpecialEffects(ProgressRowBar btnProgress)
        {
            //搜索时间(秒)
            int searchTime = 180;
            HdlThreadLogic.Current.RunThread(() =>
            {
                //进度条特效
                for (int i = 1; i <= searchTime; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //进度条特效
                        btnProgress.SetValue(i, searchTime);
                    });
                    //界面关闭
                    if (this.Parent == null)
                    {
                        break;
                    }
                    //如果已经搜索到蓝牙,则直接到100%
                    if (listDevice.Count > 0)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            btnProgress.SetValue(searchTime, searchTime);
                        });
                        break;
                    }
                    if (i % 5 == 1)
                    {
                        //每5秒搜索一次
                        this.StartSearchBluetooth();
                    }
                    //超时
                    if (i >= searchTime)
                    {
                        System.Threading.Thread.Sleep(3000);
                        //最后的时间还是搜不到的话
                        if (listDevice.Count == 0)
                        {
                            //显示失败界面
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                this.ShowFailView();
                            });
                        }
                        break;
                    }
                }
            });
        }
        #endregion
        #region ■ 显示选择蓝牙列表的界面_____________
        /// <summary>
        /// 显示选择蓝牙列表的界面(当匹配到多个蓝牙的时候使用)
        /// </summary>
        /// <param name="listName">蓝牙名字列表</param>
        /// <param name="selectEvent">选择事件(考虑到Ios和安卓,所以这样定参数)</param>
        private void ShowBluetoothListView(List<string> listName, Action<int> selectEvent)
        {
            if (this.Parent == null) { return; }
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new BottomItemSelectControl(listName.Count, Language.StringByID(StringId.ChooseInfraredRemoteControl), false);
                //初始化
                contr.AddRowMenu(listName, new List<int>());
                contr.FinishOnlyEvent += (div, value) =>
                {
                    if (div == 0)
                    {
                        //如果点击了取消,则关闭这个界面,不管了
                        this.CloseForm();
                    }
                    else
                    {
                        //调用回调函数
                        selectEvent?.Invoke(value);
                    }
                };
            });
        }
        #endregion
        #region ■ 显示失败界面_______________________
        /// <summary>
        /// 显示失败界面
        /// </summary>
        private void ShowFailView()
        {
            //清空body
            this.ClearBodyFrame();
            //图片
            var btnPic = new PicViewControl(180, 180);
            btnPic.Y = Application.GetRealHeight(48);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "Public/TipIcon_Failed.png";
            bodyFrameLayout.AddChidren(btnPic);
            //没有发现红外遥控器
            var btnMsgNot = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnMsgNot.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnMsgNot.TextAlignment = TextAlignment.Center;
            btnMsgNot.TextColor = CSS.CSS_Color.AuxiliaryColor2;
            btnMsgNot.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnMsgNot.TextID = StringId.NoInfraredRemoteControlFound;
            bodyFrameLayout.AddChidren(btnMsgNot);
            //1、请检查设备是否正常通电
            //2、请检查蓝牙功能是否正常开启
            //3、请检查指示灯是否蓝色快闪状态
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg3);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.TextualColor, Application.GetRealHeight(20), btnMsgNot.Bottom + Application.GetRealHeight(8),
                 TextAlignment.Center, true);
            //重新搜索
            var btnReSearch = this.AddBottomClickButton(Language.StringByID(StringId.ReSearch));
            btnReSearch.ButtonClickEvent += (sender, e) =>
            {
                //检测蓝牙需要的东西
                btnReSearch.CanClick = false;
                HdlBluetoothLogic.Current.CheckCanScanBluetooth((result) =>
                {
                    btnReSearch.CanClick = true;
                    if (result == true)
                    {
                        //初始化中部信息
                        this.InitMiddleFrame();
                    }
                });
            };
        }
        #endregion
    }
}