static void CreateActivityFile(List <Mesg> messages, String filename, Dynastream.Fit.DateTime startTime) { // The combination of file type, manufacturer id, product id, and serial number should be unique. // When available, a non-random serial number should be used. Dynastream.Fit.File fileType = Dynastream.Fit.File.Activity; ushort manufacturerId = Manufacturer.Development; ushort productId = 0; float softwareVersion = 1.0f; Random random = new Random(); uint serialNumber = (uint)random.Next(); // Every FIT file MUST contain a File ID message var fileIdMesg = new FileIdMesg(); fileIdMesg.SetType(fileType); fileIdMesg.SetManufacturer(manufacturerId); fileIdMesg.SetProduct(productId); fileIdMesg.SetTimeCreated(startTime); fileIdMesg.SetSerialNumber(serialNumber); // A Device Info message is a BEST PRACTICE for FIT ACTIVITY files var deviceInfoMesg = new DeviceInfoMesg(); deviceInfoMesg.SetDeviceIndex(DeviceIndex.Creator); deviceInfoMesg.SetManufacturer(Manufacturer.Development); deviceInfoMesg.SetProduct(productId); deviceInfoMesg.SetProductName("FIT Cookbook"); // Max 20 Chars deviceInfoMesg.SetSerialNumber(serialNumber); deviceInfoMesg.SetSoftwareVersion(softwareVersion); deviceInfoMesg.SetTimestamp(startTime); // 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.V20); // Write the FIT header to the output stream encoder.Open(fitDest); // Write the messages to the file, in the proper sequence encoder.Write(fileIdMesg); encoder.Write(deviceInfoMesg); foreach (Mesg message in messages) { encoder.Write(message); } // 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}"); }
static void CreateWorkout(WorkoutMesg workoutMesg, List <WorkoutStepMesg> workoutSteps) { // The combination of file type, manufacturer id, product id, and serial number should be unique. // When available, a non-random serial number should be used. Dynastream.Fit.File fileType = Dynastream.Fit.File.Workout; ushort manufacturerId = Manufacturer.Development; ushort productId = 0; Random random = new Random(); uint serialNumber = (uint)random.Next(); // Every FIT file MUST contain a File ID message var fileIdMesg = new FileIdMesg(); fileIdMesg.SetType(fileType); fileIdMesg.SetManufacturer(manufacturerId); fileIdMesg.SetProduct(productId); fileIdMesg.SetTimeCreated(new Dynastream.Fit.DateTime(System.DateTime.UtcNow)); fileIdMesg.SetSerialNumber(serialNumber); // 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($"{workoutMesg.GetWktNameAsString().Replace(' ', '_')}.fit", 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); // Write the messages to the file, in the proper sequence encoder.Write(fileIdMesg); encoder.Write(workoutMesg); foreach (WorkoutStepMesg workoutStep in workoutSteps) { encoder.Write(workoutStep); } // 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}"); }
public static void Start(DateTime?start = null) { Trace.TraceInformation("Start()"); var activityFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\BeamNG.drive\\VeloActivities"; if (!Directory.Exists(activityFolder)) { Directory.CreateDirectory(activityFolder); } var filepath = activityFolder + "\\" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".fit"; csvFile = new StreamWriter(filepath + ".csv", true); startTime = start ?? DateTime.UtcNow; totalDistance = 0; // Create file encode object encoder = new Encode(ProtocolVersion.V20); fitDest = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); encoder.Open(fitDest); var fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message fileIdMesg.SetType(Dynastream.Fit.File.Activity); fileIdMesg.SetManufacturer(Manufacturer.Bryton); // Types defined in the profile are available fileIdMesg.SetProduct(1704); fileIdMesg.SetSerialNumber(5122); fileIdMesg.SetTimeCreated(new Dynastream.Fit.DateTime(startTime)); // Encode each message, a definition message is automatically generated and output if necessary encoder.Write(fileIdMesg); Resume(start); sessionMesg = new SessionMesg(); sessionMesg.SetStartTime(new Dynastream.Fit.DateTime(startTime)); currentLapMesg = new LapMesg(); currentLapMesg.SetStartTime(new Dynastream.Fit.DateTime(startTime)); }
/// <summary> /// Demonstrate the encoding of a 'Settings File' by writing a 'Settings File' containing a 'User Profile' Message. /// This example is simpler than the 'Monitoring File' example. /// </summary> static void EncodeSettingsFile() { // Generate some FIT messages FileIdMesg fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message fileIdMesg.SetType(Dynastream.Fit.File.Settings); fileIdMesg.SetManufacturer(Manufacturer.Development); // Types defined in the profile are available fileIdMesg.SetProduct(1); 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")); FileStream fitDest = new FileStream("ExampleSettings.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read); // Create file encode object Encode encodeDemo = new Encode(ProtocolVersion.V10); // 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); // Update header datasize and file CRC encodeDemo.Close(); fitDest.Close(); Console.WriteLine("Encoded FIT file ExampleSettings.fit"); return; }
static public void Start() { var assettoFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Assetto Corsa\\SimCyclingActivities"; if (!Directory.Exists(assettoFolder)) { Directory.CreateDirectory(assettoFolder); } var filepath = assettoFolder + "\\" + System.DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".fit"; var now = new DateTime(System.DateTime.Now); // Create file encode object encoder = new Encode(ProtocolVersion.V20); fitDest = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); encoder.Open(fitDest); var fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message fileIdMesg.SetType(Dynastream.Fit.File.Activity); fileIdMesg.SetManufacturer(Manufacturer.Dynastream); // Types defined in the profile are available fileIdMesg.SetProduct(22); fileIdMesg.SetSerialNumber(1234); fileIdMesg.SetTimeCreated(now); // Encode each message, a definition message is automatically generated and output if necessary encoder.Write(fileIdMesg); sessionMesg = new SessionMesg(); sessionMesg.SetStartTime(now); currentLapMesg = new LapMesg(); currentLapMesg.SetStartTime(now); }
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"); }
/// <summary> /// Demonstrates encoding a 'MonitoringB File' of a made up device which counts steps and reports the battery status of the device. /// </summary> static void EncodeMonitoringFile() { System.DateTime systemStartTime = System.DateTime.Now; System.DateTime systemTimeNow = systemStartTime; FileStream fitDest = new FileStream("ExampleMonitoringFile.fit", FileMode.Create, FileAccess.ReadWrite, FileShare.Read); // Create file encode object Encode encodeDemo = new Encode(ProtocolVersion.V10); // Write our header encodeDemo.Open(fitDest); // Generate some FIT messages FileIdMesg fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message fileIdMesg.SetTimeCreated(new Dynastream.Fit.DateTime(systemTimeNow)); fileIdMesg.SetManufacturer(Manufacturer.Development); fileIdMesg.SetProduct(1); fileIdMesg.SetSerialNumber(12345); fileIdMesg.SetNumber(0); fileIdMesg.SetType(Dynastream.Fit.File.MonitoringB); // See the 'FIT FIle Types Description' document for more information about this file type. encodeDemo.Write(fileIdMesg); // Write the 'File ID Message' DeviceInfoMesg deviceInfoMesg = new DeviceInfoMesg(); deviceInfoMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow)); deviceInfoMesg.SetSerialNumber(12345); deviceInfoMesg.SetManufacturer(Manufacturer.Development); deviceInfoMesg.SetBatteryStatus(Dynastream.Fit.BatteryStatus.Good); encodeDemo.Write(deviceInfoMesg); MonitoringMesg monitoringMesg = new MonitoringMesg(); // By default, each time a new message is written the Local Message Type 0 will be redefined to match the new message. // In this case,to avoid having a definition message each time there is a DeviceInfoMesg, we can manually set the Local Message Type of the MonitoringMessage to '1'. // By doing this we avoid an additional 7 definition messages in our FIT file. monitoringMesg.LocalNum = 1; // Simulate some data Random numberOfCycles = new Random(); // Fake a number of cycles for (int i = 0; i < 4; i++) // Each of these loops represent a quarter of a day { for (int j = 0; j < 6; j++) // Each of these loops represent 1 hour { monitoringMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow)); monitoringMesg.SetActivityType(Dynastream.Fit.ActivityType.Walking); // Setting this to walking will cause Cycles to be interpretted as steps. monitoringMesg.SetCycles(monitoringMesg.GetCycles() + numberOfCycles.Next(0, 1000)); // Cycles are accumulated (i.e. must be increasing) encodeDemo.Write(monitoringMesg); systemTimeNow = systemTimeNow.AddHours(1); // Add an hour to our contrieved timestamp } deviceInfoMesg.SetTimestamp(new Dynastream.Fit.DateTime(systemTimeNow)); deviceInfoMesg.SetBatteryStatus(Dynastream.Fit.BatteryStatus.Good); // Report the battery status every quarter day encodeDemo.Write(deviceInfoMesg); } // Update header datasize and file CRC encodeDemo.Close(); fitDest.Close(); Console.WriteLine("Encoded FIT file ExampleMonitoringFile.fit"); }
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(); }
protected override Tuple <string, ICollection <Mesg> > Convert(Workout workout, WorkoutSamples workoutSamples) { // MESSAGE ORDER MATTERS var messages = new List <Mesg>(); var startTime = GetStartTimeUtc(workout); var endTime = GetEndTimeUtc(workout); var title = WorkoutHelper.GetTitle(workout); var sport = GetGarminSport(workout); var subSport = GetGarminSubSport(workout); if (sport == Sport.Invalid) { Log.Warning("Unsupported Sport Type - Skipping {@Sport}", workout.Fitness_Discipline); return(new Tuple <string, ICollection <Mesg> >(string.Empty, null)); } var fileIdMesg = new FileIdMesg(); fileIdMesg.SetSerialNumber(_serialNumber); fileIdMesg.SetTimeCreated(startTime); fileIdMesg.SetManufacturer(_manufacturerId); fileIdMesg.SetProduct(_productId); fileIdMesg.SetType(Dynastream.Fit.File.Activity); messages.Add(fileIdMesg); var eventMesg = new EventMesg(); eventMesg.SetTimestamp(startTime); eventMesg.SetData(0); eventMesg.SetEvent(Event.Timer); eventMesg.SetEventType(EventType.Start); eventMesg.SetEventGroup(0); messages.Add(eventMesg); var deviceInfoMesg = new DeviceInfoMesg(); deviceInfoMesg.SetTimestamp(startTime); deviceInfoMesg.SetSerialNumber(_serialNumber); deviceInfoMesg.SetManufacturer(_manufacturerId); deviceInfoMesg.SetProduct(_productId); deviceInfoMesg.SetSoftwareVersion(_softwareVersion); deviceInfoMesg.SetDeviceIndex(0); deviceInfoMesg.SetSourceType(SourceType.Local); deviceInfoMesg.SetProductName("PelotonToGarmin"); // Max 20 Chars messages.Add(deviceInfoMesg); var userProfileMesg = new UserProfileMesg(); userProfileMesg.SetPowerSetting(DisplayPower.PercentFtp); messages.Add(userProfileMesg); var sportMesg = new SportMesg(); sportMesg.SetSport(sport); sportMesg.SetSubSport(subSport); messages.Add(sportMesg); var zoneTargetMesg = new ZonesTargetMesg(); zoneTargetMesg.SetFunctionalThresholdPower((ushort)workout.Ftp_Info.Ftp); zoneTargetMesg.SetPwrCalcType(PwrZoneCalc.PercentFtp); var maxHr = GetUserMaxHeartRate(workoutSamples); if (maxHr is object) { zoneTargetMesg.SetMaxHeartRate(maxHr.Value); zoneTargetMesg.SetHrCalcType(HrZoneCalc.PercentMaxHr); } messages.Add(zoneTargetMesg); var trainingMesg = new TrainingFileMesg(); trainingMesg.SetTimestamp(startTime); trainingMesg.SetTimeCreated(startTime); trainingMesg.SetSerialNumber(_serialNumber); trainingMesg.SetManufacturer(_manufacturerId); trainingMesg.SetProduct(_productId); trainingMesg.SetType(Dynastream.Fit.File.Workout); messages.Add(trainingMesg); AddMetrics(messages, workoutSamples, startTime); var workoutSteps = new List <WorkoutStepMesg>(); var laps = new List <LapMesg>(); if (workoutSamples.Target_Performance_Metrics?.Target_Graph_Metrics?.FirstOrDefault(w => w.Type == "cadence")?.Graph_Data is object) { var stepsAndLaps = GetWorkoutStepsAndLaps(workoutSamples, startTime, sport, subSport); workoutSteps = stepsAndLaps.Values.Select(v => v.Item1).ToList(); laps = stepsAndLaps.Values.Select(v => v.Item2).ToList(); } else { laps = GetWorkoutLaps(workoutSamples, startTime, sport, subSport).ToList(); } var workoutMesg = new WorkoutMesg(); workoutMesg.SetWktName(title.Replace(_spaceSeparator, " ")); workoutMesg.SetCapabilities(32); workoutMesg.SetSport(sport); workoutMesg.SetSubSport(subSport); workoutMesg.SetNumValidSteps((ushort)workoutSteps.Count); messages.Add(workoutMesg); // add steps in order foreach (var step in workoutSteps) { messages.Add(step); } // Add laps in order foreach (var lap in laps) { messages.Add(lap); } messages.Add(GetSessionMesg(workout, workoutSamples, startTime, endTime, (ushort)laps.Count)); var activityMesg = new ActivityMesg(); activityMesg.SetTimestamp(endTime); activityMesg.SetTotalTimerTime(workoutSamples.Duration); activityMesg.SetNumSessions(1); activityMesg.SetType(Activity.Manual); activityMesg.SetEvent(Event.Activity); activityMesg.SetEventType(EventType.Stop); var timezoneOffset = (int)TimeZoneInfo.Local.GetUtcOffset(base.GetEndTimeUtc(workout)).TotalSeconds; var timeStamp = (uint)((int)endTime.GetTimeStamp() + timezoneOffset); activityMesg.SetLocalTimestamp(timeStamp); messages.Add(activityMesg); return(new Tuple <string, ICollection <Mesg> >(title, messages)); }
/// <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"); }
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}"); }
/// <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(); }
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 { } }