wei
2021-03-23 fe2b566c8d9c097d2568ad211088fa5bc6f8c229
HDL_ON/UI/UI2/4-PersonalCenter/AddDevice/AddMiniRemoteControlDirection4Page.cs
@@ -22,6 +22,10 @@
        /// </summary>
        private string wifiPsw = string.Empty;
        /// <summary>
        /// 新设备的sid(用来匹配哪一个设备是新的)
        /// </summary>
        private string newDeviceSid = string.Empty;
        /// <summary>
        /// 超时线程是否运行
        /// </summary>
        private bool timeoutThreadActivity = false;
@@ -61,12 +65,9 @@
            //这个界面的背景需要白色
            bodyFrameLayout.BackgroundColor = UI.CSS.CSS_Color.MainBackgroundColor;
#if __IOS__
#endif
#if __Android__
            //添加接收蓝牙反馈的事件
            HdlAndroidBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
#endif
            HdlBluetoothLogic.Current.AddReceiveEvent(this.BluetoothReceiveEvent);
            //添加云端反馈事件
            HdlCloudReceiveLogic.Current.AddCloudReceiveEvent("AddMiniRemoteControlDirection4Page", this.CloudReceiveEvent);
@@ -179,7 +180,7 @@
        /// <summary>
        /// 显示成功界面
        /// </summary>
        private void ShowSuccessView()
        private void ShowSuccessView(Entity.Function newDevice)
        {
            //清空body
            this.ClearBodyFrame();
@@ -212,6 +213,29 @@
            bodyFrameLayout.AddChidren(btnUse);
            btnUse.ButtonClickEvent += (sender, e) =>
            {
                if (newDevice == null)
                {
                    //关闭掉全部的界面,应该不会发生
                    HdlFormLogic.Current.CloseAllOpenForm();
                    return;
                }
                var form = HdlFormLogic.Current.GetFormByName("AddMiniRemoteControlDirection1Page") as AddMiniRemoteControlDirection1Page;
                //if (form.AddDeviceEvent != null)
                {
                    //代表这个是由温总那边调用的,直接回调函数
                    form.AddDeviceEvent?.Invoke(newDevice);
                    //关闭掉这个界面
                    this.CloseForm();
                    //再把AddMiniRemoteControlDirection1Page界面关了
                    HdlFormLogic.Current.CloseFormByFormName("AddMiniRemoteControlDirection1Page");
                }
                //else
                //{
                //    //代表这并不是由温总的界面调用的,则关闭掉全部的界面
                //    HdlFormLogic.Current.CloseAllOpenForm();
                //    //然后再把温总的界面new起来
                //    new UI2.PersonalCenter.PirDevice.Method().MainView(this, newDevice,()=> { });
                //}
            };
        }
@@ -255,7 +279,9 @@
            btnReDo.ButtonClickEvent += (sender, e) =>
            {
                //重新初始化中部信息
                this.InitMiddleFrame();
                this.CloseForm();
                var form = new AddMiniRemoteControlDirection2Page();
                form.AddForm(this.wifiName, this.wifiPsw);
            };
        }
@@ -268,14 +294,107 @@
        /// </summary>
        private void SendAccountAndPswToBluetooth()
        {
#if __IOS__
#endif
#if __Android__
            //获取发送到蓝牙的数据
            var sendData = this.GetSendToBluetoothData();
            //不等待
            HdlAndroidBluetoothLogic.Current.SendData(sendData);
#endif
            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();
                    this.newDeviceSid = linqArry[0]["sid"].ToString();
                }
                else
                {
                    //非数组类型
                    strSpk = json["objects"]["spk"].ToString();
                    this.newDeviceSid = json["objects"]["sid"].ToString();
                }
                if (strSpk != Entity.SPK.IrModule)
                {
                    //如果不是红外宝的话
                    return false;
                }
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 刷新红外宝信息
        /// </summary>
        /// <param name="successEvent">成功的回调事件</param>
        private void RefreshMiniRemoteControlInfo(Action<Entity.Function> 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());
                        Entity.Function newDevice = null;
                        foreach (var newFunction in listDevice.list)
                        {
                            //添加到缓存当中
                            newFunction.SaveFunctionFile();
                            Entity.FunctionList.List.IniFunctionList(newFunction.savePath, true);
                            if (newFunction.sid == this.newDeviceSid)
                            {
                                //这个设备是新追加的
                                newDevice = newFunction;
                            }
                        }
                        //调用回调函数
                        successEvent?.Invoke(newDevice);
                        return;
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(3000);
                    }
                }
            });
        }
        #endregion
@@ -289,21 +408,28 @@
        /// <param name="i_data">推送的内容</param>
        private void CloudReceiveEvent(CloudPushEnum pushEnum, string i_data)
        {
            if (pushEnum != CloudPushEnum.A新设备上报) { return; }
            //检测是否是新的红外宝
            if (this.CheckIsNewMiniRemoteControl(pushEnum, i_data) == false)
            {
                return;
            }
            //刷新红外宝信息
            this.RefreshMiniRemoteControlInfo((Entity.Function newDevice) =>
            {
                //移除这个事件
                HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
                //停止线程
                this.timeoutThreadActivity = false;
            //接收到就移除这个事件
            HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
            //停止线程
            this.timeoutThreadActivity = false;
            //添加设备成功之后,处理一些东西
            this.DoSomethingAfterAddDeviceSuccess();
                //添加设备成功之后,处理一些东西
                this.DoSomethingAfterAddDeviceSuccess(newDevice);
            });
        }
        /// <summary>
        /// 添加设备成功之后,处理一些东西
        /// </summary>
        private void DoSomethingAfterAddDeviceSuccess()
        private void DoSomethingAfterAddDeviceSuccess(Entity.Function newDevice)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
@@ -334,7 +460,7 @@
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //显示成功界面
                            this.ShowSuccessView();
                            this.ShowSuccessView(newDevice);
                        });
                    }
                }
@@ -367,8 +493,8 @@
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //300秒超时
                int timeout = 300;
                //360秒超时
                int timeout = 360;
                while (this.timeoutThreadActivity == false && this.Parent != null)
                {
                    System.Threading.Thread.Sleep(1000);
@@ -399,11 +525,7 @@
        public override void CloseFormBefore()
        {
            //摧毁蓝牙
#if __IOS__
#endif
#if __Android__
            HdlAndroidBluetoothLogic.Current.Dispone();
#endif
            HdlBluetoothLogic.Current.Dispone();
            HdlCloudReceiveLogic.Current.RemoveCloudReceiveEvent("AddMiniRemoteControlDirection4Page");
            base.CloseFormBefore();