xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs
@@ -21,9 +21,9 @@
        /// </summary>
        private bool isDeviceThreadStart = false;
        /// <summary>
        /// 等待设备的回馈的超时时间(单位:秒)
        /// 等待设备的回馈的超时时间(单位:百毫秒)
        /// </summary>
        private int waitDeviceTimeOut = 3;
        private int waitDeviceTimeOut = 20;
        /// <summary>
        /// 主题超时的线程是否开启
        /// </summary>
@@ -60,6 +60,10 @@
        /// 进度值的显示文本
        /// </summary>
        private NormalViewControl btnProgressView = null;
        /// <summary>
        /// 网关是否允许入网的标识
        /// </summary>
        private bool gatewayCanAddDevice = false;
        #endregion
@@ -138,20 +142,8 @@
            btnProgressView.Text = "0%";
            frameProgress.AddChidren(btnProgressView);
            HdlThreadLogic.Current.RunThread(() =>
            {
                //蓝才刚说有时候网关会收不到入网的命令,所以发三次
                for (int i = 0; i < 3; i++)
                {
                    if (this.Parent == null)
                    {
                        return;
                    }
                    //让网关允许入网
                    this.realGateway.AddNewDeviceToGateway(180);
                    System.Threading.Thread.Sleep(1000);
                }
            });
            //允许设备入网
            this.StartDeviceCanAddToGateway(false);
            //添加监视设备新上报的事件
            this.realGateway.GwResDataAction += this.AdjustGatewayResultData;
@@ -190,8 +182,6 @@
                this.StartTopicTimeOutThread();
                return;
            }
            //中断主题超时线程
            this.topTimeOut = -100;
            lock (this.dicNewDevice)
            {
@@ -226,19 +216,16 @@
                }
                //刷新超时时间
                this.waitDeviceTimeOut = 3;
                this.waitDeviceTimeOut = 20;
                //获取设备的固定属性
                HdlDeviceFixedAttributeLogic.Current.SetAllFixedAttributeToDevice(device);
                if ((device is OTADevice) == false)
                {
                    //不需要200端点的那个设备
                    this.dicNewDevice[device.DeviceAddr].Add(device);
                //不需要200端点的那个设备  2020.01.13 变更:ota也加进来
                this.dicNewDevice[device.DeviceAddr].Add(device);
                    //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
                    this.StartShowDeviceAddSuccessFormThread();
                }
                //有新设备,开启显示设备信息界面的线程(里面会等待三秒这样)
                this.StartShowDeviceAddSuccessFormThread();
            }
        }
@@ -263,12 +250,12 @@
                while (this.waitDeviceTimeOut >= 0)
                {
                    //等待下一个回路
                    System.Threading.Thread.Sleep(1000);
                    System.Threading.Thread.Sleep(100);
                    this.waitDeviceTimeOut--;
                }
                //停止接收
                this.realGateway.GwResDataAction -= this.AdjustGatewayResultData;
                System.Threading.Thread.Sleep(500);
                System.Threading.Thread.Sleep(200);
                //目前就弄一个
                Application.RunOnMainThread(() =>
@@ -278,6 +265,10 @@
                        //重新变更UI
                        foreach (var device in listDevice)
                        {
                            if (device is OTADevice)
                            {
                                continue;
                            }
                            device.IconPath = string.Empty;
                            device.ReSave();
                        }
@@ -344,12 +335,18 @@
                while (this.Parent != null)
                {
                    System.Threading.Thread.Sleep(500);
                    timeCount++;
                    if (timeCount >= 240)
                    //网关允许设备入网后才开始计时
                    if (this.gatewayCanAddDevice == true)
                    {
                        //120秒后,再次发送网关设备入网命令
                        this.realGateway.AddNewDeviceToGateway(180);
                        timeCount = 0;
                        timeCount++;
                        if (timeCount >= 360)
                        {
                            this.gatewayCanAddDevice = false;
                            //再次发送允许设备入网
                            this.StartDeviceCanAddToGateway(true);
                            timeCount = 0;
                        }
                    }
                    Application.RunOnMainThread(() =>
                    {
@@ -379,15 +376,34 @@
        /// <returns></returns>
        private int CheckIsDeviceComming(string topic, string resultData)
        {
            if (topic == gatewayId + "/Device/DeviceAnnounce_Respon")
            if (topic == gatewayId + "/Device/SearchNewDevice")
            {
                //网关回复设备已经可以入网
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchNewDeviceResult>(jobject["Data"].ToString());
                if (info.time > 0)
                {
                    this.gatewayCanAddDevice = true;
                }
                return 0;
            }
            else if (topic == gatewayId + "/Device/DeviceAnnounce_Respon")
            {
                this.topTimeOut = topMaxTime;
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceAnnounceInfo>(jobject["Data"].ToString());
                if (info.IsNew == 6 || info.IsNew == 7)
                {
                    return 0;
                }
                //网关告知客户端有设备声明
                this.SetDeviceProgressValue(1);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetActiveEP_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备活动端点信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -398,13 +414,13 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(2);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetActiveEPSimpleDesc_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备所有活动端点简单描述符信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -415,13 +431,13 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(3);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceGetDefaultBind_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端获取设备默认绑定表信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -432,13 +448,13 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(4);
                return 2;
            }
            else if (topic == gatewayId + "/Device/DeviceAutoBindZBCoord_Respon")
            {
                this.topTimeOut = topMaxTime;
                //网关告知客户端设备自动绑定协调器信息
                var jobject = Newtonsoft.Json.Linq.JObject.Parse(resultData);
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceResultInfo>(jobject["Data"].ToString());
@@ -449,13 +465,13 @@
                    HdlLogLogic.Current.WriteLog(-1, resultData);
                    return 2;
                }
                this.topTimeOut = topMaxTime;
                //设置进度值
                this.SetDeviceProgressValue(5);
                return 2;
            }
            else if (topic == gatewayId + "/DeviceInComingRespon")
            {
                this.topTimeOut = topMaxTime;
                //网关最终上报节点设备信息
                this.SetDeviceProgressValue(6);
                return 1;
@@ -506,7 +522,7 @@
                    System.Threading.Thread.Sleep(1000);
                    this.topTimeOut--;
                }
                if (this.topTimeOut < 0 && this.topTimeOut >= -10)
                if (this.topTimeOut < 0)
                {
                    //响应超时,请重新入网
                    this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uResponseTimeoutsAndReAccessNetwork));
@@ -515,6 +531,33 @@
                        //关闭界面
                        this.CloseForm();
                    });
                }
            });
        }
        #endregion
        #region ■ 允许设备入网_______________________
        /// <summary>
        /// 允许设备入网
        /// </summary>
        /// <param name="close"></param>
        private void StartDeviceCanAddToGateway(bool close)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                if (close == true)
                {
                    //关闭入网模式
                    this.realGateway.AddNewDeviceToGateway(0);
                    System.Threading.Thread.Sleep(2000);
                }
                while (gatewayCanAddDevice == false && this.Parent != null)
                {
                    //让网关允许入网
                    this.realGateway.AddNewDeviceToGateway(180);
                    System.Threading.Thread.Sleep(6000);
                }
            });
        }
@@ -555,6 +598,28 @@
            public int Result = -1;
        }
        /// <summary>
        /// 网关告知客户端有设备声明
        /// </summary>
        private class DeviceAnnounceInfo
        {
            /// <summary>
            /// 蓝才刚说 6和7 这两个是设备重启上报的,目前暂时不能显示到入网步骤那里的
            /// </summary>
            public int IsNew = -1;
        }
        /// <summary>
        /// 网关回复允许设备入网
        /// </summary>
        private class SearchNewDeviceResult
        {
            /// <summary>
            /// 允许设备入网的时间
            /// </summary>
            public int time = 0;
        }
        #endregion
    }
}