From 5a5a2b696866f947b6025d26c3302e8ffef46435 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 11 一月 2021 16:11:20 +0800
Subject: [PATCH] 2021-01-11 1.更新MQTT连接和本地搜索网关方法。2.子账号相关接口完善

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs |  274 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 179 insertions(+), 95 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index 79e4a78..2f278d8 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -25,8 +25,11 @@
             curView = this;
             myLoading = new Loading ();
 
+            myLoading.TextColor = 0xFFA9A9A9;
+            myLoading.LodingBackgroundColor = SkinStyle.Current.MainColor;
+
             //#if __IOS__
-            myLoading.LodingBackgroundColor = 0x00999999;
+            //myLoading.LodingBackgroundColor = 0x20999999;
             //#else
             //            myLoading.LodingBackgroundColor = SkinStyle.Current.ViewColor;
             //#endif
@@ -36,6 +39,8 @@
                     myLoading.Hide ();
                     btnCloseLoading.RemoveFromParent ();
                     WirelessConfig (new byte [] { 1 });//close
+                    //2020-07-02 鍏抽棴鑷姩鎼滅储绾跨▼
+                    StopSearchDeviceThead ();
                     if (needRefresh) {
                         needRefresh = false;
                         SearchDeviceList ();
@@ -50,6 +55,7 @@
             curView = null;
             addedCommon.Clear ();
             BackgroundColor = SkinStyle.Current.MainColor;
+            //StopSearchDeviceThead ();
             base.RemoveFromParent ();
         }
 
@@ -94,8 +100,8 @@
             };
 
             var btnSearch = new Button () {
-                Width = Application.GetRealWidth (75),
-                Height = Application.GetRealHeight (75),
+                Width = Application.GetMinRealAverage (75),
+                Height = Application.GetMinRealAverage (75),
                 X = Application.GetRealWidth (520),
                 Y = Application.GetRealHeight (10),
                 UnSelectedImagePath = "Item/Refresh.png",
@@ -146,11 +152,11 @@
         {
             if (gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
 
-                Console.WriteLine ("Assign OnePortMqttFR锛孒DL MQTT");
+                Utlis.WriteLine ("Assign OnePortMqttFR锛孒DL MQTT");
                 //RemoteSetting ();
                 RemoteSettingWithHDLMQTT ();
             } else {
-                Console.WriteLine ("Assign OnePortWirelessFR");
+                Utlis.WriteLine ("Assign OnePortWirelessFR");
                 RemoteSettingWithOldDevice ();
                 //RemoteSettingWithMQTT ();
             }
@@ -195,7 +201,7 @@
                     if (common.Type == DeviceType.MusicModel)
                         continue;
                     if (common.isMixBox) {
-                        Console.WriteLine ("IsMixBox");
+                        Utlis.WriteLine ("IsMixBox");
                         continue;
                     }
                     countDevcie++;
@@ -243,14 +249,17 @@
                         if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR || gatewayDeicve.Type == DeviceType.OnePortMqttFR) {
                             Control.ControlBytesSend (Command.GotoConfigMode, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
                             //new Alert ("", "缃戝叧宸茬粡杩涘叆閰嶉妯″紡锛岃鎵嬪姩鏂拌澶囪繘鍏ラ厤棰戞ā寮忋��", "Close").Show ();
-                            new Alert ("", "Searching for new devices, Please make sure all new devices are in configuration mode.", "OK").Show ();
+                            new Alert ("", ErrorCode.SearchingForNewDevices, "OK").Show ();
 
                             this.AddChidren (myLoading);
                             //myLoading.Start ("鐐瑰嚮灞忓箷鍏抽棴閰嶉妯″紡");
-                            myLoading.Start ("Searching, Please touch the here to exit the searching mode.");
+                            myLoading.Start (ErrorCode.SearchingExitTheSearchingMode);
                             //myLoading.Start ("Waiting for new device, Please touch the here to exit the waiting mode.");
                             
                             this.AddChidren (btnCloseLoading);
+
+                            //2020-07-02 缃戝叧閰嶉妯″紡鏃讹紝寮�鍚嚜鍔ㄦ悳绱㈢嚎绋�
+                            StartSearchDeviceThead ();
                         }
                     });
                 });
@@ -463,7 +472,7 @@
                                             btnDelDevice.Parent.RemoveFromParent ();
                                         });
                                     } catch (Exception ex) {
-                                        Console.WriteLine (ex.Message);
+                                        Utlis.WriteLine (ex.Message);
                                     } finally {
                                         Application.RunOnMainThread (() => {
                                             MainPage.Loading.Hide ();
@@ -501,7 +510,7 @@
                         //            }
                         //        }
                         //    } catch (Exception ex) {
-                        //        Console.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
+                        //        Utlis.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
                         //    }
                         //});
                     }
@@ -523,7 +532,7 @@
                     //杩欓噷鎼滅储璁惧锛岀洿鍒板畬鎴愬氨閫�鍑�
                     readDevice ();
                 } catch (Exception ex) {
-                    Console.WriteLine (ex.Message);
+                    Utlis.WriteLine (ex.Message);
                 }
                 Application.RunOnMainThread (action);
             }) { IsBackground = true }.Start ();
@@ -568,7 +577,7 @@
                     ms.WriteByte (common.SubnetID);
                     ms.WriteByte (common.DeviceID);
                 }
-                Console.WriteLine ("鎼滅储闈炵綉鍏宠澶囷紝缃戠粶鍦板潃鏄�:" + CommonPage.EndPoint.ToString ());
+                Utlis.WriteLine ("鎼滅储闈炵綉鍏宠澶囷紝缃戠粶鍦板潃鏄�:" + CommonPage.EndPoint.ToString ());
                 var control = new Control ();
                 control.Send (new Target () {
                     IPEndPoint = CommonPage.EndPoint,
@@ -704,10 +713,10 @@
                                 return;
                             }
                         }
-                        BindGatewaysNew ();
+                        GotoHomePage ();
                     }
                 } catch (Exception ex) {
-                    Console.WriteLine (ex.Message);
+                    Utlis.WriteLine (ex.Message);
                     Application.RunOnMainThread (() => {
                         new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                     });
@@ -754,7 +763,7 @@
                     }
 
                 } catch (Exception ex) {
-                    Console.WriteLine (ex.Message);
+                    Utlis.WriteLine (ex.Message);
                     Application.RunOnMainThread (() => {
                         //淇敼缃戝叧杩炴帴妯″紡澶辫触
                         new Alert ("", "Failed to modify gateway connection mode!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
@@ -905,80 +914,73 @@
                 }
             }
 
-            BindGatewaysNew ();
-
+            GotoHomePage ();
 
         }
 
 
-        /// <summary>
-        /// Gateway setting succeeded锛宒ata up.Please wait...
-        /// 缁戝畾Mac(鍗板害鐗�)
-        /// </summary>
-        void BindGatewaysNew ()
-        {
-            Application.RunOnMainThread (() => {
-                MainPage.Loading.Start ("Gateway setting succeeded锛宒ata up. Please wait...");
-            });
-            IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
-            var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
+        ///// <summary>
+        ///// Gateway setting succeeded锛宒ata up.Please wait...
+        ///// 缁戝畾Mac(鍗板害鐗�)
+        ///// </summary>
+        //void BindGatewaysNew ()
+        //{
+        //    //Application.RunOnMainThread (() => {
+        //    //    MainPage.Loading.Start ("Gateway setting succeeded锛宒ata up. Please wait...");
+        //    //});
+        //    //IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
+        //    //var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
 
-            //璧版柊鏇存柊缁戝畾鎺ュ彛
-            var mBindMacObj = new BindMacObj () {
-                LoginAccessToken = MainPage.LoginUser.LoginTokenString,
-                HomeId = UserConfig.Instance.CurrentRegion.Id,
-                MAC = gatewayMAC
+        //    ////璧版柊鏇存柊缁戝畾鎺ュ彛
+        //    //var mBindMacObj = new BindMacObj () {
+        //    //    LoginAccessToken = MainPage.LoginUser.LoginTokenString,
+        //    //    HomeId = UserConfig.Instance.CurrentRegion.Id,
+        //    //    MAC = gatewayMAC
 
-            };
-          
-            string urlHead = MainPage.RequestHttpsHost;
-            if (mBindMacObj.IsOtherAccountCtrl) {
-                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
-                mBindMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
-            }
+        //    //};
+
+        //    //string urlHead = MainPage.RequestHttpsHost;
+        //    //if (mBindMacObj.IsOtherAccountCtrl) {
+        //    //    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
+        //    //    mBindMacObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
+        //    //}
 
 
-            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
+        //    //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mBindMacObj);
 
-            //var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
-            var respone = MainPage.RequestHttps (API.BindMac, requestJson, urlHead);
-            if (respone.StateCode.ToUpper () != "SUCCESS") {
-                GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
-                Application.RunOnMainThread (() => {
-                    new Alert ("", $"Failed to bind Mac address! {ErrorCode.Reason }{respone.StateCode}", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                    if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
-                        new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                    }
-                });
-                return;
-            }
-            //GetNowHomeGateway ();
-            AddMacToUserConfig (gatewayMAC);
+        //    ////var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = gatewayMAC, IsReBind = true });
+        //    //var respone = MainPage.RequestHttps (API.BindMac, requestJson, urlHead);
+        //    //if (respone.StateCode.ToUpper () != StateCode.SUCCESS) {
+        //    //    GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
+        //    //    Application.RunOnMainThread (() => {
+        //    //        new Alert ("", $"Failed to bind Mac address! {ErrorCode.Reason }{respone.StateCode}", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //    //        if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
+        //    //            new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+        //    //        }
+        //    //    });
+        //    //    return;
+        //    //}
+        //    //GetNowHomeGateway ();
 
-        }
+        //    var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
+        //    AddMacToUserConfig (gatewayMAC);
+
+        //}
 
         /// <summary>
-        /// 淇敼鍒囨崲鏈湴浣忓畢 缃戝叧
+        /// 璺宠浆涓婚〉闈�
         /// </summary>
-        void AddMacToUserConfig (string gatewayMAC)
+        void GotoHomePage ()
         {
-
-            UserConfig.Instance.GatewayMAC = gatewayMAC;
-            UserConfig.Instance.SaveUserConfig ();
             MainPage.LoginUser.LastTime = DateTime.Now;
             Application.RunOnMainThread (() => {
-                bodyView.RemoveAll ();
+                this.RemoveFromParent ();
                 //MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                 UserMiddle.Init (true);
                 MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                 UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
             });
-
-            //Utlis.ShowAppLinkStatus (AppLinkStatus.WiFi);
-
         }
-
-
 
 
         ///// <summary>
@@ -1018,7 +1020,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") {
+        //        if (respone.StateCode.ToUpper () != StateCode.SUCCESS) {
         //            GetNowHomeGatewayAfterBindMacFailed ();//2020-01-11
         //            Application.RunOnMainThread (() => {
         //                new Alert ("", "Failed to update Mac address, gateway binding failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
@@ -1049,7 +1051,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") {
+        //        if (respone.StateCode.ToUpper () != StateCode.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 ();
@@ -1066,35 +1068,117 @@
         //}
 
 
-        /// <summary>
-        /// 缁戝畾澶辫触涔嬪悗锛屾煡璇㈠埛鏂颁竴娆″綋鍓嶄綇瀹� 缃戝叧鍒楄〃
-        /// 2020-01-11
-        /// </summary>
-        void GetNowHomeGatewayAfterBindMacFailed ()
-        {
+        ///// <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 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);
+        //    var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
+        //    var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
+        //    if (revertObj3.StateCode.ToUpper () == StateCode.SUCCESS) {
+        //        var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
+        //        //2020-01-11
+        //        UserConfig.Instance.SetNowHomeGateways (infoResult.PageData);
                 
-            } 
+        //    } 
 
+
+        //}
+
+        #region 2020-07-02
+        /// <summary>
+        /// 瀹氭椂鎼滅储璁惧绾跨▼
+        /// </summary>
+        Thread searchDeviceThead;
+        bool bStartSearch;
+
+        /// <summary>
+        /// 2020-07-02
+        /// 缃戝叧閰嶉妯″紡鏃讹紝寮�鍚嚜鍔ㄦ悳绱㈢嚎绋�
+        /// </summary>
+        void StartSearchDeviceThead ()
+        {
+            StopSearchDeviceThead ();
+            CommonPage.LocalPhoneFindDevice = true;
+            CommonPage.FindGatewayChilren = true;
+            CommonPage.RandomHigh = (byte)new Random ().Next (255);
+            CommonPage.RandomLow = (byte)new Random ().Next (255);
+
+            bStartSearch = true;
+            searchDeviceThead = new Thread ((obj) => {
+                while (bStartSearch) {
+                    try {
+                        //瀹氭椂4S
+                        Thread.Sleep (4000);
+
+                        var ms = new System.IO.MemoryStream ();
+                        ms.WriteByte (CommonPage.RandomHigh);
+                        ms.WriteByte (CommonPage.RandomLow);
+
+                        var list = CommonPage.DeviceList;
+                        int tempCount = list.Count;
+                        string s = ";";
+                        for (int i = 0; i < list.Count; i++) {
+                            Common common = list [i];
+                            if (common.SubnetID != gatewayDeicve.SubnetID)
+                                continue;
+                            if (s.Contains (";" + common.SubnetID.ToString () + ":" + common.DeviceID.ToString ()))
+                                continue;
+                            s += common.SubnetID.ToString () + ":" + common.DeviceID.ToString () + ";";
+                            ms.WriteByte (common.SubnetID);
+                            ms.WriteByte (common.DeviceID);
+                        }
+                        Utlis.WriteLine ("SearchDeviceThead鎼滅储闈炵綉鍏宠澶囷紝缃戠粶鍦板潃鏄�:" + CommonPage.EndPoint.ToString ());
+                        var control = new Control ();
+                        control.Send (new Target () {
+                            IPEndPoint = CommonPage.EndPoint,
+                            Command = Command.ReadDeviceModul,
+                            SubnetID = gatewayDeicve.SubnetID,
+                            DeviceID = 0xFF,
+                            AddData = ms.ToArray ()
+                        }, SendCount.Zero, false);
+
+                        ms.Close ();
+                        
+                    } catch {
+
+                    }
+                }
+            });
+            searchDeviceThead.Start ();
+
+
+            Utlis.WriteLine ("----StartSearchDeviceThead----");
+        }
+
+        /// <summary>
+        /// 鏆傚仠鎼滅储璁惧绾跨▼
+        /// </summary>
+        void StopSearchDeviceThead ()
+        {
+            CommonPage.LocalPhoneFindDevice = false;
+            CommonPage.FindGatewayChilren = false;
+            bStartSearch = false;
+            if (searchDeviceThead != null)
+                searchDeviceThead.Abort ();
+
+            Utlis.WriteLine ("----StopSearchDeviceThead----");
 
         }
+        #endregion
 
         ///// <summary>
         ///// 鏌ヨ褰撳墠浣忓畢 缃戝叧鍒楄〃
@@ -1117,7 +1201,7 @@
         //    //var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
         //    var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
         //    var revertObj3 = MainPage.RequestHttps (API.GetSingleHomeGatewayPagger, requestJson3, urlHead);
-        //    if (revertObj3.StateCode.ToUpper () == "SUCCESS") {
+        //    if (revertObj3.StateCode.ToUpper () == StateCode.SUCCESS) {
         //        var infoResult = Newtonsoft.Json.JsonConvert.DeserializeObject<GetGatewayResult> (revertObj3.ResponseData.ToString ());
         //        var responseGatewayDataObj = infoResult.PageData;
         //        if (responseGatewayDataObj != null && responseGatewayDataObj.Count > 0) {
@@ -1220,7 +1304,7 @@
         //            }
 
         //        } catch (Exception ex) {
-        //            Console.WriteLine (ex.Message);
+        //            Utlis.WriteLine (ex.Message);
         //            Application.RunOnMainThread (() => {
         //                new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
         //            });
@@ -1235,7 +1319,7 @@
         //    //Room.InitAllRoom ();
         //}
 
-      
+
 
         ///// <summary>
         ///// 淇敼URL

--
Gitblit v1.8.0