Ejemplo n.º 1
0
 /// <summary>        
 /// Set SubSport field</summary>
 /// <param name="subSport_">Nullable field value to be set</param>      
 public void SetSubSport(SubSport? subSport_)
 {
     SetFieldValue(1, 0, subSport_, Fit.SubfieldIndexMainField);
 }
Ejemplo n.º 2
0
        public ICollection <LapMesg> GetWorkoutLaps(WorkoutSamples workoutSamples, Dynastream.Fit.DateTime startTime, Sport sport, SubSport subSport)
        {
            var stepsAndLaps = new List <LapMesg>();

            if (workoutSamples is null)
            {
                return(stepsAndLaps);
            }

            ushort stepIndex    = 0;
            var    speedMetrics = GetSpeedSummary(workoutSamples);

            if (workoutSamples.Segment_List.Any())
            {
                var totalElapsedTime = 0;
                foreach (var segment in workoutSamples.Segment_List)
                {
                    var lapStartTime = new Dynastream.Fit.DateTime(startTime);
                    lapStartTime.Add(segment.Start_Time_Offset);

                    totalElapsedTime += segment.Length;

                    var lapMesg = new LapMesg();
                    lapMesg.SetStartTime(lapStartTime);
                    lapMesg.SetMessageIndex(stepIndex);
                    lapMesg.SetEvent(Event.Lap);
                    lapMesg.SetLapTrigger(LapTrigger.Time);
                    lapMesg.SetSport(sport);
                    lapMesg.SetSubSport(subSport);

                    lapMesg.SetTotalElapsedTime(segment.Length);
                    lapMesg.SetTotalTimerTime(segment.Length);

                    var startIndex          = segment.Start_Time_Offset;
                    var endIndex            = segment.Start_Time_Offset + segment.Length;
                    var lapDistanceInMeters = 0f;
                    for (int i = startIndex; i < endIndex; i++)
                    {
                        if (speedMetrics is object && i < speedMetrics.Values.Length)
                        {
                            var currentSpeedInMPS = ConvertToMetersPerSecond(speedMetrics.GetValue(i), workoutSamples);
                            lapDistanceInMeters += 1 * currentSpeedInMPS;
                        }
                    }

                    lapMesg.SetTotalDistance(lapDistanceInMeters);
                    stepsAndLaps.Add(lapMesg);

                    stepIndex++;
                }
            }

            return(stepsAndLaps);
        }
        public ICollection <LapMesg> GetLapsBasedOnDistance(WorkoutSamples workoutSamples, Dynastream.Fit.DateTime startTime, Sport sport, SubSport subSport)
        {
            using var tracing = Tracing.Trace($"{nameof(FitConverter)}.{nameof(GetLapsBasedOnDistance)}")
                                .WithTag(TagKey.Format, FileFormat.Fit.ToString());

            var stepsAndLaps = new List <LapMesg>();

            if (workoutSamples is null)
            {
                return(stepsAndLaps);
            }

            var speedMetrics = GetSpeedSummary(workoutSamples);

            if (speedMetrics is null)
            {
                return(stepsAndLaps);
            }

            var speedUnit = GetDistanceUnit(speedMetrics?.Display_Unit);
            var lapMeters = 0;

            switch (speedUnit)
            {
            case DistanceUnit.Kilometers: lapMeters = 1000; break;

            default: lapMeters = 1600; break;
            }

            LapMesg lap                 = null;
            ushort  stepIndex           = 0;
            var     lapDistanceInMeters = 0f;
            float   lapLengthSeconds    = 0;

            for (var secondsSinceStart = 0; secondsSinceStart < speedMetrics.Values.Length; secondsSinceStart++)
            {
                if (lap is null || lap.GetTotalElapsedTime() is not null)
                {
                    // Start new Lap
                    var lapStartTime = new Dynastream.Fit.DateTime(startTime);
                    lapStartTime.Add(secondsSinceStart);

                    lap = new LapMesg();
                    lap.SetStartTime(lapStartTime);
                    lap.SetMessageIndex(stepIndex);
                    lap.SetEvent(Event.Lap);
                    lap.SetLapTrigger(LapTrigger.Time);
                    lap.SetSport(sport);
                    lap.SetSubSport(subSport);

                    lapLengthSeconds    = 0;
                    lapDistanceInMeters = 0f;
                }

                var currentSpeedInMPS = ConvertToMetersPerSecond(speedMetrics.GetValue(secondsSinceStart), workoutSamples);
                lapDistanceInMeters += 1 * currentSpeedInMPS;
                lapLengthSeconds++;

                if (lapDistanceInMeters >= lapMeters || secondsSinceStart == speedMetrics.Values.Length - 1)
                {
                    lap.SetTotalElapsedTime(lapLengthSeconds);
                    lap.SetTotalTimerTime(lapLengthSeconds);
                    lap.SetTotalDistance(lapDistanceInMeters);
                    stepsAndLaps.Add(lap);
                    stepIndex++;
                }
            }

            return(stepsAndLaps);
        }
Ejemplo n.º 4
0
        private Dictionary <int, Tuple <WorkoutStepMesg, LapMesg> > GetWorkoutStepsAndLaps(WorkoutSamples workoutSamples, Dynastream.Fit.DateTime startTime, Sport sport, SubSport subSport)
        {
            var stepsAndLaps = new Dictionary <int, Tuple <WorkoutStepMesg, LapMesg> >();

            if (workoutSamples is null)
            {
                return(stepsAndLaps);
            }

            var cadenceTargets = workoutSamples.Target_Performance_Metrics?.Target_Graph_Metrics?.FirstOrDefault(w => w.Type == "cadence")?.Graph_Data;

            if (cadenceTargets is null)
            {
                return(stepsAndLaps);
            }

            uint            previousCadenceLower = 0;
            uint            previousCadenceUpper = 0;
            ushort          stepIndex            = 0;
            var             duration             = 0;
            float           lapDistanceInMeters  = 0;
            WorkoutStepMesg workoutStep          = null;
            LapMesg         lapMesg      = null;
            var             speedMetrics = GetSpeedSummary(workoutSamples);

            foreach (var secondSinceStart in workoutSamples.Seconds_Since_Pedaling_Start)
            {
                var index = secondSinceStart <= 0 ? 0 : secondSinceStart - 1;
                duration++;

                if (speedMetrics is object && index < speedMetrics.Values.Length)
                {
                    var currentSpeedInMPS = ConvertToMetersPerSecond(speedMetrics.GetValue(index), workoutSamples);
                    lapDistanceInMeters += 1 * currentSpeedInMPS;
                }

                var currentCadenceLower = index < cadenceTargets.Lower.Length ? (uint)cadenceTargets.Lower[index] : 0;
                var currentCadenceUpper = index < cadenceTargets.Upper.Length ? (uint)cadenceTargets.Upper[index] : 0;

                if (currentCadenceLower != previousCadenceLower ||
                    currentCadenceUpper != previousCadenceUpper)
                {
                    if (workoutStep != null && lapMesg != null)
                    {
                        workoutStep.SetDurationValue((uint)duration * 1000);                         // milliseconds

                        var lapEndTime = new Dynastream.Fit.DateTime(startTime);
                        lapEndTime.Add(secondSinceStart);
                        lapMesg.SetTotalElapsedTime(duration);
                        lapMesg.SetTotalTimerTime(duration);
                        lapMesg.SetTimestamp(lapEndTime);
                        lapMesg.SetEventType(EventType.Stop);
                        lapMesg.SetTotalDistance(lapDistanceInMeters);

                        stepsAndLaps.Add(stepIndex, new Tuple <WorkoutStepMesg, LapMesg>(workoutStep, lapMesg));
                        stepIndex++;
                        duration            = 0;
                        lapDistanceInMeters = 0;
                    }

                    workoutStep = new WorkoutStepMesg();
                    workoutStep.SetDurationType(WktStepDuration.Time);
                    workoutStep.SetMessageIndex(stepIndex);
                    workoutStep.SetTargetType(WktStepTarget.Cadence);
                    workoutStep.SetCustomTargetValueHigh(currentCadenceUpper);
                    workoutStep.SetCustomTargetValueLow(currentCadenceLower);
                    workoutStep.SetIntensity(currentCadenceUpper > 60 ? Intensity.Active : Intensity.Rest);

                    lapMesg = new LapMesg();
                    var lapStartTime = new Dynastream.Fit.DateTime(startTime);
                    lapStartTime.Add(secondSinceStart);
                    lapMesg.SetStartTime(lapStartTime);
                    lapMesg.SetWktStepIndex(stepIndex);
                    lapMesg.SetMessageIndex(stepIndex);
                    lapMesg.SetEvent(Event.Lap);
                    lapMesg.SetLapTrigger(LapTrigger.Time);
                    lapMesg.SetSport(sport);
                    lapMesg.SetSubSport(subSport);

                    previousCadenceLower = currentCadenceLower;
                    previousCadenceUpper = currentCadenceUpper;
                }
            }

            return(stepsAndLaps);
        }
        public ICollection <LapMesg> GetLaps(PreferredLapType preferredLapType, WorkoutSamples workoutSamples, Dynastream.Fit.DateTime startTime, Sport sport, SubSport subSport)
        {
            using var tracing = Tracing.Trace($"{nameof(FitConverter)}.{nameof(GetLaps)}")
                                .WithTag(TagKey.Format, FileFormat.Fit.ToString());

            if ((preferredLapType == PreferredLapType.Default || preferredLapType == PreferredLapType.Class_Segments) &&
                workoutSamples.Segment_List.Any())
            {
                return(GetLapsBasedOnSegments(workoutSamples, startTime, sport, subSport));
            }

            return(GetLapsBasedOnDistance(workoutSamples, startTime, sport, subSport));
        }