陈嘉乐
2021-03-16 902f19750cbf3e0e3dba7139947421ecb461f93f
HDL_ON/UI/UI2/4-PersonalCenter/AddDevice/AddMiniRemoteControlDirection4Page.cs
New file
@@ -0,0 +1,533 @@
using HDL_ON.Stan;
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.UI
{
    /// <summary>
    /// 添加Evoyo的Mini智能遥控器步骤4界面
    /// </summary>
    public class AddMiniRemoteControlDirection4Page : EditorCommonForm
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// wifi名字
        /// </summary>
        private string wifiName = string.Empty;
        /// <summary>
        /// wifi密码
        /// </summary>
        private string wifiPsw = string.Empty;
        /// <summary>
        /// 超时线程是否运行
        /// </summary>
        private bool timeoutThreadActivity = false;
        /// <summary>
        /// 上传到云的图标
        /// </summary>
        private IconViewControl btnClound = null;
        /// <summary>
        /// 上传到云
        /// </summary>
        private NormalViewControl btnCloundView = null;
        /// <summary>
        /// 连接成功的图标
        /// </summary>
        private IconViewControl btnConnect = null;
        /// <summary>
        /// 连接成功
        /// </summary>
        private NormalViewControl btnConnetView = null;
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_wifiName">wifi名字</param>
        /// <param name="i_wifiPsw">wifi密码</param>
        public void ShowForm(string i_wifiName, string i_wifiPsw)
        {
            this.wifiName = i_wifiName;
            this.wifiPsw = i_wifiPsw;
            //设置头部信息
            base.SetTitleText(Language.StringByID(StringId.AddInfraredRemoteControl));
            //这个界面的背景需要白色
            bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
            //添加接收蓝牙反馈的事件
            HdlBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
            //添加云端反馈事件
            HdlCloudReceiveLogic.Current.AddCloudReceiveEvent("AddMiniRemoteControlDirection4Page", this.CloudReceiveEvent);
            //初始化中部信息
            this.InitMiddleFrame();
        }
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空body
            this.ClearBodyFrame();
            //图片
            var btnPic = new PicViewControl(282, 121);
            btnPic.Y = Application.GetRealHeight(122);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "PersonalCenter/AddDevice/WifiAndPhoneConnect.png";
            bodyFrameLayout.AddChidren(btnPic);
            //正在连接中...
            var btnSearch = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(20), false);
            btnSearch.Y = btnPic.Bottom + Application.GetRealHeight(40);
            btnSearch.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnSearch.TextID = StringId.NowConnectting;
            btnSearch.TextAlignment = TextAlignment.Center;
            bodyFrameLayout.AddChidren(btnSearch);
            //请让红外遥控尽量接近WIFI路由器
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg5);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize,
                CSS.CSS_Color.PromptingColor1, Application.GetRealHeight(20), btnSearch.Bottom + Application.GetRealHeight(4));
            //网络连接中
            var btnNetIcon = new IconViewControl(20);
            btnNetIcon.X = Application.GetRealWidth(60);
            btnNetIcon.Y = btnPic.Bottom + Application.GetRealHeight(265);
            btnNetIcon.UnSelectedImagePath = "PersonalCenter/AddDevice/NetConnectSelect.png";
            bodyFrameLayout.AddChidren(btnNetIcon);
            var btnNetView = new NormalViewControl(140, 18, true);//左右间距40再加文本60
            btnNetView.Y = btnNetIcon.Bottom + Application.GetRealHeight(16);
            btnNetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnNetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
            btnNetView.TextAlignment = TextAlignment.Center;
            btnNetView.TextID = StringId.InTheNetworkLink;
            bodyFrameLayout.AddChidren(btnNetView);
            //一条横线
            var btnLine1 = new NormalViewControl(Application.GetRealWidth(92), 2, false);
            btnLine1.X = btnNetIcon.Right + Application.GetRealWidth(3);
            btnLine1.Y = btnNetIcon.Y + (btnNetIcon.Height + 2) / 2;
            btnLine1.BackgroundColor = CSS.CSS_Color.PromptingColor1;
            bodyFrameLayout.AddChidren(btnLine1);
            //上传到云
            this.btnClound = new IconViewControl(20);
            btnClound.Gravity = Gravity.CenterHorizontal;
            btnClound.Y = btnNetIcon.Y;
            btnClound.UnSelectedImagePath = "PersonalCenter/AddDevice/UploadToClound.png";
            btnClound.SelectedImagePath = "PersonalCenter/AddDevice/UploadToCloundSelect.png";
            bodyFrameLayout.AddChidren(btnClound);
            this.btnCloundView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
            btnCloundView.Y = btnNetView.Y;
            btnCloundView.Gravity = Gravity.CenterHorizontal;
            btnCloundView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnCloundView.TextColor = CSS.CSS_Color.PromptingColor1;
            btnCloundView.TextAlignment = TextAlignment.Center;
            btnCloundView.TextID = StringId.UploadToCloud;
            bodyFrameLayout.AddChidren(btnCloundView);
            //一条横线
            var btnLine2 = new NormalViewControl(btnLine1.Width, btnLine1.Height, false);
            btnLine2.X = btnClound.Right + Application.GetRealWidth(3);
            btnLine2.Y = btnLine1.Y;
            btnLine2.BackgroundColor = CSS.CSS_Color.PromptingColor1;
            bodyFrameLayout.AddChidren(btnLine2);
            //连接成功
            this.btnConnect = new IconViewControl(20);
            btnConnect.X = bodyFrameLayout.Width - btnNetIcon.X - btnConnect.IconSize;
            btnConnect.Y = btnNetIcon.Y;
            btnConnect.UnSelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccess.png";
            btnConnect.SelectedImagePath = "PersonalCenter/AddDevice/ConnectSuccessSelect.png";
            bodyFrameLayout.AddChidren(btnConnect);
            this.btnConnetView = new NormalViewControl(btnNetView.Width, btnNetView.Height, false);
            btnConnetView.X = bodyFrameLayout.Width - btnNetView.Width - btnNetView.X;
            btnConnetView.Y = btnNetView.Y;
            btnConnetView.TextSize = CSS.CSS_FontSize.PromptFontSize_FirstLevel;
            btnConnetView.TextColor = CSS.CSS_Color.PromptingColor1;
            btnConnetView.TextAlignment = TextAlignment.Center;
            btnConnetView.TextID = StringId.ConnectSuccess;
            bodyFrameLayout.AddChidren(btnConnetView);
            HdlThreadLogic.Current.RunThread(() =>
            {
                System.Threading.Thread.Sleep(2000);
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //发送账号和密码给蓝牙
                    this.SendAccountAndPswToBluetooth();
                });
                //启动超时线程
                this.StartTimeoutThread();
            });
        }
        #endregion
        #region ■ 显示成功界面_______________________
        /// <summary>
        /// 显示成功界面
        /// </summary>
        private void ShowSuccessView()
        {
            //清空body
            this.ClearBodyFrame();
            //图片
            var btnPic = new PicViewControl(180, 180);
            btnPic.Y = Application.GetRealHeight(48);
            btnPic.Gravity = Gravity.CenterHorizontal;
            btnPic.UnSelectedImagePath = "Public/TipIcon_Successfully.png";
            bodyFrameLayout.AddChidren(btnPic);
            //添加成功
            var btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnFail.TextColor = CSS.CSS_Color.MainColor;
            btnFail.TextAlignment = TextAlignment.Center;
            btnFail.TextID = StringId.AddSuccess;
            bodyFrameLayout.AddChidren(btnFail);
            //可以开始使用红外遥控功能!
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg7);
            int yy = this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
                Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8));
            //开始使用
            var btnUse = new BottomClickButton(220);
            btnUse.Y = yy + Application.GetRealHeight(60);
            btnUse.TextID = StringId.StartUse;
            bodyFrameLayout.AddChidren(btnUse);
            btnUse.ButtonClickEvent += (sender, e) =>
            {
                //关掉界面
                this.CloseForm();
                HdlFormLogic.Current.CloseFormByFormName("AddThirdPartyDeviceMenuListPage");
                HDL_ON.UI.UI2.PersonalCenter.PirDevice.Method.GetPirDeviceList(this, () =>
                {
                    Application.RunOnMainThread(() =>
                    {
                        var page = new UI2.PersonalCenter.PirDevice.PirMain();
                        MainPage.BasePageView.AddChidren(page);
                        page.Show();
                        MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
                    });
                });
            };
        }
        #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 btnFail = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(22), false);
            btnFail.Y = btnPic.Bottom + Application.GetRealHeight(16);
            btnFail.TextSize = CSS.CSS_FontSize.SubheadingFontSize;
            btnFail.TextColor = CSS.CSS_Color.AuxiliaryColor2;
            btnFail.TextAlignment = TextAlignment.Center;
            btnFail.TextID = StringId.AddFail;
            bodyFrameLayout.AddChidren(btnFail);
            //1、请检查设备是否正常通电
            //2、请开启蓝牙功能
            //3、并长按按钮10s,指示灯常亮
            var strMsg = Language.StringByID(StringId.AddInfraredRemoteControlMsg6);
            this.AddListMsgControls(bodyFrameLayout, strMsg, CSS.CSS_FontSize.TextFontSize, CSS.CSS_Color.TextualColor,
                Application.GetRealHeight(20), btnFail.Bottom + Application.GetRealHeight(8),
                 TextAlignment.Center, true);
            //重试
            var btnReDo = this.AddBottomClickButton(Language.StringByID(StringId.Retry));
            btnReDo.ButtonClickEvent += (sender, e) =>
            {
                //重新初始化中部信息
                this.CloseForm();
                var form = new AddMiniRemoteControlDirection2Page();
                form.AddForm(this.wifiName, this.wifiPsw);
            };
        }
        #endregion
        #region ■ 发送账号和密码给蓝牙_______________
        /// <summary>
        /// 发送账号和密码给蓝牙
        /// </summary>
        private void SendAccountAndPswToBluetooth()
        {
            //获取发送到蓝牙的数据
            var sendData = this.GetSendToBluetoothData();
            //不等待
            HdlBluetoothLogic.Current.SendData(sendData);
        }
        #endregion
        #region ■ 红外宝相关方法_____________________
        /// <summary>
        /// 检测是否是新的红外宝
        /// </summary>
        /// <param name="pushEnum">推送枚举</param>
        /// <param name="i_data">推送的数据</param>
        /// <returns></returns>
        private bool CheckIsNewMiniRemoteControl(CloudPushEnum pushEnum, string i_data)
        {
            if (pushEnum != CloudPushEnum.A新设备上报) { return false; }
            try
            {
                var strSpk = string.Empty;
                //转为json
                var json = Newtonsoft.Json.Linq.JObject.Parse(i_data);
                var linqArry = json["objects"] as Newtonsoft.Json.Linq.JArray;
                if (linqArry != null)
                {
                    //数组类型
                    if (linqArry.Count == 0) { return false; }
                    strSpk = linqArry[0]["spk"].ToString();
                }
                else
                {
                    //非数组类型
                    strSpk = json["objects"]["spk"].ToString();
                }
                if (strSpk != Entity.SPK.IrModule)
                {
                    //如果不是红外宝的话
                    return false;
                }
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 刷新红外宝信息
        /// </summary>
        /// <param name="successEvent">成功的回调事件</param>
        private void RefreshMiniRemoteControlInfo(Action successEvent)
        {
            var pra = new Dictionary<string, object>();
            pra.Add("homeId", Entity.DB_ResidenceData.Instance.CurrentRegion.RegionID);
            pra.Add("spk", Entity.SPK.IrModule);
            var requestJson = DAL.Server.HttpUtil.GetSignRequestJson(pra);
            HdlThreadLogic.Current.RunThread(() =>
            {
                int count = 0;
                while (this.Parent != null)
                {
                    try
                    {
                        //超过5次都不成功,真不处理了
                        count++;
                        if (count > 5) { return; }
                        //获取全部红外宝列表
                        var result = DAL.Server.HttpUtil.RequestHttpsPostFroHome(DAL.Server.NewAPI.Api_Post_GetDevcieList, requestJson);
                        if (result == null || result.Code != DAL.Server.StateCode.SUCCESS)
                        {
                            System.Threading.Thread.Sleep(3000);
                            continue;
                        }
                        var listDevice = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.DevcieApiPack>(result.Data.ToString());
                        foreach (var newFunction in listDevice.list)
                        {
                            //添加到缓存当中
                            newFunction.SaveFunctionFile();
                            Entity.FunctionList.List.IniFunctionList(newFunction.savePath, true);
                        }
                        //调用回调函数
                        successEvent?.Invoke();
                        return;
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(3000);
                    }
                }
            });
        }
        #endregion
        #region ■ 云端反馈___________________________
        /// <summary>
        /// 云端反馈
        /// </summary>
        /// <param name="pushEnum">枚举</param>
        /// <param name="i_data">推送的内容</param>
        private void CloudReceiveEvent(CloudPushEnum pushEnum, string i_data)
        {
            //检测是否是新的红外宝
            if (this.CheckIsNewMiniRemoteControl(pushEnum, i_data) == false)
            {
                return;
            }
            //刷新红外宝信息
            this.RefreshMiniRemoteControlInfo(() =>
            {
                //移除这个事件
                HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
                //停止线程
                this.timeoutThreadActivity = false;
                //添加设备成功之后,处理一些东西
                this.DoSomethingAfterAddDeviceSuccess();
            });
        }
        /// <summary>
        /// 添加设备成功之后,处理一些东西
        /// </summary>
        private void DoSomethingAfterAddDeviceSuccess()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //整点特效而已
                for (int i = 1; i <= 6; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    if (i == 1)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //整点特效而已
                            this.btnClound.IsSelected = true;
                            this.btnCloundView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
                        });
                    }
                    else if (i == 4)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //整点特效而已
                            this.btnConnect.IsSelected = true;
                            this.btnConnetView.TextColor = CSS.CSS_Color.FirstLevelTitleColor;
                        });
                    }
                    else if (i == 6)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //显示成功界面
                            this.ShowSuccessView();
                        });
                    }
                }
            });
        }
        #endregion
        #region ■ 蓝牙反馈___________________________
        /// <summary>
        /// 蓝牙反馈
        /// </summary>
        /// <param name="i_receviceData">蓝牙反馈的数据</param>
        private void BluetoothReceiveEvent(string i_receviceData)
        {
#if DEBUG
            System.Console.WriteLine("蓝牙返回:" + i_receviceData);
#endif
        }
        #endregion
        #region ■ 超时线程___________________________
        /// <summary>
        /// 启动超时线程
        /// </summary>
        private void StartTimeoutThread()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //360秒超时
                int timeout = 360;
                while (this.timeoutThreadActivity == false && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    timeout--;
                    if (timeout == 0)
                    {
                        break;
                    }
                }
                if (timeout <= 0)
                {
                    //显示失败界面
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        this.ShowFailView();
                    });
                }
            });
        }
        #endregion
        #region ■ 界面关闭___________________________
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            //摧毁蓝牙
            HdlBluetoothLogic.Current.Dispone();
            HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
            base.CloseFormBefore();
        }
        #endregion
        #region ■ 一般方法___________________________
        /// <summary>
        /// 获取发送到蓝牙的数据
        /// </summary>
        /// <returns></returns>
        private string GetSendToBluetoothData()
        {
            var pra = new { id = "id010203", ssid = this.wifiName, password = this.wifiPsw };
            var praData = Newtonsoft.Json.JsonConvert.SerializeObject(pra);
            var sendData = "Topic:/user/id/custom/wifi/set\r\n";
            sendData += "Length:" + praData.Length + "\r\n\r\n";
            sendData += praData;
            return sendData;
        }
        #endregion
    }
}