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 |  390 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 246 insertions(+), 144 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index ed675ad..2f278d8 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
@@ -6,6 +6,7 @@
 {
     public class GuideSettingGateway : FrameLayout
     {
+       
         static byte TYPEStandardMQTT = 5;
         static byte TYPEHdlMQTT = 4;
         static GuideSettingGateway curView;
@@ -24,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
@@ -35,6 +39,8 @@
                     myLoading.Hide ();
                     btnCloseLoading.RemoveFromParent ();
                     WirelessConfig (new byte [] { 1 });//close
+                    //2020-07-02 鍏抽棴鑷姩鎼滅储绾跨▼
+                    StopSearchDeviceThead ();
                     if (needRefresh) {
                         needRefresh = false;
                         SearchDeviceList ();
@@ -49,6 +55,7 @@
             curView = null;
             addedCommon.Clear ();
             BackgroundColor = SkinStyle.Current.MainColor;
+            //StopSearchDeviceThead ();
             base.RemoveFromParent ();
         }
 
@@ -93,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",
@@ -145,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 ();
             }
@@ -194,7 +201,7 @@
                     if (common.Type == DeviceType.MusicModel)
                         continue;
                     if (common.isMixBox) {
-                        Console.WriteLine ("IsMixBox");
+                        Utlis.WriteLine ("IsMixBox");
                         continue;
                     }
                     countDevcie++;
@@ -242,12 +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 ();
                         }
                     });
                 });
@@ -460,7 +472,7 @@
                                             btnDelDevice.Parent.RemoveFromParent ();
                                         });
                                     } catch (Exception ex) {
-                                        Console.WriteLine (ex.Message);
+                                        Utlis.WriteLine (ex.Message);
                                     } finally {
                                         Application.RunOnMainThread (() => {
                                             MainPage.Loading.Hide ();
@@ -498,7 +510,7 @@
                         //            }
                         //        }
                         //    } catch (Exception ex) {
-                        //        Console.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
+                        //        Utlis.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
                         //    }
                         //});
                     }
@@ -520,12 +532,17 @@
                     //杩欓噷鎼滅储璁惧锛岀洿鍒板畬鎴愬氨閫�鍑�
                     readDevice ();
                 } catch (Exception ex) {
-                    Console.WriteLine (ex.Message);
+                    Utlis.WriteLine (ex.Message);
                 }
                 Application.RunOnMainThread (action);
             }) { IsBackground = true }.Start ();
         }
 
+        ///// <summary>
+        ///// 2020-02-11
+        ///// 鎼滅储閲嶅娆℃暟 鐢�4娆″鍔犱负6娆�
+        ///// </summary>
+        //static readonly int READ_COUNT = 6;
         /// <summary>
         /// 璇诲彇璁惧
         /// </summary>
@@ -539,7 +556,8 @@
             CommonPage.DeviceList.Clear ();
             CommonPage.searchTotal = 0;
             //濡傛灉涓ゆ閮芥病鏈夋暟鎹弽棣堬紝灏变笉璇诲彇
-            int readCount = 4;
+            //2020 - 02 - 11鎼滅储閲嶅娆℃暟 鐢�4娆″鍔犱负6娆�
+            int readCount = 6;
             while (0 < readCount) {
                 readCount--;
                 var ms = new System.IO.MemoryStream ();
@@ -559,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,
@@ -574,7 +592,7 @@
                 System.Threading.Thread.Sleep (1000);
                 //濡傛灉鏁伴噺涓嶇浉绛夛紝灏遍噸缃鏁�
                 if (tempCount != CommonPage.DeviceList.Count) {
-                    readCount = 4;
+                    readCount = 6;
                 }
             }
             CommonPage.DeviceList = CommonPage.DeviceList.FindAll ((obj) => { return obj != null && obj.SubnetID == gatewayDeicve.SubnetID; });
@@ -671,7 +689,7 @@
                     var mobytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInternetInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, serverIPBytes);
                     if (mobytes == null) {
                         Application.RunOnMainThread (() => {
-                            new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline),
+                            new Alert ("", ErrorCode.GatewayNoResponse,
                                        Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                         });
                     } else if (mobytes [0] == 0xF5) {
@@ -684,7 +702,7 @@
                             var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, currentRegionIdBytes);
                             if (bindReginIdStatus == null) {
                                 Application.RunOnMainThread (() => {
-                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                                    new Alert ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                 });
                                 return;
                             } else if (bindReginIdStatus [0] != 0xF8) {
@@ -695,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 ();
                     });
@@ -723,59 +741,29 @@
             byte [] gatewayBytes = null;
             string [] strServerIP = (MainPage.SeviceIP).Split ('.');
             string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
-            MainPage.Loading.Start ("Configuring device...");
+            MainPage.Loading.Start ("Configuring gateway...");
             System.Threading.Tasks.Task.Run (() => {
                 try {
 
-                    //璇诲彇涓�绔彛浜ゆ崲鏈恒�佹棤绾跨綉鍏崇殑閰嶇疆淇℃伅
-                    gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
-
-                    gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
-                    gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
-                    gatewayDeicve.Remote_UserName = "Admin";
-                    gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
-                    //gatewayDeicve.Remote_Password = "85521566";
-                    byte [] ggn = new byte [20];
-                    byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
-                    gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
-                    Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
-
-                    byte [] gpn = new byte [20];
-                    byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
-                    Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
-
-                    byte [] gun = new byte [8];
-                    byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
-                    Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
-
-                    byte [] gpw = new byte [8];
-                    byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
-                    Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
-
-
-                    gatewayBytes [0] = TYPEHdlMQTT;//HDL MQTT
-                    Array.Copy (ggn, 0, gatewayBytes, 1, 20);
-                    Array.Copy (gpn, 0, gatewayBytes, 21, 20);
-                    Array.Copy (gun, 0, gatewayBytes, 41, 8);
-                    Array.Copy (gpw, 0, gatewayBytes, 49, 8);
-                    //1.淇敼鐢ㄦ埛鍚嶄俊鎭�
-                    Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
-                    ////2.璇诲彇涓�娆$鐞嗕俊鎭�
-                    //byte [] adminBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
-                    ////3.淇敼绠$悊淇℃伅
-                    //if (SetGateWayAdminInfo (adminBytes)) {
-                    //    //4.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
-                    //    GatewaySettingSucceeded ();
+                    ////璇诲彇涓�绔彛浜ゆ崲鏈恒�佹棤绾跨綉鍏崇殑閰嶇疆淇℃伅
+                    //gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
+                    //if (gatewayBytes == null) {
+                    //    //璇诲彇涓�绔彛浜ゆ崲鏈恒�佹棤绾跨綉鍏崇殑閰嶇疆淇℃伅澶辫触
+                    //    new Alert ("", "Read gateway configuration failed!", "Close").Show ();
+                    //    return;
                     //}
-
-                    //2.淇敼绠$悊淇℃伅
-                    if (SetGateWayAdminInfo ()) {
-                        //3.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
-                        GatewaySettingSucceeded ();
+                   
+                    //1.淇敼鐢ㄦ埛鍚嶄俊鎭� 鍜屼慨鏀硅繛鎺ユā寮�
+                    if (SetGateWayModelInfo()) {
+                        //2.淇敼绠$悊淇℃伅
+                        if (SetGateWayAdminInfo ()) {
+                            //3.鍐欓厤缃垚鍔熷悗涓嬩竴姝ユ搷浣�
+                            GatewaySettingSucceeded ();
+                        }
                     }
 
                 } 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 ();
@@ -789,6 +777,40 @@
                 }
             });
             //Room.InitAllRoom ();
+        }
+
+        bool SetGateWayModelInfo () {
+            byte [] gatewayBytes = new byte [60];
+            gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
+            gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
+            gatewayDeicve.Remote_UserName = "Admin";
+            gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
+            //gatewayDeicve.Remote_Password = "85521566";
+            byte [] ggn = new byte [20];
+            byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
+            gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
+            Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
+
+            byte [] gpn = new byte [20];
+            byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
+            Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
+
+            byte [] gun = new byte [8];
+            byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
+            Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
+
+            byte [] gpw = new byte [8];
+            byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
+            Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
+
+
+            gatewayBytes [0] = TYPEHdlMQTT;//HDL MQTT
+            Array.Copy (ggn, 0, gatewayBytes, 1, 20);
+            Array.Copy (gpn, 0, gatewayBytes, 21, 20);
+            Array.Copy (gun, 0, gatewayBytes, 41, 8);
+            Array.Copy (gpw, 0, gatewayBytes, 49, 8);
+            byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
+            return CheckIsSuccessfulWithBytes (backBytes, "Failed to modify gateway connection mode.");
         }
 
         /// <summary>
@@ -847,7 +869,7 @@
         {
             if (backBytes == null) {
                 Application.RunOnMainThread (() => {
-                    new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline),
+                    new Alert ("", ErrorCode.GatewayNoResponse,
                                Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                 });
                 return false;
@@ -880,7 +902,7 @@
                 var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
                 if (bindReginIdStatus == null) {
                     Application.RunOnMainThread (() => {
-                        new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                        new Alert ("", ErrorCode.GatewayNoResponse, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                     });
                     return;
                 } else if (bindReginIdStatus [0] != 0xF8) {
@@ -892,75 +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;
             });
-
         }
-
-
 
 
         ///// <summary>
@@ -1000,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 ();
@@ -1031,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 ();
@@ -1048,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>
         ///// 鏌ヨ褰撳墠浣忓畢 缃戝叧鍒楄〃
@@ -1099,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) {
@@ -1202,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 ();
         //            });
@@ -1217,7 +1319,7 @@
         //    //Room.InitAllRoom ();
         //}
 
-      
+
 
         ///// <summary>
         ///// 淇敼URL

--
Gitblit v1.8.0