From ac2ee45fadc64fe840bbba6264df3ca4622adb15 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期日, 12 一月 2020 16:06:13 +0800
Subject: [PATCH] 2020-01-12 1.优化修改。

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs |  353 ++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 193 insertions(+), 160 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index ff4698b..0ed1ed1 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -228,7 +228,7 @@
                             }
                             if (Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, common.SubnetID, common.DeviceID, new byte [] { bigClass, minClass, (byte)k }) == null) {
                                 j++;
-                                //杩炵画涓ゆ璇诲彇涓嶅埌鏁版嵁鍒欒烦鍑鸿寰幆
+                                //杩炵画涓ゆ璇诲彇涓嶅埌鏁版嵁鍒欒烦鍑鸿寰幆锛屼唬琛ㄨ澶囩绾�2020-01-11
                                 if (j == 2 && k == 2)
                                     break;
                             } else {
@@ -774,9 +774,6 @@
                         GatewaySettingSucceeded ();
                     }
 
-
-
-
                 } catch (Exception ex) {
                     Console.WriteLine (ex.Message);
                     Application.RunOnMainThread (() => {
@@ -840,63 +837,6 @@
             return CheckIsSuccessfulWithBytes (backBytes);
         }
 
-
-        /// <summary>
-        /// 杩滅▼閰嶇疆 Standard MQTT妯″紡
-        /// </summary>
-        void RemoteSettingWithStandardMQTT ()
-        {
-            byte [] gatewayBytes = null;
-            string [] strServerIP = (MainPage.SeviceIP).Split ('.');
-            string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
-            MainPage.Loading.Start ("Configuring device...");
-            System.Threading.Tasks.Task.Run (() => {
-                try {
-
-                    //璇诲彇涓�绔彛浜ゆ崲鏈恒�佹棤绾跨綉鍏崇殑閰嶇疆淇℃伅
-                    gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
-                    //杩炴帴绫诲瀷閰嶇疆涓�5 Standard MQTT妯″紡
-                    gatewayBytes [0] = TYPEStandardMQTT;//Standard MQTT
-                    Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
-                    //1.淇敼URL
-                    //2.淇敼璇诲彇瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
-                    //3.淇敼AES鍔犲瘑
-                    //4.淇敼涓婚
-
-                    //1.淇敼URL
-                    if (SetGateWayURL ("https://developer.hdlcontrol.com/api/")) {
-                        //2.淇敼瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
-                        if (SetGateWayClientIDUsernamePassword ("85521566", "Admin", "85521566")) {
-                            //3.淇敼AES鍔犲瘑
-                            if (SetGateWayAESEncryption (true, "85521566")) {
-                                //4.1淇敼璁㈤槄涓婚
-                                if (SetGateWayTopic ("SubscribeTopic")) {
-                                    //4.2淇敼鍙戝竷涓婚
-                                    if (SetGateWayTopic ("PublishTopic", true)) {
-                                        //5.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
-                                        GatewaySettingSucceeded ();
-                                    }
-                                }
-                            }
-                        }
-                    }
-
-                } catch (Exception ex) {
-                    Console.WriteLine (ex.Message);
-                    Application.RunOnMainThread (() => {
-                        new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                    });
-                    bool canRemove = false;
-                } finally {
-                    Application.RunOnMainThread (() => {
-                        MainPage.Loading.Hide ();
-                        //this.RemoveAll ();
-                    });
-                }
-            });
-            //Room.InitAllRoom ();
-        }
-
         /// <summary>
         /// 鍒ゆ柇鏄惁淇鎴愬姛 F8 = 鎴愬姛锛� F5=澶辫触
         /// </summary>
@@ -919,82 +859,6 @@
                 });
                 return false;
             }
-        }
-
-        /// <summary>
-        /// 淇敼URL
-        /// </summary>
-        /// <returns></returns>
-        bool SetGateWayURL (string urlStr = "", int pointID = 1883)
-        {
-            byte [] sendBytes = new byte [63];
-            byte [] url = CommonPage.MyEncodingGB2312.GetBytes (urlStr);
-            Array.Copy (url, 0, sendBytes, 1, 60 < url.Length ? 60 : url.Length);
-            sendBytes [0] = GuideSettingGateway.TYPEStandardMQTT;
-            sendBytes [61] = Convert.ToByte (pointID / 256);
-            sendBytes [62] = Convert.ToByte (pointID % 256);
-            var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayURL, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
-
-            return CheckIsSuccessfulWithBytes (backBytes);
-        }
-
-
-        /// <summary>
-        /// 淇敼瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
-        /// </summary>
-        /// <returns></returns>
-        bool SetGateWayClientIDUsernamePassword (string clientIDStr = "", string usernameStr = "", string passwordStr = "")
-        {
-
-            byte [] userInfoBytes = new byte [64];
-            byte [] clientID = CommonPage.MyEncodingGB2312.GetBytes (clientIDStr);
-            Array.Copy (clientID, 0, userInfoBytes, 0, 32 < clientID.Length ? 32 : clientID.Length);
-            byte [] username = CommonPage.MyEncodingGB2312.GetBytes (usernameStr);
-            Array.Copy (username, 0, userInfoBytes, 32, 16 < username.Length ? 16 : username.Length);
-            byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
-            Array.Copy (password, 0, userInfoBytes, 48, 16 < password.Length ? 16 : password.Length);
-            var setUserInfoBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayClientIDUsernamePassword, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, userInfoBytes);
-
-            return CheckIsSuccessfulWithBytes (setUserInfoBytes);
-        }
-
-        /// <summary>
-        /// 淇敼AES鍔犲瘑
-        /// </summary>
-        /// <returns></returns>
-        bool SetGateWayAESEncryption (bool enable = false, string passwordStr = "")
-        {
-            byte [] sendBytes = new byte [17];
-            sendBytes [0] = enable ? (byte)1 : (byte)0; //0:涓嶄娇鑳�  1 - 255:浣胯兘
-            byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
-            Array.Copy (password, 0, sendBytes, 1, 16 < password.Length ? 16 : password.Length);
-            var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAESEncryption, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
-
-            return CheckIsSuccessfulWithBytes (backBytes);
-        }
-
-        /// <summary>
-        /// 淇敼涓婚
-        /// </summary>
-        /// <param name="bPublish">鏄惁涓哄彂甯冧富棰�</param>
-        /// <param name="bDefault">鏄惁榛樿</param>
-        /// <returns></returns>
-        bool SetGateWayTopic (string topicInfoStr = "", bool bPublish = false, bool bDefault = false)
-        {
-            byte [] sendBytes = new byte [50];
-            sendBytes [0] = bPublish ? (byte)2 : (byte)1; //1锛氳闃�  2锛氬彂甯�
-            sendBytes [1] = bDefault ? (byte)0 : (byte)1; //1:闈為粯璁� 0:榛樿
-
-            byte [] topicInfo = CommonPage.MyEncodingGB2312.GetBytes (topicInfoStr);
-            Array.Copy (topicInfo, 0, sendBytes, 2, 48 < topicInfo.Length ? 48 : topicInfo.Length);
-            var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayTopic, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
-
-            if (backBytes != null && backBytes.Length >= 2) {
-                if (backBytes [0] == sendBytes [0] && backBytes [1] == sendBytes [1]) {
-                    return true;
-                }
-            }
-            return CheckIsSuccessfulWithBytes (backBytes);
         }
 
 
@@ -1043,10 +907,9 @@
             IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
             var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
             var oldMac = "";
-            if (UserConfig.Instance.CheckHomeGateways () && !string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId)) {
-
-
-                //淇敼MAC鎺ュ彛
+            //2020-01-11 
+            if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
+                //褰撳墠浣忓畢瀛樺湪Mac 淇敼MAC鎺ュ彛
                 oldMac = UserConfig.Instance.CurrentRegion.HomeGateways [0].GatewayUniqueId;
                 if(oldMac == gatewayMAC) {//鍜岀粦瀹歮ac涓�鑷达紝涓嶇敤淇敼
                     AddMacToUserConfig (gatewayMAC);
@@ -1070,6 +933,7 @@
                 //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
                 var respone = MainPage.RequestHttps (API.UpdateMac, requestJson, urlHead);
                 if (respone.StateCode.ToUpper () != "SUCCESS") {
+                    GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
                     Application.RunOnMainThread (() => {
                         new Alert ("", "Failed to update Mac address, gateway binding failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                         if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
@@ -1100,6 +964,7 @@
                 //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
                 var respone = MainPage.RequestHttps (API.BindGatewayToHome, requestJson, urlHead);
                 if (respone.StateCode.ToUpper () != "SUCCESS" && respone.StateCode != "BindGatewaysExists") {
+                    GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
                     Application.RunOnMainThread (() => {
                         new Alert ("", "Failed to bind Mac address, gateway binding failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                         if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
@@ -1163,6 +1028,38 @@
 
         }
 
+
+
+        /// <summary>
+        /// 缁戝畾澶辫触涔嬪悗锛屾煡璇㈠埛鏂颁竴娆″綋鍓嶄綇瀹� 缃戝叧鍒楄〃
+        /// 2020-01-11
+        /// </summary>
+        void GetNowHomeGatewayAfterBindMacFailed ()
+        {
+
+            var requestObj3 = new GetSingleHomeGatewayPaggerObj ();
+            requestObj3.ReqDto.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
+            requestObj3.ReqDto.HomeId = UserConfig.Instance.CurrentRegion.Id;
+            requestObj3.ReqDto.PageSetting.Page = 1;
+            requestObj3.ReqDto.PageSetting.PageSize = 10;
+            string urlHead = MainPage.RequestHttpsHost;
+            if (requestObj3.IsOtherAccountCtrl) {
+                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+                requestObj3.ReqDto.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+            }
+
+            var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
+            var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
+            if (revertObj3.StateCode.ToUpper () == "SUCCESS") {
+                var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
+                //2020-01-11
+                UserConfig.Instance.SetNowHomeGateways (infoResult.PageData);
+                
+            } 
+
+
+        }
+
         ///// <summary>
         ///// 鏌ヨ褰撳墠浣忓畢 缃戝叧鍒楄〃
         ///// </summary>
@@ -1222,27 +1119,163 @@
 
 
 
-        /// <summary>
-        /// 
-        /// </summary>
-        /// <param name="stateCodeStr"></param>
-        void ShowGetAllBindGatewarysPaggerErrorInfo (string stateCodeStr)
-        {
-            string mes = "";
-            //2020-01-06 寰呰ˉ鍏�
-            if (stateCodeStr == ErrorCode.NetworkError) {
-                mes = ErrorCode.NetworkError;
-            } else {
-                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
+        ///// <summary>
+        ///// 
+        ///// </summary>
+        ///// <param name="stateCodeStr"></param>
+        //void ShowGetAllBindGatewarysPaggerErrorInfo (string stateCodeStr)
+        //{
+        //    string mes = "";
+        //    //2020-01-06 寰呰ˉ鍏�
+        //    if (stateCodeStr == ErrorCode.NetworkError) {
+        //        mes = ErrorCode.NetworkError;
+        //    } else {
+        //        mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
 
-            }
-            if (!string.IsNullOrEmpty (mes)) {
-                Application.RunOnMainThread (() => {
-                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                });
-            }
+        //    }
+        //    if (!string.IsNullOrEmpty (mes)) {
+        //        Application.RunOnMainThread (() => {
+        //            new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //        });
+        //    }
 
 
-        }
+        //}
+
+        //*****************StandardMQTT 閰嶇疆鏂瑰紡 鐩墠鐢ㄤ笉涓�*****************
+        ///// <summary>
+        ///// 杩滅▼閰嶇疆 Standard MQTT妯″紡
+        ///// </summary>
+        //void RemoteSettingWithStandardMQTT ()
+        //{
+        //    byte [] gatewayBytes = null;
+        //    string [] strServerIP = (MainPage.SeviceIP).Split ('.');
+        //    string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
+        //    MainPage.Loading.Start ("Configuring device...");
+        //    System.Threading.Tasks.Task.Run (() => {
+        //        try {
+
+        //            //璇诲彇涓�绔彛浜ゆ崲鏈恒�佹棤绾跨綉鍏崇殑閰嶇疆淇℃伅
+        //            gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+        //            //杩炴帴绫诲瀷閰嶇疆涓�5 Standard MQTT妯″紡
+        //            gatewayBytes [0] = TYPEStandardMQTT;//Standard MQTT
+        //            Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
+        //            //1.淇敼URL
+        //            //2.淇敼璇诲彇瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
+        //            //3.淇敼AES鍔犲瘑
+        //            //4.淇敼涓婚
+
+        //            //1.淇敼URL
+        //            if (SetGateWayURL ("https://developer.hdlcontrol.com/api/")) {
+        //                //2.淇敼瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
+        //                if (SetGateWayClientIDUsernamePassword ("85521566", "Admin", "85521566")) {
+        //                    //3.淇敼AES鍔犲瘑
+        //                    if (SetGateWayAESEncryption (true, "85521566")) {
+        //                        //4.1淇敼璁㈤槄涓婚
+        //                        if (SetGateWayTopic ("SubscribeTopic")) {
+        //                            //4.2淇敼鍙戝竷涓婚
+        //                            if (SetGateWayTopic ("PublishTopic", true)) {
+        //                                //5.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
+        //                                GatewaySettingSucceeded ();
+        //                            }
+        //                        }
+        //                    }
+        //                }
+        //            }
+
+        //        } catch (Exception ex) {
+        //            Console.WriteLine (ex.Message);
+        //            Application.RunOnMainThread (() => {
+        //                new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //            });
+        //            bool canRemove = false;
+        //        } finally {
+        //            Application.RunOnMainThread (() => {
+        //                MainPage.Loading.Hide ();
+        //                //this.RemoveAll ();
+        //            });
+        //        }
+        //    });
+        //    //Room.InitAllRoom ();
+        //}
+
+      
+
+        ///// <summary>
+        ///// 淇敼URL
+        ///// </summary>
+        ///// <returns></returns>
+        //bool SetGateWayURL (string urlStr = "", int pointID = 1883)
+        //{
+        //    byte [] sendBytes = new byte [63];
+        //    byte [] url = CommonPage.MyEncodingGB2312.GetBytes (urlStr);
+        //    Array.Copy (url, 0, sendBytes, 1, 60 < url.Length ? 60 : url.Length);
+        //    sendBytes [0] = GuideSettingGateway.TYPEStandardMQTT;
+        //    sendBytes [61] = Convert.ToByte (pointID / 256);
+        //    sendBytes [62] = Convert.ToByte (pointID % 256);
+        //    var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayURL, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
+
+        //    return CheckIsSuccessfulWithBytes (backBytes);
+        //}
+
+
+        ///// <summary>
+        ///// 淇敼瀹㈡埛绔疘D锛岀敤鎴峰悕锛屽瘑鐮�
+        ///// </summary>
+        ///// <returns></returns>
+        //bool SetGateWayClientIDUsernamePassword (string clientIDStr = "", string usernameStr = "", string passwordStr = "")
+        //{
+
+        //    byte [] userInfoBytes = new byte [64];
+        //    byte [] clientID = CommonPage.MyEncodingGB2312.GetBytes (clientIDStr);
+        //    Array.Copy (clientID, 0, userInfoBytes, 0, 32 < clientID.Length ? 32 : clientID.Length);
+        //    byte [] username = CommonPage.MyEncodingGB2312.GetBytes (usernameStr);
+        //    Array.Copy (username, 0, userInfoBytes, 32, 16 < username.Length ? 16 : username.Length);
+        //    byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
+        //    Array.Copy (password, 0, userInfoBytes, 48, 16 < password.Length ? 16 : password.Length);
+        //    var setUserInfoBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayClientIDUsernamePassword, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, userInfoBytes);
+
+        //    return CheckIsSuccessfulWithBytes (setUserInfoBytes);
+        //}
+
+        ///// <summary>
+        ///// 淇敼AES鍔犲瘑
+        ///// </summary>
+        ///// <returns></returns>
+        //bool SetGateWayAESEncryption (bool enable = false, string passwordStr = "")
+        //{
+        //    byte [] sendBytes = new byte [17];
+        //    sendBytes [0] = enable ? (byte)1 : (byte)0; //0:涓嶄娇鑳�  1 - 255:浣胯兘
+        //    byte [] password = CommonPage.MyEncodingGB2312.GetBytes (passwordStr);
+        //    Array.Copy (password, 0, sendBytes, 1, 16 < password.Length ? 16 : password.Length);
+        //    var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAESEncryption, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
+
+        //    return CheckIsSuccessfulWithBytes (backBytes);
+        //}
+
+        ///// <summary>
+        ///// 淇敼涓婚
+        ///// </summary>
+        ///// <param name="bPublish">鏄惁涓哄彂甯冧富棰�</param>
+        ///// <param name="bDefault">鏄惁榛樿</param>
+        ///// <returns></returns>
+        //bool SetGateWayTopic (string topicInfoStr = "", bool bPublish = false, bool bDefault = false)
+        //{
+        //    byte [] sendBytes = new byte [50];
+        //    sendBytes [0] = bPublish ? (byte)2 : (byte)1; //1锛氳闃�  2锛氬彂甯�
+        //    sendBytes [1] = bDefault ? (byte)0 : (byte)1; //1:闈為粯璁� 0:榛樿
+
+        //    byte [] topicInfo = CommonPage.MyEncodingGB2312.GetBytes (topicInfoStr);
+        //    Array.Copy (topicInfo, 0, sendBytes, 2, 48 < topicInfo.Length ? 48 : topicInfo.Length);
+        //    var backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayTopic, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
+
+        //    if (backBytes != null && backBytes.Length >= 2) {
+        //        if (backBytes [0] == sendBytes [0] && backBytes [1] == sendBytes [1]) {
+        //            return true;
+        //        }
+        //    }
+        //    return CheckIsSuccessfulWithBytes (backBytes);
+        //}
+
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0