From bba578c2f0acf2eca747edcb69426771e0cadd32 Mon Sep 17 00:00:00 2001
From: WJC <wjc@hdlchina.com.cn>
Date: 星期一, 23 三月 2020 18:20:50 +0800
Subject: [PATCH] 2020-03-23-3

---
 ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs |  113 ++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 89 insertions(+), 24 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs b/ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs
index a32c91a..4102a31 100755
--- a/ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs
+++ b/ZigbeeApp/Shared/Phone/UserCenter/Device/DeviceSearchForm.cs
@@ -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;
@@ -343,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(() =>
                     {
@@ -378,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());
@@ -397,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());
@@ -414,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());
@@ -431,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());
@@ -448,7 +465,6 @@
                     HdlLogLogic.Current.WriteLog(-1, resultData);
                     return 2;
                 }
-                this.topTimeOut = topMaxTime;
                 //璁剧疆杩涘害鍊�
                 this.SetDeviceProgressValue(5);
                 return 2;
@@ -521,6 +537,33 @@
 
         #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);
+                }
+            });
+        }
+
+        #endregion
+
         #region 鈻� 鐢婚潰鍏抽棴___________________________
 
         /// <summary>
@@ -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
     }
 }

--
Gitblit v1.8.0