From 9ef48d7b2da7c408b53f73be0f6eef3cbac1c84a Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期四, 19 十一月 2020 10:23:45 +0800
Subject: [PATCH] Evoyo.Home1.1.0120111601_Release

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |  190 ++++++++++++++++++++++++++---------------------
 1 files changed, 104 insertions(+), 86 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index beab8a5..b9fc01d 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -19,6 +19,10 @@
         /// 鏄惁姝e湪鎼滅储缃戝叧
         /// </summary>
         public static bool IsSearchingGateway = false;
+        /// <summary>
+        /// 鍙戦�佺殑鏃堕棿闂撮殧(鍗曚綅:绉�)
+        /// </summary>
+        public static int SendTime = 3;
         /// <summary>
         /// 涓婁竴娆$殑浣忓畢ID
         /// </summary>
@@ -64,36 +68,45 @@
         {
             new System.Threading.Thread(() =>
             {
-                var broadBytes = new byte[44];// byteHomeId[0] ,//H
-                broadBytes[0] = 0xfe;
-                broadBytes[1] = 0x29;
-                broadBytes[2] = 0x00;
-                broadBytes[3] = 0x00;
-                broadBytes[4] = 0x00;
-                broadBytes[5] = 0x00;
-                broadBytes[6] = 0x00;
-                broadBytes[43] = 0x02;
+                var broadBytes = new byte[44];
+
                 while (true)
                 {
                     try
-                    {
-                        //妫�娴嬭兘鍚﹀彂閫佸箍鎾�
-                        if (CheckCanSendBroadcast(broadBytes) == false)
-                        {
-                            continue;
+                    {
+                        //妫�娴嬭兘鍚﹀彂閫佸箍鎾� 0:涓嶈兘鍙戦�佸箍鎾� 1:鍙互鍙戦�佸箍鎾� 2:浣忓畢ID鍙樻洿
+                        int result = CheckCanSendBroadcast();
+                        if (result == 0)
+                        {
+                            continue;
+                        }
+                        if (result == 2)
+                        {
+                            //閲嶆柊鍒濆鍖栧箍鎾暟鎹�
+                            broadBytes = new byte[44];
+                            broadBytes[0] = 0xfe;
+                            broadBytes[1] = 0x29;
+                            broadBytes[2] = 0x00;
+                            broadBytes[3] = 0x00;
+                            broadBytes[4] = 0x00;
+                            broadBytes[5] = 0x00;
+                            broadBytes[6] = 0x00;
+                            broadBytes[43] = 0x02;
+                            var tempBytes = System.Text.Encoding.UTF8.GetBytes(oldHomeID);
+                            System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
                         }
 
                         var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
-                        
+                        
                         //娓呯┖鎺ユ敹鍒扮殑IP
-                        listReceiveIP.Clear();
-                        if (broadcastIpAddress.ToString() != "0.0.0.0")
+                        listReceiveIP.Clear();
+                        if (broadcastIpAddress.ToString() != "0.0.0.0")
                         {
-                            //骞挎挱鍑哄幓
-                            FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
-                        }
-                        //绛夊緟3绉�,涓嬮潰鐨勬帴鏀舵柟娉曚細鍘绘帴鏀跺箍鎾�
-                        System.Threading.Thread.Sleep(3000);
+                            //骞挎挱鍑哄幓
+                            FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
+                        }
+                        //绛夊緟3绉�,涓嬮潰鐨勬帴鏀舵柟娉曚細鍘绘帴鏀跺箍鎾�
+                        System.Threading.Thread.Sleep(SendTime * 1000);
 
                         if (listReceiveIP.Count > 0)
                         {
@@ -104,7 +117,7 @@
                         }
                         else
                         {
-                            //灞�鍩熺綉骞挎挱涓嶅埌缃戝叧
+                            //灞�鍩熺綉骞挎挱涓嶅埌缃戝叧
                             ZbGateway.IsRemote = true;
                             //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
                             Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
@@ -137,7 +150,7 @@
                             System.Threading.Thread.Sleep(500);
                             continue;
                         }
-                        var ipEndPoint = new System.Net.IPEndPoint(0, 0);
+                        var ipEndPoint = new System.Net.IPEndPoint(IPAddress.Any, 0);
                         var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
                         if (bytes[43] == 0xA2)
                         {
@@ -187,15 +200,18 @@
                             var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                             if (gateWay == null)
                             {
-                                zbGateWay.OnlineTime = DateTime.Now;
+                                //鍒锋柊缃戝叧鐨勫湪绾挎椂闂寸偣
+                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(zbGateWay.GwId);
+
                                 ZbGateway.GateWayList.Add(zbGateWay);
                                 await zbGateWay.StartLocalMqtt(ipAddress);
                             }
                             else
-                            {
+                            {
+                                //鍒锋柊缃戝叧鐨勫湪绾挎椂闂寸偣
+                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(gateWay.GwId);
+
                                 gateWay.IsVirtual = false;
-                                //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
-                                gateWay.OnlineTime = DateTime.Now;
                                 if (gateWay.GwIP != ipAddress)
                                 {
                                     await gateWay.DisConnectLocalMqttClient("2");
@@ -260,6 +276,7 @@
                                 Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
                             }
                         }
+                        await System.Threading.Tasks.Task.Delay(20);
                     }
                     catch (Exception ex)
                     {
@@ -310,59 +327,60 @@
         {
             //涓轰簡閭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;
+        }
+
+        /// <summary>
+        /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾�(0:涓嶈兘鍙戦�佸箍鎾� 1:鍙互鍙戦�佸箍鎾� 2:浣忓畢ID鍙樻洿)
+        /// </summary>
+        /// <returns></returns>
+        private static int CheckCanSendBroadcast()
+        {
+            if (string.IsNullOrEmpty(Config.Instance.HomeId))
+            {
+                //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+                oldHomeID = "?";
+                System.Threading.Thread.Sleep(1000);
+                return 0;
+            }
+            //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
+            if (Config.Instance.HomeId == oldHomeID)
+            {
+                return 1;
+            }
+
+            //鍥犱负閭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();
+
+            //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷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);
+                }
+            }
+            //浣忓畢ID鍙樻洿
+            oldHomeID = Config.Instance.HomeId;
+
+            return 2;
         }
 
         #endregion
@@ -419,11 +437,11 @@
                         return;
                     }
                     int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
-                    //璋冭瘯鐢�
-                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
-                    {
-                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "鍙戦�侀暱搴�(BeginSendTo)锛�" + value + "  鍙戦�佸唴瀹�:" + System.Text.Encoding.UTF8.GetString(bytes));
-                    }
+                    //璋冭瘯鐢�
+                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+                    {
+                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "鍙戦�侀暱搴�(BeginSendTo)锛�" + value + "  鍙戦�佸唴瀹�:" + System.Text.Encoding.UTF8.GetString(bytes));
+                    }
                 }
                 catch (Exception ex)
                 {

--
Gitblit v1.8.0