示例#1
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));
                }
            }
        }
        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)
            {
            }
        }
示例#3
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]);
            }
        }