Ejemplo n.º 1
0
        public void FillFITMessage(FITMessage workoutMessage)
        {
            FITMessageField capabilities = new FITMessageField((Byte)FITWorkoutFieldIds.Capabilities);
            FITMessageField sportType = new FITMessageField((Byte)FITWorkoutFieldIds.SportType);
            FITMessageField numValidSteps = new FITMessageField((Byte)FITWorkoutFieldIds.NumSteps);
            FITMessageField unknown = new FITMessageField((Byte)FITWorkoutFieldIds.Unknown);
            FITMessageField workoutName = new FITMessageField((Byte)FITWorkoutFieldIds.WorkoutName);

            sportType.SetEnum((Byte)Options.Instance.GetFITSport(Category));
            capabilities.SetUInt32z(32);
            numValidSteps.SetUInt16(StepCount);
            unknown.SetUInt16(0xFFFF);

            workoutMessage.AddField(capabilities);
            if (!String.IsNullOrEmpty(Name))
            {
                workoutName.SetString(Name, (Byte)(Constants.MaxNameLength + 1));
                workoutMessage.AddField(workoutName);
            }
            workoutMessage.AddField(numValidSteps);
            workoutMessage.AddField(unknown);
            workoutMessage.AddField(sportType);
        }
        public void TestRegularStepFITDeserialization()
        {
            Workout placeholderWorkout = new Workout("Test", PluginMain.GetApplication().Logbook.ActivityCategories[0]);
            RegularStep placeholderStep = new RegularStep(placeholderWorkout);
            FITMessage serializedMessage = new FITMessage(FITGlobalMessageIds.WorkoutStep);
            FITMessageField nameField = new FITMessageField((Byte)FITWorkoutStepFieldIds.StepName);
            FITMessageField intensityField = new FITMessageField((Byte)FITWorkoutStepFieldIds.Intensity);
            FITMessageField durationTypeField = new FITMessageField((Byte)FITWorkoutStepFieldIds.DurationType);
            FITMessageField durationValueField = new FITMessageField((Byte)FITWorkoutStepFieldIds.DurationValue);
            FITMessageField targetTypeField = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetType);
            FITMessageField targetValueField = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetValue);
            FITMessageField targetLowRangeField = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetCustomValueLow);
            FITMessageField targetHighRangeField = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetCustomValueHigh);

            // Setup message
            serializedMessage.AddField(nameField);
            serializedMessage.AddField(intensityField);
            serializedMessage.AddField(durationTypeField);
            serializedMessage.AddField(durationValueField);
            serializedMessage.AddField(targetTypeField);
            serializedMessage.AddField(targetValueField);
            serializedMessage.AddField(targetLowRangeField);
            serializedMessage.AddField(targetHighRangeField);
            durationTypeField.SetEnum((Byte)FITWorkoutStepDurationTypes.Calories);
            durationValueField.SetUInt32(456);
            targetTypeField.SetEnum((Byte)FITWorkoutStepTargetTypes.Cadence);
            targetValueField.SetUInt32(0);
            targetLowRangeField.SetUInt32(80);
            targetHighRangeField.SetUInt32(90);

            // Active step
            nameField.SetString("TestStep1");
            intensityField.SetEnum((Byte)FITWorkoutStepIntensity.Active);
            placeholderStep.DeserializeFromFIT(serializedMessage);
            Assert.AreEqual("TestStep1", placeholderStep.Name, "Name not properly FIT deserialized for active step");
            Assert.AreEqual(RegularStep.StepIntensity.Active, placeholderStep.Intensity, "Intensity not properly FIT deserialized for active step");

            // Rest step
            nameField.SetString("TestStep2");
            intensityField.SetEnum((Byte)FITWorkoutStepIntensity.Rest);
            placeholderStep.DeserializeFromFIT(serializedMessage);
            Assert.AreEqual("TestStep2", placeholderStep.Name, "Name not properly FIT deserialized for rest step");
            Assert.AreEqual(RegularStep.StepIntensity.Rest, placeholderStep.Intensity, "Intensity not properly FIT deserialized for rest step");

            // Warmup step
            nameField.SetString("TestStep3");
            intensityField.SetEnum((Byte)FITWorkoutStepIntensity.Warmup);
            placeholderStep.DeserializeFromFIT(serializedMessage);
            Assert.AreEqual("TestStep3", placeholderStep.Name, "Name not properly FIT deserialized for warmup step");
            Assert.AreEqual(RegularStep.StepIntensity.Warmup, placeholderStep.Intensity, "Intensity not properly FIT deserialized for warmup step");

            // Cooldown step
            nameField.SetString("TestStep4");
            intensityField.SetEnum((Byte)FITWorkoutStepIntensity.Cooldown);
            placeholderStep.DeserializeFromFIT(serializedMessage);
            Assert.AreEqual("TestStep4", placeholderStep.Name, "Name not properly FIT deserialized for cooldown step");
            Assert.AreEqual(RegularStep.StepIntensity.Cooldown, placeholderStep.Intensity, "Intensity not properly FIT deserialized for cooldown step");

            // Duration & target test
            Assert.IsTrue(placeholderStep.Duration is CaloriesDuration, "Duration not properly FIT deserialized in regular step");
            Assert.IsTrue(placeholderStep.Target is BaseCadenceTarget, "Target not properly FIT deserialized in regular step");
        }
        public void TestFITDeserialization()
        {
            ILogbook logbook = PluginMain.GetApplication().Logbook;
            Workout placeholderWorkout = null;
            FITMessage serializedMessage = new FITMessage(FITGlobalMessageIds.Workout);
            FITMessageField nameField = new FITMessageField((Byte)FITWorkoutFieldIds.WorkoutName);
            FITMessageField numStepsField = new FITMessageField((Byte)FITWorkoutFieldIds.NumSteps);
            FITMessageField categoryField = new FITMessageField((Byte)FITWorkoutFieldIds.SportType);

            try
            {
                placeholderWorkout = WorkoutImporter.ImportWorkoutFromMessage(serializedMessage, logbook.ActivityCategories[0]);
                Assert.Fail("Workout without name was FIT deserialized");
            }
            catch (FITParserException)
            {
            }

            nameField.SetString(String.Empty);
            serializedMessage.AddField(nameField);

            try
            {
                placeholderWorkout = WorkoutImporter.ImportWorkoutFromMessage(serializedMessage, logbook.ActivityCategories[0]);
                Assert.Fail("Workout without step count was FIT deserialized");
            }
            catch (FITParserException)
            {
            }

            numStepsField.SetUInt16(0);
            serializedMessage.AddField(numStepsField);

            // Name
            nameField.SetString("TestName");
            placeholderWorkout = WorkoutImporter.ImportWorkoutFromMessage(serializedMessage, logbook.ActivityCategories[0]);
            Assert.AreEqual("TestName", placeholderWorkout.Name, "Workout name not properly FIT deserialized");
            GarminWorkoutManager.Instance.RemoveWorkout(placeholderWorkout);

            nameField.SetString("Workout2");
            placeholderWorkout = WorkoutImporter.ImportWorkoutFromMessage(serializedMessage, logbook.ActivityCategories[0]);
            Assert.AreEqual("Workout2", placeholderWorkout.Name, "Workout name not properly FIT deserialized");
            GarminWorkoutManager.Instance.RemoveWorkout(placeholderWorkout);

            // No need to test category, we ignore them in the application at this time (TODO)

            // Number of steps is tested in the steps list test since it requires multiple messages

            // Do not test steps deserialization here, it has it's own test
        }
Ejemplo n.º 4
0
        public virtual void SerializeToFIT(Stream stream, bool serializeDefiniton)
        {
            FITMessage message = new FITMessage(FITGlobalMessageIds.WorkoutStep);
            FITMessageField stepId = new FITMessageField((Byte)FITWorkoutStepFieldIds.MessageIndex);
            FITMessageField durationType = new FITMessageField((Byte)FITWorkoutStepFieldIds.DurationType);
            FITMessageField durationValue = new FITMessageField((Byte)FITWorkoutStepFieldIds.DurationValue);
            FITMessageField intensity = new FITMessageField((Byte)FITWorkoutStepFieldIds.Intensity);
            FITMessageField stepName = new FITMessageField((Byte)FITWorkoutStepFieldIds.StepName);
            FITMessageField targetHigh = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetCustomValueHigh);
            FITMessageField targetLow = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetCustomValueLow);
            FITMessageField targetType = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetType);
            FITMessageField targetValue = new FITMessageField((Byte)FITWorkoutStepFieldIds.TargetValue);

            // Fill in invalid data
            durationType.SetEnum((Byte)0xFF);
            durationValue.SetUInt32(0xFFFFFFFF);
            intensity.SetEnum((Byte)0xFF);
            stepName.SetString("", (Byte)(Constants.MaxNameLength + 1));
            targetHigh.SetUInt32(0xFFFFFFFF);
            targetLow.SetUInt32(0xFFFFFFFF);
            targetType.SetEnum((Byte)0xFF);
            targetValue.SetUInt32(0xFFFFFFFF);

            message.AddField(stepName);
            message.AddField(durationValue);
            message.AddField(targetValue);
            message.AddField(targetLow);
            message.AddField(targetHigh);
            message.AddField(stepId);
            message.AddField(durationType);
            message.AddField(targetType);
            message.AddField(intensity);

            FillFITStepMessage(message);

            message.Serialize(stream, serializeDefiniton);
        }
Ejemplo n.º 5
0
        public void SerializeToFITSettings(Stream outputStream)
        {
            FITMessage userProfileMessage = new FITMessage(FITGlobalMessageIds.UserProfile);
            FITMessageField profileName = new FITMessageField((Byte)FITUserProfileFieldIds.FriendlyName);
            FITMessageField gender = new FITMessageField((Byte)FITUserProfileFieldIds.Gender);
            FITMessageField weight = new FITMessageField((Byte)FITUserProfileFieldIds.Weight);
            FITMessageField restingHR = new FITMessageField((Byte)FITUserProfileFieldIds.RestingHR);

            profileName.SetString(ProfileName, (Byte)(Constants.MaxNameLength + 1));
            userProfileMessage.AddField(profileName);

            gender.SetEnum(IsMale ? (Byte)FITGenders.Male : (Byte)FITGenders.Female);
            userProfileMessage.AddField(gender);

            weight.SetUInt16((UInt16)(WeightInKilos * 10));
            userProfileMessage.AddField(weight);

            restingHR.SetUInt8(RestingHeartRate);
            userProfileMessage.AddField(restingHR);

            userProfileMessage.Serialize(outputStream);

            (m_ActivityProfiles[(int)GarminCategories.Biking] as GarminBikingActivityProfile).SerializeBikeProfiles(outputStream);
        }
        public virtual void SerializeFITSpeedZones(Stream outputStream)
        {
            UInt16 i = 0;

            foreach (GarminFitnessNamedSpeedZone zone in m_SpeedZones)
            {
                FITMessage zonesTargetMessage = new FITMessage(FITGlobalMessageIds.SpeedZones);
                FITMessageField index = new FITMessageField((Byte)FITSpeedZonesFieldIds.MessageIndex);
                FITMessageField zoneName = new FITMessageField((Byte)FITSpeedZonesFieldIds.Name);
                FITMessageField highSpeed = new FITMessageField((Byte)FITSpeedZonesFieldIds.HighSpeed);

                if (i == 0)
                {
                    index.SetUInt16(i);
                    zoneName.SetString(zone.Name, (Byte)(Constants.MaxNameLength + 1));
                    highSpeed.SetUInt16((UInt16)(zone.Low * 1000));

                    zonesTargetMessage.AddField(index);
                    zonesTargetMessage.AddField(zoneName);
                    zonesTargetMessage.AddField(highSpeed);

                    zonesTargetMessage.Serialize(outputStream);
                    zonesTargetMessage.Clear();
                }

                index.SetUInt16((UInt16)(i + 1));
                zoneName.SetString(zone.Name, (Byte)(Constants.MaxNameLength + 1));
                highSpeed.SetUInt16((UInt16)(zone.High * 1000));

                zonesTargetMessage.AddField(index);
                zonesTargetMessage.AddField(zoneName);
                zonesTargetMessage.AddField(highSpeed);

                zonesTargetMessage.Serialize(outputStream, false);

                ++i;
            }
        }
        public virtual void SerializeFITHRZones(Stream outputStream)
        {
            UInt16 i = 0;

            foreach (GarminFitnessValueRange<GarminFitnessDoubleRange> zone in m_HeartRateZones)
            {
                FITMessage zonesTargetMessage = new FITMessage(FITGlobalMessageIds.HRZones);
                FITMessageField index = new FITMessageField((Byte)FITHRZonesFieldIds.MessageIndex);
                FITMessageField zoneName = new FITMessageField((Byte)FITHRZonesFieldIds.Name);
                FITMessageField highBPM = new FITMessageField((Byte)FITHRZonesFieldIds.HighBPM);

                if (i == 0)
                {
                    index.SetUInt16(i);
                    zoneName.SetString(String.Format("HR Zone {0}", i), (Byte)(Constants.MaxNameLength + 1));
                    highBPM.SetUInt8((Byte)(zone.Lower * MaximumHeartRate));

                    zonesTargetMessage.AddField(index);
                    zonesTargetMessage.AddField(zoneName);
                    zonesTargetMessage.AddField(highBPM);

                    zonesTargetMessage.Serialize(outputStream);
                    zonesTargetMessage.Clear();
                }

                index.SetUInt16((UInt16)(i + 1));
                zoneName.SetString(String.Format("HR Zone {0}", i + 1), (Byte)(Constants.MaxNameLength + 1));
                highBPM.SetUInt8((Byte)(zone.Upper * MaximumHeartRate));

                zonesTargetMessage.AddField(index);
                zonesTargetMessage.AddField(zoneName);
                zonesTargetMessage.AddField(highBPM);

                zonesTargetMessage.Serialize(outputStream, false);

                ++i;
            }
        }
        public void SerializeBikeProfiles(Stream outputStream)
        {
            UInt16 i = 0;

            foreach (GarminBikeProfile bike in m_Bikes)
            {
                FITMessage bikeProfileMessage = new FITMessage(FITGlobalMessageIds.BikeProfile);
                FITMessageField index = new FITMessageField((Byte)FITBikeProfileFieldIds.MessageIndex);
                FITMessageField name = new FITMessageField((Byte)FITBikeProfileFieldIds.Name);
                FITMessageField odometer = new FITMessageField((Byte)FITBikeProfileFieldIds.Odometer);
                FITMessageField customWheelSize = new FITMessageField((Byte)FITBikeProfileFieldIds.CustomWheelSize);
                FITMessageField autoWheelSize = new FITMessageField((Byte)FITBikeProfileFieldIds.AutoWheelSize);
                FITMessageField weight = new FITMessageField((Byte)FITBikeProfileFieldIds.Weight);
                FITMessageField useAutoWheelSize = new FITMessageField((Byte)FITBikeProfileFieldIds.AutoWheelSetting);
                FITMessageField cadenceSensor = new FITMessageField((Byte)FITBikeProfileFieldIds.SpeedCadenceSensorEnabled);
                FITMessageField powerSensor = new FITMessageField((Byte)FITBikeProfileFieldIds.PowerSensorEnabled);

                index.SetUInt16(i);
                name.SetString(bike.Name, (Byte)(Constants.MaxNameLength + 1));
                odometer.SetUInt32((UInt32)(bike.OdometerInMeters * 100));
                customWheelSize.SetUInt16(bike.WheelSize);
                autoWheelSize.SetUInt16(bike.WheelSize);
                weight.SetUInt16((UInt16)Math.Round(Weight.Convert(bike.WeightInPounds, Weight.Units.Pound, Weight.Units.Kilogram) * 10, 0));
                useAutoWheelSize.SetEnum(bike.AutoWheelSize ? (Byte)FITBoolean.True : (Byte)FITBoolean.False);
                cadenceSensor.SetEnum(bike.HasCadenceSensor ? (Byte)FITBoolean.True : (Byte)FITBoolean.False);
                powerSensor.SetEnum(bike.HasPowerSensor ? (Byte)FITBoolean.True : (Byte)FITBoolean.False);

                bikeProfileMessage.AddField(index);
                bikeProfileMessage.AddField(name);
                bikeProfileMessage.AddField(odometer);
                bikeProfileMessage.AddField(customWheelSize);
                bikeProfileMessage.AddField(autoWheelSize);
                bikeProfileMessage.AddField(weight);
                bikeProfileMessage.AddField(useAutoWheelSize);
                bikeProfileMessage.AddField(cadenceSensor);
                bikeProfileMessage.AddField(powerSensor);

                bikeProfileMessage.Serialize(outputStream, i == 0);

                ++i;
            }
        }