From 0fafbe4b132d03dc8f1f99de585bfcb5e63ab60a Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期一, 09 一月 2023 14:02:01 +0800
Subject: [PATCH] 多网关支持

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs |  114 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 95 insertions(+), 19 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
index 79a02cd..23d5bfa 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
@@ -17,6 +17,21 @@
         {
             BackgroundColor = SkinStyle.Current.MainColor;
             inThisView = true;
+
+
+            //var secretKeyPack = HttpServerRequest.Current.ApplyDeviceSecret ("3782DACE9D900428");
+            //if (secretKeyPack != null) {
+            //    if (secretKeyPack.Code == StateCode.SUCCESS) {
+            //        var pack = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceSecret> (secretKeyPack.Data.ToString ());
+            //        if (pack != null) {
+            //            byte [] usefullBytes = new byte [17];
+            //            usefullBytes [0] = 1;
+            //            byte [] ddd = System.Text.Encoding.ASCII.GetBytes (pack.deviceSecret);
+            //            Array.Copy (ddd, 0, usefullBytes, 1, 16 < ddd.Length ? 16 : ddd.Length);
+            //        }
+            //    }
+            //}
+
         }
 
         public override void RemoveFromParent ()
@@ -427,6 +442,8 @@
                         MainPage.Loading.Start ("Searching for gateway...");
                     });
                     #region 鎼滅储璁惧
+                    //鏄惁闇�瑕佸姞瀵嗘娴嬨�併�佷负浜嗛�傞厤澶氱綉鍏筹紝鎼滅储娣诲姞缃戝叧鐨勬椂鍊欓渶瑕佸叧鎺夊姞瀵嗘娴嬶紝鐒跺悗鍚屾椂鍙戦�佸瘑鏂囷紝鏄庢枃鎼滅储缃戝叧 2023-01-06 16:49:30
+                    CommonPage.needEncryptionDetection = false;
                     ////2020-01-14 榛樿鏄庢枃寮�濮嬫悳绱�
                     UserConfig.Instance.IsLocalEncrypt = false;
                     UserConfig.Instance.EncryptedPasswordCorrect = true;
@@ -482,8 +499,57 @@
                             readCount = 3;
                         }
                     }
-                    CommonPage.FindGateway = false;
 
+                    //2023-01-06 16:33:09 澧炲姞鍚屾椂鍙戦�佸瘑鏂囨悳绱�
+                    CommonPage.needEncryptionDetection = true;
+                    UserConfig.Instance.IsLocalEncrypt = true;
+                    readCount = 3;
+                    while (0 < readCount) {
+                        readCount--;
+                        System.IO.MemoryStream ms = new System.IO.MemoryStream ();
+                        ms.WriteByte (CommonPage.RandomHigh);
+                        ms.WriteByte (CommonPage.RandomLow);
+
+                        List<Common> list = CommonPage.GateWayList;
+
+                        int tempCount = list.Count;
+                        string s = ";";
+                        for (int i = 0; i < list.Count; i++) {
+                            Common common = list [i];
+                            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);
+                        }
+                        Control control = new Control ();
+
+                        Utlis.WriteLine ("缁勬挱鎼滅储缃戝叧锛屾悳绱㈠湴鍧�鏄�:" + CommonPage.EndPoint.ToString () + "    " + CommonPage.GateWayList.Count);
+                        control.Send (new Target () {
+                            IPEndPoint = CommonPage.EndPoint,
+                            Command = Command.ReadGateway,
+                            SubnetID = 0xFF,
+                            DeviceID = 0xFF,
+                            AddData = ms.ToArray ()
+                        }, SendCount.Zero, false);
+
+                        Utlis.WriteLine ("骞挎挱鎼滅储缃戝叧锛屾悳绱㈠湴鍧�鏄�:" + new Net.NetWiFi ().BroadcastIpAddress.ToString () + "    " + CommonPage.GateWayList.Count);
+                        control.Send (new Target () {
+                            IPEndPoint = new System.Net.IPEndPoint (new Net.NetWiFi ().BroadcastIpAddress, 6000),
+                            Command = Command.ReadGateway,
+                            SubnetID = 0xFF,
+                            DeviceID = 0xFF,
+                            AddData = ms.ToArray ()
+                        }, SendCount.Zero, false);
+
+                        System.Threading.Thread.Sleep (800);
+                        //濡傛灉鏁伴噺涓嶇浉绛夛紝灏遍噸缃鏁�
+                        if (tempCount != CommonPage.GateWayList.Count) {
+                            readCount = 3;
+                        }
+                    }
+
+                    CommonPage.FindGateway = false;
                     CommonPage.LocalPhoneFindDevice = false;
                     #endregion
                 } catch (Exception ex) {
@@ -588,11 +654,13 @@
         /// <returns></returns>
         static bool CheckIsSuccessfulWithBytes (byte [] backBytes, string errorStr = "Modify gateway configuration failed,please try again!")
         {
-            if (backBytes == null) {
-                Application.RunOnMainThread (() => {
-                    new Alert ("", ErrorCode.GatewayNoResponse,
-                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                });
+            if (backBytes == null ) {
+                if (CommonPage.needEncryptionDetection) {
+                    Application.RunOnMainThread (() => {
+                        new Alert ("", ErrorCode.GatewayNoResponse,
+                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                    });
+                }
                 return false;
             } else if (backBytes [0] == 0xF8) {//F8 = 鎴愬姛锛� F5=澶辫触
                 return true;
@@ -645,14 +713,18 @@
             //    //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
             //    Utlis.WriteLine ($"============>LocalEncryptKey 鍔犲瘑鎴愬姛Key涓猴細{UserConfig.Instance.LocalEncryptKey}");
             //}
-
+            CommonPage.needEncryptionDetection = false;
             var backBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { });
-          
+            CommonPage.needEncryptionDetection = true;
+
             if (backBytes == null) {
-                Application.RunOnMainThread (() => {
-                    new Alert ("", "No response from gateway. Please make sure the gateway is online and its firmware is up to date.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
-                });
-               
+                backBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { });
+                if (backBytes == null) {
+                    Application.RunOnMainThread (() => {
+                        new Alert ("", "No response from gateway. Please make sure the gateway is online and its firmware is up to date.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                    });
+                    return false;
+                }
             }
 
             if (backBytes.Length >= 37) {
@@ -666,17 +738,21 @@
                 if (enable || backIDStr == UserConfig.Instance.CurrentRegion.Id || string.IsNullOrEmpty (backIDStr)) {
                    
 
-                    if (!UserConfig.Instance.IsLocalEncrypt) {
+                    if (!UserConfig.Instance.IsLocalEncrypt || true) {//涓嶇鍔犳病鍔犲瘑锛岄兘閲嶆柊鍔犲瘑涓�娆★紝闃叉澶氱綉鍏崇殑鏃跺�欐湁浠庣綉鍏虫病鍔犲瘑 2022-12-26 13:42:09
                         //濡傛灉娌″姞瀵嗭紝璧颁慨鏀瑰姞瀵嗘柟娉�
+                        CommonPage.needEncryptionDetection = false;
                         var result = SetGateWayLocalEncryption (common);
+                        CommonPage.needEncryptionDetection = true;
                         if (!result) {
-                            Application.RunOnMainThread (() => {
-                                MainPage.Loading.Hide ();
-                                new Alert ("", "Encryption gateway failed, please try again.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
+                            result = SetGateWayLocalEncryption (common);
+                            if (!result) {
+                                Application.RunOnMainThread (() => {
+                                    MainPage.Loading.Hide ();
+                                    new Alert ("", "Encryption gateway failed, please try again.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
 
-                            });
-                          
-                            return result;//淇敼澶辫触 杩斿洖goNext false
+                                });
+                                return result;//淇敼澶辫触 杩斿洖goNext false
+                            }
                         }
                         //淇敼鎴愬姛,璁剧疆鏈湴涓哄姞瀵嗘柟寮�
                         UserConfig.Instance.IsLocalEncrypt = true;

--
Gitblit v1.8.0