From 0e0584b87851c56745bf73b1a961b80613dd0721 Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期二, 19 五月 2020 16:35:16 +0800
Subject: [PATCH] 2020-05-19-4

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |  102 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 56 insertions(+), 46 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 89ec837..ec0ce95 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -104,18 +104,25 @@
                             {
                                 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");
@@ -189,7 +196,6 @@
                                         getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
                                         {
                                             IpAddress = ipAddress,
-                                            Time = time,
                                             GwName = gwName,
                                             HomeId = homeID,
                                             IsMainGateWay = isMainGateWay,
@@ -241,7 +247,6 @@
                                         else
                                         {
                                             gateWay.PubKey = pubKey;
-                                            gateWay.getGatewayBaseInfo.Time = time;
                                             gateWay.getGatewayBaseInfo.GwName = gwName;
                                             gateWay.getGatewayBaseInfo.HomeId = homeID;
                                             await gateWay.StartLocalMqtt(ipAddress);
@@ -303,48 +308,7 @@
                     try
                     {
                         //瀹氭椂妫�娴嬭繙绋嬭繛鎺ユ儏鍐�
-                        await ZbGateway.StartRemoteMqtt();
-                        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);
@@ -369,7 +333,11 @@
         public static class FindGateWaySocket
         {
             //鏈湴Socket
-            public static System.Net.Sockets.Socket busSocket;
+            public static Socket busSocket;
+            /// <summary>
+            /// 璁℃椂鍣�
+            /// </summary>
+            private static int timeCount = 0;
 
             /// <summary>
             /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
@@ -415,7 +383,7 @@
             {
                 get
                 {
-                    return null == busSocket ? false : true;
+                    return busSocket == null ? false : true;
                 }
             }
 
@@ -427,12 +395,54 @@
             {
                 try
                 {
+                    //妫�娴嬭繛鎺ョ姸鎬�
+                    CheckConnectiton();
+
                     Start(7624);
                     busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
                 }
                 catch { }
             }
 
+            /// <summary>
+            /// 妫�娴嬭繛鎺ョ姸鎬�
+            /// </summary>
+            private static void CheckConnectiton()
+            {
+                if (busSocket == null) { return; }
+                timeCount++;
+                if (timeCount < 20)
+                {
+                    //姣�10绉掓娴嬩竴娆�
+                    return;
+                }
+                timeCount = 0;
+
+                bool blockingState = busSocket.Blocking;
+                try
+                {
+                    byte[] tmp = new byte[1];
+
+                    busSocket.Blocking = false;
+                    busSocket.Send(tmp, 0, 0);
+                    //tcp杩樺湪杩炴帴鐫�
+                    busSocket.Blocking = blockingState;
+                }
+                catch (SocketException e)
+                {
+                    // 10035 == WSAEWOULDBLOCK
+                    if (e.NativeErrorCode.Equals(10035))
+                    {
+                        //Still Connected, but the Send would block
+                    }
+                    else
+                    {
+                        //tcp宸茬粡鏂紑浜嗚繛鎺�
+                        Stop();
+                    }
+                }
+            }
+
             /// <summary>
             /// 寮傛鍙戦�佹暟鎹粨鏉�
             /// </summary>

--
Gitblit v1.8.0