コード例 #1
0
        public static bool TryParseLactatMeasurements(this DetailedActivity activity)
        {
            if (activity._LactateVersion == Version)
            {
                return(false);
            }

            activity._LactateVersion = Version;

            var measurements = GetLactateFromDescription(activity.Description);

            measurements.AddRange(GetLactateFromDescription(activity.PrivateNote));
            var intervalLaps = activity.Laps?.Where(lap => lap.IsInterval).ToList() ?? new List <Lap>();

            foreach (var lap in intervalLaps)
            {
                lap.Lactate = null;
            }

            foreach (var measurement in measurements)
            {
                if (measurement.Lap == null)
                {
                    activity.Lactate = measurement.Value;
                }
                else if (intervalLaps.Count >= measurement.Lap.Value)
                {
                    intervalLaps[measurement.Lap.Value - 1].Lactate = measurement.Value;
                }
            }

            return(true);
        }
コード例 #2
0
        public async Task ProcessAsync(IEvent @event)
        {
            var providerUpdateEvent = (IntegrationProviderUpdateEvent)@event;

            _logger.Information(nameof(StravaProviderUpdateEventHandler), providerUpdateEvent.Properties);

            StravaUpdateNotification stravaUpdate =
                ((JObject)providerUpdateEvent.Data.ProviderData)
                .ToObject <StravaUpdateNotification>();

            if (stravaUpdate.ObjectType != "activity")
            {
                _logger.LogWarning($"Unsupported Strava object type '{stravaUpdate.ObjectType}'.");
                return;
            }

            string accessToken = await _stravaAuthenticationService.GetAccessTokenAsync(stravaUpdate.OwnerId);

            if (accessToken is null)
            {
                _logger.LogWarning($"Failed to retrieve Strava access token for owner ID '{stravaUpdate.OwnerId}'.");
                return;
            }

            DetailedActivity activity = await _stravaClient.GetActivityAsync(stravaUpdate.ObjectId, accessToken);

            if (activity.Type != ActivityType.Ride)
            {
                _logger.LogWarning($"Unsupported Strava activity type '{activity.Type}'.");
                return;
            }

            await _fhirClient.EnsurePatientDeviceAsync(providerUpdateEvent.Data.UserId);

            IoMTModel ioMTModel = new BikeRide
            {
                Distance            = activity.Distance.Value,
                Duration            = activity.ElapsedTime.Value,
                DeviceId            = providerUpdateEvent.Data.UserId,
                PatientId           = providerUpdateEvent.Data.UserId,
                MeasurementDateTime = activity.StartDate.Value
            };

            await _iomtDataPublisher.PublishAsync(ioMTModel);
        }
コード例 #3
0
        private List <long> GetIntervalSegments(DetailedActivity activity)
        {
            var intervalLaps = activity.Laps
                               .Where(lap => lap.IsInterval)
                               .Select(
                lap => new
            {
                lap.StartDate,
                EndDate = lap.StartDate.AddSeconds(lap.ElapsedTime)
            })
                               .ToList();

            return(activity.SegmentEfforts
                   .Where(segment => intervalLaps.Any(lap => segment.StartDate >= lap.StartDate && segment.StartDate < lap.EndDate))
                   .Select(segment => segment.Segment.Id)
                   .Distinct()
                   .ToList());
        }
コード例 #4
0
        public static bool TryParseFeelingParameter(this DetailedActivity activity)
        {
            if (activity._FeelingVersion == Version)
            {
                return(false);
            }

            activity._FeelingVersion = Version;

            var parameter = GetFeelingFromDescription(activity.Description) ?? GetFeelingFromDescription(activity.PrivateNote);

            if (parameter == null)
            {
                return(false);
            }

            activity.Feeling = parameter;
            return(true);
        }
コード例 #5
0
        public static async Task GetActivityByIdAsync(int id)
        {
            //Получаем токен
            TokenModel token;

            token = Token.RenewToken();
            Configuration.ApiKey.Add("access_token", token.Access_token);
            Configuration.ApiKey.Add("refresh_token", token.Refresh_token);
            Console.WriteLine(token.Access_token);

            //Подключаемся к БД
            string         connString    = "mongodb://192.168.1.200:27017";
            MongoClient    client        = new(connString);
            IMongoDatabase mongoDatabase = client.GetDatabase("strava");

            var apiInst          = new ActivitiesApi();
            var includeAllEffots = true;

            try
            {
                DetailedActivity result = apiInst.GetActivityById(id, includeAllEffots);
                var collection          = mongoDatabase.GetCollection <GetActivity>("activities");

                GetActivity getActivity = new()
                {
                    Id               = result.Id,
                    Name             = result.Name,
                    Distance         = result.Distance,
                    Moving_time      = result.MovingTime,
                    Start_date_local = result.StartDateLocal,
                    Average_speed    = result.AverageSpeed,
                    Max_speed        = result.MaxSpeed,
                    Average_temp     = result.AverageSpeed,
                    Workout_type     = result.WorkoutType,
                    Calories         = result.Calories
                };
                await collection.InsertOneAsync(getActivity);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
コード例 #6
0
        private static bool CheckIfLapIsInterval(DetailedActivity activity, int i)
        {
            var currentLap = activity.Laps[i];

            if (currentLap.IsInterval)
            {
                return(true);
            }

            var similarIntervalLaps = activity.Laps
                                      .Where(lap => lap.IsInterval)
                                      .Where(lap => Math.Abs(lap.Distance - currentLap.Distance) < currentLap.Distance * 0.5)
                                      .Where(lap => Math.Abs(lap.AverageSpeed - currentLap.AverageSpeed) < currentLap.AverageSpeed * 0.1)
                                      .ToList();


            if (similarIntervalLaps.Any() && HasNonIntervalNeighbor(i, activity.Laps))
            {
                return(true);
            }

            return(false);
        }
コード例 #7
0
        private bool IsSimilarActivity(DetailedActivity activity1, DetailedActivity activity2)
        {
            if (activity1.StartLatlng?.Count != 2 || activity2.StartLatlng?.Count != 2)
            {
                return(false);
            }

            if (Math.Abs(activity1.StartLatlng[0] - activity2.StartLatlng[0]) > 1 || Math.Abs(activity1.StartLatlng[1] - activity2.StartLatlng[1]) > 1)
            {
                return(false);
            }

            var lapsDifference         = Math.Abs(activity1.Laps.Count(lap => lap.IsInterval) - activity2.Laps.Count(lap => lap.IsInterval));
            var lapsDistanceDifference = Math.Abs(
                activity1.Laps.Where(lap => lap.IsInterval).Average(lap => lap.Distance) -
                activity2.Laps.Where(lap => lap.IsInterval).Average(lap => lap.Distance)) /
                                         100;
            var segmentsDifference = GetSegmentDifference(GetIntervalSegments(activity1), GetIntervalSegments(activity2));

            var differenceScore = lapsDifference + lapsDistanceDifference + segmentsDifference;

            return(differenceScore < 10);
        }
コード例 #8
0
        private IReadOnlyList <double> GetLactate(DetailedActivity activity)
        {
            var result = new List <double>();

            if (activity.Lactate.HasValue)
            {
                result.Add(activity.Lactate.Value);
            }

            if (activity.Laps == null)
            {
                return(result);
            }

            foreach (var lap in activity.Laps)
            {
                if (lap.Lactate.HasValue)
                {
                    result.Add(lap.Lactate.Value);
                }
            }

            return(result);
        }
コード例 #9
0
ファイル: MainForm.cs プロジェクト: frisa/Runtastic2Strava
        private void btnLoad_Click(object sender, EventArgs e)
        {
            _token = CStravaImporter.RenewToken(tbClient.Text, tbClientSecret.Text, tbRefreshToken.Text);
            if (Configuration.ApiKey.ContainsKey("access_token"))
            {
                Configuration.ApiKey.Remove("access_token");
            }
            Configuration.ApiKey.Add("access_token", _token.access_token);

            if (Configuration.ApiKey.ContainsKey("refresh_token"))
            {
                Configuration.ApiKey.Remove("refresh_token");
            }
            Configuration.ApiKey.Add("refresh_token", _token.refresh_token);
            log("Tokens Loaded");

            _blRuntasticActivities = new BindingList <RuntasticActivity>();
            dgvImport.DataSource   = _blRuntasticActivities;
            int index = 0;

            String sPathSessions      = Path.Combine(tbPath.Text, "Sport-sessions");
            String sPathGps           = Path.Combine(sPathSessions, "GPS-data");
            String sPathSessionAlbums = Path.Combine(tbPath.Text, "Photos\\Images-meta-data\\Sport-session-albums");
            String sPathPhotos        = Path.Combine(tbPath.Text, "Photos");

            foreach (string sSessionFilePath in Directory.GetFiles(sPathSessions))
            {
                String            sSessionFile   = Path.GetFileName(sSessionFilePath);
                String            sGpsFile       = Path.Combine(sPathGps, Path.ChangeExtension(sSessionFile, "gpx"));
                String            sPhotoFile     = Path.ChangeExtension(sSessionFile, "json");
                RuntasticActivity ac             = Newtonsoft.Json.JsonConvert.DeserializeObject <RuntasticActivity>(System.IO.File.ReadAllText(sSessionFilePath), new EpochDateTimeConverter());
                DetailedActivity  resultActivity = null;
                index++;
                if (File.Exists(sGpsFile))
                {
                    try
                    {
                        Upload resultUpload;
                        var    file      = File.OpenRead(sGpsFile);
                        var    apiUpload = new UploadsApi();
                        resultUpload = apiUpload.CreateUpload(file, null, null, null, null, "gpx", null);
                        int timeOut = 0;
                        do
                        {
                            System.Threading.Thread.Sleep(2000);
                            resultUpload = apiUpload.GetUploadById(resultUpload.Id);
                            System.Windows.Forms.Application.DoEvents();
                            if (30 < timeOut++)
                            {
                                MessageBox.Show("Timeout");
                                break;
                            }
                        }while ((resultUpload.Status.ToString().Contains("Your activity is still being processed.")));
                        if ((resultUpload.Status.ToString().Contains("Your activity is ready.")))
                        {
                            logActivity(ac, Color.Green);
                            log(index.ToString() + " -> " + resultUpload.ActivityId + " uploaded form:" + Path.GetFileName(file.Name));
                        }
                        else
                        {
                            logActivity(ac, Color.Red);
                            log(index.ToString() + " -> " + resultUpload.Status.ToString() + " " + Path.GetFileName(file.Name));
                        }
                        System.Windows.Forms.Application.DoEvents();
                    }
                    catch (Exception except)
                    {
                        log(index.ToString() + " -> " + except.Message);
                        logActivity(ac, Color.Red);
                    }
                }
                else
                {
                    try
                    {
                        ActivitiesApi apiActivities  = new ActivitiesApi();
                        var           name           = "Activity " + index;
                        var           type           = "Run";
                        var           startDateLocal = ac.created_at.ToString("yyyy-MM-dd-THH:mm:ssZ");
                        var           elapsedTime    = (int)(ac.duration / 1000);
                        var           distance       = ac.distance;
                        var           photoIds       = "";
                        resultActivity = apiActivities.CreateActivity(name, type, startDateLocal, elapsedTime,
                                                                      null, distance, null, photoIds, null);
                        logActivity(ac, Color.Green);
                        log(index.ToString() + " -> " + resultActivity.Name + " done manualy");
                    }
                    catch (Exception except)
                    {
                        log(index.ToString() + " -> " + except.Message);
                        logActivity(ac, Color.Red);
                    }
                }
            }
        }
コード例 #10
0
        public static bool TryTagIntervalLaps(this DetailedActivity activity)
        {
            if (activity._IntervalVersion == Version)
            {
                return(false);
            }

            activity._IntervalVersion = Version;
            activity._LactateVersion  = null;

            if (activity.Laps == null)
            {
                return(true);
            }

            foreach (var lap in activity.Laps)
            {
                lap.IsInterval = false;
            }

            for (var i = 1; i < activity.Laps.Count - 1; i++)
            {
                if (!IsIntervalLap(i, activity.Laps, true))
                {
                    continue;
                }

                var similarLaps = GetSimilarLaps(i, activity.Laps, 0.1);

                if (similarLaps.Count < 2)
                {
                    continue;
                }

                foreach (var similarLap in similarLaps)
                {
                    similarLap.Lap.IsInterval = true;
                }
            }

            // Remove short interval laps that has no similar interval laps based on distance.
            foreach (var lap in activity.Laps.Where(lap => lap.IsInterval))
            {
                if (lap.Distance < 500 && activity.Laps.Count(lap2 => lap2.IsInterval && Math.Abs(lap.Distance - lap2.Distance) < 200) <= 1)
                {
                    lap.IsInterval = false;
                }
            }

            // Remove first lap if it's slower than the rest of the intervals by a given margin.
            if (activity.Laps.Count > 1 && activity.Laps[0].IsInterval)
            {
                var isSlowestLapByMargin =
                    activity.Laps[0].AverageSpeed < activity.Laps.Skip(1).Where(lap => lap.IsInterval).Min(lap => lap.AverageSpeed) * 0.99;

                if (isSlowestLapByMargin)
                {
                    activity.Laps[0].IsInterval = false;
                }
            }

            if (IsAutoLapOrSimilar(activity.Laps))
            {
                foreach (var lap in activity.Laps)
                {
                    lap.IsInterval = false;
                }
            }

            for (var i = 0; i < activity.Laps.Count; i++)
            {
                var matchingLap = GetClosestMatchingLap(i, activity.Laps);

                if (matchingLap != null && matchingLap.IsInterval && CheckIfLapIsInterval(activity, i))
                {
                    activity.Laps[i].IsInterval = true;
                }
            }

            return(true);
        }