JLChen
2021-01-11 5a5a2b696866f947b6025d26c3302e8ffef46435
Crabtree/SmartHome/UI/SimpleControl/Phone/Guide/GuideAddGateway.cs
@@ -83,10 +83,11 @@
            etRemark.EditorEnterAction += (obj) => {
                Application.HideSoftInput ();
            };
            if (MainPage.LoginUser.AccountType == 1) {
                etRemark.Enable = false;
                etRemark.BorderWidth = 0;
            }
            //if (MainPage.LoginUser.AccountType == 1) {
            //    etRemark.Enable = false;
            //    etRemark.BorderWidth = 0;
            //}
            var BottomView = new FrameLayout () {
                Y = Application.GetRealHeight (400 - 88),
@@ -283,6 +284,8 @@
                };
                EventHandler<MouseEventArgs> eHandler = (sender, e) => {
                    //跳转之前先绑定APP
                    MainPage.Loading.Start ("Please wait...");
                    bool needHide = true;
                    new System.Threading.Thread (() => {
@@ -299,11 +302,17 @@
                    bool goNext = false;
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            //
                            goNext = CheckIfGoNext (common);
                            if (goNext) {
                                //先重置
                                goNext = false;
                                //网关版本符合要求,继续判断是否绑定网关成功,绑定成功了才能进行下一步跳转设备搜索页面
                                goNext = CheckIfBindGatewaySuccess (common);
                            }
                         
                        } catch (Exception ex) {
                            Console.WriteLine (ex.Message);
                            Utlis.WriteLine (ex.Message);
                        } finally {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
@@ -312,17 +321,18 @@
                                    CommonPage.FindGatewayChilrenIPAddress = common.IPAddress;
                                    inThisView = false;
                                    if (inView != null) {
                                        Console.WriteLine ("inView != null");
                                        Utlis.WriteLine ("inView != null");
                                        //跳转设备搜索页面
                                        var deviceListView = new GuideSettingGateway (common);
                                        (inView.Parent as PageLayout).AddChidren (deviceListView);
                                        deviceListView.ShowPage ();
                                        (inView.Parent as PageLayout).PageIndex = (inView.Parent as PageLayout).ChildrenCount - 1;
                                    } else {
                                        Console.WriteLine ("inView == null");
                                        Utlis.WriteLine ("inView == null");
                                        UserHomePage.FrameLayoutMain.AddChidren (UserMiddle.guidePageView);
                                        var guide = new GuideAddResidence ();
                                        UserMiddle.guidePageView.AddChidren (guide);
                                        guide.ShowHomeList ();
                                        guide.ShowHomeView ();
                                        UserMiddle.guidePageView.PageIndex = 0;
                                        //重置为加密模式
                                        UserConfig.Instance.IsLocalEncrypt = true;
@@ -341,16 +351,62 @@
            }
        }
        /// <summary>
        /// 检测是否绑定网关成功
        /// </summary>
        /// <returns></returns>
        static bool CheckIfBindGatewaySuccess (GatewayBase gatewayBase)
        {
            try {
                if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
                    //之前绑定过网关,并且和当前搜索到到网关一致,子网号设备号都要一致
                    if (UserConfig.Instance.HomeGateway.mac.ToUpper () == gatewayBase.MAC.Replace (".", "").ToUpper ()
                        && UserConfig.Instance.HomeGateway.subnetId == gatewayBase.SubnetID
                        && UserConfig.Instance.HomeGateway.deviceId == gatewayBase.DeviceID
                        ) {
                        return true;
                    }
                }
                var gatewayMAC = gatewayBase.MAC.Replace (".", "");
                //重新绑定
                var bindGatewayObj = new BindGatewayObj () {
                    homeId = UserConfig.Instance.CurrentRegion.Id,
                    subnetId = gatewayBase.SubnetID,
                    deviceId = gatewayBase.DeviceID,
                    gatewayType = GatewayType.BUSUDPGATEWAY.ToString (),
                    mac = gatewayMAC
                };
                var revertObj = HttpServerRequest.Current.BindGateway (bindGatewayObj);
                if (revertObj.Code == StateCode.SUCCESS) {
                    var homeGatewayInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<HomeGatewayInfo> (revertObj.Data.ToString ());
                    if (homeGatewayInfo != null && !string.IsNullOrEmpty (homeGatewayInfo.gatewayId)) {
                        //绑定网关成功,并且成功获取到新绑定的gatewayId
                        UserConfig.Instance.HomeGateway = homeGatewayInfo;
                        UserConfig.Instance.SaveUserConfig ();
                        return true;
                    }
                } else {
                    IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
                }
            } catch {
                Utlis.WriteLine ("绑定网关失败");
            }
            return false;
        }
        static bool inThisView = false;
        Alert checkInternetAlert = new Alert ("", "Your phone in not connected to WIFI network, please connet", "Close", "Search again");
        Alert confirmAlert = new Alert ("", "Please make sure gateway is powered up and comect to WiFi router.", "Close", "Search again");
        Alert confirmAlert1 = new Alert ("", "If not all the gateways are in the list,you can search again.", "Close", "Search again");
        //Alert checkInternetAlert = new Alert ("", "Your phone in not connected to WIFI network, please connet", "Close", "Search again");
        //Alert confirmAlert = new Alert ("", "Please make sure gateway is powered up and comect to WiFi router.", "Close", "Search again");
        //Alert confirmAlert1 = new Alert ("", "If not all the gateways are in the list,you can search again.", "Close", "Search again");
        //Alert wrongPasswordAlert = new Alert ("", ErrorCode.GatewayPasswordError, "Close", "Search again");
        void SearchGatewayList ()
        {
            if (UserConfig.Instance.internetStatus == 0 || UserConfig.Instance.internetStatus == 1) {
                Alert checkInternetAlert = new Alert ("", ErrorCode.NotConnectedToWIFI, "Close", "Search again");
                checkInternetAlert.Show ();
                checkInternetAlert.ResultEventHandler += (dsss, eee) => {
@@ -370,6 +426,7 @@
                    #region 搜索设备
                    ////2020-01-14 默认明文开始搜索
                    UserConfig.Instance.IsLocalEncrypt = false;
                    UserConfig.Instance.EncryptedPasswordCorrect = true;
                    CommonPage.LocalPhoneFindDevice = true;
                    CommonPage.RandomHigh = (byte)new Random ().Next (255);
@@ -398,7 +455,7 @@
                        }
                        Control control = new Control ();
                        Console.WriteLine ("组播搜索网关,搜索地址是:" + CommonPage.EndPoint.ToString () + "    " + CommonPage.GateWayList.Count);
                        Utlis.WriteLine ("组播搜索网关,搜索地址是:" + CommonPage.EndPoint.ToString () + "    " + CommonPage.GateWayList.Count);
                        control.Send (new Target () {
                            IPEndPoint = CommonPage.EndPoint,
                            Command = Command.ReadGateway,
@@ -407,7 +464,7 @@
                            AddData = ms.ToArray ()
                        }, SendCount.Zero, false);
                        Console.WriteLine ("广播搜索网关,搜索地址是:" + new Net.NetWiFi ().BroadcastIpAddress.ToString () + "    " + CommonPage.GateWayList.Count);
                        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,
@@ -427,23 +484,21 @@
                    CommonPage.LocalPhoneFindDevice = false;
                    #endregion
                } catch (Exception ex) {
                    Console.WriteLine (ex.ToString ());
                    Utlis.WriteLine (ex.ToString ());
                } finally {
                    Application.RunOnMainThread (() => {
                        if (CommonPage.GateWayList.Count == 0) {
                            MainPage.Loading.Hide ();
                            if (this != null && this.Parent != null) {
                                confirmAlert.Show ();
                                confirmAlert.ResultEventHandler += (ddf, ddd) => {
                                    MainPage.Loading.Hide ();
                                    if (ddd) {
                                        SearchGatewayList ();
                                    }
                                };
                                ShowConfirmAlert ();
                            }
                        } else {
                            if (this != null && this.Parent != null) {
                                if (inThisView) {// this.Parent.GetChildren (this.Parent.ChildrenCount - 1) == this) {
                                    Alert confirmAlert1 = new Alert ("", ErrorCode.IfAllTheGatewaysAreNotInTheList, "Close", "Search again");
                                    confirmAlert1.Show ();
                                    confirmAlert1.ResultEventHandler += (ddf, ddd) => {
                                        MainPage.Loading.Hide ();
@@ -459,7 +514,49 @@
            }) { IsBackground = true }.Start ();
        }
        /// <summary>
        /// 提示
        /// Please make sure gateway is powered up and comect to WiFi router.
        /// 或者
        ///
        /// </summary>
        static void ShowConfirmAlert () {
            if (UserConfig.Instance.IsLocalEncrypt) {
                //密码检验正确
                if (UserConfig.Instance.EncryptedPasswordCorrect) {
                    Alert confirmAlert = new Alert ("", ErrorCode.MakeSureGatewayIsPoweredUp, "Close", "Search again");
                    confirmAlert.Show ();
                    confirmAlert.ResultEventHandler += (ddf, ddd) => {
                        MainPage.Loading.Hide ();
                        if (ddd) {
                            inView.SearchGatewayList ();
                        }
                    };
                } else {
                    Alert wrongPasswordAlert = new Alert ("", ErrorCode.GatewayPasswordError, "Close", "Search again");
                    wrongPasswordAlert.Show ();
                    wrongPasswordAlert.ResultEventHandler += (ddf, ddd) => {
                        MainPage.Loading.Hide ();
                        if (ddd) {
                            inView.SearchGatewayList ();
                        }
                    };
                }
            } else {
                Alert confirmAlert = new Alert ("", ErrorCode.MakeSureGatewayIsPoweredUp, "Close", "Search again");
                confirmAlert.Show ();
                confirmAlert.ResultEventHandler += (ddf, ddd) => {
                    MainPage.Loading.Hide ();
                    if (ddd) {
                        inView.SearchGatewayList ();
                    }
                };
            }
        }
        //static byte [] AseKeyBytes;
@@ -486,11 +583,11 @@
        /// </summary>
        /// <param name="backBytes"></param>
        /// <returns></returns>
        static bool CheckIsSuccessfulWithBytes (byte [] backBytes)
        static bool CheckIsSuccessfulWithBytes (byte [] backBytes, string errorStr = "Modify gateway configuration failed,please try again!")
        {
            if (backBytes == null) {
                Application.RunOnMainThread (() => {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline),
                    new Alert ("", ErrorCode.GatewayNoResponse,
                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
                return false;
@@ -498,7 +595,7 @@
                return true;
            } else {
                Application.RunOnMainThread (() => {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed),
                    new Alert ("", errorStr,
                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
                return false;
@@ -532,19 +629,19 @@
            });
            bool goNext = false;
            if (!UserConfig.Instance.IsLocalEncrypt) {
                //如果没加密,走修改加密方法
                var result = SetGateWayLocalEncryption (common);
                if (!result) {
                    MainPage.LoadingHide ();
                    return result;//修改失败 返回goNext false
                }
                //修改成功,设置本地为加密方式
                UserConfig.Instance.IsLocalEncrypt = true;
                //string aseKeyStr = CommonPage.MyEncodingUTF8.GetString (AseKeyBytes);
                //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
                System.Console.WriteLine ($"============>LocalEncryptKey 加密成功Key为:{UserConfig.Instance.LocalEncryptKey}");
            }
            //if (!UserConfig.Instance.IsLocalEncrypt) {
            //    //如果没加密,走修改加密方法
            //    var result = SetGateWayLocalEncryption (common);
            //    if (!result) {
            //        MainPage.LoadingHide ();
            //        return result;//修改失败 返回goNext false
            //    }
            //    //修改成功,设置本地为加密方式
            //    UserConfig.Instance.IsLocalEncrypt = true;
            //    //string aseKeyStr = CommonPage.MyEncodingUTF8.GetString (AseKeyBytes);
            //    //UserConfig.Instance.LocalEncryptKey = aseKeyStr;
            //    Utlis.WriteLine ($"============>LocalEncryptKey 加密成功Key为:{UserConfig.Instance.LocalEncryptKey}");
            //}
            var backBytes = Control.ControlBytesSendHasReturn (Command.Read_APP_Data_STORE_1D5C_CMD, common.SubnetID, common.DeviceID, new byte [] { });
          
@@ -552,9 +649,8 @@
                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 ();
                });
            }
            MainPage.LoadingHide ();
            if (backBytes.Length >= 37) {
                //******************住宅ID string类型*************
@@ -565,19 +661,62 @@
                //2020-01-12
                backIDStr = backIDStr.Replace ("\0", "");
                if (enable || backIDStr == UserConfig.Instance.CurrentRegion.Id || string.IsNullOrEmpty (backIDStr)) {
                    if (!UserConfig.Instance.IsLocalEncrypt) {
                        //如果没加密,走修改加密方法
                        var 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
                        }
                        //修改成功,设置本地为加密方式
                        UserConfig.Instance.IsLocalEncrypt = true;
                        //Utlis.WriteLine ($"============>LocalEncryptKey 加密成功Key为:{UserConfig.Instance.LocalEncryptKey}");
                    }
                    goNext = true;
                } else {
                    Application.RunOnMainThread (() => {
                        new Alert ("", ErrorCode.UnableToBindGateway, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                    UnableToBindGatewayAlertShow ();
                }
            } else {
                Application.RunOnMainThread (() => {
                    new Alert ("", ErrorCode.GatewayVersionLow, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
            MainPage.LoadingHide ();
            return goNext;
        }
        static void UnableToBindGatewayAlertShow ()
        {
            if (inView == null)
                return;
            Application.RunOnMainThread (() => {
                //if (this != null && this.Parent != null) {
                var alter = new Alert ("", ErrorCode.UnableToBindGateway, Language.StringByID (R.MyInternationalizationString.Close));
                alter.ResultEventHandler += (sender1, e1) => {
                    if (e1) {
                        if (inView == null)
                            return;
                        //重置为加密模式
                        UserConfig.Instance.IsLocalEncrypt = true;
                        (inView.Parent as PageLayout).PageIndex -= 1;
                    }
                };
                alter.Show ();
                //}
            });
        }
    }
}