Esempio n. 1
0
        public static IRepeatDuration Create(IRepeatDuration.RepeatDurationType type, RepeatStep parent)
        {
            IRepeatDuration newDuration;

            switch (type)
            {
                case IRepeatDuration.RepeatDurationType.RepeatCount:
                    {
                        newDuration = new RepeatCountDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilTime:
                    {
                        newDuration = new RepeatUntilTimeDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilDistance:
                    {
                        newDuration = new RepeatUntilDistanceDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilHeartRateAbove:
                    {
                        newDuration = new RepeatUntilHeartRateAboveDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilHeartRateBelow:
                    {
                        newDuration = new RepeatUntilHeartRateBelowDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilCalories:
                    {
                        newDuration = new RepeatUntilCaloriesDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilPowerAbove:
                    {
                        newDuration = new RepeatUntilPowerAboveDuration(parent);
                        break;
                    }
                case IRepeatDuration.RepeatDurationType.RepeatUntilPowerBelow:
                    {
                        newDuration = new RepeatUntilPowerBelowDuration(parent);
                        break;
                    }
                default:
                    {
                        Debug.Assert(false);
                        newDuration = null;
                        break;
                    }
            }

            parent.Duration = newDuration;

            return newDuration;
        }
Esempio n. 2
0
        public RepeatStep(Byte numRepeats, Workout parent)
            : base(StepType.Repeat, parent)
        {
            Debug.Assert(numRepeats <= Constants.MaxRepeats);

            RepeatCountDuration repeatCountDuration = new RepeatCountDuration(this);
            repeatCountDuration.RepetitionCount = numRepeats;
            Duration = repeatCountDuration;

            m_StepsToRepeat.Add(new RegularStep(parent));
        }
        public void TestTCXSerialization()
        {
            XmlDocument testDocument = new XmlDocument();
            XmlNode database;
            XmlAttribute attribute;
            Workout placeholderWorkout = new Workout("Test", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            RepeatStep placeholderStep = new RepeatStep(placeholderWorkout);

            // Setup document
            testDocument.AppendChild(testDocument.CreateXmlDeclaration("1.0", "UTF-8", "no"));
            database = testDocument.CreateNode(XmlNodeType.Element, "TrainingCenterDatabase", null);
            testDocument.AppendChild(database);
            attribute = testDocument.CreateAttribute("xmlns", "xsi", GarminFitnessPlugin.Constants.xmlns);
            attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            database.Attributes.Append(attribute);

            // Repeat count
            RepeatCountDuration countDuration = new RepeatCountDuration(placeholderStep);
            placeholderStep.Duration = countDuration;
            countDuration.RepetitionCount = 4;
            countDuration.Serialize(database, "RepeatCountDuration1", testDocument);
            int durationPosition1 = testDocument.InnerXml.IndexOf(repeatCountDurationResult1);
            Assert.GreaterOrEqual(durationPosition1, 0, "Invalid repeat count duration serialization");
            countDuration.RepetitionCount = 10;
            countDuration.Serialize(database, "RepeatCountDuration2", testDocument);
            int durationPosition2 = testDocument.InnerXml.IndexOf(repeatCountDurationResult2);
            Assert.GreaterOrEqual(durationPosition2, 0, "Invalid repeat count duration serialization");
            Assert.Greater(durationPosition2, durationPosition1, "Repeat count durations serialization don't differ");

            // All other repeat durtions should not serialize in TCX
            try
            {
                RepeatUntilCaloriesDuration caloriesDuration = new RepeatUntilCaloriesDuration(placeholderStep);
                caloriesDuration.Serialize(database, "RepeatUntilCaloriesDuration", testDocument);
                Assert.Fail("Repeat until calories should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilDistanceDuration distanceDuration = new RepeatUntilDistanceDuration(placeholderStep);
                distanceDuration.Serialize(database, "RepeatUntilDistanceDuration", testDocument);
                Assert.Fail("Repeat until distance should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilHeartRateAboveDuration hrAboveDuration = new RepeatUntilHeartRateAboveDuration(placeholderStep);
                hrAboveDuration.Serialize(database, "RepeatUntilHRAboveDuration", testDocument);
                Assert.Fail("Repeat until HRAbove should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilHeartRateBelowDuration hrBelowDuration = new RepeatUntilHeartRateBelowDuration(placeholderStep);
                hrBelowDuration.Serialize(database, "RepeatUntilHRBelowDuration", testDocument);
                Assert.Fail("Repeat until HRBelow should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilPowerAboveDuration powerAboveDuration = new RepeatUntilPowerAboveDuration(placeholderStep);
                powerAboveDuration.Serialize(database, "RepeatUntilPowerAboveDuration", testDocument);
                Assert.Fail("Repeat until PowerAbove should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilPowerBelowDuration powerBelowDuration = new RepeatUntilPowerBelowDuration(placeholderStep);
                powerBelowDuration.Serialize(database, "RepeatUntilPowerBelowDuration", testDocument);
                Assert.Fail("Repeat until PowerBelow should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilTimeDuration timeDuration = new RepeatUntilTimeDuration(placeholderStep);
                timeDuration.Serialize(database, "RepeatUntilTimeDuration", testDocument);
                Assert.Fail("Repeat until time should not serialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilCaloriesDuration caloriesDuration = new RepeatUntilCaloriesDuration(placeholderStep);
                caloriesDuration.Serialize(database, "RepeatUntilCaloriesDuration", testDocument);
                Assert.Fail("Repeat until calories should not serialize");
            }
            catch (NotSupportedException)
            {
            }
        }
        public void TestTCXDeserialization()
        {
            XmlDocument testDocument = new XmlDocument();
            XmlNode readNode;
            XmlNode database;
            Workout placeholderWorkout = new Workout("Test", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            RepeatStep placeholderStep = new RepeatStep(placeholderWorkout);

            // Setup document
            testDocument.AppendChild(testDocument.CreateXmlDeclaration("1.0", "UTF-8", "no"));
            database = testDocument.CreateNode(XmlNodeType.Element, "TrainingCenterDatabase", null);
            testDocument.AppendChild(database);
            XmlAttribute attribute = testDocument.CreateAttribute("xmlns", "xsi", GarminFitnessPlugin.Constants.xmlns);
            attribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            database.Attributes.Append(attribute);
            readNode = testDocument.CreateElement("TestNode");
            database.AppendChild(readNode);

            // Repeat count
            readNode.InnerXml = repeatCountDurationResult1;
            RepeatCountDuration countDuration = new RepeatCountDuration(placeholderStep);
            countDuration.Deserialize(readNode.FirstChild);
            Assert.AreEqual(4, countDuration.RepetitionCount, "Invalid number of repetitions deserialized");

            readNode.InnerXml = repeatCountDurationResult2;
            countDuration = new RepeatCountDuration(placeholderStep);
            countDuration.Deserialize(readNode.FirstChild);
            Assert.AreEqual(10, countDuration.RepetitionCount, "Invalid number of repetitions deserialized");

            // All other repeat durtions should not deserialize from TCX
            try
            {
                RepeatUntilCaloriesDuration caloriesDuration = new RepeatUntilCaloriesDuration(placeholderStep);
                caloriesDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until calories should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilDistanceDuration distanceDuration = new RepeatUntilDistanceDuration(placeholderStep);
                distanceDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until distance should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilHeartRateAboveDuration hrAboveDuration = new RepeatUntilHeartRateAboveDuration(placeholderStep);
                hrAboveDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until HRAbove should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilHeartRateBelowDuration hrBelowDuration = new RepeatUntilHeartRateBelowDuration(placeholderStep);
                hrBelowDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until HRBelow should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilPowerAboveDuration powerAboveDuration = new RepeatUntilPowerAboveDuration(placeholderStep);
                powerAboveDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until PowerAbove should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilPowerBelowDuration powerBelowDuration = new RepeatUntilPowerBelowDuration(placeholderStep);
                powerBelowDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until PowerBelow should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilTimeDuration timeDuration = new RepeatUntilTimeDuration(placeholderStep);
                timeDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until time should not deserialize");
            }
            catch (NotSupportedException)
            {
            }

            try
            {
                RepeatUntilCaloriesDuration caloriesDuration = new RepeatUntilCaloriesDuration(placeholderStep);
                caloriesDuration.Deserialize(readNode.FirstChild);
                Assert.Fail("Repeat until calories should not deserialize");
            }
            catch (NotSupportedException)
            {
            }
        }
        public void TestFITSerialization()
        {
            Workout placeholderWorkout = new Workout("Test", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            RepeatStep placeholderStep = new RepeatStep(placeholderWorkout);
            FITMessage serializedMessage = new FITMessage(FITGlobalMessageIds.WorkoutStep);
            FITMessageField messageField;

            // Repeat count
            RepeatCountDuration repeatDuration = new RepeatCountDuration(placeholderStep);
            repeatDuration.RepetitionCount = 3;
            repeatDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat count duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatCount, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat count duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat count duration");
            Assert.AreEqual(3, messageField.GetUInt32(), "Invalid target value in field for repeat count duration");
            serializedMessage.Clear();

            repeatDuration.RepetitionCount = 7;
            repeatDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat count duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatCount, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat count duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat count duration");
            Assert.AreEqual(7, messageField.GetUInt32(), "Invalid target value in field for repeat count duration");
            serializedMessage.Clear();

            // Repeat until calories
            RepeatUntilCaloriesDuration caloriesDuration = new RepeatUntilCaloriesDuration(placeholderStep);
            caloriesDuration.CaloriesToSpend = 550;
            caloriesDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until calories duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilCalories, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until calories duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until calories duration");
            Assert.AreEqual(550, messageField.GetUInt32(), "Invalid target value in field for repeat until calories duration");
            serializedMessage.Clear();

            caloriesDuration.CaloriesToSpend = 750;
            caloriesDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until calories duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilCalories, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until calories duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until calories duration");
            Assert.AreEqual(750, messageField.GetUInt32(), "Invalid target value in field for repeat until calories duration");
            serializedMessage.Clear();

            // Repeat until distance
            RepeatUntilDistanceDuration distanceDuration = new RepeatUntilDistanceDuration(1, Length.Units.Kilometer, placeholderStep);
            distanceDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until distance duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilDistance, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until distance duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until distance duration");
            Assert.AreEqual(100000, messageField.GetUInt32(), "Invalid target value in field for repeat until distance duration");
            serializedMessage.Clear();

            distanceDuration = new RepeatUntilDistanceDuration(1, Length.Units.Mile, placeholderStep);
            distanceDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until distance duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilDistance, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until distance duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until distance duration");
            Assert.AreEqual(160934, messageField.GetUInt32(), "Invalid target value in field for repeat until distance duration");
            serializedMessage.Clear();

            // Repeat until HRAbove
            RepeatUntilHeartRateAboveDuration hrAboveDuration = new RepeatUntilHeartRateAboveDuration(placeholderStep);
            hrAboveDuration.IsPercentageMaxHeartRate = false;
            hrAboveDuration.MaxHeartRate = 160;
            hrAboveDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until HRAbove duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilHeartRateGreaterThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until HRAbove duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until HRAbove duration");
            Assert.AreEqual(260, messageField.GetUInt32(), "Invalid target value in field for repeat until HRAbove duration");
            serializedMessage.Clear();

            hrAboveDuration.IsPercentageMaxHeartRate = true;
            hrAboveDuration.MaxHeartRate = 80;
            hrAboveDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until HRAbove duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilHeartRateGreaterThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until HRAbove duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until HRAbove duration");
            Assert.AreEqual(80, messageField.GetUInt32(), "Invalid target value in field for repeat until HRAbove duration");
            serializedMessage.Clear();

            // Repeat until HRBelow
            RepeatUntilHeartRateBelowDuration hrBelowDuration = new RepeatUntilHeartRateBelowDuration(placeholderStep);
            hrBelowDuration.IsPercentageMaxHeartRate = false;
            hrBelowDuration.MinHeartRate = 150;
            hrBelowDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until HRBelow duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilHeartRateLessThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until HRBelow duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until HRBelow duration");
            Assert.AreEqual(250, messageField.GetUInt32(), "Invalid target value in field for repeat until HRBelow duration");
            serializedMessage.Clear();

            hrBelowDuration.IsPercentageMaxHeartRate = true;
            hrBelowDuration.MinHeartRate = 70;
            hrBelowDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until HRBelow duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilHeartRateLessThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until HRBelow duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until HRBelow duration");
            Assert.AreEqual(70, messageField.GetUInt32(), "Invalid target value in field for repeat until HRBelow duration");
            serializedMessage.Clear();

            // Repeat until PowerAbove
            RepeatUntilPowerAboveDuration powerAboveDuration = new RepeatUntilPowerAboveDuration(placeholderStep);
            powerAboveDuration.IsPercentFTP = false;
            powerAboveDuration.MaxPower = 250;
            powerAboveDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until PowerAbove duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilPowerGreaterThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until PowerAbove duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until PowerAbove duration");
            Assert.AreEqual(1250, messageField.GetUInt32(), "Invalid target value in field for repeat until PowerAbove duration");
            serializedMessage.Clear();

            powerAboveDuration.IsPercentFTP = true;
            powerAboveDuration.MaxPower = 100;
            powerAboveDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until PowerAbove duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilPowerGreaterThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until PowerAbove duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until PowerAbove duration");
            Assert.AreEqual(100, messageField.GetUInt32(), "Invalid target value in field for repeat until PowerAbove duration");
            serializedMessage.Clear();

            // Repeat until HRBelow
            RepeatUntilPowerBelowDuration powerBelowDuration = new RepeatUntilPowerBelowDuration(placeholderStep);
            powerBelowDuration.IsPercentFTP = false;
            powerBelowDuration.MinPower = 150;
            powerBelowDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until PowerBelow duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilPowerLessThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until PowerBelow duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until PowerBelow duration");
            Assert.AreEqual(1150, messageField.GetUInt32(), "Invalid target value in field for repeat until PowerBelow duration");
            serializedMessage.Clear();

            powerBelowDuration.IsPercentFTP = true;
            powerBelowDuration.MinPower = 270;
            powerBelowDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until PowerBelow duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilPowerLessThan, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until PowerBelow duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until PowerBelow duration");
            Assert.AreEqual(270, messageField.GetUInt32(), "Invalid target value in field for repeat until PowerBelow duration");
            serializedMessage.Clear();

            // Repeat until time
            RepeatUntilTimeDuration timeDuration = new RepeatUntilTimeDuration(placeholderStep);
            timeDuration.TimeInSeconds = 600;
            timeDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until time duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilTime, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until time duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until time duration");
            Assert.AreEqual(600000, messageField.GetUInt32(), "Invalid target value in field for repeat until time duration");
            serializedMessage.Clear();

            timeDuration.TimeInSeconds = 350;
            timeDuration.FillFITStepMessage(serializedMessage);
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            Assert.IsNotNull(messageField, "Duration type field not serialized for repeat until time duration");
            Assert.AreEqual(FITWorkoutStepDurationTypes.RepeatUntilTime, (FITWorkoutStepDurationTypes)messageField.GetEnum(), "Invalid duration type in field for repeat until time duration");
            messageField = serializedMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            Assert.IsNotNull(messageField, "Target value field not serialized for repeat until time duration");
            Assert.AreEqual(350000, messageField.GetUInt32(), "Invalid target value in field for repeat until time duration");
            serializedMessage.Clear();
        }
Esempio n. 6
0
        public static IRepeatDuration Create(FITMessage stepMessage, RepeatStep parent)
        {
            FITMessageField durationTypeField = stepMessage.GetField((Byte)FITWorkoutStepFieldIds.DurationType);
            FITMessageField targetField = stepMessage.GetField((Byte)FITWorkoutStepFieldIds.TargetValue);
            IRepeatDuration newDuration = new RepeatCountDuration(parent);

            if (durationTypeField == null)
            {
                throw new FITParserException("Missing repeat duration type field");
            }
            else if (targetField == null)
            {
                throw new FITParserException("Missing repeat target value field");
            }
            else
            {
                switch ((FITWorkoutStepDurationTypes)durationTypeField.GetEnum())
                {
                    case FITWorkoutStepDurationTypes.RepeatCount:
                        {
                            RepeatCountDuration repeatDuration = (RepeatCountDuration)Create(IRepeatDuration.RepeatDurationType.RepeatCount, parent);
                            repeatDuration.RepetitionCount = (Byte)targetField.GetUInt32();
                            newDuration = repeatDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilTime:
                        {
                            RepeatUntilTimeDuration timeDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilTime, parent) as RepeatUntilTimeDuration;
                            timeDuration.TimeInSeconds = (UInt16)(targetField.GetUInt32() / 1000.0f);
                            newDuration = timeDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilDistance:
                        {
                            RepeatUntilDistanceDuration distanceDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilDistance, parent) as RepeatUntilDistanceDuration;
                            distanceDuration.SetDistanceInBaseUnit(Length.Convert(targetField.GetUInt32(), Length.Units.Centimeter, distanceDuration.BaseUnit));
                            newDuration = distanceDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilHeartRateGreaterThan:
                        {
                            RepeatUntilHeartRateAboveDuration hrDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilHeartRateAbove, parent) as RepeatUntilHeartRateAboveDuration;
                            UInt32 hrValue = targetField.GetUInt32();

                            if (hrValue >= 100)
                            {
                                hrDuration.IsPercentageMaxHeartRate = false;
                                hrDuration.MaxHeartRate = (Byte)(hrValue - 100);
                            }
                            else
                            {
                                hrDuration.IsPercentageMaxHeartRate = true;
                                hrDuration.MaxHeartRate = (Byte)hrValue;
                            }
                            newDuration = hrDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilHeartRateLessThan:
                        {
                            RepeatUntilHeartRateBelowDuration hrDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilHeartRateBelow, parent) as RepeatUntilHeartRateBelowDuration;
                            UInt32 hrValue = targetField.GetUInt32();

                            if (hrValue >= 100)
                            {
                                hrDuration.IsPercentageMaxHeartRate = false;
                                hrDuration.MinHeartRate = (Byte)(hrValue - 100);
                            }
                            else
                            {
                                hrDuration.IsPercentageMaxHeartRate = true;
                                hrDuration.MinHeartRate = (Byte)hrValue;
                            }
                            newDuration = hrDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilCalories:
                        {
                            RepeatUntilCaloriesDuration caloriesDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilCalories, parent) as RepeatUntilCaloriesDuration;
                            caloriesDuration.CaloriesToSpend = (UInt16)targetField.GetUInt32();
                            newDuration = caloriesDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilPowerGreaterThan:
                        {
                            RepeatUntilPowerAboveDuration powerDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilPowerAbove, parent) as RepeatUntilPowerAboveDuration;
                            UInt32 powerValue = targetField.GetUInt32();

                            if (powerValue >= 1000)
                            {
                                powerDuration.IsPercentFTP = false;
                                powerDuration.MaxPower = (UInt16)(powerValue - 1000);
                            }
                            else
                            {
                                powerDuration.IsPercentFTP = true;
                                powerDuration.MaxPower = (UInt16)powerValue;
                            }
                            newDuration = powerDuration;
                            break;
                        }
                    case FITWorkoutStepDurationTypes.RepeatUntilPowerLessThan:
                        {
                            RepeatUntilPowerBelowDuration powerDuration = Create(IRepeatDuration.RepeatDurationType.RepeatUntilPowerBelow, parent) as RepeatUntilPowerBelowDuration;
                            UInt32 powerValue = targetField.GetUInt32();

                            if (powerValue >= 1000)
                            {
                                powerDuration.IsPercentFTP = false;
                                powerDuration.MinPower = (UInt16)(powerValue - 1000);
                            }
                            else
                            {
                                powerDuration.IsPercentFTP = true;
                                powerDuration.MinPower = (UInt16)powerValue;
                            }
                            newDuration = powerDuration;
                            break;
                        }
                }
            }

            parent.Duration = newDuration;

            return newDuration;
        }
Esempio n. 7
0
        public new void Deserialize_V0(Stream stream, DataVersion version)
        {
            // Call base deserialization
            Deserialize(typeof(IStep), stream, version);

            byte[] intBuffer = new byte[sizeof(Int32)];
            Int32 stepCount;

            RepeatCountDuration repeatCountDuration = new RepeatCountDuration(this);
            repeatCountDuration.Deserialize(stream, version);
            Duration = repeatCountDuration;

            stream.Read(intBuffer, 0, sizeof(Int32));
            stepCount = BitConverter.ToInt32(intBuffer, 0);

            // In case the repeat was already registered on the workout
            ParentConcreteWorkout.Steps.RemoveSteps(StepsToRepeat, false);
            m_StepsToRepeat.Clear();
            for (int i = 0; i < stepCount; ++i)
            {
                IStep.StepType type;

                stream.Read(intBuffer, 0, sizeof(Int32));
                type = (IStep.StepType)BitConverter.ToInt32(intBuffer, 0);
                if (type == IStep.StepType.Regular)
                {
                    m_StepsToRepeat.Add(new RegularStep(stream, version, ParentConcreteWorkout));
                }
                else if (type == IStep.StepType.Link)
                {
                    WorkoutLinkStep tempLink = new WorkoutLinkStep(stream, version, ParentConcreteWorkout);

                    if (tempLink.LinkedWorkout != null)
                    {
                        m_StepsToRepeat.Add(tempLink);
                    }
                    else
                    {
                        WorkoutStepsList linkSteps = new WorkoutStepsList(ParentConcreteWorkout);

                        linkSteps.Deserialize(stream, Constants.CurrentVersion);
                        m_StepsToRepeat.AddRange(linkSteps);
                    }
                }
                else
                {
                    m_StepsToRepeat.Add(new RepeatStep(stream, version, ParentConcreteWorkout));
                }
            }
        }
Esempio n. 8
0
        public override void Deserialize(XmlNode parentNode)
        {
            bool repeatsRead = false;
            List<IStep> stepsToRepeat = new List<IStep>();

            base.Deserialize(parentNode);

            for (int i = 0; i < parentNode.ChildNodes.Count; ++i)
            {
                XmlNode child = parentNode.ChildNodes[i];

                if (child.Name == "Repetitions")
                {
                    RepeatCountDuration repeatCountDuration = new RepeatCountDuration(this);

                    repeatCountDuration.Deserialize(child);
                    Duration = repeatCountDuration;
                    repeatsRead = true;
                }
                else if (child.Name == "Child")
                {
                    string stepTypeString = child.Attributes[0].Value;
                    IStep newStep = null;

                    if (stepTypeString == Constants.StepTypeTCXString[(int)IStep.StepType.Regular])
                    {
                        newStep = new RegularStep(ParentConcreteWorkout);
                    }
                    else if (stepTypeString == Constants.StepTypeTCXString[(int)IStep.StepType.Repeat])
                    {
                        newStep = new RepeatStep(ParentConcreteWorkout);
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    stepsToRepeat.Add(newStep);
                    newStep.Deserialize(child);
                }
            }

            if (!repeatsRead || stepsToRepeat.Count == 0)
            {
                throw new GarminFitnessXmlDeserializationException("Information missing in the XML node", parentNode);
            }

            ParentConcreteWorkout.Steps.RemoveSteps(m_StepsToRepeat, false);
            // In case the repeat wasn't yet registered on the workout
            m_StepsToRepeat.Clear();
            for (int i = 0; i < stepsToRepeat.Count; ++i)
            {
                m_StepsToRepeat.Add(stepsToRepeat[i]);
            }
        }