/// <summary>
        /// Runs a simple check to verify connectivity to remote API
        /// </summary>
        /// <returns></returns>
        private async Task checkConnection()
        {
            bool connected = false;

            lblStatusValue.Text = "Checking connection...";
            await Task.Run(async() => {
                System.Threading.Thread.Sleep(1500);
                using (IUnitOfWorkDataProvider p = AppStorage.GetUnitOfWorkDataProvider())
                {
                    //if we have credentials test connection
                    connected = ((await remoteDataRepository.TestConnection()) != string.Empty);
                }
            });

            this.Invoke((MethodInvoker) delegate
            {
                if (connected)
                {
                    lblStatusValue.Text      = "Connected";
                    lblStatusValue.ForeColor = Color.Green;
                }
                else
                {
                    lblStatusValue.Text      = "Not Connected";
                    lblStatusValue.ForeColor = Color.Red;
                }
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Checks for a connection and initializes UI for connections
        /// </summary>
        /// <returns></returns>
        public async Task LoadDataAsync()
        {
            if (!_loading || _firstRun)
            {
                _loading = true;
                lblPermissions.Visible = false;
                btnConnections.Visible = false;

                using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider())
                {
                    _currentUserId = dp.Settings.GetAsString(SettingKeyType.LastUserID);
                }

                remoteDataRepository.Initialize(AppConfig.JohnDeereApiUrl, AppConfig.JohnDeereWellKnownUrl, RemoteProviderType.OAuthProvider,
                                                DataHelper.SaveNewToken, DataHelper.IsTokenExpired,
                                                AppConfig.AppId, AppConfig.AppSecret, string.Empty, string.Empty);

                btnExit.Visible = _showExitButton;
                if (_firstRun)
                {
                    pnlConnectionSettingsConnected.Visible   = false;
                    pnlConnectionSettingsLinkAccount.Visible = false;
                    pnlConnectionSettingsVerifyCode.Visible  = true;
                    pnlNoNetwork.Visible = false;
                    return;
                }

                pnlConnectionSettingsConnected.Visible   = false;
                pnlConnectionSettingsLinkAccount.Visible = false;
                pnlConnectionSettingsVerifyCode.Visible  = false;
                pnlNoNetwork.Visible   = false;
                pnlLoading.Visible     = true;
                lblLoadingMessage.Text = "Checking for connection";
                bool connected = false;
                bool noNetwork = false;

                await Task.Run(async() =>
                {
                    System.Threading.Thread.Sleep(1000);

                    //check network state
                    if (!NetworkHelper.HasInternetConnection("https://google.com"))
                    {
                        connected = false;
                        noNetwork = true;
                    }
                    else
                    {
                        //we have a network connection so verify we can connect to John Deere
                        using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider())
                        {
                            var accessToken         = dp.Settings.GetAsString(SettingKeyType.JDAccessToken);
                            var refreshToken        = dp.Settings.GetAsString(SettingKeyType.JDRefreshToken);
                            var deereCredentialDate = dp.Settings.GetLastJohnDeereLoginDateTime();
                            var accessTokenExpires  = dp.Settings.GetAccessTokenExpiresUTC();

                            //if we have an access token and refresh token and we last logged in within the last 360 days
                            //we should have at a minimum a valid refresh token
                            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(refreshToken) && (deereCredentialDate.HasValue && deereCredentialDate.Value.AddDays(360) >= DateTime.UtcNow))
                            {
                                try
                                {
                                    remoteDataRepository.SetAuthData(accessToken, refreshToken);
                                    _currentUserId = await remoteDataRepository.TestConnection();
                                    if (_currentUserId != string.Empty)
                                    {
                                        connected = true;
                                    }
                                }
                                catch (Exception exc)
                                {
                                    Logger.Log(exc);
                                    connected = false;
                                }
                            }
                        }
                    }
                });

                pnlLoading.Visible = false;
                if (noNetwork == false)
                {
                    pnlConnectionSettingsConnected.Visible   = connected;
                    pnlConnectionSettingsLinkAccount.Visible = !connected;
                    pnlConnectionSettingsVerifyCode.Visible  = false;
                    pnlNoNetwork.Visible = false;

                    if (connected)
                    {
                        await showPermissionButtonIfNeeded();
                    }
                }
                else
                {
                    pnlConnectionSettingsConnected.Visible   = false;
                    pnlConnectionSettingsLinkAccount.Visible = false;
                    pnlConnectionSettingsVerifyCode.Visible  = false;
                    pnlNoNetwork.Visible = true;
                }

                _loading = false;
            }
        }