From d6fb0646531172f23648441c224cdcccd721b894 Mon Sep 17 00:00:00 2001
From: xm <1271024303@qq.com>
Date: 星期一, 14 十二月 2020 09:59:01 +0800
Subject: [PATCH] 请合并代码,完成晾衣架最终功能。

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |  153 ++++++++++++++++++---------------------------------
 1 files changed, 54 insertions(+), 99 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 91553e0..beab8a5 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -64,7 +64,6 @@
         {
             new System.Threading.Thread(() =>
             {
-                var searchCount = 3;
                 var broadBytes = new byte[44];// byteHomeId[0] ,//H
                 broadBytes[0] = 0xfe;
                 broadBytes[1] = 0x29;
@@ -85,57 +84,30 @@
                         }
 
                         var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
-                        if (Shared.Application.IsWifi)
+                        
+                        //娓呯┖鎺ユ敹鍒扮殑IP
+                        listReceiveIP.Clear();
+                        if (broadcastIpAddress.ToString() != "0.0.0.0")
                         {
-                            if (0 < listReceiveIP.Count)
-                            {
-                                searchCount = 3;
-                                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);
-                                }
-                            }
+                            //骞挎挱鍑哄幓
+                            FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
+                        }
+                        //绛夊緟3绉�,涓嬮潰鐨勬帴鏀舵柟娉曚細鍘绘帴鏀跺箍鎾�
+                        System.Threading.Thread.Sleep(3000);
+
+                        if (listReceiveIP.Count > 0)
+                        {
+                            //灞�鍩熺綉鑳藉骞挎挱寰楀埌缃戝叧
+                            ZbGateway.IsRemote = false;
+                            //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
+                            Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                         }
                         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");
-                            }
-                            //闈瀢ifi鎯呭喌涓�,涓嶉渶瑕佸箍鎾�
-                            System.Threading.Thread.Sleep(1000);
-                            continue;
-                        }
-                        searchCount--;
-
-                        //姣忕骞挎挱鍙戠幇涓�娆$綉鍏�
-                        int count = 2;
-                        listReceiveIP.Clear();
-                        while (0 < count--)
-                        {
-                            //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級
-                            for (int i = 0; i < listReceiveIP.Count; i++)
-                            {
-                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(listReceiveIP[i]), 7624), broadBytes);
-                            }
-                            //骞挎挱鍙戦��
-                            if (broadcastIpAddress.ToString() != "0.0.0.0")
-                            {
-                                FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
-                            }
-                            System.Threading.Thread.Sleep(1000);
                         }
                     }
                     catch { }
@@ -160,13 +132,13 @@
                 {
                     try
                     {
-                        if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0)
+                        if (FindGateWaySocket.udpClient == null || FindGateWaySocket.udpClient.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);
+                        }
+                        var ipEndPoint = new System.Net.IPEndPoint(0, 0);
+                        var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
                         if (bytes[43] == 0xA2)
                         {
                             //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞�
@@ -190,7 +162,6 @@
                                 HomeId = homeID,
                                 IsMainGateWay = isMainGateWay,
                                 GwId = id,
-                                GatewayOnlineFlage = true,
                                 PubKey = pubKey
                             };
                             //閫氳鍒扮綉鍏冲垪琛�
@@ -206,11 +177,17 @@
                                 {
                                     listReceiveIP.Add(ipAddress);
                                 }
+                                //2020.08.07杩藉姞:鍗充娇绌虹殑浣忓畢id涔熷缓绔嬮摼鎺�,瀵瑰簲鎵嬪姩鎭㈠缃戝叧鍑哄巶璁剧疆鏃�,鍥犱负娌℃湁瑙g粦,浣嗘槸浣忓畢id涓虹┖鐨勯棶棰�
+                                else if (Shared.Phone.UserCenter.HdlGatewayLogic.Current.HomeIdIsEmpty(homeID) == true)
+                                {
+                                    listReceiveIP.Add(ipAddress);
+                                }
                             }
                             //缃戝叧鍒楄〃瀛樺偍澶勭悊
                             var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
                             if (gateWay == null)
                             {
+                                zbGateWay.OnlineTime = DateTime.Now;
                                 ZbGateway.GateWayList.Add(zbGateWay);
                                 await zbGateWay.StartLocalMqtt(ipAddress);
                             }
@@ -218,7 +195,7 @@
                             {
                                 gateWay.IsVirtual = false;
                                 //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
-                                gateWay.GatewayOnlineFlage = true;
+                                gateWay.OnlineTime = DateTime.Now;
                                 if (gateWay.GwIP != ipAddress)
                                 {
                                     await gateWay.DisConnectLocalMqttClient("2");
@@ -284,7 +261,14 @@
                             }
                         }
                     }
-                    catch { }
+                    catch (Exception ex)
+                    {
+                        //璋冭瘯鐢�
+                        if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+                        {
+                            Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive寮傚父\r\n" + ex.Message);
+                        }
+                    }
                 }
             })
             { IsBackground = true }.Start();
@@ -352,12 +336,14 @@
                 //鏈�鍚庡啀鏂紑mqtt杩炴帴
                 for (int i = 0; i < list.Count; i++)
                 {
-                    list[i].DisConnectLocalMqttClient("1");
+                    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)
@@ -390,24 +376,19 @@
         public static class FindGateWaySocket
         {
             //鏈湴Socket
-            public static Socket busSocket;
+            public static UdpClient udpClient;
 
             /// <summary>
             /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
             /// </summary>
             /// <param name="port"></param>
-            public static void Start(int port = 7624)
+            public static void Start()
             {
-                if (IsRunning)
+                if (udpClient!=null)
                 {
                     return;
                 }
-                //瀹氫箟缃戠粶绫诲瀷锛屾暟鎹繛鎺ョ被鍨嬪拰缃戠粶鍗忚UDP
-                busSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-                busSocket.EnableBroadcast = true;
-                busSocket.ReceiveTimeout = 1000;
-                busSocket.SendTimeout = 1000;
-                busSocket.Bind(new IPEndPoint(IPAddress.Any, port));
+                udpClient = new UdpClient { EnableBroadcast = true };
             }
 
             /// <summary>
@@ -417,23 +398,12 @@
             {
                 try
                 {
-                    busSocket?.Close();
+                    udpClient?.Close();
                 }
                 catch { }
-                busSocket = null;
+                udpClient = null;
 
                 Console.WriteLine("BusSocket鍏抽棴鎴愬姛!");
-            }
-
-            /// <summary>
-            /// 褰撳墠鐨凷ocket鏄惁杩愯
-            /// </summary>
-            public static bool IsRunning
-            {
-                get
-                {
-                    return busSocket == null ? false : true;
-                }
             }
 
             /// <summary>
@@ -444,11 +414,16 @@
             {
                 try
                 {
-                    if (IsRunning)
+                    if (udpClient == null)
                     {
-                        busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
+                        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));
+                    }
                 }
                 catch (Exception ex)
                 {
@@ -459,28 +434,8 @@
                     }
                 }
             }
-
-            /// <summary>
-            /// 寮傛鍙戦�佹暟鎹粨鏉�
-            /// </summary>
-            /// <param name="iar"></param>
-            private static void asyncEndSend(IAsyncResult iar)
-            {
-                try
-                {
-                    int bytesSent = busSocket.EndSendTo(iar);
-                }
-                catch (Exception ex)
-                {
-                    //璋冭瘯鐢�
-                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
-                    {
-                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "寮傛鍙戦�佺粨鏉熷紓甯�(asyncEndSend)\r\n" + ex.Message);
-                    }
-                }
-            }
         }
 
         #endregion
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.8.0