Ejemplo n.º 1
0
        public FirstLaunchForm()
        {
            InitializeComponent();

            remoteDataRepository.Initialize(AppConfig.JohnDeereApiUrl, AppConfig.JohnDeereWellKnownUrl,
                                            RemoteProviderType.OAuthProvider,
                                            DataHelper.SaveNewToken, DataHelper.IsTokenExpired,
                                            AppConfig.AppId, AppConfig.AppSecret, null, null);
        }
Ejemplo n.º 2
0
 private void initRemoteRepo()
 {
     using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider())
     {
         accessToken  = dp.Settings.GetAsString(SettingKeyType.JDAccessToken);
         refreshToken = dp.Settings.GetAsString(SettingKeyType.JDRefreshToken);
     }
     remoteDataRepository.Initialize(AppConfig.JohnDeereApiUrl, AppConfig.JohnDeereWellKnownUrl, RemoteProviderType.OAuthProvider,
                                     DataHelper.SaveNewToken, DataHelper.IsTokenExpired,
                                     AppConfig.AppId, AppConfig.AppSecret, accessToken, refreshToken);
 }
Ejemplo n.º 3
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;
            }
        }