From 0f5d7d18f98d6d961b3d21dc2b1b59905e261fff Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期二, 23 二月 2021 17:09:17 +0800
Subject: [PATCH] 20210223-1

---
 HDL_ON/DAL/DriverLayer/Control.cs |  247 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 178 insertions(+), 69 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index 20d2fa3..e8827a2 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -65,42 +65,80 @@
         /// </summary>
         public bool IsSearchLocalGatewaySuccessful = false;
       
+        ///// <summary>
+        ///// 
+        ///// </summary>
+        //bool _gatewayOnline1 = false;
+        ///// <summary>
+        ///// 缃戝叧鏄惁鍦ㄧ嚎
+        ///// </summary>
+        //public bool GatewayOnline1
+        //{
+        //    get
+        //    {
+        //        return _gatewayOnline;
+        //    }
+        //    set
+        //    {
+        //        if (_gatewayOnline != value)
+        //        {
+        //            if(value)
+        //            {
+        //                if (IsRemote)//濡傛灉鏄繙绋�
+        //                {
+        //                    if(DB_ResidenceData.Instance.HomeGateway == null)
+        //                    {
+        //                        return;
+        //                    }
+        //                    if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//杩滅▼鎯呭喌涓嬶紝缃戝叧鏈摼鎺ユ湇鍔″櫒涓嶈兘淇敼涓婚〉缃戝叧鐘舵��
+        //                    {
+        //                        new System.Threading.Thread(() => {
+        //                            System.Threading.Thread.Sleep(3000);
+        //                            var pm = new DAL.Server.HttpServerRequest();
+        //                            pm.GetGatewayInfo();
+        //                        }) { IsBackground = true }.Start();
+        //                        return;
+        //                    }
+        //                }
+        //                _gatewayOnline = value;
+        //                //淇敼涓婚〉杩炴帴鐘舵��
+        //                HomePage.LoadEvent_CheckLinkStatus();
+        //                MainPage.Log($"缃戝叧鍦ㄧ嚎锛屽埛鏂拌澶囩姸鎬�");
+        //                new System.Threading.Thread(() =>
+        //                {
+        //                    System.Threading.Thread.Sleep(1000);
+        //                    FunctionList.List.ReadAllFunctionStatus();
+        //                })
+        //                { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start();
+        //            }
+        //            else
+        //            {
+        //                _gatewayOnline = value;
+        //                //淇敼涓婚〉杩炴帴鐘舵��
+        //                HomePage.LoadEvent_CheckLinkStatus();
+        //            }
+        //        }
+        //    }
+        //}
+
+
+        bool _GatewayOnline_Local = false;
         /// <summary>
-        /// 
+        /// 缃戝叧鍦ㄧ嚎-灞�鍩熺綉
         /// </summary>
-        bool _gatewayOnline = false;
-        /// <summary>
-        /// 缃戝叧鏄惁鍦ㄧ嚎
-        /// </summary>
-        public bool GatewayOnline
+        public bool GatewayOnline_Local
         {
             get
             {
-                return _gatewayOnline;
+                return _GatewayOnline_Local;
             }
             set
             {
-                if (_gatewayOnline != value)
+                if (_GatewayOnline_Local != value)
                 {
-                    if(value)
+                    _GatewayOnline_Local = value;
+                    if (value)
                     {
-                        if (IsRemote)//濡傛灉鏄繙绋�
-                        {
-                            if(DB_ResidenceData.Instance.HomeGateway == null)
-                            {
-                                return;
-                            }
-                            if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//杩滅▼鎯呭喌涓嬶紝缃戝叧鏈摼鎺ユ湇鍔″櫒涓嶈兘淇敼涓婚〉缃戝叧鐘舵��
-                            {
-                                new System.Threading.Thread(() => {
-                                    System.Threading.Thread.Sleep(3000);
-                                    var pm = new DAL.Server.HttpServerRequest();
-                                    pm.GetGatewayInfo();
-                                }) { IsBackground = true }.Start();
-                                return;
-                            }
-                        }
-                        _gatewayOnline = value;
                         //淇敼涓婚〉杩炴帴鐘舵��
                         HomePage.LoadEvent_CheckLinkStatus();
                         MainPage.Log($"缃戝叧鍦ㄧ嚎锛屽埛鏂拌澶囩姸鎬�");
@@ -113,13 +151,68 @@
                     }
                     else
                     {
-                        _gatewayOnline = value;
                         //淇敼涓婚〉杩炴帴鐘舵��
                         HomePage.LoadEvent_CheckLinkStatus();
                     }
                 }
             }
         }
+        bool _GatewayOnline_Cloud = false;
+        /// <summary>
+        /// 缃戝叧鍦ㄧ嚎-浜戠
+        /// </summary>
+        public bool GatewayOnline_Cloud
+        {
+            get
+            {
+                return _GatewayOnline_Cloud;
+            }
+            set
+            {
+                if (_GatewayOnline_Cloud != value)
+                {
+                    _GatewayOnline_Cloud = value;
+                    if(GatewayOnline_Local)
+                    {
+                        return;
+                    }
+                    if (value)
+                    {
+                        if (DB_ResidenceData.Instance.HomeGateway == null)
+                        {
+                            return;
+                        }
+                        if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//杩滅▼鎯呭喌涓嬶紝缃戝叧鏈摼鎺ユ湇鍔″櫒涓嶈兘淇敼涓婚〉缃戝叧鐘舵��
+                        {
+                            new System.Threading.Thread(() =>
+                            {
+                                System.Threading.Thread.Sleep(3000);
+                                var pm = new DAL.Server.HttpServerRequest();
+                                pm.GetGatewayInfo();
+                            })
+                            { IsBackground = true }.Start();
+                            return;
+                        }
+                        //淇敼涓婚〉杩炴帴鐘舵��
+                        HomePage.LoadEvent_CheckLinkStatus();
+                        MainPage.Log($"缃戝叧鍦ㄧ嚎锛屽埛鏂拌澶囩姸鎬�");
+                        new System.Threading.Thread(() =>
+                        {
+                            System.Threading.Thread.Sleep(1000);
+                            FunctionList.List.ReadAllFunctionStatus();
+                        })
+                        { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start();
+                    }
+                    else
+                    {
+                        //淇敼涓婚〉杩炴帴鐘舵��
+                        HomePage.LoadEvent_CheckLinkStatus();
+                    }
+                }
+            }
+        }
+
+
         /// <summary>
         /// 缃戝叧閫氳ID
         /// </summary>
@@ -128,7 +221,7 @@
         /// <summary>
         /// 鏄惁涓鸿繙绋嬭繛鎺�
         /// </summary>
-        public bool IsRemote = false;
+        //public bool IsRemote = false;
         /// <summary>
         /// 閫氳鍦板潃IP
         /// </summary>
@@ -197,21 +290,18 @@
             {
                 return;
             }
+            Ins.GatewayOnline_Local = false;
             var ggg = DB_ResidenceData.Instance.GatewayType == 0 ? "涓�绔彛" : "A缃戝叧";
             var ggg1 = MainPage.InternetStatus == 1 ? "4G" : "wifi";
             MainPage.Log($"鎼滅储缃戝叧鍒楄〃锛岀綉鍏崇被鍨�:{ggg};缃戠粶绫诲瀷:{ggg1}");
             if (MainPage.InternetStatus == 0)
             {
-                Ins.GatewayOnline = false;
+                Ins.GatewayOnline_Cloud = false;
                 return;
             }
             else if (MainPage.InternetStatus == 1)
             {
-                //if (!Ins.IsRemote)
-                {
-                    //Ins.IsRemote = true;
-                    DAL.Mqtt.MqttClient.InitState();
-                }
+                DAL.Mqtt.MqttClient.InitState();
             }
             else if (MainPage.InternetStatus == 2)
             {
@@ -224,7 +314,10 @@
                     for (int i = 0; i < 5; i++)
                     {
                         if (IsSearchLocalGatewaySuccessful)
+                        {
+                            Ins.GatewayOnline_Local = true;
                             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) });
@@ -236,15 +329,7 @@
                         System.Threading.Thread.Sleep(500);
                     }
 
-                    if (!IsSearchLocalGatewaySuccessful)
-                    {
-                        //鏈湴鎼滅储涓嶅埌缃戝叧
-                        if (!Ins.IsRemote)
-                        {
-                            Ins.IsRemote = true;
-                            DAL.Mqtt.MqttClient.InitState();
-                        }
-                    }
+                    DAL.Mqtt.MqttClient.InitState();
                 })
                 { IsBackground = true }.Start();
             }
@@ -255,7 +340,7 @@
         /// </summary>
         public void ControlScene(Scene scene)
         {
-            if (Ins.IsRemote || DB_ResidenceData.Instance.GatewayType == 1)
+            if (!Ins.GatewayOnline_Local || DB_ResidenceData.Instance.GatewayType == 1)
             {
                 new System.Threading.Thread(() =>
                 {
@@ -321,8 +406,8 @@
                     break;
             }
 
-            //杩滅▼閫氳
-            if (Ins.IsRemote || useRemote == true)
+            //杩滅▼閫氳  --娑傞甫璁惧蹇呴』闇�瑕佽繙绋�
+            if (useRemote == true)
             {
                 DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
                 //ALink鎺у埗銆丅us鎺у埗浣跨敤鍚屼竴涓帴鍙f帶鍒讹紝鐢变簯绔礋璐hВ鏋�
@@ -331,30 +416,54 @@
                 actionObjs.Add(apiControlData);
                 var pack = httpServer.ControlDevice(actionObjs);
             }
-            //鏈湴閫氳
             else
             {
-                //Bus鎺у埗
-                if (DB_ResidenceData.Instance.GatewayType == 0)
+                ////杩滅▼閫氳
+                //if (Ins.GatewayOnline_Local == false && Ins.GatewayOnline_Cloud == true)
+                //{
+                //    DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
+                //    //ALink鎺у埗銆丅us鎺у埗浣跨敤鍚屼竴涓帴鍙f帶鍒讹紝鐢变簯绔礋璐hВ鏋�
+                //    var apiControlData = function.GetApiControlData(commandDictionary);
+                //    var actionObjs = new List<ApiAlinkControlActionObj>();
+                //    actionObjs.Add(apiControlData);
+                //    var pack = httpServer.ControlDevice(actionObjs);
+                //}
+                //鏈湴閫氳
+                if (Ins.GatewayOnline_Local)
                 {
-                    try
+                    //Bus鎺у埗
+                    if (DB_ResidenceData.Instance.GatewayType == 0)
                     {
-                        new Control_Udp().WriteBusData(function, commandDictionary);
+                        try
+                        {
+                            new Control_Udp().WriteBusData(function, commandDictionary);
+                        }
+                        catch (Exception ex)
+                        {
+                            MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
+                        }
                     }
-                    catch (Exception ex)
+                    //ALink鎺у埗
+                    else
                     {
-                        MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
+                        var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary);
+                        var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
+                        var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
+                        new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id);
+                        MainPage.Log($"鏈湴閫氳 鍙戦�丠DL-Link鏁版嵁:{functionControlDataJson}");
                     }
                 }
-                //ALink鎺у埗
+                //杩滅▼閫氳
                 else
                 {
-                    var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary);
-                    var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
-                    var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
-                    new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id);
-                    MainPage.Log($"鏈湴閫氳 鍙戦�丠DL-Link鏁版嵁:{functionControlDataJson}");
+                    DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
+                    //ALink鎺у埗銆丅us鎺у埗浣跨敤鍚屼竴涓帴鍙f帶鍒讹紝鐢变簯绔礋璐hВ鏋�
+                    var apiControlData = function.GetApiControlData(commandDictionary);
+                    var actionObjs = new List<ApiAlinkControlActionObj>();
+                    actionObjs.Add(apiControlData);
+                    var pack = httpServer.ControlDevice(actionObjs);
                 }
+
             }
         }
         /// <summary>
@@ -428,13 +537,9 @@
         public void SendReadCommand(Function function)
         {
             function.refreshTime = DateTime.Now;
-            if (Ins.IsRemote)
+            if (Ins.GatewayOnline_Local)
             {
-                var pm = new DAL.Server.HttpServerRequest();
-                var pack = pm.RefreshDeviceStatus(new List<string>() { function.deviceId });
-            }
-            else
-            {
+
                 if (DB_ResidenceData.Instance.GatewayType == 0)
                 {
                     try
@@ -466,6 +571,11 @@
                     new Control_Udp().SendLocalHdlLinkData(sendBytes, readDataObj.id);
                 }
             }
+            else
+            {
+                var pm = new DAL.Server.HttpServerRequest();
+                var pack = pm.RefreshDeviceStatus(new List<string>() { function.deviceId });
+            }
         }
         ///// <summary>
         ///// 鍙戦�丄鍗忚璇诲彇鍛戒护
@@ -489,7 +599,7 @@
         /// <param name="scene"></param>
         static void ControlAProtocolScene(Scene scene)
         {
-            if (Ins.IsRemote)
+            if (!Ins.GatewayOnline_Local)//缃戝叧鏈湴涓嶅湪绾�
             {
                 //浜戠鍋氬鐞嗗彂閫佷竴绔彛鍦烘櫙
                 //if (DB_ResidenceData.Instance.GatewayType == 0)
@@ -594,8 +704,8 @@
                     if (device.device_mac == DB_ResidenceData.Instance.residenceGatewayMAC)
                     {
                         MainPage.Log("鏈湴鎼滅储缃戝叧鎴愬姛");
-                        Control.Ins.IsSearchLocalGatewaySuccessful = true;
-                        Ins.GatewayOnline = true;
+                        Ins.IsSearchLocalGatewaySuccessful = true;
+                        Ins.GatewayOnline_Local = true;
                         if (!string.IsNullOrEmpty(device.gatewayId))
                         {
                             Ins.GatewayId = device.gatewayId;
@@ -605,7 +715,6 @@
                             Ins.GatewayId = device.device_mac;
                         }
                         reportIp = "239.0.168.188";// device.ip_address;
-                        Ins.IsRemote = false;
                     }
                 }
                 else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||

--
Gitblit v1.8.0