Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatted workout steps to Garmin activities #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fitparser/fit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,8 @@ export const FIT = {
0: "active",
1: "rest",
2: "warmup",
3: "cooldown"
3: "cooldown",
5: "interval",
},
session_trigger: {
0: "activity_end",
Expand Down
87 changes: 87 additions & 0 deletions src/fitparser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ export class FitParser {
fitFileActivity.workoutNotes = fitObj.workout.notes
}

// Add workout steps.
if (fitObj.workout_steps) {
fitFileActivity.workoutSteps = this.formatWorkoutSteps(fitObj.workout_steps)
}

// Found devices in the FIT file? Generate device IDs.
if (fitObj.devices?.length > 0) {
const getDeviceString = (d) => `${d.manufacturer}.${d.product_name || d.device_type || d.source_type || d.device_index}.${d.serial_number}`.replace(/\_/g, "").replace(/\s/g, "")
Expand Down Expand Up @@ -307,6 +312,88 @@ export class FitParser {
logger.info("FitParser.parse", logHelper.user(user), logHelper.fitFileActivity(fitFileActivity), `Data: ${logFields.join(", ")}`)
}

private intensity_prefixes = {
"warmup": "🟢 Warmup: ",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we probably want to do something with localization of the text here

"cooldown": "🟡 Cooldown: ",
"interval": "🚀 Interval: ",
"rest": "🌿 Rest: ",
}

private formatWorkoutSteps(workout_steps: any): string {
let formatted_step_lines = []

for (let step of workout_steps) {
let line = this.intensity_prefixes[step.intensity] || ""

switch (step.duration_type) {
case "time":
line += this.formatMsTime(step.duration_value)
break
case "repeat_until_steps_cmplt":
let rep_start_index = step.duration_value
let rep_count = step.target_value

let rep_line = `🔄 Repeat ${rep_count}x:`

for (var i = rep_start_index; i < formatted_step_lines.length; i++) {
formatted_step_lines[i] = " - " + formatted_step_lines[i]
}

formatted_step_lines.splice(rep_start_index, 0, rep_line)
continue
}

switch (step.target_type) {
case "speed":
if (step.target_value) {
line += ` @ ${this.formatSpeedAsPace(step.target_value)}`
Copy link
Contributor Author

@PabiGamito PabiGamito Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is specific to running. We probably want to format this for cycling activities in a way that makes sense for cycling workouts. Plus we should add support for power and heart rate target types as well.

} else if (step.custom_target_value_low && step.custom_target_value_high) {
let max_pace = this.formatSpeedAsPace(step.custom_target_value_high)
let min_pace = this.formatSpeedAsPace(step.custom_target_value_low)
line += ` @ ${max_pace}-${min_pace}`
}
break
}

formatted_step_lines.push(line)
}

return formatted_step_lines.join("\n")
}

private formatMsTime(timeMs: number) {
if (timeMs < 1000) {
return "0s"
}

let duration_h = (timeMs / 1000 / 60 / 60 ) >> 0;
let remainder_ms = timeMs % (1000 * 60 * 60)
let duration_m = (remainder_ms / 1000 / 60) >> 0;
remainder_ms = remainder_ms % (1000 * 60)
let duration_s = (remainder_ms / 1000) >> 0;

let formatted_time = ""
if (duration_h > 0) {
formatted_time += `${duration_h}h`
}
if (duration_m > 0) {
formatted_time += `${duration_m}min`
}
if (duration_s > 0) {
formatted_time += `${duration_s}s`
}

return formatted_time
}

private formatSpeedAsPace(milliseconds_per_meter) {
let s_per_km = 1000*1000 / milliseconds_per_meter
let pace_m_part = (s_per_km / 60) >> 0
let pace_s_part = (s_per_km % 60) >> 0

return `${pace_m_part}:${pace_s_part.toString().padStart(2, "0")}/km`
}

// DATABASE DATA
// --------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions src/fitparser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface FitFileActivity {
workoutName?: string
/** The workout notes. */
workoutNotes?: string
/** The formatted workout steps. */
workoutSteps?: string
/** Devices used in the activity. */
devices?: string[]
/** Devices battery status. */
Expand Down