From f77a3add1e7ab266e473da9128bbe5758ba3a890 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期日, 20 十二月 2020 19:29:03 +0800
Subject: [PATCH] Merge branch 'NewFilePath' into CJL

---
 HDL_ON/DAL/DriverLayer/Control.cs |   81 +++++++++++++++++++++++++++++-----------
 1 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index d1164c5..2e8555f 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -25,6 +25,8 @@
 
     public class Control
     {
+       
+
         static Control _control;
         public static Control Ins
         {
@@ -58,6 +60,14 @@
             return t.ToString();
         }
 
+        /// <summary>
+        /// 鏄惁鎼滅储鏈湴缃戝叧鎴愬姛
+        /// </summary>
+        public bool IsSearchLocalGatewaySuccessful = false;
+      
+        /// <summary>
+        /// 
+        /// </summary>
         bool _gatewayOnline = false;
         /// <summary>
         /// 缃戝叧鏄惁鍦ㄧ嚎
@@ -182,30 +192,55 @@
         public void SearchLoaclGateway()
         {
             MainPage.Log($"鎼滅储鏈湴缃戝叧鍒楄〃锛岀綉鍏崇被鍨�:{DB_ResidenceData.Instance.GatewayType}");
-            OpenUdp(DB_ResidenceData.Instance.GatewayType == 0 ? 6000 : 8585);
-            new System.Threading.Thread(() =>
+            if (MainPage.InternetStatus == 0)
             {
-                for (int i = 0; i < 5; i++)
-                {
-                    if (GatewayOnline)
-                        break;
-                    if (DB_ResidenceData.Instance.GatewayType == 0)
-                    {
-                        new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
-                    }
-                    else if (DB_ResidenceData.Instance.GatewayType == 1)
-                    {
-                        new Control_Udp().SearchLocalGateway();
-                    }
-                    System.Threading.Thread.Sleep(500);
-                }
-            })
-            { IsBackground = true }.Start();
-            if (!GatewayOnline)//缃戝叧涓嶅湪绾跨殑鏃跺�欐墠鍙皾璇曡繙绋嬭繛鎺�
-            {
-                Ins.IsRemote = true;
-                DAL.Mqtt.MqttClient.InitState();
+                Ins.GatewayOnline = false;
+                return;
             }
+            else if (MainPage.InternetStatus == 1)
+            {
+                if (!Ins.IsRemote)
+                {
+                    Ins.IsRemote = true;
+                    DAL.Mqtt.MqttClient.InitState();
+                }
+            }
+            else if (MainPage.InternetStatus == 2)
+            {
+                OpenUdp(DB_ResidenceData.Instance.GatewayType == 0 ? 6000 : 8585);
+                //閲嶇疆鎼滅储缃戝叧鏍囧織
+                IsSearchLocalGatewaySuccessful = false;
+
+                new System.Threading.Thread(() =>
+                {
+                    for (int i = 0; i < 5; i++)
+                    {
+                        if (IsSearchLocalGatewaySuccessful)
+                            break;
+                        if (DB_ResidenceData.Instance.GatewayType == 0)
+                        {
+                            new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
+                        }
+                        else if (DB_ResidenceData.Instance.GatewayType == 1)
+                        {
+                            new Control_Udp().SearchLocalGateway();
+                        }
+                        System.Threading.Thread.Sleep(500);
+                    }
+
+                    if (!IsSearchLocalGatewaySuccessful)
+                    {
+                        //鏈湴鎼滅储涓嶅埌缃戝叧
+                        if (!Ins.IsRemote)
+                        {
+                            Ins.IsRemote = true;
+                            DAL.Mqtt.MqttClient.InitState();
+                        }
+                    }
+                })
+                { IsBackground = true }.Start();
+            }
+            
         }
 
         /// <summary>
@@ -441,6 +476,8 @@
                     var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceModule>(bodyJObj.objects.ToString());
                     if (device.device_mac == DB_ResidenceData.Instance.residenceGatewayMAC)
                     {
+                        //鏈湴鎼滅储缃戝叧鎴愬姛
+                        Control.Ins.IsSearchLocalGatewaySuccessful = true;
                         Ins.GatewayOnline = true;
                         if (!string.IsNullOrEmpty(device.gatewayId))
                         {

--
Gitblit v1.8.0