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 |  243 +++++++++++++++++++++++++++++++++++-------------
 1 files changed, 176 insertions(+), 67 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideSettingGateway.cs
index ed675ad..14fa2e4 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) {
@@ -698,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 ();
                     });
@@ -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) {
@@ -955,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);
 
         }
 
@@ -1077,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>
         ///// 鏌ヨ褰撳墠浣忓畢 缃戝叧鍒楄〃
@@ -1202,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 ();
         //            });
@@ -1217,7 +1326,7 @@
         //    //Room.InitAllRoom ();
         //}
 
-      
+
 
         ///// <summary>
         ///// 淇敼URL

--
Gitblit v1.8.0