Ejemplo n.º 1
0
        private MemoryActivity ParseActivity(FitnessActivitiesPastModel healthGraphActivity)
        {
            var activityDetails = new MemoryActivity();

            activityDetails.StartTime = healthGraphActivity.StartTime;
            activityDetails.Sport     = healthGraphActivity.Type.ToFitSport();

            // TODO: watch for other samples too!
            var numberOfTimeframes = healthGraphActivity.Path.Count;

            for (var i = 0; i < numberOfTimeframes; i++)
            {
                var timeFrame = new ActivityTimeFrame();
                timeFrame.Timestamp = activityDetails.StartTime.AddSeconds(healthGraphActivity.Distance[i].Timestamp);
                timeFrame.Position  = new Position((float)healthGraphActivity.Path[i].Longitude, (float)healthGraphActivity.Path[i].Latitude,
                                                   (float)healthGraphActivity.Path[i].Altitude);
                if (healthGraphActivity.HeartRate.Count > i)
                {
                    timeFrame.HeartRate = (byte)healthGraphActivity.HeartRate[i].HeartRate;
                }

                activityDetails.AddTimeFrame(timeFrame);
            }

            return(activityDetails);
        }
Ejemplo n.º 2
0
        public async Task <MemoryActivity> DownloadAsync(IActivitySummary activity)
        {
            if (_client == null)
            {
                return(null);
            }

            var pptActivity = activity as PPTActivitySummary;

            if (pptActivity == null)
            {
                return(null);
            }

            var         exercise = pptActivity.Exercise; // PolarPersonalTrainerLib model
            GpxExercise gpsData  = null;

            try
            {
                gpsData = await _client.GetGpsData(exercise);
            }
            catch (PPTException)
            {
                Log.Diagnostics("No GPS data found for activity {0}.", activity.Name);
            }

            return(await Task.Run <MemoryActivity>(() =>
            {
                var samples = new[]
                {
                    exercise.HeartRate.Values.Count,
                    exercise.CadenceValues.Count,
                    exercise.SpeedValues.Count
                };
                var samplesCount = samples.Max();     // number of samples
                var result = new MemoryActivity();
                DateTime timestamp = exercise.StartTime;
                // process samples
                for (int i = 0; i < samplesCount; i++)
                {
                    var timeFrame = new ActivityTimeFrame();
                    timeFrame.Timestamp = timestamp;
                    if (exercise.HeartRate.Values.Count > i)
                    {
                        timeFrame.HeartRate = exercise.HeartRate.Values[i];
                    }
                    if (exercise.CadenceValues.Count > i)
                    {
                        timeFrame.Cadence = exercise.CadenceValues[i];
                    }
                    if (exercise.SpeedValues.Count > i)
                    {
                        timeFrame.Speed = new Speed(exercise.SpeedValues[i], SpeedUnit.KilometerPerHour);
                    }
                    if (gpsData != null)
                    {
                        var trackPoint = gpsData.Track.GetTrackPoint(i);
                        if (trackPoint != null)
                        {
                            timeFrame.Position = new Position(Convert.ToSingle(trackPoint.Longitude),
                                                              Convert.ToSingle(trackPoint.Latitude),
                                                              Convert.ToSingle(trackPoint.Elevation));
                        }
                    }

                    result.AddTimeFrame(timeFrame);
                    timestamp = timestamp.AddSeconds(exercise.RecordingRate);     // time of next sample
                }
                var summary = ActivitySummary.FromActivity(pptActivity);
                result.SetSummary(summary);
                return result;
            }));
        }
Ejemplo n.º 3
0
        public async Task <MemoryActivity> DownloadAsync(IActivitySummary activity)
        {
            var stravaActivity = activity as StravaActivitySummary;

            if (stravaActivity != null) // only Strava activity can be downloaded
            {
                var streams = await _client.Activities.GetActivityStreams(stravaActivity.Activity, StreamType.Time, StreamType.LatLng, StreamType.Watts, StreamType.HeartRate, StreamType.Cadence, StreamType.VelocitySmooth, StreamType.Altitude, StreamType.Moving);

                Debug.WriteLine("Memory allocated for downlading: " + FileHelper.GetSizeString(GC.GetTotalMemory(false)));

                // determine what streams we have
                StravaSharp.Stream timeStream = null, posStream = null, hrStream = null, speedStream = null, powerStream = null, cadenceStream = null, altitudeStream = null, movingStream = null;
                foreach (var stream in streams)
                {
                    if (stream.Type == StreamType.Time)
                    {
                        timeStream = stream;
                    }
                    else if (stream.Type == StreamType.HeartRate)
                    {
                        hrStream = stream;
                    }
                    else if (stream.Type == StreamType.LatLng)
                    {
                        posStream = stream;
                    }
                    else if (stream.Type == StreamType.VelocitySmooth)
                    {
                        speedStream = stream;
                    }
                    else if (stream.Type == StreamType.Cadence)
                    {
                        cadenceStream = stream;
                    }
                    else if (stream.Type == StreamType.Watts)
                    {
                        powerStream = stream;
                    }
                    else if (stream.Type == StreamType.Altitude)
                    {
                        altitudeStream = stream;
                    }
                    else if (stream.Type == StreamType.Moving)
                    {
                        movingStream = stream;
                    }
                    Log.Diagnostics("{0} stream length: {1}", stream.Type, stream.Data.Length);
                }

                return(await Task.Run <MemoryActivity>(() =>
                {
                    // build activity from the streams' frames
                    // pauses are not recognized this way

                    var isMoving = false;
                    var destActivity = new MemoryActivity {
                        Sport = stravaActivity.Sport, Name = stravaActivity.Name
                    };
                    DateTime startDate = stravaActivity.Activity.StartDate;
                    for (int i = 0; i < streams[0].Data.Length; i++)
                    {
                        var timeFrame = new ActivityTimeFrame();
                        timeFrame.Timestamp = startDate.AddSeconds(Convert.ToInt32(timeStream.Data[i]));

                        var moving = Convert.ToBoolean(movingStream.Data[i]);
                        if (isMoving && !moving)
                        {
                            // now stops
                            timeFrame.Type = ActivityTimeFrameType.Stop;
                            destActivity.AddTimeFrame(timeFrame);
                            isMoving = false;
                            continue;
                        }
                        else if (!isMoving && moving)
                        {
                            // now starts moving again
                            timeFrame.Type = ActivityTimeFrameType.Start;
                            destActivity.AddTimeFrame(timeFrame);
                            isMoving = true;
                        }
                        else if (!isMoving && !moving)
                        {
                            continue;     // still standing
                        }
                        if (hrStream != null)
                        {
                            timeFrame.HeartRate = Convert.ToByte(hrStream.Data[i]);
                        }
                        if (posStream != null)
                        {
                            // position is json array
                            var pos = posStream.Data[i] as JArray;
                            if (pos != null)
                            {
                                var p = new SharpGeo.Position();
                                p.Latitude = pos[0].ToObject <float>();
                                p.Longitude = pos[1].ToObject <float>();
                                if (altitudeStream != null)
                                {
                                    p.Altitude = Convert.ToSingle(altitudeStream.Data[i]);
                                }
                                timeFrame.Position = p;
                            }
                        }
                        if (speedStream != null)
                        {
                            timeFrame.Speed = new Speed(Convert.ToSingle(speedStream.Data[i]), SpeedUnit.MeterPerSecond);
                        }
                        if (cadenceStream != null)
                        {
                            timeFrame.Cadence = Convert.ToByte(cadenceStream.Data[i]);
                        }
                        if (powerStream != null)
                        {
                            timeFrame.Power = Convert.ToUInt16(powerStream.Data[i]);
                        }
                        destActivity.AddTimeFrame(timeFrame);
                    }
                    var summary = new Models.ActivitySummary();
                    summary.ElapsedTime = stravaActivity.ElapsedTime;
                    summary.MovingTime = stravaActivity.MovingTime;
                    summary.Sport = stravaActivity.Sport;
                    summary.AvgSpeed = stravaActivity.AvgSpeed;
                    summary.MaxSpeed = stravaActivity.MaxSpeed;
                    summary.AvgHeartRate = stravaActivity.AvgHeartRate;
                    summary.MaxHeartRate = stravaActivity.MaxHeartRate;
                    summary.AvgCadence = stravaActivity.AvgCadence;
                    summary.MaxCadence = stravaActivity.MaxCadence;
                    if (stravaActivity.HasRealPowerData)
                    {
                        summary.AvgPower = stravaActivity.AvgPower;
                        summary.MaxPower = stravaActivity.MaxPower;
                    }
                    summary.Ascent = stravaActivity.Ascent;
                    summary.Descent = stravaActivity.Descent;
                    destActivity.SetSummary(summary);
                    return destActivity;
                }));
            }

            return(null);
        }