Пример #1
0
        private void StravaDownload()
        {
            var athlete = _uow.CRUDRepository.GetByUserId <Athlete>(_jobDetails.UserId).FirstOrDefault();

            if (athlete != null)
            {
                Token currentToken = new Token();
                currentToken.RefreshToken = athlete.RefreshToken;
                currentToken.AccessToken  = athlete.Token;
                currentToken.ExpiresAt    = athlete.ExpiresAt;
                currentToken.ExpiresIn    = athlete.ExpiresIn;

                var expires = DateHelpers.UnixTimeStampToDateTime(currentToken.ExpiresAt);

                var expiresIn = expires.Subtract(DateTime.Now).TotalMinutes;

                if (expiresIn < 60)
                {
                    var clientId     = ConfigurationManager.AppSettings["stravaApiClientId"];
                    var clientSecret = ConfigurationManager.AppSettings["stravaApiClientSecret"];


                    var newToken = StravaTokenRefresh.RefreshToken(currentToken, clientId, clientSecret);

                    athlete.RefreshToken = newToken.Result.RefreshToken;
                    athlete.Token        = newToken.Result.AccessToken;
                    athlete.ExpiresAt    = newToken.Result.ExpiresAt;
                    athlete.ExpiresIn    = newToken.Result.ExpiresIn;

                    _uow.CRUDRepository.Update <Athlete>(athlete);
                }
            }



            if (_jobDetails.ActivityId != null)
            {
                // download full details for an individual activity.
                StravaActivityDownload s = new StravaActivityDownload(_uow, _jobDetails.UserId);
                s.ActivityDetailsDownload(_jobDetails.ActivityId.Value);
                _jobDetails.MarkJobComplete();
                _uow.Complete();
            }
            else
            {
                // if _jobDetails isn't for an individual activity then it must be a request to search for new activities.
                StravaActivityScan s = new StravaActivityScan(_uow, _jobDetails.UserId);
                s.AddActivitesForAthlete();
                _jobDetails.MarkJobComplete();
                _uow.Complete();
            }
        }
 /// <summary>
 /// Convert DateTime to Unix timestamp.
 /// </summary>
 /// <param name="date">Date to convert</param>
 /// <returns></returns>
 public static int ToUnixDateTime(this DateTime date)
 {
     return(DateHelpers.DateTimeToUnixTimeStamp(date));
 }