From ac39680a5ace5c02c63369014993fbfc959dbe5f Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 24 二月 2020 09:09:10 +0800
Subject: [PATCH] 2020-02-24 1.优化更新。

---
 Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs |  149 ++++++++++++++++++++++++++++++++++---------------
 1 files changed, 103 insertions(+), 46 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
index b66d7a9..0013c38 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/EquipmentPublicClass.cs
@@ -21,57 +21,79 @@
         /// <summary>
         /// 妫�娴�/杩炴帴杩滅▼
         /// </summary>
-        public static void CheckLinkRemote (int status)
+        public static void CheckLinkRemote (int status, bool IsReconnectRemote = true)
         {
-            if (isConnectiong) {
-                return;
-            }
-            isConnectiong = true;
+            //if (isConnectiong) {
+            //    return;
+            //}
+            //isConnectiong = true;
             UserConfig.Instance.internetStatus = status;
 #if wallon
                 return;
 #endif
-            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
+            if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin || isConnectiong) {
                 return;
             }
 
+            isConnectiong = true;
             new System.Threading.Thread (async () => {
                 try {
                     if (status == 0) {
                         Application.RunOnMainThread (() => {
                             MainPage.AddTip (Language.StringByID (R.MyInternationalizationString.InternetStatusTip));
-                            UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
+                            MainPage.WiFiStatus = "CrabtreeAdd/WiFiUnlink.png";
+                            UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
+
                         });
+
+                        await DisConnectRemoteAsync ();
+
                     } else {
-                        if (MainPage.LoginUser == null || !MainPage.LoginUser.IsLogin) {
-                            return;
-                        }
-                        Application.RunOnMainThread (() => {
-                            if (UserMiddle.LinkStatusTip.BackgroundColor == SkinStyle.Current.DelColor) {
-                                UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.MainColor;
-                            }
-                        });
+
+                        //Application.RunOnMainThread (() => {
+                        //    if (UserMiddle.LinkStatusTip.BackgroundColor == SkinStyle.Current.DelColor) {
+                        //        UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.MainColor;
+                        //    }
+                        //});
                         var localFileList = IO.FileUtils.ReadFiles ();
                         var gateWayList = localFileList.FindAll ((obj) => {
                             return (obj.StartsWith ("Equipment_")) && (
                              obj.Split ('_') [1].ToString () == DeviceType.OnePortBus.ToString () ||
                                 obj.Split ('_') [1].ToString () == DeviceType.RCU.ToString () ||
                                 obj.Split ('_') [1].ToString () == DeviceType.SuperWireless.ToString () ||
-                                obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString ());
+                                obj.Split ('_') [1].ToString () == DeviceType.OnePortWirelessFR.ToString () ||
+                                obj.Split ('_') [1].ToString () == DeviceType.OnePortMqttFR.ToString ());
                         });
                         bool canRemote = false;
-                        if (CommonPage.IsRemote) {
-                            //await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
-                            //await SmartHome.MqttCommon.StartCloudMqtt ();
+                       
+                        if (gateWayList == null || gateWayList.Count == 0) {
+                            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                            await DisConnectRemoteAsync ();
+                        }
 
+
+                        if (IsReconnectRemote) {
+                            if (CommonPage.IsRemote) {
+                                CommonPage.IsRemote = false;
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+                            }
+                        }
+
+                        if (CommonPage.IsRemote) {
+                            //await SmartHome.MqttCommon.StartCloudMqtt ();
                             SmartHome.MqttCommon.MqttRemoteSend (new byte [] { }, 3);
                         }
+
+                        //鏈湴鏄惁瀛樺湪涓庡綋鍓嶄綇瀹呯粦瀹氱綉鍏崇殑鏁版嵁
+                        bool HomeGatewaysExistsLocally = false;
                         foreach (var gatewayFileName in gateWayList) {
                             var tempStrings = gatewayFileName.Split ('_');
                             var gateWayString = CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (gatewayFileName));
                             var common = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayBase> (gateWayString);
                             if (common != null) {
                                 if (common.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC.Replace (".", "")) {
+                                    HomeGatewaysExistsLocally = true;
                                     canRemote = true;
                                     if (status == 2) {
                                         //var result = Control.ControlBytesSendHasReturn (Command.ReadGateway, common.SubnetID, common.DeviceID, new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) });
@@ -83,36 +105,18 @@
                                             SubnetID = common.SubnetID,
                                             DeviceID = common.DeviceID,
                                             AddData = new byte [] { (byte)new Random ().Next (255), (byte)new Random ().Next (255) },
-                                        }, SendCount.Three, true,true);
-                                      var  result = control.UsefulBytes;
+                                        }, SendCount.Three, true, true);
+                                        var result = control.UsefulBytes;
 
                                         #endregion
                                         if (result != null) {
                                             canRemote = false;
                                             var mac0 = CommonPage.byteToHex16 (result [5]) + "." + CommonPage.byteToHex16 (result [6]) + "." + CommonPage.byteToHex16 (result [7]) + "." + CommonPage.byteToHex16 (result [8]) + "." + CommonPage.byteToHex16 (result [9]) + "." + CommonPage.byteToHex16 (result [10]) + "." + CommonPage.byteToHex16 (result [11]) + "." + CommonPage.byteToHex16 (result [12]);
                                             if (common.MAC == mac0) {
-                                                //var gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, common.SubnetID, common.DeviceID, new byte [] { });
-                                                #region
-                                                control.Send (new Target () {
-                                                    IPEndPoint = new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000),
-                                                    Command = Command.ReadGateWayModelInfo,
-                                                    SubnetID = common.SubnetID,
-                                                    DeviceID = common.DeviceID,
-                                                    AddData = new byte [] { },
-                                                }, SendCount.Three, true,true);
-                                                var gatewayBytes = control.UsefulBytes;
-                                                #endregion
-                                                if (gatewayBytes != null) {
-                                                    common.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 1, 20).Trim ('\0');
-                                                    common.Remote_ProjectName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 21, 20).Trim ('\0');
-                                                    common.Remote_UserName = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 41, 8).Trim ('\0');
-                                                    common.Remote_Password = CommonPage.MyEncodingGB2312.GetString (gatewayBytes, 49, 8).Trim ('\0');
-                                                    var requestJson2 = @"{'Token':'" + MainPage.LoginUser.LoginTokenString + "','MAC':'" + common.MAC + "','Password':'" + common.Remote_Password + "'}";
-                                                    var revertObj2 = MainPage.RequestHttps ("UpdateRemoteSwitchPassword", requestJson2, true, false);
-                                                    return;
-                                                }
+                                                CommonPage.IsRemote = false;
                                                 Application.RunOnMainThread (() => {
-                                                    UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
+                                                    MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
+                                                    UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
                                                     MainPage.Loading.Hide ();
                                                 });
                                                 Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
@@ -126,12 +130,51 @@
                                 }
                             }
                         }
-                        if (canRemote && !CommonPage.IsRemote) {
-                            if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.MAC)) {
-                                Shared.SimpleControl.CommonPage.IsRemote = true;
-                                await SmartHome.MqttCommon.StartCloudMqtt ();
+
+
+
+                        //鑳借繙绋�
+                        if (canRemote) {
+                            if (!CommonPage.IsRemote) {
+                                if (UserConfig.Instance.CheckHomeGateways ()) {
+                                    if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
+                                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
+                                        CommonPage.IsRemote = true;
+                                        SmartHome.MqttCommon.IsGatewayOnline = false;
+                                        //await SmartHome.MqttCommon.StartCloudMqtt ();
+                                    }
+                                }
                             }
+
+                        } else {
+
+                            //涓嶈兘杩滅▼锛屽鏋滀箣鍓嶈繙绋嬫ā寮忓垯鏂紑MQTT杩炴帴,鏄剧ず涓烘湭杩炴帴
+                            if (CommonPage.IsRemote) {
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                                CommonPage.IsRemote = false;
+                                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+
+                            }
+
+                            if (!HomeGatewaysExistsLocally) {
+                                Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
+                            }
+
                         }
+
+
+                        //if (canRemote && !CommonPage.IsRemote) {
+                        //    if (UserConfig.Instance.CheckHomeGateways ()) {
+                        //        if (!string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
+                        //            Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
+                        //            Shared.SimpleControl.CommonPage.IsRemote = true;
+                        //            //await SmartHome.MqttCommon.StartCloudMqtt ();
+                        //        }
+                        //    }
+                        //}
+
+
+
                     }
                 } catch (Exception ex) {
                     Console.WriteLine ("CheckLinkRemote : " + ex.ToString ());
@@ -143,6 +186,20 @@
                 }
             }) { IsBackground = true }.Start ();
         }
+
+
+
+        /// <summary>
+        /// 鍏抽棴杩滅▼杩炴帴妯″紡锛屽苟鏂紑MQTT
+        /// </summary>
+        /// <returns></returns>
+        static async System.Threading.Tasks.Task DisConnectRemoteAsync (){
+
+            if (CommonPage.IsRemote) {
+                CommonPage.IsRemote = false;
+                await SmartHome.MqttCommon.DisConnectRemoteMqttClient ();
+            }
+        }
     }
 }
 

--
Gitblit v1.8.0