public static void Stop() { var now = new DateTime(System.DateTime.Now); TerminateLap(); sessionMesg.SetTimestamp(now); sessionMesg.SetSport(Sport.Cycling); sessionMesg.SetSubSport(SubSport.VirtualActivity); sessionMesg.SetTotalDistance(State.TripTotalKm * 1000); sessionMesg.SetTotalElapsedTime(State.TripTotalTime); sessionMesg.SetFirstLapIndex(0); sessionMesg.SetNumLaps(numLaps); sessionMesg.SetEvent(Event.Session); sessionMesg.SetEventType(EventType.Stop); sessionMesg.SetEventGroup(0); activityMesg = new ActivityMesg(); activityMesg.SetTimestamp(now); activityMesg.SetTotalTimerTime(State.TripTotalTime); activityMesg.SetNumSessions(1); activityMesg.SetType(Activity.Manual); activityMesg.SetEvent(Event.Activity); activityMesg.SetEventType(EventType.Stop); activityMesg.SetEventGroup(0); encoder.Write(sessionMesg); encoder.Write(activityMesg); encoder.Close(); fitDest.Close(); }
public static void Stop(DateTime?time = null) { Trace.TraceInformation("Stop()"); var now = time ?? DateTime.UtcNow; TerminateLap(time); if (!isPaused) { Pause(time); } sessionMesg.SetTimestamp(new Dynastream.Fit.DateTime(now)); sessionMesg.SetSport(Sport.Cycling); sessionMesg.SetSubSport(SubSport.VirtualActivity); sessionMesg.SetTotalDistance(totalDistance); sessionMesg.SetTotalElapsedTime((float)(now - startTime).TotalSeconds); sessionMesg.SetTotalTimerTime((float)totalTimerTime.TotalSeconds); sessionMesg.SetFirstLapIndex(0); sessionMesg.SetNumLaps(numLaps); sessionMesg.SetEvent(Event.Session); sessionMesg.SetEventType(EventType.Stop); sessionMesg.SetEventGroup(0); sessionMesg.SetStartTime(new Dynastream.Fit.DateTime(startTime)); activityMesg = new ActivityMesg(); activityMesg.SetTimestamp(new Dynastream.Fit.DateTime(now)); activityMesg.SetTotalTimerTime((float)totalTimerTime.TotalSeconds); activityMesg.SetNumSessions(1); activityMesg.SetType(Activity.Manual); activityMesg.SetEvent(Event.Activity); activityMesg.SetEventType(EventType.Stop); activityMesg.SetEventGroup(0); encoder.Write(sessionMesg); encoder.Write(activityMesg); encoder.Close(); fitDest.Close(); csvFile.Close(); }
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"); }
/// <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(); }