From 2029e64ec0491f7e511dc15bffaf821bfbabe2de Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期一, 20 七月 2020 14:10:22 +0800
Subject: [PATCH] 上传一个很特别的新版本

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |  487 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 272 insertions(+), 215 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 002e5a4..91553e0 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -12,19 +12,25 @@
 {
     public static class Application
     {
+        #region 鈻� 鍙橀噺澹版槑___________________________
+
         public static bool isInited;
         /// <summary>
         /// 鏄惁姝e湪鎼滅储缃戝叧
         /// </summary>
         public static bool IsSearchingGateway = false;
         /// <summary>
-        /// 鏂板彂鐜扮殑缃戝叧閫氱煡
-        /// </summary>
-        public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
-        /// <summary>
         /// 涓婁竴娆$殑浣忓畢ID
         /// </summary>
         private static string oldHomeID = string.Empty;
+        /// <summary>
+        /// 鎺ユ敹鐨処P
+        /// </summary>
+        private static List<string> listReceiveIP = new List<string>();
+
+        #endregion
+
+        #region 鈻� 鍒濆鍖朹____________________________
 
         public static void Init()
         {
@@ -37,10 +43,28 @@
             //鍒濆鍖朣ocket
             FindGateWaySocket.Start();
 
-            new System.Threading.Thread(async () =>
+            //鍙戦�佸箍鎾暟鎹�
+            StartSendBroadcastData();
+
+            //鎺ユ敹骞挎挱鏁版嵁
+            StartReceiveBroadcastData();
+
+            ///寮�鍚繙绋嬭繛鎺�
+            StartRemoteMqtt();
+        }
+
+        #endregion
+
+        #region 鈻� 鍙戦�佸箍鎾暟鎹甠______________________
+
+        /// <summary>
+        /// 鍙戦�佸箍鎾暟鎹�
+        /// </summary>
+        private static void StartSendBroadcastData()
+        {
+            new System.Threading.Thread(() =>
             {
-                var gateWayList = new List<ZbGateway> { };
-                var searchCount = 6;
+                var searchCount = 3;
                 var broadBytes = new byte[44];// byteHomeId[0] ,//H
                 broadBytes[0] = 0xfe;
                 broadBytes[1] = 0x29;
@@ -54,56 +78,18 @@
                 {
                     try
                     {
-                        if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
-                        {
-                            //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
-                            oldHomeID = "?";
-                            System.Threading.Thread.Sleep(1000);
-                            continue;
-                        }
-                        //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
-                        if (Shared.Common.Config.Instance.HomeId != oldHomeID)
+                        //妫�娴嬭兘鍚﹀彂閫佸箍鎾�
+                        if (CheckCanSendBroadcast(broadBytes) == false)
                         {
-                            //鍥犱负閭d竴鐬棿锛屾湁鍙兘mqtt浼氬姞鍥炴潵,鎵�浠ュ厛鍔犵紦瀛�
-                            var list = new List<ZbGateway>();
-                            list.AddRange(ZbGateway.GateWayList);
-                            //鐒跺悗娓呯┖鎺�
-                            ZbGateway.GateWayList.Clear();
-                            //鏈�鍚庡啀鏂紑mqtt杩炴帴
-                            for (int i = 0; i < list.Count; i++)
-                            {
-                                list[i].DisConnectLocalMqttClient("1");
-                            }
-                            list.Clear();
-                            oldHomeID = Shared.Common.Config.Instance.HomeId;
-                            var tempBytes = System.Text.Encoding.UTF8.GetBytes(Shared.Common.Config.Instance.HomeId);
-                            System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
-                            //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷D鐩稿悓鐨勭綉鍏�
-                            var gateWayFileList = Global.FileListByHomeId().FindAll(obj => obj.StartsWith("Gateway_"));
-                            foreach (var filePath in gateWayFileList)
-                            {
-                                var paths = filePath.Split('_');
-                                if (paths.Length < 3)
-                                    continue;
-
-                                var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
-                                if (gateWay == null)
-                                {
-                                    gateWay = new ZbGateway { IsVirtual = true };
-                                    gateWay.GwId = paths[2];
-                                    gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
-                                    ZbGateway.GateWayList.Add(gateWay);
-                                }
-                            }
+                            continue;
                         }
 
                         var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
-
                         if (Shared.Application.IsWifi)
                         {
-                            if (0 < gateWayList.Count)
+                            if (0 < listReceiveIP.Count)
                             {
-                                searchCount = 6;
+                                searchCount = 3;
                                 ZbGateway.IsRemote = false;
                                 //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
                                 Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
@@ -128,177 +114,191 @@
                             {
                                 ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
                             }
+                            //闈瀢ifi鎯呭喌涓�,涓嶉渶瑕佸箍鎾�
+                            System.Threading.Thread.Sleep(1000);
+                            continue;
                         }
-
                         searchCount--;
 
-                        #region 1绉掓悳绱竴娆$綉鍏�
-                        //姣�0.5绉掑箍鎾彂鐜颁竴娆$綉鍏筹紝鍏�1s
+                        //姣忕骞挎挱鍙戠幇涓�娆$綉鍏�
                         int count = 2;
-                        new System.Threading.Thread(() =>
-                        {
-                            gateWayList.Clear();
-                            while (0 < count--)
-                            {
-                                try
-                                {
-                                    //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級
-                                    for (int i = 0; i < gateWayList.Count; i++)
-                                    {
-                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes);
-                                    }
-                                    //骞挎挱鍙戦��
-                                    if (broadcastIpAddress.ToString() != "0.0.0.0")
-                                    {
-                                        FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
-                                    }
-                                }
-                                catch{}
-                                System.Threading.Thread.Sleep(500);
-                            }
-                        })
-                        { IsBackground = true }.Start();
-
-                        while (0 < count)
+                        listReceiveIP.Clear();
+                        while (0 < count--)
                         {
-                            try
+                            //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級
+                            for (int i = 0; i < listReceiveIP.Count; i++)
                             {
-                                if (FindGateWaySocket.busSocket == null)
-                                {
-                                    System.Threading.Thread.Sleep(100);
-                                    continue;
-                                }
-                                var bytes = new byte[1024];
-
-                                var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
-                                if (bytes[43] == 0xA2)
-                                {
-                                    //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞�
-                                    var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}";
-                                    var homeID = System.Text.Encoding.UTF8.GetString(bytes, 7, 36);
-                                    homeID = homeID.Replace('\0', ' ').Trim();
-                                    var isMainGateWay = bytes[44] == 1;
-                                    var time = (bytes[48] & 0xff) << 24 | (bytes[47] & 0xff) << 16 | (bytes[46] & 0xff) << 8 | (bytes[45] & 0xff);
-                                    var gwNameLength = bytes[49];
-                                    var gwName = System.Text.Encoding.UTF8.GetString(bytes, 50, gwNameLength);
-                                    var gwIdLength = bytes[49 + gwNameLength + 1];
-                                    var id = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 2, gwIdLength);
-                                    var pubKeyLengthByte0 = bytes[49 + gwNameLength + 1 + gwIdLength + 1];
-                                    var pubKeyLengthByte1 = bytes[49 + gwNameLength + 1 + gwIdLength + 2];
-                                    int pubKeyLength = ((pubKeyLengthByte1 & 0xff) << 8 | (pubKeyLengthByte0 & 0xff));
-                                    var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
-                                    var zbGateWay = new ZbGateway
-                                    {
-                                        GwIP = ipAddress,
-                                        GwName = gwName,
-                                        HomeId = homeID,
-                                        IsMainGateWay = isMainGateWay,
-                                        GwId = id,
-                                        GatewayOnlineFlage = true,
-                                        PubKey = pubKey
-                                    };
-                                    //閫氳鍒扮綉鍏冲垪琛�
-                                    if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null)
-                                    {
-                                        //缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧
-                                        if (Shared.Common.Config.Instance.HomeId == homeID)
-                                        {
-                                            gateWayList.Add(zbGateWay);
-                                        }
-                                        //UI鐣岄潰姝e湪鎼滅储锛屼笉蹇呴厤褰撳墠浣忓畢鍒板埌缃戝叧姝ゆ椂涔熼�氳
-                                        else if (IsSearchingGateway)
-                                        {
-                                            gateWayList.Add(zbGateWay);
-                                        }
-                                        //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳 2020.01.14鍙樻洿锛氭病杩欎釜蹇呰,涓婇潰閭d釜鍙橀噺灏辫兘澶勭悊
-                                        //else if (homeID == string.Empty)
-                                        //{
-                                        //    gateWayList.Add(zbGateWay);
-                                        //}
-                                    }
-                                    //缃戝叧鍒楄〃瀛樺偍澶勭悊
-                                    var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
-                                    if (gateWay == null)
-                                    {
-                                        ZbGateway.GateWayList.Add(zbGateWay);
-                                        await zbGateWay.StartLocalMqtt(ipAddress);
-                                        NewGateWayAction?.Invoke(zbGateWay);
-                                    }
-                                    else
-                                    {
-                                        gateWay.IsVirtual = false;
-                                        //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
-                                        gateWay.GatewayOnlineFlage = true;
-                                        if (gateWay.GwIP != ipAddress)
-                                        {
-                                            await gateWay.DisConnectLocalMqttClient("2");
-                                            ZbGateway.GateWayList.Remove(gateWay);
-                                            gateWay = zbGateWay;
-                                            ZbGateway.GateWayList.Add(gateWay);
-                                            await zbGateWay.StartLocalMqtt(ipAddress);
-                                        }
-                                        else
-                                        {
-                                            gateWay.PubKey = pubKey;
-                                            gateWay.GwName = gwName;
-                                            gateWay.HomeId = homeID;
-                                            await gateWay.StartLocalMqtt(ipAddress);
-                                        }
-
-                                        //涓荤綉鍏宠缃�
-                                        if (isMainGateWay && oldHomeID == gateWay.HomeId)
-                                        {
-                                            for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
-                                            {
-                                                var gw = ZbGateway.GateWayList[i];
-                                                //缃戝叧ID涓嶆槸褰撳墠鐨勭綉鍏筹紝鍒欐妸缃戝叧鍒楄〃涓叾浠栫綉鍏虫爣璁颁负涓嶆槸涓荤綉鍏�
-                                                if (gw.GwId != id && oldHomeID == gw.HomeId)
-                                                {
-                                                    gw.IsMainGateWay = false;
-                                                }
-                                            }
-                                            //鏍囪褰撳墠缃戝叧鏄富缃戝叧
-                                            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;
-                                            }
-                                        }
-                                    }
-                                }
-                                else
-                                {
-                                    //璋冭瘯鐢�
-                                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
-                                    {
-                                        var data33 = Encoding.UTF8.GetString(bytes).Replace("锟�)", string.Empty).TrimStart('\0').TrimEnd('\0');
-                                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
-                                    }
-                                }
+                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(listReceiveIP[i]), 7624), broadBytes);
                             }
-                            catch{}
+                            //骞挎挱鍙戦��
+                            if (broadcastIpAddress.ToString() != "0.0.0.0")
+                            {
+                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
+                            }
+                            System.Threading.Thread.Sleep(1000);
                         }
-                        #endregion
                     }
                     catch { }
-                    System.Threading.Thread.Sleep(500);
                 }
             })
             { IsBackground = true }.Start();
+        }
 
-            ///杩滅▼涓荤綉鍏虫洿鏂�
+        #endregion
+
+        #region 鈻� 鎺ユ敹骞挎挱鏁版嵁_______________________
+
+        /// <summary>
+        /// 鎺ユ敹骞挎挱鏁版嵁
+        /// </summary>
+        private static void StartReceiveBroadcastData()
+        {
+            //鏁版嵁鎺ユ敹
+            new System.Threading.Thread(async () =>
+            {
+                while (true)
+                {
+                    try
+                    {
+                        if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0)
+                        {
+                            System.Threading.Thread.Sleep(500);
+                            continue;
+                        }
+                        var bytes = new byte[1024];
+                        var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
+                        if (bytes[43] == 0xA2)
+                        {
+                            //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞�
+                            var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}";
+                            var homeID = System.Text.Encoding.UTF8.GetString(bytes, 7, 36);
+                            homeID = homeID.Replace('\0', ' ').Trim();
+                            var isMainGateWay = bytes[44] == 1;
+                            var time = (bytes[48] & 0xff) << 24 | (bytes[47] & 0xff) << 16 | (bytes[46] & 0xff) << 8 | (bytes[45] & 0xff);
+                            var gwNameLength = bytes[49];
+                            var gwName = System.Text.Encoding.UTF8.GetString(bytes, 50, gwNameLength);
+                            var gwIdLength = bytes[49 + gwNameLength + 1];
+                            var id = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 2, gwIdLength);
+                            var pubKeyLengthByte0 = bytes[49 + gwNameLength + 1 + gwIdLength + 1];
+                            var pubKeyLengthByte1 = bytes[49 + gwNameLength + 1 + gwIdLength + 2];
+                            int pubKeyLength = ((pubKeyLengthByte1 & 0xff) << 8 | (pubKeyLengthByte0 & 0xff));
+                            var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
+                            var zbGateWay = new ZbGateway
+                            {
+                                GwIP = ipAddress,
+                                GwName = gwName,
+                                HomeId = homeID,
+                                IsMainGateWay = isMainGateWay,
+                                GwId = id,
+                                GatewayOnlineFlage = true,
+                                PubKey = pubKey
+                            };
+                            //閫氳鍒扮綉鍏冲垪琛�
+                            if (listReceiveIP.Find(obj => obj == zbGateWay.GwId) == null)
+                            {
+                                //缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧
+                                if (Shared.Common.Config.Instance.HomeId == homeID)
+                                {
+                                    listReceiveIP.Add(ipAddress);
+                                }
+                                //UI鐣岄潰姝e湪鎼滅储锛屼笉蹇呴厤褰撳墠浣忓畢鍒板埌缃戝叧姝ゆ椂涔熼�氳
+                                else if (IsSearchingGateway)
+                                {
+                                    listReceiveIP.Add(ipAddress);
+                                }
+                            }
+                            //缃戝叧鍒楄〃瀛樺偍澶勭悊
+                            var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
+                            if (gateWay == null)
+                            {
+                                ZbGateway.GateWayList.Add(zbGateWay);
+                                await zbGateWay.StartLocalMqtt(ipAddress);
+                            }
+                            else
+                            {
+                                gateWay.IsVirtual = false;
+                                //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
+                                gateWay.GatewayOnlineFlage = true;
+                                if (gateWay.GwIP != ipAddress)
+                                {
+                                    await gateWay.DisConnectLocalMqttClient("2");
+                                    ZbGateway.GateWayList.Remove(gateWay);
+                                    gateWay = zbGateWay;
+                                    ZbGateway.GateWayList.Add(gateWay);
+                                    await zbGateWay.StartLocalMqtt(ipAddress);
+                                }
+                                else
+                                {
+                                    gateWay.PubKey = pubKey;
+                                    gateWay.GwName = gwName;
+                                    gateWay.HomeId = homeID;
+                                    await gateWay.StartLocalMqtt(ipAddress);
+                                }
+
+                                //涓荤綉鍏宠缃�
+                                if (isMainGateWay && oldHomeID == gateWay.HomeId)
+                                {
+                                    for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
+                                    {
+                                        var gw = ZbGateway.GateWayList[i];
+                                        //缃戝叧ID涓嶆槸褰撳墠鐨勭綉鍏筹紝鍒欐妸缃戝叧鍒楄〃涓叾浠栫綉鍏虫爣璁颁负涓嶆槸涓荤綉鍏�
+                                        if (gw.GwId != id && oldHomeID == gw.HomeId)
+                                        {
+                                            gw.IsMainGateWay = false;
+                                        }
+                                    }
+                                    //鏍囪褰撳墠缃戝叧鏄富缃戝叧
+                                    gateWay.IsMainGateWay = true;
+                                }
+                            }
+
+                            //娴嬭瘯鑳藉惁骞挎挱寰楀埌缃戝叧,閫氬父鎯呭喌涓嬩笉妫�娴�
+                            if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
+                            {
+                                if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest == null)
+                                {
+                                    //闃叉寮傚父,铏界劧鍑犵巼寰堜綆
+                                    Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway = 0;
+                                    continue;
+                                }
+                                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;
+                                    }
+                                }
+                            }
+                        }
+                        else
+                        {
+                            //璋冭瘯鐢�
+                            if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+                            {
+                                var data33 = Encoding.UTF8.GetString(bytes).Replace("锟�)", string.Empty).TrimStart('\0').TrimEnd('\0');
+                                Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
+                            }
+                        }
+                    }
+                    catch { }
+                }
+            })
+            { IsBackground = true }.Start();
+        }
+
+        #endregion
+
+        #region 鈻� 寮�鍚繙绋嬭繛鎺______________________
+
+        /// <summary>
+        /// 寮�鍚繙绋嬭繛鎺�
+        /// </summary>
+        private static void StartRemoteMqtt()
+        {
             new System.Threading.Thread(async () =>
             {
                 while (true)
@@ -315,6 +315,10 @@
             { IsBackground = true }.Start();
         }
 
+        #endregion
+
+        #region 鈻� 涓�鑸柟娉昣__________________________
+
         /// <summary>
         /// 閲嶆柊鎼滅储,娉ㄦ剰锛氳皟鐢ㄨ鏂规硶,鍒欎績浣垮叏閮ㄧ綉鍏崇殑mqtt鍏ㄩ儴鏂紑,鐒跺悗閲嶆柊鎼滅储(鍙兘浼氭湁1绉掑欢杩�)
         /// </summary>
@@ -323,6 +327,61 @@
             //涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊�
             oldHomeID = "**";
         }
+
+        /// <summary>
+        /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾�
+        /// </summary>
+        /// <returns></returns>
+        private static bool CheckCanSendBroadcast(byte[] broadBytes)
+        {
+            if (string.IsNullOrEmpty(Config.Instance.HomeId))
+            {
+                //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+                oldHomeID = "?";
+                System.Threading.Thread.Sleep(1000);
+                return false;
+            }
+            //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
+            if (Config.Instance.HomeId != oldHomeID)
+            {
+                //鍥犱负閭d竴鐬棿锛屾湁鍙兘mqtt浼氬姞鍥炴潵,鎵�浠ュ厛鍔犵紦瀛�
+                var list = new List<ZbGateway>();
+                list.AddRange(ZbGateway.GateWayList);
+                //鐒跺悗娓呯┖鎺�
+                ZbGateway.GateWayList.Clear();
+                //鏈�鍚庡啀鏂紑mqtt杩炴帴
+                for (int i = 0; i < list.Count; i++)
+                {
+                    list[i].DisConnectLocalMqttClient("1");
+                }
+                list.Clear();
+                oldHomeID = Shared.Common.Config.Instance.HomeId;
+                var tempBytes = System.Text.Encoding.UTF8.GetBytes(Shared.Common.Config.Instance.HomeId);
+                System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
+                //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷D鐩稿悓鐨勭綉鍏�
+                var gateWayFileList = Global.FileListByHomeId().FindAll(obj => obj.StartsWith("Gateway_"));
+                foreach (var filePath in gateWayFileList)
+                {
+                    var paths = filePath.Split('_');
+                    if (paths.Length < 3)
+                        continue;
+
+                    var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
+                    if (gateWay == null)
+                    {
+                        gateWay = new ZbGateway { IsVirtual = true };
+                        gateWay.GwId = paths[2];
+                        gateWay.HomeId = Config.Instance.HomeId;
+                        ZbGateway.GateWayList.Add(gateWay);
+                    }
+                }
+            }
+            return true;
+        }
+
+        #endregion
+
+        #region 鈻� Socket_____________________________
 
         /// <summary>
         /// 鎺ユ敹澶勭悊UDP鏁版嵁鍖�
@@ -356,13 +415,9 @@
             /// </summary>
             public static void Stop()
             {
-                if (!IsRunning)
-                {
-                    return;
-                }
                 try
                 {
-                    busSocket.Close();
+                    busSocket?.Close();
                 }
                 catch { }
                 busSocket = null;
@@ -392,8 +447,8 @@
                     if (IsRunning)
                     {
                         busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
-                    }
-                    
+                    }
+
                 }
                 catch (Exception ex)
                 {
@@ -415,7 +470,7 @@
                 {
                     int bytesSent = busSocket.EndSendTo(iar);
                 }
-                catch(Exception ex) 
+                catch (Exception ex)
                 {
                     //璋冭瘯鐢�
                     if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
@@ -425,5 +480,7 @@
                 }
             }
         }
+
+        #endregion
     }
 }

--
Gitblit v1.8.0