From 98f882f9b8b15a89f8242e6494f0d7346ec4e8c3 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Fri, 25 Sep 2020 12:18:37 +0300 Subject: [PATCH] Integrate detectRepeats() with ZWO generation --- examples/halvfems.txt | 12 ++++++++++++ src/generateZwo.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/examples/halvfems.txt b/examples/halvfems.txt index 6357936..00d54af 100644 --- a/examples/halvfems.txt +++ b/examples/halvfems.txt @@ -21,17 +21,29 @@ Interval: 12:00 90rpm 90% Rest: 4:00 85rpm 55% Interval: 1:00 60rpm 90% + # 00:00 Start off at slow cadence Interval: 1:00 90rpm 90% + # 00:00 Now bump that cadence up Interval: 1:00 60rpm 90% + # 00:00 Back to 60 RPM Interval: 1:00 90rpm 90% + # 00:00 Back to normal cadence Interval: 1:00 60rpm 90% + # 00:00 Grinding... Interval: 1:00 90rpm 90% + # 00:00 Back to normal cadence Interval: 1:00 60rpm 90% + # 00:00 And grind at 60 RPM again Interval: 1:00 90rpm 90% + # 00:00 Back to 90 RPM Interval: 1:00 60rpm 90% + # 00:00 Lower it again to 60 RPM Interval: 1:00 90rpm 90% + # 00:00 Increase the cadence to 90 RPM Interval: 1:00 60rpm 90% + # 00:00 Last grinding minute Interval: 1:00 90rpm 90% + # 00:00 Last minute... be strong! Rest: 4:00 85rpm 55% diff --git a/src/generateZwo.ts b/src/generateZwo.ts index 7ebcdac..8d0dcb6 100644 --- a/src/generateZwo.ts +++ b/src/generateZwo.ts @@ -1,5 +1,6 @@ import * as xml from "xml"; import { Interval, Workout, Comment } from "./ast"; +import { detectRepeats, RepeatedInterval } from "./detectRepeats"; // Zwift Workout XML generator @@ -43,7 +44,33 @@ const generateSteadyStateInterval = ({ duration, intensity, cadence, comments }: }; }; -const generateInterval = (interval: Interval): xml.XmlObject => { +const generateRepeatInterval = (repInterval: RepeatedInterval): xml.XmlObject => { + const [on, off] = repInterval.intervals; + return { + IntervalsT: [ + { + _attr: { + Repeat: repInterval.times, + + OnDuration: on.duration.value, + OnPower: on.intensity.from, + ...(on.cadence ? { Cadence: on.cadence } : {}), + + OffDuration: off.duration.value, + OffPower: off.intensity.from, + ...(off.cadence ? { CadenceResting: off.cadence } : {}), + }, + }, + ...generateTextEvents(repInterval.comments), + ], + }; +}; + +const generateInterval = (interval: Interval | RepeatedInterval): xml.XmlObject => { + if (interval.type === "repeat") { + return generateRepeatInterval(interval); + } + const { intensity } = interval; if (intensity.from < intensity.to) { return generateRangeInterval("Warmup", interval); @@ -62,7 +89,7 @@ export const generateZwo = ({ name, author, description, intervals }: Workout): { author: author }, { description: description }, { sportType: "bike" }, - ...intervals.map(generateInterval), + ...detectRepeats(intervals).map(generateInterval), ], }, { indent: " " },