-
Notifications
You must be signed in to change notification settings - Fork 6
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
PabiGamito
wants to merge
2
commits into
strautomator:master
Choose a base branch
from
PabiGamito:garmin-workout-steps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, "") | ||
|
@@ -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: ", | ||
"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)}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
// -------------------------------------------------------------------------- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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