Пример #1
0
        private FileResult TestReadingAllFilesInDirectoryUsingSdkParser(Stream stream, bool validateCrc)
        {
            int recordCount = 0;

            System.DateTime maxTime = System.DateTime.MinValue;

            var parser = new Decode();

            if (validateCrc)
            {
                Assert.IsTrue(parser.CheckIntegrity(stream));
            }

            parser.MesgEvent += (sender, args) =>
            {
                if (args.mesg.Name == "Record")
                {
                    var             recordMesg = new RecordMesg(args.mesg);
                    System.DateTime timeStamp  = recordMesg.GetTimestamp().GetDateTime();
                    if (timeStamp > maxTime)
                    {
                        maxTime = timeStamp;
                    }
                    recordCount++;
                }
            };

            Assert.IsTrue(parser.Read(stream));
            return(new FileResult {
                Records = recordCount, LatestDateTimeInFile = maxTime
            });
        }
Пример #2
0
        public static void AddRecord()
        {
            var now = new DateTime(System.DateTime.Now);

            if (lastRecordTimeStamp == now.GetTimeStamp())
            {
                return; // do not record twice with same timestamp
            }

            try
            {
                var newRecord = new RecordMesg();
                var hr        = State.CyclistHeartRate > 0 ? (byte?)State.CyclistHeartRate : null;
                var cad       = State.BikeCadence > 0 ? (byte?)State.BikeCadence : null;

                newRecord.SetTimestamp(now);

                newRecord.SetHeartRate(hr);
                newRecord.SetCadence(cad);
                newRecord.SetPower((ushort)State.CyclistPower);
                newRecord.SetGrade(State.BikeIncline);
                newRecord.SetDistance(State.TripTotalKm * 1000);
                newRecord.SetSpeed(State.BikeSpeedKmh / 3.6f);
                newRecord.SetAltitude(RaceState.Instance.CarPositions[0].Y);

                encoder.Write(newRecord);

                lastRecordTimeStamp = now.GetTimeStamp();
            } catch (Exception e)
            {
                Console.Write("Failed to write record.");
                Console.WriteLine(e.Message);
            }
        }
Пример #3
0
        public static void AddRecord(FitRecord record, DateTime?time = null)
        {
            Trace.TraceInformation("AddRecord()");
            if (isPaused)
            {
                return;
            }

            var now = time ?? DateTime.UtcNow;

            if (now - lastRecordTime < TimeSpan.FromSeconds(1))
            {
                return; // do not record twice with same timestamp
            }

            totalDistance += record.Speed / 3.6f * (float)(now - lastEventTime).TotalSeconds;
            csvFile.WriteLine($"{(int)(now - startTime).TotalSeconds,7},{record}," + totalDistance.ToString("0.0", CultureInfo.InvariantCulture));

            totalTimerTime += (now - lastEventTime);

            try
            {
                var newRecord = new RecordMesg();
                var hr        = record.Heartrate > 0 ? (byte?)record.Heartrate : null;
                var cad       = record.Cadence > 0 ? (byte?)record.Cadence : null;

                newRecord.SetTimestamp(new Dynastream.Fit.DateTime(now));

                newRecord.SetHeartRate(hr);
                newRecord.SetCadence(cad);
                newRecord.SetPower((ushort)record.Power);
                // newRecord.SetGrade(State.BikeIncline);
                newRecord.SetDistance(totalDistance);
                newRecord.SetSpeed(record.Speed / 3.6f);
                newRecord.SetEnhancedSpeed(record.Speed / 3.6f);
                if (record.Lat != 0f && record.Lon != 0f)
                {
                    var semiLat = record.Lat * Math.Pow(2, 31) / 180.0; // convert degrees to semicircles
                    var semiLon = record.Lon * Math.Pow(2, 31) / 180.0;
                    newRecord.SetPositionLong((int)semiLon);
                    newRecord.SetPositionLat((int)semiLat);
                }
                newRecord.SetAltitude(record.Alt);
                newRecord.SetEnhancedAltitude(record.Alt);

                encoder.Write(newRecord);

                lastRecordTime = now;
                lastEventTime  = now;
            }
            catch (Exception e)
            {
                Trace.TraceError("Error while writing FIT record: " + e.Message);
            }
        }
Пример #4
0
        public static Speed?GetValidSpeed(this RecordMesg msg)
        {
            var speed = msg.GetSpeed();

            if (speed.HasValue && speed.Value != InvalidSpeed)
            {
                return(new Speed(speed.Value, SpeedUnit.MeterPerSecond));
            }

            return(null);
        }
Пример #5
0
        public static float?GetValidDistance(this RecordMesg msg)
        {
            var distance = msg.GetDistance();

            if (distance.HasValue && Convert.ToUInt32(distance.Value) != InvalidDistance / 100)
            {
                return(distance);
            }

            return(null);
        }
Пример #6
0
        public static byte?GetValidCadence(this RecordMesg msg)
        {
            var cadence = msg.GetCadence();

            if (cadence.HasValue && cadence.Value != InvalidCadence)
            {
                return(cadence);
            }

            return(null);
        }
Пример #7
0
        public static ushort?GetValidPower(this RecordMesg msg)
        {
            var power = msg.GetPower();

            if (power.HasValue && power.Value != InvalidPower)
            {
                return(power.Value);
            }

            return(null);
        }
Пример #8
0
        public static byte?GetValidHeartRate(this RecordMesg msg)
        {
            var hr = msg.GetHeartRate();

            if (hr.HasValue && hr.Value != InvalidHeartRate)
            {
                return(hr);
            }

            return(null);
        }
Пример #9
0
        public async Task Run()
        {
            await Task.Run(() =>
            {
                using var outStream = new FileStream(_outputFile, FileMode.Create, FileAccess.Write);
                var encode          = new Encode(outStream, ProtocolVersion.V20);

                var fileId = new FileIdMesg();
                fileId.SetType(File.Activity);
                fileId.SetTimeCreated(new Dynastream.Fit.DateTime(DateTime.Now));
                encode.Write(fileId);
                foreach (var record in _dataStream.Records)
                {
                    var rcrd = new RecordMesg();

                    rcrd.SetTimestamp(new Dynastream.Fit.DateTime(record.Timestamp));
                    rcrd.SetAltitude((float?)record.Elevation);
                }
            });
        }
Пример #10
0
        /// <summary>
        /// Adds a new record.
        /// </summary>
        private void HandleRecordMessage(object sender, MesgEventArgs e)
        {
            RecordMesg msg    = (RecordMesg)e.mesg;
            FitRecord  record = new FitRecord(msg, Records);

            Records.Add(record);
            FitRecordMessage message = null;

            if (Messages.Count > 0)
            {
                message = Messages[Messages.Count - 1] as FitRecordMessage;
            }
            if (message != null)
            {
                message.Last = Records.Count - 1;
            }
            else
            {
                message = new FitRecordMessage(Records.Count - 1);
                Messages.Add(message);
            }
        }
Пример #11
0
        static public void CreateLapSwimActivity()
        {
            // Example Swim Data representing a 500 yard pool swim using different strokes and drills.
            var swimData = new List <Dictionary <string, object> >()
            {
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 20U }, { "stroke", "Freestyle" }, { "strokes", 30U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 25U }, { "stroke", "Freestyle" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 30U }, { "stroke", "Freestyle" }, { "strokes", 10U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 35U }, { "stroke", "Freestyle" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Idle" }, { "duration", 60U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 20U }, { "stroke", "Backstroke" }, { "strokes", 30U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 25U }, { "stroke", "Backstroke" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 30U }, { "stroke", "Backstroke" }, { "strokes", 10U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 35U }, { "stroke", "Backstroke" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Idle" }, { "duration", 60U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 20U }, { "stroke", "Breaststroke" }, { "strokes", 30U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 25U }, { "stroke", "Breaststroke" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 30U }, { "stroke", "Breaststroke" }, { "strokes", 10U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 35U }, { "stroke", "Breaststroke" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Idle" }, { "duration", 60U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 20U }, { "stroke", "Butterfly" }, { "strokes", 30U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 25U }, { "stroke", "Butterfly" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 30U }, { "stroke", "Butterfly" }, { "strokes", 10U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 35U }, { "stroke", "Butterfly" }, { "strokes", 20U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Idle" }, { "duration", 60U }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 40U }, { "stroke", "Drill" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 40U }, { "stroke", "Drill" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 40U }, { "stroke", "Drill" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Active" }, { "duration", 40U }, { "stroke", "Drill" }
                },
                new Dictionary <string, object>()
                {
                    { "type", "Lap" }
                },
            };

            const string FileName = "ActivityEncodeRecipeLapSwim.fit";
            var          messages = new List <Mesg>();

            // The starting timestamp for the activity
            var startTime = new Dynastream.Fit.DateTime(System.DateTime.UtcNow);


            // Timer Events are a BEST PRACTICE for FIT ACTIVITY files
            var eventMesgStart = new EventMesg();

            eventMesgStart.SetTimestamp(startTime);
            eventMesgStart.SetEvent(Event.Timer);
            eventMesgStart.SetEventType(EventType.Start);
            messages.Add(eventMesgStart);

            //
            // Create a Length or Lap message for each item in the sample swim data. Calculate
            // distance, duration, and stroke count for each lap and the overall session.
            //

            // Session Accumulators
            uint   sessionTotalElapsedTime = 0;
            float  sessionDistance         = 0;
            ushort sessionNumLengths       = 0;
            ushort sessionNumActiveLengths = 0;
            ushort sessionTotalStrokes     = 0;
            ushort sessionNumLaps          = 0;

            // Lap accumulators
            uint   lapTotalElapsedTime = 0;
            float  lapDistance         = 0;
            ushort lapNumActiveLengths = 0;
            ushort lapNumLengths       = 0;
            ushort lapFirstLengthIndex = 0;
            ushort lapTotalStrokes     = 0;
            var    lapStartTime        = new Dynastream.Fit.DateTime(startTime);

            var    poolLength     = 22.86f;
            var    poolLengthUnit = DisplayMeasure.Statute;
            var    timestamp      = new Dynastream.Fit.DateTime(startTime);
            ushort messageIndex   = 0;

            foreach (var swimLength in swimData)
            {
                string type = (string)swimLength["type"];

                if (type.Equals("Lap"))
                {
                    // Create a Lap message, set its fields, and write it to the file
                    var lapMesg = new LapMesg();
                    lapMesg.SetMessageIndex(sessionNumLaps);
                    lapMesg.SetTimestamp(timestamp);
                    lapMesg.SetStartTime(lapStartTime);
                    lapMesg.SetTotalElapsedTime(lapTotalElapsedTime);
                    lapMesg.SetTotalTimerTime(lapTotalElapsedTime);
                    lapMesg.SetTotalDistance(lapDistance);
                    lapMesg.SetFirstLengthIndex(lapFirstLengthIndex);
                    lapMesg.SetNumActiveLengths(lapNumActiveLengths);
                    lapMesg.SetNumLengths(lapNumLengths);
                    lapMesg.SetTotalStrokes(lapTotalStrokes);
                    lapMesg.SetAvgStrokeDistance(lapDistance / lapTotalStrokes);
                    lapMesg.SetSport(Sport.Swimming);
                    lapMesg.SetSubSport(SubSport.LapSwimming);
                    messages.Add(lapMesg);

                    sessionNumLaps++;

                    // Reset the Lap accumulators
                    lapFirstLengthIndex = messageIndex;
                    lapNumActiveLengths = 0;
                    lapNumLengths       = 0;
                    lapTotalElapsedTime = 0;
                    lapDistance         = 0;
                    lapTotalStrokes     = 0;
                    lapStartTime        = new Dynastream.Fit.DateTime(timestamp);
                }
                else
                {
                    uint duration   = (uint)swimLength["duration"];
                    var  lengthType = (LengthType)Enum.Parse(typeof(LengthType), type);

                    // Create a Length message and its fields
                    var lengthMesg = new LengthMesg();
                    lengthMesg.SetMessageIndex(messageIndex++);
                    lengthMesg.SetStartTime(timestamp);
                    lengthMesg.SetTotalElapsedTime(duration);
                    lengthMesg.SetTotalTimerTime(duration);
                    lengthMesg.SetLengthType(lengthType);

                    timestamp.Add(duration);
                    lengthMesg.SetTimestamp(timestamp);

                    // Create the Record message that pairs with the Length Message
                    var recordMesg = new RecordMesg();
                    recordMesg.SetTimestamp(timestamp);
                    recordMesg.SetDistance(sessionDistance + poolLength);

                    // Is this an Active Length?
                    if (lengthType == LengthType.Active)
                    {
                        // Get the Active data from the model
                        string     stroke     = swimLength.ContainsKey("stroke") ? (String)swimLength["stroke"] : "Freestyle";
                        uint       strokes    = swimLength.ContainsKey("strokes") ? (uint)swimLength["strokes"] : 0;
                        SwimStroke swimStroke = (SwimStroke)Enum.Parse(typeof(SwimStroke), stroke);

                        // Set the Active data on the Length Message
                        lengthMesg.SetAvgSpeed(poolLength / (float)duration);
                        lengthMesg.SetSwimStroke(swimStroke);

                        if (strokes > 0)
                        {
                            lengthMesg.SetTotalStrokes((ushort)strokes);
                            lengthMesg.SetAvgSwimmingCadence((byte)(strokes * 60U / duration));
                        }

                        // Set the Active data on the Record Message
                        recordMesg.SetSpeed(poolLength / (float)duration);
                        if (strokes > 0)
                        {
                            recordMesg.SetCadence((byte)((strokes * 60U) / duration));
                        }

                        // Increment the "Active" accumulators
                        sessionNumActiveLengths++;
                        lapNumActiveLengths++;
                        sessionDistance     += poolLength;
                        lapDistance         += poolLength;
                        sessionTotalStrokes += (ushort)strokes;
                        lapTotalStrokes     += (ushort)strokes;
                    }

                    // Write the messages to the file
                    messages.Add(recordMesg);
                    messages.Add(lengthMesg);

                    // Increment the "Total" accumulators
                    sessionTotalElapsedTime += duration;
                    lapTotalElapsedTime     += duration;
                    sessionNumLengths++;
                    lapNumLengths++;
                }
            }

            // Timer Events are a BEST PRACTICE for FIT ACTIVITY files
            var eventMesgStop = new EventMesg();

            eventMesgStop.SetTimestamp(timestamp);
            eventMesgStop.SetEvent(Event.Timer);
            eventMesgStop.SetEventType(EventType.StopAll);
            messages.Add(eventMesgStop);

            // Every FIT ACTIVITY file MUST contain at least one Session message
            var sessionMesg = new SessionMesg();

            sessionMesg.SetMessageIndex(0);
            sessionMesg.SetTimestamp(timestamp);
            sessionMesg.SetStartTime(startTime);
            sessionMesg.SetTotalElapsedTime(sessionTotalElapsedTime);
            sessionMesg.SetTotalTimerTime(sessionTotalElapsedTime);
            sessionMesg.SetTotalDistance(sessionDistance);
            sessionMesg.SetSport(Sport.Swimming);
            sessionMesg.SetSubSport(SubSport.LapSwimming);
            sessionMesg.SetFirstLapIndex(0);
            sessionMesg.SetNumLaps(sessionNumLaps);
            sessionMesg.SetPoolLength(poolLength);
            sessionMesg.SetPoolLengthUnit(poolLengthUnit);
            sessionMesg.SetNumLengths(sessionNumLengths);
            sessionMesg.SetNumActiveLengths(sessionNumActiveLengths);
            sessionMesg.SetTotalStrokes(sessionTotalStrokes);
            sessionMesg.SetAvgStrokeDistance(sessionDistance / sessionTotalStrokes);
            messages.Add(sessionMesg);

            // Every FIT ACTIVITY file MUST contain EXACTLY one Activity message
            var activityMesg = new ActivityMesg();

            activityMesg.SetTimestamp(timestamp);
            activityMesg.SetNumSessions(1);
            var timezoneOffset = (int)TimeZoneInfo.Local.BaseUtcOffset.TotalSeconds;

            activityMesg.SetLocalTimestamp((uint)((int)timestamp.GetTimeStamp() + timezoneOffset));
            activityMesg.SetTotalTimerTime(sessionTotalElapsedTime);
            messages.Add(activityMesg);

            CreateActivityFile(messages, FileName, startTime);
        }
Пример #12
0
        /// <summary>
        /// Writes the data to a FIT file.
        /// </summary>
        /// <param name="fileName">Name of the FIT file to write to.</param>
        /// <param name="start">Start date/time of the activity.</param>
        /// <param name="laps">Lap and record data to be written.</param>
        /// <param name="calories">Calories used for the activity.</param>
        /// <param name="work">Work done for the activity.</param>
        static void WriteFitFile(string fileName, System.DateTime start, LapsList laps, int calories, uint work)
        {
            // open the encoder and stream
            Encode     encoder   = new Encode(ProtocolVersion.V20);
            FileStream fitStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

            encoder.Open(fitStream);

            // write the file ID message
            FileIdMesg fileIdMsg = new FileIdMesg();

            fileIdMsg.SetType(Dynastream.Fit.File.Activity);
            fileIdMsg.SetManufacturer(Manufacturer.StagesCycling);
            fileIdMsg.SetProduct(3);
            fileIdMsg.SetSerialNumber(1);
            fileIdMsg.SetTimeCreated(new Dynastream.Fit.DateTime(start));
            encoder.Write(fileIdMsg);

            // write the record and lap messages
            foreach (Lap lap in laps)
            {
                // write the record messages
                foreach (Record record in lap.Records)
                {
                    RecordMesg recordMsg = new RecordMesg();
                    recordMsg.SetTimestamp(new Dynastream.Fit.DateTime(record.Time.ToUniversalTime()));
                    recordMsg.SetHeartRate((byte)record.HeartRate);
                    recordMsg.SetCadence((byte)record.Cadence);
                    recordMsg.SetDistance((float)record.Distance * 1000);
                    recordMsg.SetSpeed((float)(record.Speed / 3.6));
                    recordMsg.SetPower((ushort)record.Power);
                    encoder.Write(recordMsg);
                }

                // write the lap message
                Record   first  = lap.Records[0];
                Record   last   = lap.Records[lap.Records.Count - 1];
                TimeSpan time   = last.Time - first.Time;
                LapMesg  lapMsg = new LapMesg();
                lapMsg.SetTimestamp(new Dynastream.Fit.DateTime(last.Time.ToUniversalTime()));
                lapMsg.SetStartTime(new Dynastream.Fit.DateTime(first.Time.ToUniversalTime()));
                lapMsg.SetTotalElapsedTime((int)time.TotalSeconds);
                lapMsg.SetTotalTimerTime((int)time.TotalSeconds);
                lapMsg.SetTotalDistance((float)(last.Distance - first.Distance) * 1000);
                lapMsg.SetTotalCalories((ushort)lap.Calories);
                lapMsg.SetTotalWork(lap.Work * 1000);
                lapMsg.SetEvent(Event.Lap);
                lapMsg.SetEventType(EventType.Stop);
                lapMsg.SetIntensity(Intensity.Active);
                lapMsg.SetLapTrigger(LapTrigger.Manual);
                lapMsg.SetSport(Sport.Cycling);
                Summary lapSummary = GetLapSummary(lap);
                lapMsg.SetAvgCadence((byte)lapSummary.AveCadence);
                lapMsg.SetMaxCadence((byte)lapSummary.MaxCadence);
                lapMsg.SetAvgHeartRate((byte)lapSummary.AveHeartRate);
                lapMsg.SetMaxHeartRate((byte)lapSummary.MaxHeartRate);
                lapMsg.SetAvgPower((ushort)lapSummary.AvePower);
                lapMsg.SetMaxPower((ushort)lapSummary.MaxPower);
                lapMsg.SetAvgSpeed((float)lapSummary.AveSpeed / 3.6f);
                lapMsg.SetMaxSpeed((float)lapSummary.MaxSpeed / 3.6f);
                encoder.Write(lapMsg);
            }

            // get the first and last records
            Record   firstRecord = laps[0].Records[0];
            Lap      lastLap     = laps[laps.Count - 1];
            Record   lastRecord  = lastLap.Records[lastLap.Records.Count - 1];
            TimeSpan totalTime   = lastRecord.Time - firstRecord.Time;

            // write the session message
            SessionMesg sessionMsg = new SessionMesg();

            sessionMsg.SetTimestamp(new Dynastream.Fit.DateTime(lastRecord.Time.ToUniversalTime()));
            sessionMsg.SetStartTime(new Dynastream.Fit.DateTime(firstRecord.Time.ToUniversalTime()));
            sessionMsg.SetTotalElapsedTime((int)totalTime.TotalSeconds);
            sessionMsg.SetTotalTimerTime((int)totalTime.TotalSeconds);
            sessionMsg.SetTotalDistance((float)(lastRecord.Distance - firstRecord.Distance) * 1000);
            sessionMsg.SetTotalCalories((ushort)calories);
            sessionMsg.SetTotalWork(work * 1000);
            sessionMsg.SetFirstLapIndex(0);
            sessionMsg.SetNumLaps((ushort)laps.Count);
            sessionMsg.SetEvent(Event.Session);
            sessionMsg.SetEventType(EventType.Stop);
            sessionMsg.SetSport(Sport.Cycling);
            sessionMsg.SetSubSport(SubSport.Spin);
            Summary sessionSummary = GetSessionSummary(laps);

            sessionMsg.SetAvgCadence((byte)sessionSummary.AveCadence);
            sessionMsg.SetMaxCadence((byte)sessionSummary.MaxCadence);
            sessionMsg.SetAvgHeartRate((byte)sessionSummary.AveHeartRate);
            sessionMsg.SetMaxHeartRate((byte)sessionSummary.MaxHeartRate);
            sessionMsg.SetAvgPower((ushort)sessionSummary.AvePower);
            sessionMsg.SetMaxPower((ushort)sessionSummary.MaxPower);
            sessionMsg.SetAvgSpeed((float)sessionSummary.AveSpeed / 3.6f);
            sessionMsg.SetMaxSpeed((float)sessionSummary.MaxSpeed / 3.6f);
            encoder.Write(sessionMsg);

            // write the activity message
            ActivityMesg activityMsg = new ActivityMesg();

            activityMsg.SetTimestamp(new Dynastream.Fit.DateTime(lastRecord.Time.ToUniversalTime()));
            activityMsg.SetTotalTimerTime((int)totalTime.TotalSeconds);
            activityMsg.SetNumSessions(1);
            activityMsg.SetType(Activity.Manual);
            activityMsg.SetEvent(Event.Activity);
            activityMsg.SetEventType(EventType.Stop);
            encoder.Write(activityMsg);

            // close the encoder and stream
            encoder.Close();
            fitStream.Close();
        }
Пример #13
0
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Generate some FIT messages
            FileIdMesg fileIdMesg = new FileIdMesg();

            fileIdMesg.SetManufacturer(Manufacturer.Dynastream);  // Types defined in the profile are available
            fileIdMesg.SetProduct(1000);
            fileIdMesg.SetSerialNumber(12345);

            //UserProfileMesg myUserProfile = new UserProfileMesg();
            //myUserProfile.SetGender(Gender.Female);
            //float myWeight = 63.1F;
            //myUserProfile.SetWeight(myWeight);
            //myUserProfile.SetAge(99);
            //myUserProfile.SetFriendlyName(Encoding.UTF8.GetBytes("TestUser"));

            var route = GpxClass.FromFile("route.gpx");

            CourseMesg course = new CourseMesg();

            course.SetName(Encoding.UTF8.GetBytes("route from gpx"));
            course.SetSport(Sport.Cycling);

            var baseDate = route.metadata.timeSpecified ? route.metadata.time : System.DateTime.Now.AddDays(-1);

            FileStream fitDest = new FileStream("Test.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

            // Create file encode object
            Encode encodeDemo = new Encode();

            // Write our header
            encodeDemo.Open(fitDest);
            // Encode each message, a definition message is automatically generated and output if necessary
            encodeDemo.Write(fileIdMesg);
            //encodeDemo.Write(myUserProfile);

            encodeDemo.Write(course);

            var lap = new LapMesg();

            encodeDemo.Write(lap);


            var firstTrk = route.trk.First();

            var firstTrkSeg = firstTrk.trkseg.First();

            var firstPoint = firstTrkSeg.trkpt.First();

            lap.SetTimestamp(new fit.DateTime(firstPoint.time));
            lap.SetStartPositionLat(firstPoint.lat.RawInt());
            lap.SetStartPositionLong(firstPoint.lon.RawInt());

            var lastPoint = firstTrkSeg.trkpt.Last();

            lap.SetEndPositionLat(lastPoint.lat.RawInt());
            lap.SetEndPositionLong(lastPoint.lon.RawInt());

            var e = new EventMesg();

            e.SetTimestamp(new fit.DateTime(firstPoint.time));
            e.SetEventType(EventType.Start);
            e.SetEventGroup(0);
            e.SetEvent(Event.Timer);
            e.SetData(null);
            encodeDemo.Write(e);

            foreach (var point in firstTrkSeg.trkpt)
            {
                var p = new RecordMesg();
                p.SetPositionLat(point.lat.RawInt());
                p.SetPositionLong(point.lon.RawInt());
                //p.SetDistance(point. 10665.65f);
                p.SetAltitude(Convert.ToSingle(point.ele));
                p.SetTimestamp(new fit.DateTime(baseDate));

                encodeDemo.Write(p);
            }

            var eventStop = new EventMesg();

            eventStop.SetData(null);
            eventStop.SetTimestamp(new fit.DateTime(lastPoint.time));
            eventStop.SetEvent(Event.Timer);
            eventStop.SetEventType(EventType.StopDisableAll);
            encodeDemo.Write(eventStop);

            // Update header datasize and file CRC
            encodeDemo.Close();

            fitDest.Close();

            Console.WriteLine("Encoded FIT file test.fit");
            stopwatch.Stop();
            Console.WriteLine("Time elapsed: {0:0.#}s", stopwatch.Elapsed.TotalSeconds);

            Console.ReadKey();
        }
Пример #14
0
 public static bool HasPosition(this RecordMesg mesg)
 {
     return(mesg.GetPositionLat().HasValue&& mesg.GetPositionLat().Value != InvalidPosition);
 }
Пример #15
0
 public static void SetPositionLongInDegrees(this RecordMesg mesg, double lon)
 {
     mesg.SetPositionLong(Degree2SemiCircles(lon));
 }
Пример #16
0
        public void OnMesg(object sender, MesgEventArgs e)
        {
            // Notify any subscribers of either our general mesg event or specific profile mesg event
            if (MesgEvent != null)
            {
                MesgEvent(sender, e);
            }

            switch (e.mesg.Num)
            {
            case (ushort)MesgNum.FileId:
                if (FileIdMesgEvent != null)
                {
                    FileIdMesg    fileIdMesg    = new FileIdMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = fileIdMesg;
                    FileIdMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.FileCreator:
                if (FileCreatorMesgEvent != null)
                {
                    FileCreatorMesg fileCreatorMesg = new FileCreatorMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = fileCreatorMesg;
                    FileCreatorMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Software:
                if (SoftwareMesgEvent != null)
                {
                    SoftwareMesg  softwareMesg  = new SoftwareMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = softwareMesg;
                    SoftwareMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.SlaveDevice:
                if (SlaveDeviceMesgEvent != null)
                {
                    SlaveDeviceMesg slaveDeviceMesg = new SlaveDeviceMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = slaveDeviceMesg;
                    SlaveDeviceMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Capabilities:
                if (CapabilitiesMesgEvent != null)
                {
                    CapabilitiesMesg capabilitiesMesg = new CapabilitiesMesg(e.mesg);
                    MesgEventArgs    mesgEventArgs    = new MesgEventArgs();
                    mesgEventArgs.mesg = capabilitiesMesg;
                    CapabilitiesMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.FileCapabilities:
                if (FileCapabilitiesMesgEvent != null)
                {
                    FileCapabilitiesMesg fileCapabilitiesMesg = new FileCapabilitiesMesg(e.mesg);
                    MesgEventArgs        mesgEventArgs        = new MesgEventArgs();
                    mesgEventArgs.mesg = fileCapabilitiesMesg;
                    FileCapabilitiesMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.MesgCapabilities:
                if (MesgCapabilitiesMesgEvent != null)
                {
                    MesgCapabilitiesMesg mesgCapabilitiesMesg = new MesgCapabilitiesMesg(e.mesg);
                    MesgEventArgs        mesgEventArgs        = new MesgEventArgs();
                    mesgEventArgs.mesg = mesgCapabilitiesMesg;
                    MesgCapabilitiesMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.FieldCapabilities:
                if (FieldCapabilitiesMesgEvent != null)
                {
                    FieldCapabilitiesMesg fieldCapabilitiesMesg = new FieldCapabilitiesMesg(e.mesg);
                    MesgEventArgs         mesgEventArgs         = new MesgEventArgs();
                    mesgEventArgs.mesg = fieldCapabilitiesMesg;
                    FieldCapabilitiesMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.DeviceSettings:
                if (DeviceSettingsMesgEvent != null)
                {
                    DeviceSettingsMesg deviceSettingsMesg = new DeviceSettingsMesg(e.mesg);
                    MesgEventArgs      mesgEventArgs      = new MesgEventArgs();
                    mesgEventArgs.mesg = deviceSettingsMesg;
                    DeviceSettingsMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.UserProfile:
                if (UserProfileMesgEvent != null)
                {
                    UserProfileMesg userProfileMesg = new UserProfileMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = userProfileMesg;
                    UserProfileMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.HrmProfile:
                if (HrmProfileMesgEvent != null)
                {
                    HrmProfileMesg hrmProfileMesg = new HrmProfileMesg(e.mesg);
                    MesgEventArgs  mesgEventArgs  = new MesgEventArgs();
                    mesgEventArgs.mesg = hrmProfileMesg;
                    HrmProfileMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.SdmProfile:
                if (SdmProfileMesgEvent != null)
                {
                    SdmProfileMesg sdmProfileMesg = new SdmProfileMesg(e.mesg);
                    MesgEventArgs  mesgEventArgs  = new MesgEventArgs();
                    mesgEventArgs.mesg = sdmProfileMesg;
                    SdmProfileMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.BikeProfile:
                if (BikeProfileMesgEvent != null)
                {
                    BikeProfileMesg bikeProfileMesg = new BikeProfileMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = bikeProfileMesg;
                    BikeProfileMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.ZonesTarget:
                if (ZonesTargetMesgEvent != null)
                {
                    ZonesTargetMesg zonesTargetMesg = new ZonesTargetMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = zonesTargetMesg;
                    ZonesTargetMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Sport:
                if (SportMesgEvent != null)
                {
                    SportMesg     sportMesg     = new SportMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = sportMesg;
                    SportMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.HrZone:
                if (HrZoneMesgEvent != null)
                {
                    HrZoneMesg    hrZoneMesg    = new HrZoneMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = hrZoneMesg;
                    HrZoneMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.SpeedZone:
                if (SpeedZoneMesgEvent != null)
                {
                    SpeedZoneMesg speedZoneMesg = new SpeedZoneMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = speedZoneMesg;
                    SpeedZoneMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.CadenceZone:
                if (CadenceZoneMesgEvent != null)
                {
                    CadenceZoneMesg cadenceZoneMesg = new CadenceZoneMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = cadenceZoneMesg;
                    CadenceZoneMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.PowerZone:
                if (PowerZoneMesgEvent != null)
                {
                    PowerZoneMesg powerZoneMesg = new PowerZoneMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = powerZoneMesg;
                    PowerZoneMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.MetZone:
                if (MetZoneMesgEvent != null)
                {
                    MetZoneMesg   metZoneMesg   = new MetZoneMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = metZoneMesg;
                    MetZoneMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Goal:
                if (GoalMesgEvent != null)
                {
                    GoalMesg      goalMesg      = new GoalMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = goalMesg;
                    GoalMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Activity:
                if (ActivityMesgEvent != null)
                {
                    ActivityMesg  activityMesg  = new ActivityMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = activityMesg;
                    ActivityMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Session:
                if (SessionMesgEvent != null)
                {
                    SessionMesg   sessionMesg   = new SessionMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = sessionMesg;
                    SessionMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Lap:
                if (LapMesgEvent != null)
                {
                    LapMesg       lapMesg       = new LapMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = lapMesg;
                    LapMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Length:
                if (LengthMesgEvent != null)
                {
                    LengthMesg    lengthMesg    = new LengthMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = lengthMesg;
                    LengthMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Record:
                if (RecordMesgEvent != null)
                {
                    RecordMesg    recordMesg    = new RecordMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = recordMesg;
                    RecordMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Event:
                if (EventMesgEvent != null)
                {
                    EventMesg     eventMesg     = new EventMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = eventMesg;
                    EventMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.DeviceInfo:
                if (DeviceInfoMesgEvent != null)
                {
                    DeviceInfoMesg deviceInfoMesg = new DeviceInfoMesg(e.mesg);
                    MesgEventArgs  mesgEventArgs  = new MesgEventArgs();
                    mesgEventArgs.mesg = deviceInfoMesg;
                    DeviceInfoMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.TrainingFile:
                if (TrainingFileMesgEvent != null)
                {
                    TrainingFileMesg trainingFileMesg = new TrainingFileMesg(e.mesg);
                    MesgEventArgs    mesgEventArgs    = new MesgEventArgs();
                    mesgEventArgs.mesg = trainingFileMesg;
                    TrainingFileMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Hrv:
                if (HrvMesgEvent != null)
                {
                    HrvMesg       hrvMesg       = new HrvMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = hrvMesg;
                    HrvMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Course:
                if (CourseMesgEvent != null)
                {
                    CourseMesg    courseMesg    = new CourseMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = courseMesg;
                    CourseMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.CoursePoint:
                if (CoursePointMesgEvent != null)
                {
                    CoursePointMesg coursePointMesg = new CoursePointMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = coursePointMesg;
                    CoursePointMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Workout:
                if (WorkoutMesgEvent != null)
                {
                    WorkoutMesg   workoutMesg   = new WorkoutMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = workoutMesg;
                    WorkoutMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.WorkoutStep:
                if (WorkoutStepMesgEvent != null)
                {
                    WorkoutStepMesg workoutStepMesg = new WorkoutStepMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = workoutStepMesg;
                    WorkoutStepMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Schedule:
                if (ScheduleMesgEvent != null)
                {
                    ScheduleMesg  scheduleMesg  = new ScheduleMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = scheduleMesg;
                    ScheduleMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Totals:
                if (TotalsMesgEvent != null)
                {
                    TotalsMesg    totalsMesg    = new TotalsMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = totalsMesg;
                    TotalsMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.WeightScale:
                if (WeightScaleMesgEvent != null)
                {
                    WeightScaleMesg weightScaleMesg = new WeightScaleMesg(e.mesg);
                    MesgEventArgs   mesgEventArgs   = new MesgEventArgs();
                    mesgEventArgs.mesg = weightScaleMesg;
                    WeightScaleMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.BloodPressure:
                if (BloodPressureMesgEvent != null)
                {
                    BloodPressureMesg bloodPressureMesg = new BloodPressureMesg(e.mesg);
                    MesgEventArgs     mesgEventArgs     = new MesgEventArgs();
                    mesgEventArgs.mesg = bloodPressureMesg;
                    BloodPressureMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.MonitoringInfo:
                if (MonitoringInfoMesgEvent != null)
                {
                    MonitoringInfoMesg monitoringInfoMesg = new MonitoringInfoMesg(e.mesg);
                    MesgEventArgs      mesgEventArgs      = new MesgEventArgs();
                    mesgEventArgs.mesg = monitoringInfoMesg;
                    MonitoringInfoMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Monitoring:
                if (MonitoringMesgEvent != null)
                {
                    MonitoringMesg monitoringMesg = new MonitoringMesg(e.mesg);
                    MesgEventArgs  mesgEventArgs  = new MesgEventArgs();
                    mesgEventArgs.mesg = monitoringMesg;
                    MonitoringMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.MemoGlob:
                if (MemoGlobMesgEvent != null)
                {
                    MemoGlobMesg  memoGlobMesg  = new MemoGlobMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = memoGlobMesg;
                    MemoGlobMesgEvent(sender, mesgEventArgs);
                }
                break;

            case (ushort)MesgNum.Pad:
                if (PadMesgEvent != null)
                {
                    PadMesg       padMesg       = new PadMesg(e.mesg);
                    MesgEventArgs mesgEventArgs = new MesgEventArgs();
                    mesgEventArgs.mesg = padMesg;
                    PadMesgEvent(sender, mesgEventArgs);
                }
                break;
            }
        }
Пример #17
0
        public void WriteTimeFrame(ActivityTimeFrame timeFrame)
        {
            if (_encoder == null)
            {
                return;
            }

            if (timeFrame.Type == ActivityTimeFrameType.Start)
            {
                // resumed
                var startMsg = new EventMesg();
                startMsg.SetEventType(EventType.Start);
                startMsg.SetEvent(Event.OffCourse);
                startMsg.SetTimestamp(new Dynastream.Fit.DateTime(timeFrame.Timestamp));
                _encoder.Write(startMsg);
            }
            else if (timeFrame.Type == ActivityTimeFrameType.Stop)
            {
                // stopped
                var stopMsg = new EventMesg();
                stopMsg.SetEventType(EventType.Stop);
                stopMsg.SetEvent(Event.OffCourse);
                stopMsg.SetTimestamp(new Dynastream.Fit.DateTime(timeFrame.Timestamp));
                _encoder.Write(stopMsg);
            }
            else
            {
                var recordMsg = new RecordMesg();
                recordMsg.SetTimestamp(new Dynastream.Fit.DateTime(timeFrame.Timestamp));
                if (timeFrame.HasPosition)
                {
                    recordMsg.SetPositionLatInDegrees(timeFrame.Position.Value.Latitude);
                    recordMsg.SetPositionLongInDegrees(timeFrame.Position.Value.Longitude);
                    recordMsg.SetAltitude(timeFrame.Position.Value.Altitude);
                }
                if (timeFrame.HeartRate.HasValue)
                {
                    recordMsg.SetHeartRate(timeFrame.HeartRate.Value);
                }
                if (timeFrame.Power.HasValue)
                {
                    recordMsg.SetPower(timeFrame.Power.Value);
                }
                if (timeFrame.Cadence.HasValue)
                {
                    recordMsg.SetCadence(timeFrame.Cadence.Value);
                }
                if (timeFrame.Speed.HasValue)
                {
                    recordMsg.SetSpeed(timeFrame.Speed.Value.GetValueAs(SpeedUnit.MeterPerSecond));
                }
                if (timeFrame.Distance.HasValue)
                {
                    recordMsg.SetDistance(timeFrame.Distance.Value.GetValueAs(DistanceUnit.Meter));
                }
                _encoder.Write(recordMsg);

                // Write R-R values
                if (timeFrame.HasRRValues)
                {
                    var hrvMsg = new HrvMesg();
                    for (int i = 0; i < timeFrame.RRValues.Length; i++)
                    {
                        hrvMsg.SetTime(i, timeFrame.RRValues[i] / 1000.0f);
                    }
                    _encoder.Write(hrvMsg);
                }
            }
        }
Пример #18
0
        static void OnMesg(object sender, MesgEventArgs e)
        {
            Mesg msg = e.mesg;

            if (msg.Num == MesgNum.Record)
            {
                RecordMesg recordMesg = new RecordMesg(msg);
                Console.Write("Data,{0},record,", msg.LocalNum);
                if (recordMesg.GetTimestamp() != null)
                {
                    Console.Write("timestamp,{0},s,", recordMesg.GetTimestamp().GetTimeStamp());
                }
                if ((recordMesg.GetDistance() != null) && (recordMesg.GetDistance() != (uint)Fit.BaseType[Fit.UInt32].invalidValue))
                {
                    Console.Write("distance, {0:0.0}, m,", recordMesg.GetDistance());
                }
                if ((recordMesg.GetSpeed() != null) && (recordMesg.GetSpeed() != (ushort)Fit.BaseType[Fit.UInt16].invalidValue))
                {
                    Console.Write("speed,{0:0.000},m/s,", recordMesg.GetSpeed());
                }
                if ((recordMesg.GetCadence() != null) && (recordMesg.GetCadence() != (byte)Fit.BaseType[Fit.UInt8].invalidValue))
                {
                    Console.Write("cadence,{0},rpm,", recordMesg.GetCadence());
                }
                if ((recordMesg.GetEnhancedSpeed() != null) && (recordMesg.GetEnhancedSpeed() != (uint)Fit.BaseType[Fit.UInt32].invalidValue))
                {
                    Console.Write("enhanced_speed,{0:0.000},m/s,", recordMesg.GetEnhancedSpeed());
                }
                if ((recordMesg.GetHeartRate() != null) && (recordMesg.GetHeartRate() != (byte)Fit.BaseType[Fit.UInt8].invalidValue))
                {
                    Console.Write("heart_rate,{0},bpm,", recordMesg.GetHeartRate());
                }
                Console.Write("\n");
            }
            else if (msg.Num == MesgNum.Hr)
            {
                HrMesg hrMesg = new HrMesg(msg);
                Console.Write("Data,{0},hr,", msg.LocalNum);
                int count;
                if ((hrMesg.GetTimestamp() != null) && (hrMesg.GetTimestamp().GetTimeStamp() != (uint)Fit.BaseType[Fit.UInt32].invalidValue))
                {
                    Console.Write("timestamp,{0},,", hrMesg.GetTimestamp().GetTimeStamp());
                }
                if (hrMesg.GetNumFilteredBpm() > 0)
                {
                    Console.Write("filtered_bpm,");
                    count = hrMesg.GetNumFilteredBpm();
                    for (int i = 0; i < count; i++)
                    {
                        Console.Write("{0}", hrMesg.GetFilteredBpm(i));
                        if (i < count - 1)
                        {
                            Console.Write("|");
                        }
                    }
                    Console.Write(",bpm,");
                }
                if (hrMesg.GetNumEventTimestamp12() > 0)
                {
                    Console.Write("event_timestamp_12,");
                    count = hrMesg.GetNumEventTimestamp12();
                    for (int i = 0; i < count; i++)
                    {
                        Console.Write("{0}", hrMesg.GetEventTimestamp12(i));
                        if (i < count - 1)
                        {
                            Console.Write("|");
                        }
                    }
                    Console.Write(",,");
                }
                if (hrMesg.GetNumEventTimestamp() > 0)
                {
                    Console.Write("event_timestamp,");
                    count = hrMesg.GetNumEventTimestamp();
                    for (int i = 0; i < count; i++)
                    {
                        Console.Write("{0:G}", hrMesg.GetEventTimestamp(i));
                        if (i < count - 1)
                        {
                            Console.Write("|");
                        }
                    }
                    Console.Write(",s,");
                }
                if (hrMesg.GetFractionalTimestamp() != null)
                {
                    Console.Write("fractional_timestamp,{0:0.######},s,", hrMesg.GetFractionalTimestamp());
                }
                Console.Write("\n");
            }
        }
Пример #19
0
 public FitRecord(RecordMesg msg, FitRecordList records)
 {
     AbsolutePressure           = msg.GetAbsolutePressure();
     AccumulatedPower           = msg.GetAccumulatedPower();
     ActivityType               = msg.GetActivityType();
     Altitude                   = msg.GetAltitude();
     BallSpeed                  = FitFile.GetSpeed(msg.GetBallSpeed());
     BatterySoc                 = msg.GetBatterySoc();
     Cadence256                 = msg.GetCadence256();
     Cadence                    = msg.GetCadence();
     Calories                   = msg.GetCalories();
     CnsLoad                    = msg.GetCnsLoad();
     CombinedPedalSmoothness    = msg.GetCombinedPedalSmoothness();
     CompressedAccumulatedPower = msg.GetCompressedAccumulatedPower();
     CompressedSpeedDistance    = FitFile.GetByteList(msg.GetNumCompressedSpeedDistance(), msg.GetCompressedSpeedDistance);
     CycleLength                = msg.GetCycleLength();
     Cycles                        = msg.GetCycles();
     Depth                         = msg.GetDepth();
     DeviceIndex                   = msg.GetDeviceIndex();
     Distance                      = FitFile.GetDistance(msg.GetDistance());
     EnhancedAltitude              = msg.GetEnhancedAltitude();
     EnhancedSpeed                 = FitFile.GetSpeed(msg.GetEnhancedSpeed());
     FractionalCadence             = msg.GetFractionalCadence();
     GpsAccuracy                   = msg.GetGpsAccuracy();
     Grade                         = msg.GetGrade();
     HeartRate                     = msg.GetHeartRate();
     LeftPco                       = msg.GetLeftPco();
     LeftPedalSmoothness           = msg.GetLeftPedalSmoothness();
     LeftPowerPhase                = FitFile.GetFloatList(msg.GetNumLeftPowerPhase(), msg.GetLeftPowerPhase);
     LeftPowerPhasePeak            = FitFile.GetFloatList(msg.GetNumLeftPowerPhasePeak(), msg.GetLeftPowerPhasePeak);
     LeftRightBalance              = msg.GetLeftRightBalance();
     LeftTorqueEffectiveness       = msg.GetLeftTorqueEffectiveness();
     MotorPower                    = msg.GetMotorPower();
     N2Load                        = msg.GetN2Load();
     NdlTime                       = msg.GetNdlTime();
     NextStopDepth                 = msg.GetNextStopDepth();
     NextStopTime                  = msg.GetNextStopTime();
     Latitude                      = FitFile.GetDegrees(msg.GetPositionLat());
     Longitude                     = FitFile.GetDegrees(msg.GetPositionLong());
     Power                         = msg.GetPower();
     Resistence                    = msg.GetResistance();
     RightPco                      = msg.GetRightPco();
     RightPedalSmoothness          = msg.GetRightPedalSmoothness();
     RightPowerPhase               = FitFile.GetFloatList(msg.GetNumRightPowerPhase(), msg.GetRightPowerPhase);
     RightPowerPhasePeak           = FitFile.GetFloatList(msg.GetNumRightPowerPhasePeak(), msg.GetRightPowerPhasePeak);
     RightTorqueEffectiveness      = msg.GetRightTorqueEffectiveness();
     SaturatedHemoglobinPercent    = msg.GetSaturatedHemoglobinPercent();
     SaturatedHemoglobinPercentMax = msg.GetSaturatedHemoglobinPercentMax();
     SaturatedHemoglobinPercentMin = msg.GetSaturatedHemoglobinPercentMin();
     Speed                         = FitFile.GetSpeed(msg.GetSpeed());
     Speed1s                       = FitFile.GetFloatList(msg.GetNumSpeed1s(), msg.GetSpeed1s);
     StanceTime                    = msg.GetStanceTime();
     StanceTimeBalance             = msg.GetStanceTimeBalance();
     StanceTimePercent             = msg.GetStanceTimePercent();
     StepLength                    = msg.GetStepLength();
     StrokeType                    = msg.GetStrokeType();
     Temperature                   = msg.GetTemperature();
     Time128                       = msg.GetTime128();
     TimeFromCourse                = FitFile.GetTimeSpan(msg.GetTimeFromCourse());
     Timestamp                     = FitFile.GetDateTime(msg.GetTimestamp());
     TotalCycles                   = msg.GetTotalCycles();
     TotalHemoglobinConc           = msg.GetTotalHemoglobinConc();
     TotalHemoglobinConcMax        = msg.GetTotalHemoglobinConcMax();
     TotalHemoglobinConcMin        = msg.GetTotalHemoglobinConcMin();
     VerticalOscillation           = msg.GetVerticalOscillation();
     VerticalRatio                 = msg.GetVerticalRatio();
     VerticalSpeed                 = FitFile.GetSpeed(msg.GetVerticalSpeed());
     Zone = msg.GetZone();
 }
Пример #20
0
        public static void EncodeCourse()
        {
            const string filename = "CourseEncodeRecipe.fit";

            // Example Record Data Defining a Course
            var courseData = new List <Dictionary <string, object> >()
            {
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262849U }, { "position_lat", 463583114 }, { "position_long", -1131028903 }, { "altitude", 329f }, { "distance", 0f }, { "speed", 0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262855U }, { "position_lat", 463583127 }, { "position_long", -1131031938 }, { "altitude", 328.6f }, { "distance", 22.03f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262869U }, { "position_lat", 463583152 }, { "position_long", -1131038159 }, { "altitude", 327.6f }, { "distance", 67.29f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262876U }, { "position_lat", 463583164 }, { "position_long", -1131041346 }, { "altitude", 327f }, { "distance", 90.52f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262876U }, { "position_lat", 463583164 }, { "position_long", -1131041319 }, { "altitude", 327f }, { "distance", 90.72f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262891U }, { "position_lat", 463588537 }, { "position_long", -1131041383 }, { "altitude", 327f }, { "distance", 140.72f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262891U }, { "position_lat", 463588549 }, { "position_long", -1131041383 }, { "altitude", 327f }, { "distance", 140.82f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262897U }, { "position_lat", 463588537 }, { "position_long", -1131038293 }, { "altitude", 327.6f }, { "distance", 163.26f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262911U }, { "position_lat", 463588512 }, { "position_long", -1131032041 }, { "altitude", 328.4f }, { "distance", 208.75f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262918U }, { "position_lat", 463588499 }, { "position_long", -1131028879 }, { "altitude", 329f }, { "distance", 231.8f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262918U }, { "position_lat", 463588499 }, { "position_long", -1131028903 }, { "altitude", 329f }, { "distance", 231.97f }, { "speed", 3.0f }
                },
                new Dictionary <string, object>()
                {
                    { "timestamp", 961262933U }, { "position_lat", 463583127 }, { "position_long", -1131028903 }, { "altitude", 329f }, { "distance", 281.96f }, { "speed", 3.0f }
                },
            };

            // Create the output stream, this can be any type of stream, including a file or memory stream. Must have read/write access.
            FileStream fitDest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

            // Create a FIT Encode object
            Encode encoder = new Encode(ProtocolVersion.V10);

            // Write the FIT header to the output stream
            encoder.Open(fitDest);

            // Reference points for the course
            var firstRecord    = courseData[0];
            var lastRecord     = courseData[courseData.Count - 1];
            var halfwayRecord  = courseData[courseData.Count / 2];
            var startTimestamp = (uint)firstRecord["timestamp"];
            var endTimestamp   = (uint)lastRecord["timestamp"];
            var startDateTime  = new Dynastream.Fit.DateTime(startTimestamp);
            var endDateTime    = new Dynastream.Fit.DateTime(endTimestamp);

            // Every FIT file MUST contain a File ID message
            var fileIdMesg = new FileIdMesg();

            fileIdMesg.SetType(Dynastream.Fit.File.Course);
            fileIdMesg.SetManufacturer(Manufacturer.Development);
            fileIdMesg.SetProduct(ProductId);
            fileIdMesg.SetTimeCreated(startDateTime);
            fileIdMesg.SetSerialNumber(startDateTime.GetTimeStamp());
            encoder.Write(fileIdMesg);

            // Every FIT file MUST contain a Course message
            var courseMesg = new CourseMesg();

            courseMesg.SetName("Garmin Field Day");
            courseMesg.SetSport(Sport.Cycling);
            encoder.Write(courseMesg);

            // Every FIT COURSE file MUST contain a Lap message
            var lapMesg = new LapMesg();

            lapMesg.SetStartTime(startDateTime);
            lapMesg.SetTimestamp(startDateTime);
            lapMesg.SetTotalElapsedTime(endTimestamp - startTimestamp);
            lapMesg.SetTotalTimerTime(endTimestamp - startTimestamp);
            lapMesg.SetStartPositionLat((int)firstRecord["position_lat"]);
            lapMesg.SetStartPositionLong((int)firstRecord["position_long"]);
            lapMesg.SetEndPositionLat((int)lastRecord["position_lat"]);
            lapMesg.SetEndPositionLong((int)lastRecord["position_long"]);
            lapMesg.SetTotalDistance((float)lastRecord["distance"]);
            encoder.Write(lapMesg);

            // Timer Events are REQUIRED for FIT COURSE files
            var eventMesgStart = new EventMesg();

            eventMesgStart.SetTimestamp(startDateTime);
            eventMesgStart.SetEvent(Event.Timer);
            eventMesgStart.SetEventType(EventType.Start);
            encoder.Write(eventMesgStart);

            // Every FIT COURSE file MUST contain Record messages
            foreach (var record in courseData)
            {
                var timestamp = (uint)record["timestamp"];
                var latitude  = (int)record["position_lat"];
                var longitude = (int)record["position_long"];
                var distance  = (float)record["distance"];
                var speed     = (float)record["speed"];
                var altitude  = (float)record["altitude"];

                var recordMesg = new RecordMesg();
                recordMesg.SetTimestamp(new Dynastream.Fit.DateTime(timestamp));
                recordMesg.SetPositionLat(latitude);
                recordMesg.SetPositionLong(longitude);
                recordMesg.SetDistance(distance);
                recordMesg.SetSpeed(speed);
                recordMesg.SetAltitude(altitude);
                encoder.Write(recordMesg);

                // Add a Course Point at the halfway point of the route
                if (record == halfwayRecord)
                {
                    var coursePointMesg = new CoursePointMesg();
                    coursePointMesg.SetTimestamp(new Dynastream.Fit.DateTime(timestamp));
                    coursePointMesg.SetName("Halfway");
                    coursePointMesg.SetType(CoursePoint.Generic);
                    coursePointMesg.SetPositionLat(latitude);
                    coursePointMesg.SetPositionLong(longitude);
                    coursePointMesg.SetDistance(distance);
                    encoder.Write(coursePointMesg);
                }
            }

            // Timer Events are REQUIRED for FIT COURSE files
            var eventMesgStop = new EventMesg();

            eventMesgStop.SetTimestamp(endDateTime);
            eventMesgStop.SetEvent(Event.Timer);
            eventMesgStop.SetEventType(EventType.StopAll);
            encoder.Write(eventMesgStop);

            // Update the data size in the header and calculate the CRC
            encoder.Close();

            // Close the output stream
            fitDest.Close();

            Console.WriteLine($"Encoded FIT file {fitDest.Name}");
        }
Пример #21
0
 public static void SetPositionLatInDegrees(this RecordMesg mesg, double lat)
 {
     mesg.SetPositionLat(Degree2SemiCircles(lat));
 }
Пример #22
0
        static public void CreateTimeBasedActivity()
        {
            const double TwoPI = Math.PI * 2.0;
            const double SemicirclesPerMeter = 107.173;
            const string FileName            = "ActivityEncodeRecipe.fit";

            var messages = new List <Mesg>();

            // The starting timestamp for the activity
            var startTime = new Dynastream.Fit.DateTime(System.DateTime.UtcNow);

            // Timer Events are a BEST PRACTICE for FIT ACTIVITY files
            var eventMesgStart = new EventMesg();

            eventMesgStart.SetTimestamp(startTime);
            eventMesgStart.SetEvent(Event.Timer);
            eventMesgStart.SetEventType(EventType.Start);
            messages.Add(eventMesgStart);

            // Create the Developer Id message for the developer data fields.
            var developerIdMesg = new DeveloperDataIdMesg();

            // It is a BEST PRACTICE to reuse the same Guid for all FIT files created by your platform
            byte[] appId = new Guid("00010203-0405-0607-0809-0A0B0C0D0E0F").ToByteArray();
            for (int i = 0; i < appId.Length; i++)
            {
                developerIdMesg.SetApplicationId(i, appId[i]);
            }
            developerIdMesg.SetDeveloperDataIndex(0);
            developerIdMesg.SetApplicationVersion(110);
            messages.Add(developerIdMesg);

            // Create the Developer Data Field Descriptions
            var doughnutsFieldDescMesg = new FieldDescriptionMesg();

            doughnutsFieldDescMesg.SetDeveloperDataIndex(0);
            doughnutsFieldDescMesg.SetFieldDefinitionNumber(0);
            doughnutsFieldDescMesg.SetFitBaseTypeId(FitBaseType.Float32);
            doughnutsFieldDescMesg.SetFieldName(0, "Doughnuts Earned");
            doughnutsFieldDescMesg.SetUnits(0, "doughnuts");
            doughnutsFieldDescMesg.SetNativeMesgNum(MesgNum.Session);
            messages.Add(doughnutsFieldDescMesg);

            FieldDescriptionMesg hrFieldDescMesg = new FieldDescriptionMesg();

            hrFieldDescMesg.SetDeveloperDataIndex(0);
            hrFieldDescMesg.SetFieldDefinitionNumber(1);
            hrFieldDescMesg.SetFitBaseTypeId(FitBaseType.Uint8);
            hrFieldDescMesg.SetFieldName(0, "Heart Rate");
            hrFieldDescMesg.SetUnits(0, "bpm");
            hrFieldDescMesg.SetNativeFieldNum(RecordMesg.FieldDefNum.HeartRate);
            hrFieldDescMesg.SetNativeMesgNum(MesgNum.Record);
            messages.Add(hrFieldDescMesg);

            // Every FIT ACTIVITY file MUST contain Record messages
            var timestamp = new Dynastream.Fit.DateTime(startTime);

            // Create one hour (3600 seconds) of Record data
            for (uint i = 0; i <= 3600; i++)
            {
                // Create a new Record message and set the timestamp
                var recordMesg = new RecordMesg();
                recordMesg.SetTimestamp(timestamp);

                // Fake Record Data of Various Signal Patterns
                recordMesg.SetDistance(i);                                                          // Ramp
                recordMesg.SetSpeed(1);                                                             // Flatline
                recordMesg.SetHeartRate((byte)((Math.Sin(TwoPI * (0.01 * i + 10)) + 1.0) * 127.0)); // Sine
                recordMesg.SetCadence((byte)(i % 255));                                             // Sawtooth
                recordMesg.SetPower((ushort)((i % 255) < 127 ? 150 : 250));                         // Square
                recordMesg.SetAltitude((float)Math.Abs(((double)i % 255.0) - 127.0));               // Triangle
                recordMesg.SetPositionLat(0);
                recordMesg.SetPositionLong((int)Math.Round(i * SemicirclesPerMeter));

                // Add a Developer Field to the Record Message
                var hrDevField = new DeveloperField(hrFieldDescMesg, developerIdMesg);
                recordMesg.SetDeveloperField(hrDevField);
                hrDevField.SetValue((byte)((Math.Sin(TwoPI * (0.01 * i + 10)) + 1.0) * 127.0)); // Sine

                // Write the Rercord message to the output stream
                messages.Add(recordMesg);

                // Increment the timestamp by one second
                timestamp.Add(1);
            }

            // Timer Events are a BEST PRACTICE for FIT ACTIVITY files
            var eventMesgStop = new EventMesg();

            eventMesgStop.SetTimestamp(timestamp);
            eventMesgStop.SetEvent(Event.Timer);
            eventMesgStop.SetEventType(EventType.StopAll);
            messages.Add(eventMesgStop);

            // Every FIT ACTIVITY file MUST contain at least one Lap message
            var lapMesg = new LapMesg();

            lapMesg.SetMessageIndex(0);
            lapMesg.SetTimestamp(timestamp);
            lapMesg.SetStartTime(startTime);
            lapMesg.SetTotalElapsedTime(timestamp.GetTimeStamp() - startTime.GetTimeStamp());
            lapMesg.SetTotalTimerTime(timestamp.GetTimeStamp() - startTime.GetTimeStamp());
            messages.Add(lapMesg);

            // Every FIT ACTIVITY file MUST contain at least one Session message
            var sessionMesg = new SessionMesg();

            sessionMesg.SetMessageIndex(0);
            sessionMesg.SetTimestamp(timestamp);
            sessionMesg.SetStartTime(startTime);
            sessionMesg.SetTotalElapsedTime(timestamp.GetTimeStamp() - startTime.GetTimeStamp());
            sessionMesg.SetTotalTimerTime(timestamp.GetTimeStamp() - startTime.GetTimeStamp());
            sessionMesg.SetSport(Sport.StandUpPaddleboarding);
            sessionMesg.SetSubSport(SubSport.Generic);
            sessionMesg.SetFirstLapIndex(0);
            sessionMesg.SetNumLaps(1);

            // Add a Developer Field to the Session message
            var doughnutsEarnedDevField = new DeveloperField(doughnutsFieldDescMesg, developerIdMesg);

            doughnutsEarnedDevField.SetValue(sessionMesg.GetTotalElapsedTime() / 1200.0f);
            sessionMesg.SetDeveloperField(doughnutsEarnedDevField);
            messages.Add(sessionMesg);

            // Every FIT ACTIVITY file MUST contain EXACTLY one Activity message
            var activityMesg = new ActivityMesg();

            activityMesg.SetTimestamp(timestamp);
            activityMesg.SetNumSessions(1);
            var timezoneOffset = (int)TimeZoneInfo.Local.BaseUtcOffset.TotalSeconds;

            activityMesg.SetLocalTimestamp((uint)((int)timestamp.GetTimeStamp() + timezoneOffset));
            activityMesg.SetTotalTimerTime(timestamp.GetTimeStamp() - startTime.GetTimeStamp());
            messages.Add(activityMesg);

            CreateActivityFile(messages, FileName, startTime);
        }
Пример #23
0
 public static float GetPositionLongInDegrees(this RecordMesg mesg)
 {
     return(SemiCircles2Degree(mesg.GetPositionLong().GetValueOrDefault()));
 }
Пример #24
0
        /// <summary>
        /// Encode to a fit file
        /// </summary>
        /// <param name="db"></param>
        /// <param name="outputFileName"></param>
        public static void EncodeActivityFile(TrainingCenterDatabase_t db, string outputFileName)
        {
            /*
             *          string assemblyFilePath = Assembly.GetExecutingAssembly().GetName().CodeBase;
             *          string assemblyPath = assemblyFilePath.Replace(SysPath.GetFileName(assemblyFilePath), "");
             */

            if (string.IsNullOrEmpty(outputFileName))
            {
                outputFileName = "out";
            }

            //outputFileName = assemblyPath + outputFileName;

            //  write FIT file
            Encode        encoder = new Encode(ProtocolVersion.V20);
            SysFileStream fitDest = new SysFileStream(outputFileName + ".fit",
                                                      SysFileMode.Create,
                                                      SysFileAccess.ReadWrite,
                                                      SysFileShare.Read);

            // Write our header
            encoder.Open(fitDest);

            // FIT file_id message
            FileIdMesg fileIdMesg = new FileIdMesg();

            fileIdMesg.SetType(File.Activity);  // Activity File = 4
            fileIdMesg.SetManufacturer(Manufacturer.Garmin);
            fileIdMesg.SetProduct(GarminProduct.Fr935);
            fileIdMesg.SetSerialNumber(3949668594);
            fileIdMesg.SetTimeCreated(new DateTime(db.Activities.Activity[0].Id));    // set from input file

            // Encode each message, a definition message is automatically generated and output if necessary
            encoder.OnMesgDefinition(new MesgDefinition(fileIdMesg));
            encoder.OnMesg(fileIdMesg);
            Utils.LogMessage(LogType.Information, "Wrote FileID");

            // FIT FileCreator
            FileCreatorMesg fileCreatorMesg = new FileCreatorMesg();

            fileCreatorMesg.SetSoftwareVersion(600);    // Garmin Connect
            encoder.OnMesgDefinition(new MesgDefinition(fileCreatorMesg));
            encoder.OnMesg(fileCreatorMesg);
            Utils.LogMessage(LogType.Information, "Wrote FileCreatorMesg");

            // FIT event message : not found in TCX
            EventMesg eventMesg = new EventMesg();

            eventMesg.SetTimestamp(new DateTime(db.Activities.Activity[0].Id));
            eventMesg.SetData(0);
            eventMesg.SetEvent(Event.Timer);
            eventMesg.SetEventType(EventType.Start);
            eventMesg.SetEventGroup(0);
            encoder.OnMesgDefinition(new MesgDefinition(eventMesg));
            encoder.OnMesg(eventMesg);
            Utils.LogMessage(LogType.Information, "Wrote EventMesg");

            // FIT deviceInfo message: not found in TCX
            DeviceInfoMesg devInfoMesg = new DeviceInfoMesg();

            devInfoMesg.SetTimestamp(new DateTime(db.Activities.Activity[0].Id));
            devInfoMesg.SetSerialNumber(3949668594);
            devInfoMesg.SetManufacturer(Manufacturer.Garmin);
            devInfoMesg.SetProduct(GarminProduct.Fr935);
            devInfoMesg.SetSoftwareVersion(6);
            devInfoMesg.SetDeviceIndex(0);
            devInfoMesg.SetSourceType(SourceType.Local);
            for (int i = 0; i < 4; i++)
            {
                encoder.OnMesgDefinition(new MesgDefinition(devInfoMesg));
                encoder.OnMesg(devInfoMesg);
                Utils.LogMessage(LogType.Information, "Wrote DeviceInfoMesg");
            }

            // FIT deviceSettings message: not found in TCX
            DeviceSettingsMesg devSettingsMesg = new DeviceSettingsMesg();

            devSettingsMesg.SetUtcOffset(0);
            devSettingsMesg.SetTimeOffset(7, 0);
            devSettingsMesg.SetAutoActivityDetect(0);
            devSettingsMesg.SetAutosyncMinSteps(2000);
            devSettingsMesg.SetAutosyncMinTime(240);
            devSettingsMesg.SetActiveTimeZone(0);
            devSettingsMesg.SetActivityTrackerEnabled(Bool.True);
            devSettingsMesg.SetMountingSide(Side.Left);
            devSettingsMesg.SetTimeMode(1, TimeMode.Utc);
            encoder.OnMesgDefinition(new MesgDefinition(devSettingsMesg));
            encoder.OnMesg(devSettingsMesg);
            Utils.LogMessage(LogType.Information, "Wrote DeviceSettingsMesg");

            // FIT UserProfile message: : not found in TCX
            UserProfileMesg userProfileMesg = new UserProfileMesg();

            userProfileMesg.SetActivityClass(ActivityClass.Level);
            encoder.OnMesgDefinition(new MesgDefinition(userProfileMesg));
            encoder.OnMesg(userProfileMesg);
            Utils.LogMessage(LogType.Information, "Wrote UserProfileMesg");

            // FIT Sport:
            SportMesg sportMesg = new SportMesg();

            sportMesg.SetSport(Sport.Running);
            sportMesg.SetSubSport(SubSport.Road);

            // Encode each message, a definition message is automatically generated and output if necessary
            encoder.OnMesgDefinition(new MesgDefinition(sportMesg));
            encoder.OnMesg(sportMesg);
            Utils.LogMessage(LogType.Information, "Wrote SportMesg");

            // create FIT record and lap message
            double totalTime = 0;

            foreach (Activity_t act in db.Activities.Activity)
            {
                foreach (ActivityLap_t lap in act.Lap)
                {
                    List <RecordMesg> records = new List <RecordMesg>();

                    // FIT Record message:
                    foreach (Trackpoint_t trackPoint in lap.Track)
                    {
                        RecordMesg recMesg = new RecordMesg();

                        recMesg.SetTimestamp(new DateTime(trackPoint.Time));
                        recMesg.SetPositionLat(Utils.ConvertTcxLatLongToFit(trackPoint.Position.LatitudeDegrees));
                        recMesg.SetPositionLong(Utils.ConvertTcxLatLongToFit(trackPoint.Position.LongitudeDegrees));
                        recMesg.SetDistance((float)trackPoint.DistanceMeters);
                        recMesg.SetAltitude((float)trackPoint.AltitudeMeters);
                        //recMesg.SetSpeed((float)trackPoint.Extensions.Any["Speed"]);
                        if (trackPoint.HeartRateBpm != null)
                        {
                            recMesg.SetHeartRate((byte)trackPoint.HeartRateBpm.Value);
                        }

                        // Extension
                        if (trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:RunCadence") != null)
                        {
                            if (trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:RunCadence").Count == 1)
                            {
                                int cadence = 0;
                                int.TryParse((trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:RunCadence")[0].InnerText), out cadence);
                                recMesg.SetCadence((byte)cadence);
                            }
                        }

                        if (trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:Speed") != null)
                        {
                            if (trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:Speed").Count == 1)
                            {
                                double speed = 0;
                                double.TryParse((trackPoint.Extensions.Any[0].GetElementsByTagName("ns3:Speed")[0].InnerText), out speed);
                                recMesg.SetSpeed((float)speed);
                            }
                        }

                        encoder.OnMesgDefinition(new MesgDefinition(recMesg));
                        encoder.OnMesg(recMesg);
                        Utils.LogMessage(LogType.Information, string.Format("Wrote RecMesg: {0}", recMesg.GetTimestamp().ToString()));
                    }

                    LapMesg lapMesg = new LapMesg();
                    lapMesg.SetTimestamp(new DateTime(lap.StartTime));
                    lapMesg.SetStartTime(new DateTime(lap.StartTime));
                    lapMesg.SetTotalMovingTime((float)lap.TotalTimeSeconds);
                    lapMesg.SetStartPositionLat(Utils.ConvertTcxLatLongToFit(lap.Track[0].Position.LatitudeDegrees));
                    lapMesg.SetStartPositionLong(Utils.ConvertTcxLatLongToFit(lap.Track[0].Position.LongitudeDegrees));
                    lapMesg.SetSport(Sport.Running);
                    lapMesg.SetSubSport(SubSport.Road);
                    // TODO: EndPosition
                    lapMesg.SetTotalElapsedTime((float)lap.TotalTimeSeconds);
                    totalTime += lap.TotalTimeSeconds;
                    // TODO: The rest

                    encoder.OnMesgDefinition(new MesgDefinition(lapMesg));
                    encoder.OnMesg(lapMesg);
                    Utils.LogMessage(LogType.Information, "Wrote LapMesg");
                }
            }

            // FIT Session: not found in tcx
            // FIT Activity
            ActivityMesg activityMesg = new ActivityMesg();

            activityMesg.SetTimestamp(new DateTime(db.Activities.Activity[0].Id));
            activityMesg.SetTotalTimerTime((float)totalTime);
            activityMesg.SetLocalTimestamp(new DateTime(db.Activities.Activity[0].Id).GetTimeStamp() + (uint)totalTime);
            activityMesg.SetNumSessions(1);
            activityMesg.SetType(Activity.Manual);
            activityMesg.SetEvent(Event.Activity);
            activityMesg.SetEventType(EventType.Stop);
            //activityMesg.SetTotalTimerTime(db.Activities.Activity[0].Training.);

            encoder.OnMesgDefinition(new MesgDefinition(activityMesg));
            encoder.OnMesg(activityMesg);
            Utils.LogMessage(LogType.Information, "Wrote ActivityMesg");

            // Update header datasize and file CRC
            encoder.Close();
            fitDest.Close();

            Console.WriteLine("Encoded FIT file " + outputFileName + ".fit");
        }
Пример #25
0
        private Dynastream.Fit.DateTime AddMetrics(ICollection <Mesg> messages, WorkoutSamples workoutSamples, Dynastream.Fit.DateTime startTime)
        {
            var allMetrics        = workoutSamples.Metrics;
            var hrMetrics         = allMetrics.FirstOrDefault(m => m.Slug == "heart_rate");
            var outputMetrics     = allMetrics.FirstOrDefault(m => m.Slug == "output");
            var cadenceMetrics    = allMetrics.FirstOrDefault(m => m.Slug == "cadence");
            var speedMetrics      = GetSpeedSummary(workoutSamples);
            var resistanceMetrics = allMetrics.FirstOrDefault(m => m.Slug == "resistance");
            var inclineMetrics    = GetGradeSummary(workoutSamples);
            var locationMetrics   = workoutSamples.Location_Data?.SelectMany(x => x.Coordinates).ToArray();
            var altitudeMetrics   = allMetrics.FirstOrDefault(m => m.Slug == "altitude");

            var recordsTimeStamp = new Dynastream.Fit.DateTime(startTime);

            if (workoutSamples.Seconds_Since_Pedaling_Start is object)
            {
                for (var i = 0; i < workoutSamples.Seconds_Since_Pedaling_Start.Count; i++)
                {
                    var record = new RecordMesg();
                    record.SetTimestamp(recordsTimeStamp);

                    if (speedMetrics is object && i < speedMetrics.Values.Length)
                    {
                        record.SetSpeed(ConvertToMetersPerSecond(speedMetrics.GetValue(i), workoutSamples));
                    }

                    if (hrMetrics is object && i < hrMetrics.Values.Length)
                    {
                        record.SetHeartRate((byte)hrMetrics.Values[i]);
                    }

                    if (cadenceMetrics is object && i < cadenceMetrics.Values.Length)
                    {
                        record.SetCadence((byte)cadenceMetrics.Values[i]);
                    }

                    if (outputMetrics is object && i < outputMetrics.Values.Length)
                    {
                        record.SetPower((ushort)outputMetrics.Values[i]);
                    }

                    if (resistanceMetrics is object && i < resistanceMetrics.Values.Length)
                    {
                        var resistancePercent = resistanceMetrics.Values[i] / 1;
                        record.SetResistance((byte)(254 * resistancePercent));
                    }

                    if (altitudeMetrics is object && i < altitudeMetrics.Values.Length)
                    {
                        var altitude = ConvertDistanceToMeters(altitudeMetrics.GetValue(i), altitudeMetrics.Display_Unit);
                        record.SetAltitude(altitude);
                    }

                    if (inclineMetrics is object && i < inclineMetrics.Values.Length)
                    {
                        record.SetGrade((float)inclineMetrics.GetValue(i));
                    }

                    if (locationMetrics is object && i < locationMetrics.Length)
                    {
                        // unit is semicircles
                        record.SetPositionLat(ConvertDegreesToSemicircles(locationMetrics[i].Latitude));
                        record.SetPositionLong(ConvertDegreesToSemicircles(locationMetrics[i].Longitude));
                    }

                    messages.Add(record);
                    recordsTimeStamp.Add(1);
                }
            }

            return(recordsTimeStamp);
        }
Пример #26
0
 public ExtendedRecordMesg(RecordMesg mesg) : base(mesg)
 {
     EventType = EventType.Invalid;
 }
Пример #27
0
        static void EncodeActivityFile()
        {
            // Generate some FIT messages
            var fileIdMesg      = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message
            var developerIdMesg = new DeveloperDataIdMesg();
            var fieldDescMesg   = new FieldDescriptionMesg();

            var records = new List <RecordMesg>();

            byte[] appId =
            {
                1,   2,  3,  4,
                5,   6,  7,  8,
                9,  10, 11, 12,
                13, 14, 15, 16
            };

            fileIdMesg.SetType(Dynastream.Fit.File.Activity);
            fileIdMesg.SetManufacturer(Manufacturer.Development);
            fileIdMesg.SetProduct(1);
            fileIdMesg.SetSerialNumber(12345);
            fileIdMesg.SetTimeCreated(new DateTime(621463080));

            for (int i = 0; i < appId.Length; i++)
            {
                developerIdMesg.SetApplicationId(i, appId[i]);
            }
            developerIdMesg.SetDeveloperDataIndex(0);

            fieldDescMesg.SetDeveloperDataIndex(0);
            fieldDescMesg.SetFieldDefinitionNumber(0);
            fieldDescMesg.SetFitBaseTypeId(FitBaseType.Sint8);
            fieldDescMesg.SetFieldName(0, "doughnuts_earned");
            fieldDescMesg.SetUnits(0, "doughnuts");

            for (int i = 0; i < 3; i++)
            {
                var newRecord            = new RecordMesg();
                var doughnutsEarnedField = new DeveloperField(fieldDescMesg, developerIdMesg);
                newRecord.SetDeveloperField(doughnutsEarnedField);

                newRecord.SetHeartRate((byte)(140 + (i * 2)));
                newRecord.SetCadence((byte)(88 + (i * 2)));
                newRecord.SetDistance(510 + (i * 100));
                newRecord.SetSpeed(2.8f + (i * 0.4f));
                doughnutsEarnedField.SetValue(i + 1);

                records.Add(newRecord);
            }

            // Create file encode object
            Encode encodeDemo = new Encode(ProtocolVersion.V20);

            FileStream fitDest = new FileStream("ExampleActivity.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

            // Write our header
            encodeDemo.Open(fitDest);

            // Encode each message, a definition message is automatically generated and output if necessary
            encodeDemo.Write(fileIdMesg);
            encodeDemo.Write(developerIdMesg);
            encodeDemo.Write(fieldDescMesg);
            encodeDemo.Write(records);

            // Update header datasize and file CRC
            encodeDemo.Close();
            fitDest.Close();

            Console.WriteLine("Encoded FIT file ExampleActivity.fit");
        }
Пример #28
0
        void BtnSaveFitClick(object sender, EventArgs e)
        {
            if (saveFileDlg.ShowDialog() == DialogResult.OK)
            {
                FileIdMesg fileIdMesg = new FileIdMesg();
                fileIdMesg.SetManufacturer(Manufacturer.Dynastream);
                fileIdMesg.SetProduct(1000);
                fileIdMesg.SetSerialNumber(12345);

                FileStream fitDest = new FileStream(saveFileDlg.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);

                // create file encode object
                Encode encode = new Encode();
                // write out header
                encode.Open(fitDest);
                // encode each message, a definition message is automatically generated and output if necessary
                encode.Write(fileIdMesg);

                SQLiteCommand cmd = new SQLiteCommand(_db);
                string        sql;
                // TODO: write FIT File Summary Information here, suspect that's why GarminConnect
                // is rejecting it because the summary data isn't there.
                sql             = string.Format("select fs.*, f.fileActivityDateTime from FileSummary fs join File f on f.idFile = fs.idFile where fs.idFile = {0}", _file);
                cmd.CommandText = sql;
                SQLiteDataReader rdr_summary = cmd.ExecuteReader();
                if (rdr_summary.HasRows)
                {
                    rdr_summary.Read();
                    SessionMesg sess = new SessionMesg();
                    //"fsMaxHeartRate" INTEGER,"fsMaxCadence" INTEGER,"fsMaxSpeed" FLOAT, "fsAvgPower" FLOAT, "fsMaxPower" FLOAT)
                    sess.SetTotalElapsedTime((float)Convert.ToDouble(rdr_summary["fsDuration"]));
                    sess.SetTotalMovingTime((float)Convert.ToDouble(rdr_summary["fsMovingTime"]));
                    sess.SetTotalTimerTime((float)Convert.ToDouble(rdr_summary["fsMovingTime"]));
                    sess.SetTotalDistance((float)(Convert.ToDouble(rdr_summary["fsDistance"]) * 1609.344));                     // convert miles back to metres
                    sess.SetTotalCalories(Convert.ToUInt16(rdr_summary["fsCalories"]));
                    sess.SetAvgHeartRate(Convert.ToByte(Convert.ToInt32(rdr_summary["fsAvgHeart"])));
                    sess.SetAvgCadence(Convert.ToByte(Convert.ToInt32(rdr_summary["fsAvgCadence"])));
                    sess.SetAvgSpeed((float)(Convert.ToDouble(rdr_summary["fsAvgSpeed"]) / 2.23693629));
                    sess.SetTotalAscent(Convert.ToUInt16(Convert.ToDouble(rdr_summary["fsTotalAscent"]) / 3.2808399));
                    sess.SetTotalDescent(Convert.ToUInt16(Convert.ToDouble(rdr_summary["fsTotalDescent"]) / 3.2808399));
                    sess.SetMaxHeartRate(Convert.ToByte(Convert.ToInt32(rdr_summary["fsMaxHeartRate"])));
                    sess.SetMaxCadence(Convert.ToByte(Convert.ToInt32(rdr_summary["fsMaxCadence"])));
                    sess.SetMaxSpeed(Convert.ToByte(Convert.ToInt32(rdr_summary["fsMaxSpeed"]) / 2.23693629));

                    Dynastream.Fit.DateTime dt_start = new Dynastream.Fit.DateTime(Convert.ToDateTime(rdr_summary["fileActivityDateTime"]));
                    sess.SetStartTime(dt_start);
                    encode.Write(sess);
                }
                rdr_summary.Close();

                // load and process the archived trackpoints for file

                sql             = string.Format("select * from FileTrackpoints where idFile = {0}", _file);
                cmd.CommandText = sql;
                SQLiteDataReader rdr = cmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        // TODO: need to handle `0` lng/lat coordinates so that we're not mapped
                        // as being in the middle of the atlantic ! :-)
                        RecordMesg rec             = new RecordMesg();
                        Dynastream.Fit.DateTime dt = new Dynastream.Fit.DateTime(System.DateTime.Parse((string)rdr["tpTime"]));
                        rec.SetTimestamp(dt);
                        rec.SetDistance((float)Convert.ToDouble(rdr["tpDistance"]));
                        rec.SetHeartRate(Convert.ToByte(Convert.ToInt32(rdr["tpHeart"])));
                        rec.SetCadence(Convert.ToByte(Convert.ToInt32(rdr["tpCadence"])));
                        rec.SetTemperature(Convert.ToSByte(Convert.ToInt32(rdr["tpTemperature"])));
                        rec.SetAltitude((float)(Convert.ToDouble(rdr["tpAltitude"]) / 3.2808399));                         // converted back to metres from ft in db
                        rec.SetPositionLong((int)GeoMath.degrees_to_semicircle(Convert.ToDouble(rdr["tpLongitude"])));
                        rec.SetPositionLat((int)GeoMath.degrees_to_semicircle(Convert.ToDouble(rdr["tpLatitude"])));
                        rec.SetSpeed((float)Convert.ToDouble(rdr["tpSpeed"]));
                        encode.Write(rec);
                    }
                }

                encode.Close();
                fitDest.Close();

                MessageBox.Show("File Saved Successfully");

                System.Diagnostics.Process.Start(saveFileDlg.FileName);
            }
            else
            {
            }
        }