From fb5f023820eb6783bd62d0d69d9afbd00cd3f426 Mon Sep 17 00:00:00 2001
From: 陈嘉乐 <cjl@hdlchina.com.cn>
Date: 星期一, 07 十二月 2020 16:50:47 +0800
Subject: [PATCH] 2020-12-07-1

---
 HDL_ON/DAL/DriverLayer/Control.cs |   55 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index bdf87b9..aeb874a 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -53,7 +53,7 @@
         public string Get_TimeStamp()
         {
             long t = DateTime.Now.Ticks / 10000;
-            return t.ToString(); 
+            return t.ToString();
         }
 
         bool _gatewayOnline = false;
@@ -76,10 +76,28 @@
         /// 缃戝叧閫氳ID
         /// </summary>
         public string GatewayId;
+
+        bool _isRemote = false;
         /// <summary>
         /// 鏄惁涓鸿繙绋嬭繛鎺�
         /// </summary>
-        public bool IsRemote = false;
+        public bool IsRemote {
+            get
+            {
+                return _isRemote;
+            }
+            set
+            {
+                _isRemote = value;
+                if(value)
+                {
+                    DAL.Mqtt.MqttClient.InitState();
+                }else
+                {
+                    DAL.Mqtt.MqttClient.DisConnectRemote();
+                }
+            }
+        }
         /// <summary>
         /// 閫氳鍦板潃IP
         /// </summary>
@@ -128,7 +146,7 @@
         /// </summary>
         public void CloseUdp()
         {
-            if(myUdp!= null)
+            if (myUdp != null)
             {
                 myUdp = null;
             }
@@ -161,15 +179,24 @@
         public void SearchLoaclGateway()
         {
             OpenUdp(DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585);
-
-            if (DB_ResidenceData.residenceData.GatewayType == 0)
+            new System.Threading.Thread(() =>
             {
-                myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
-            }
-            else if (DB_ResidenceData.residenceData.GatewayType == 1)
-            {
-                myUdp.SearchLocalGateway();
-            }
+                for (int i = 0; i < 5; i++)
+                {
+                    if (GatewayOnline)
+                        break;
+                    if (DB_ResidenceData.residenceData.GatewayType == 0)
+                    {
+                        myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
+                    }
+                    else if (DB_ResidenceData.residenceData.GatewayType == 1)
+                    {
+                        myUdp.SearchLocalGateway();
+                    }
+                    System.Threading.Thread.Sleep(500);
+                }
+            })
+            { IsBackground = true }.Start();
         }
 
         /// <summary>
@@ -201,13 +228,14 @@
             function.refreshTime = DateTime.Now;
             DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
             //杩滅▼閫氳
-            if (Ins.IsRemote)
+            if (!Ins.IsRemote)
             {
                 //ALink鎺у埗銆丅us鎺у埗浣跨敤鍚屼竴涓帴鍙f帶鍒讹紝鐢变簯绔礋璐hВ鏋�
                 var apiControlData = function.GetApiControlData(commandDictionary);
                 var actionObjs = new List<ApiAlinkControlActionObj>();
                 actionObjs.Add(apiControlData);
-                httpServer.ControlDevice(actionObjs);
+                var pack = httpServer.ControlDevice(actionObjs);
+                MainPage.Log($"{pack.Code}:{pack.Data}");
             }
             //鏈湴閫氳
             else
@@ -353,6 +381,7 @@
                             Ins.GatewayOnline = true;
                             Ins.GatewayId = device.gatewayId;
                             reportIp = "239.0.168.188";// device.ip_address;
+                            Ins.IsRemote = false;
                         }
                         break;
                 }

--
Gitblit v1.8.0