From f500e14c0a994487070380c50c85e0929cbc8e63 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 05 一月 2021 10:48:04 +0800
Subject: [PATCH] 2021-01-05 1.更新

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

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index 46bbfd3..14fa2e4 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,
@@ -707,7 +716,7 @@
                         BindGatewaysNew ();
                     }
                 } 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 ();
                     });
@@ -736,55 +745,25 @@
             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 ();
@@ -798,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>
@@ -964,8 +977,13 @@
             MainPage.LoginUser.LastTime = DateTime.Now;
             Application.RunOnMainThread (() => {
                 bodyView.RemoveAll ();
+                //MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                 UserMiddle.Init (true);
+                MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
+                UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
             });
+
+            //Utlis.ShowAppLinkStatus (AppLinkStatus.WiFi);
 
         }
 
@@ -1086,6 +1104,88 @@
 
 
         }
+
+        #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>
         ///// 鏌ヨ褰撳墠浣忓畢 缃戝叧鍒楄〃
@@ -1211,7 +1311,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 ();
         //            });
@@ -1226,7 +1326,7 @@
         //    //Room.InitAllRoom ();
         //}
 
-      
+
 
         ///// <summary>
         ///// 淇敼URL

--
Gitblit v1.8.0