From 48ba446936b51fffafa7c3600c0dadc6ac0e8c20 Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期五, 10 七月 2020 10:52:13 +0800
Subject: [PATCH] 2020-07-10-01

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |  163 +++++++++++++++++++++++++++---------------------------
 1 files changed, 82 insertions(+), 81 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index f4c69b6..f2f87b9 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -13,7 +13,6 @@
     public static class Application
     {
         public static bool isInited;
-        static bool isEncry;
         /// <summary>
         /// 鏄惁姝e湪鎼滅储缃戝叧
         /// </summary>
@@ -22,6 +21,10 @@
         /// 鏂板彂鐜扮殑缃戝叧閫氱煡
         /// </summary>
         public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
+        /// <summary>
+        /// 涓婁竴娆$殑浣忓畢ID
+        /// </summary>
+        private static string oldHomeID = string.Empty;
 
         public static void Init()
         {
@@ -35,7 +38,6 @@
             {
                 var gateWayList = new List<ZbGateway> { };
                 var searchCount = 6;
-                var oldHomeID = string.Empty;
                 var broadBytes = new byte[44];// byteHomeId[0] ,//H
                 broadBytes[0] = 0xfe;
                 broadBytes[1] = 0x29;
@@ -50,7 +52,9 @@
                     try
                     {
                         if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
-                        {
+                        {
+                            //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+                            oldHomeID = "?";
                             System.Threading.Thread.Sleep(1000);
                             continue;
                         }
@@ -65,7 +69,7 @@
                             //鏈�鍚庡啀鏂紑mqtt杩炴帴
                             for (int i = 0; i < list.Count; i++)
                             {
-                                list[i].DisConnect("1");
+                                list[i].DisConnectLocalMqttClient("1");
                             }
                             list.Clear();
                             oldHomeID = Shared.Common.Config.Instance.HomeId;
@@ -79,12 +83,12 @@
                                 if (paths.Length < 3)
                                     continue;
 
-                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.getGatewayBaseInfo != null) && (obj.getGatewayBaseInfo.gwID == paths[2]));
+                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
                                 if (gateWay == null)
                                 {
                                     gateWay = new ZbGateway { IsVirtual = true };
-                                    gateWay.getGatewayBaseInfo.gwID = paths[2];
-                                    gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
+                                    gateWay.GwId = paths[2];
+                                    gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
                                     ZbGateway.GateWayList.Add(gateWay);
                                 }
                             }
@@ -99,18 +103,29 @@
                             {
                                 searchCount = 6;
                                 ZbGateway.IsRemote = false;
+                                //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
+                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                             }
                             else
                             {
                                 if (searchCount < 0)
                                 {
                                     ZbGateway.IsRemote = true;
+                                    //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
+                                    Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                                 }
                             }
                         }
                         else
                         {
                             ZbGateway.IsRemote = true;
+                            //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
+                            Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
+
+                            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
+                            {
+                                ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
+                            }
                         }
 
                         searchCount--;
@@ -128,7 +143,7 @@
                                     //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級
                                     for (int i = 0; i < gateWayList.Count; i++)
                                     {
-                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes);
+                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes);
                                     }
                                     //骞挎挱鍙戦��
                                     if (broadcastIpAddress.ToString() != "0.0.0.0")
@@ -136,7 +151,11 @@
                                         FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
                                     }
                                 }
-                                catch { }
+                                catch
+                                {   
+                                    //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+                                    FindGateWaySocket.Stop();
+                                }
                                 System.Threading.Thread.Sleep(500);
                             }
                         })
@@ -173,20 +192,16 @@
                                     var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
                                     var zbGateWay = new ZbGateway
                                     {
-                                        getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
-                                        {
-                                            IpAddress = ipAddress,
-                                            Time = time,
-                                            GwName = gwName,
-                                            HomeId = homeID,
-                                            IsMainGateWay = isMainGateWay,
-                                            gwID = id,
-                                        },
+                                        GwIP = ipAddress,
+                                        GwName = gwName,
+                                        HomeId = homeID,
+                                        IsMainGateWay = isMainGateWay,
+                                        GwId = id,
                                         GatewayOnlineFlage = true,
                                         PubKey = pubKey
                                     };
                                     //閫氳鍒扮綉鍏冲垪琛�
-                                    if (gateWayList.Find(obj => obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID) == null)
+                                    if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null)
                                     {
                                         //缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧
                                         if (Shared.Common.Config.Instance.HomeId == homeID)
@@ -198,18 +213,18 @@
                                         {
                                             gateWayList.Add(zbGateWay);
                                         }
-                                        //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳
-                                        else if (homeID == string.Empty)
-                                        {
-                                            gateWayList.Add(zbGateWay);
-                                        }
+                                        //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳 2020.01.14鍙樻洿锛氭病杩欎釜蹇呰,涓婇潰閭d釜鍙橀噺灏辫兘澶勭悊
+                                        //else if (homeID == string.Empty)
+                                        //{
+                                        //    gateWayList.Add(zbGateWay);
+                                        //}
                                     }
                                     //缃戝叧鍒楄〃瀛樺偍澶勭悊
-                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
+                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                                     if (gateWay == null)
                                     {
-                                        await zbGateWay.StartLocalMqtt(ipAddress);
                                         ZbGateway.GateWayList.Add(zbGateWay);
+                                        await zbGateWay.StartLocalMqtt(ipAddress);
                                         NewGateWayAction?.Invoke(zbGateWay);
                                     }
                                     else
@@ -217,42 +232,61 @@
                                         gateWay.IsVirtual = false;
                                         //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
                                         gateWay.GatewayOnlineFlage = true;
-                                        if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress)
+                                        if (gateWay.GwIP != ipAddress)
                                         {
-                                            gateWay.DisConnect("2");
+                                            await gateWay.DisConnectLocalMqttClient("2");
                                             ZbGateway.GateWayList.Remove(gateWay);
                                             gateWay = zbGateWay;
-                                            await zbGateWay.StartLocalMqtt(ipAddress);
                                             ZbGateway.GateWayList.Add(gateWay);
+                                            await zbGateWay.StartLocalMqtt(ipAddress);
                                         }
                                         else
                                         {
                                             gateWay.PubKey = pubKey;
-                                            gateWay.getGatewayBaseInfo.Time = time;
-                                            gateWay.getGatewayBaseInfo.GwName = gwName;
-                                            gateWay.getGatewayBaseInfo.HomeId = homeID;
+                                            gateWay.GwName = gwName;
+                                            gateWay.HomeId = homeID;
                                             await gateWay.StartLocalMqtt(ipAddress);
                                         }
 
                                         //涓荤綉鍏宠缃�
-                                        if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId)
+                                        if (isMainGateWay && oldHomeID == gateWay.HomeId)
                                         {
                                             for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
                                             {
                                                 var gw = ZbGateway.GateWayList[i];
                                                 //缃戝叧ID涓嶆槸褰撳墠鐨勭綉鍏筹紝鍒欐妸缃戝叧鍒楄〃涓叾浠栫綉鍏虫爣璁颁负涓嶆槸涓荤綉鍏�
-                                                if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId)
+                                                if (gw.GwId != id && oldHomeID == gw.HomeId)
                                                 {
-                                                    gw.getGatewayBaseInfo.IsMainGateWay = false;
+                                                    gw.IsMainGateWay = false;
                                                 }
                                             }
                                             //鏍囪褰撳墠缃戝叧鏄富缃戝叧
-                                            gateWay.getGatewayBaseInfo.IsMainGateWay = true;
+                                            gateWay.IsMainGateWay = true;
                                         }
+                                    }
+
+                                    //娴嬭瘯鑳藉惁骞挎挱寰楀埌缃戝叧,閫氬父鎯呭喌涓嬩笉妫�娴�
+                                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
+                                    {
+                                        if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
+                                        {
+                                            if (gateWay == null)
+                                            {
+                                                Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = zbGateWay;
+                                            }
+                                            else
+                                            {
+                                                Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = gateWay;
+                                            }
+                                        }
                                     }
                                 }
                             }
-                            catch { }
+                            catch
+                            {
+                                //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+                                FindGateWaySocket.Stop();
+                            }
                         }
                         #endregion
                     }
@@ -270,48 +304,7 @@
                     try
                     {
                         //瀹氭椂妫�娴嬭繙绋嬭繛鎺ユ儏鍐�
-                        await ZbGateway.StartCloudMqtt(); 
-                        if (!ZbGateway.IsRemote)
-                        {
-                            System.Threading.Thread.Sleep(500);
-                            continue;
-                        }
-                        var gateWayList =ZbGateway.GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
-
-                        if (gateWayList.Count == 1)
-                        {
-                            gateWayList[0].getGatewayBaseInfo.IsMainGateWay = true;
-                        }
-                        else
-                        {
-                            if (gateWayList.Find((obj) => obj.getGatewayBaseInfo.IsMainGateWay) == null)
-                            {
-                                for (int i = 0; i < gateWayList.Count; i++)
-                                {
-                                    var gateWay = gateWayList[i];
-                                    var info = await gateWay.GetZbGwInfoAsync();
-                                    if (info == null || info.getGwData == null)
-                                    {
-                                        continue;
-                                    }
-                                    if (info.getGwData.IsDominant == 1)
-                                    {
-                                        for (int j = 0; j < gateWayList.Count; j++)
-                                        {
-                                            if (gateWayList[i].getGatewayBaseInfo.gwID == info.getGwData.GwId)
-                                            {
-                                                gateWayList[i].getGatewayBaseInfo.IsMainGateWay = true;
-                                            }
-                                            else
-                                            {
-                                                gateWayList[i].getGatewayBaseInfo.IsMainGateWay = false;
-                                            }
-                                        }
-                                        break;
-                                    }
-                                }
-                            }
-                        }
+                        await ZbGateway.StartRemoteMqtt();
                     }
                     catch { }
                     System.Threading.Thread.Sleep(2000);
@@ -320,6 +313,14 @@
             { IsBackground = true }.Start();
         }
 
+        /// <summary>
+        /// 閲嶆柊鎼滅储,娉ㄦ剰锛氳皟鐢ㄨ鏂规硶,鍒欎績浣垮叏閮ㄧ綉鍏崇殑mqtt鍏ㄩ儴鏂紑,鐒跺悗閲嶆柊鎼滅储(鍙兘浼氭湁1绉掑欢杩�)
+        /// </summary>
+        public static void ReSearch()
+        {
+            //涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊�
+            oldHomeID = "**";
+        }
 
         /// <summary>
         /// 鎺ユ敹澶勭悊UDP鏁版嵁鍖�
@@ -328,7 +329,7 @@
         public static class FindGateWaySocket
         {
             //鏈湴Socket
-            public static System.Net.Sockets.Socket busSocket;
+            public static Socket busSocket;
 
             /// <summary>
             /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
@@ -374,7 +375,7 @@
             {
                 get
                 {
-                    return null == busSocket ? false : true;
+                    return busSocket == null ? false : true;
                 }
             }
 

--
Gitblit v1.8.0