From c05a93ddb0714a310a31574aaf030e677d9a07b6 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 06 一月 2023 17:43:31 +0800
Subject: [PATCH] 增加多网关绑定支持

---
 Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs |   72 ++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs b/Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
index 324060e..ada09d8 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) {
@@ -645,14 +711,14 @@
             //    //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
             //    Utlis.WriteLine ($"============>LocalEncryptKey 鍔犲瘑鎴愬姛Key涓猴細{UserConfig.Instance.LocalEncryptKey}");
             //}
-
+            UserConfig.Instance.IsLocalEncrypt = false;
             var 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) {

--
Gitblit v1.8.0