diff --git a/lib/tcx.mjs b/lib/tcx.mjs index e93e7fc..c25d102 100644 --- a/lib/tcx.mjs +++ b/lib/tcx.mjs @@ -26,8 +26,8 @@ export function generateTrimlogInput(filename, name = "Walk", type = "walk") { time: tp.Time[0], longitude: parseFloat(tp.Position?.[0]?.LongitudeDegrees[0]), latitude: parseFloat(tp.Position?.[0]?.LatitudeDegrees[0]), - altitude: parseFloat(tp.AltitudeMeters[0]), - distance: parseFloat(tp.DistanceMeters[0]), + altitude: parseFloat(tp.AltitudeMeters?.[0]), + distance: parseFloat(tp.DistanceMeters?.[0]), pulse: parseFloat(tp.HeartRateBpm?.[0].Value?.[0]), }; @@ -42,6 +42,11 @@ export function generateTrimlogInput(filename, name = "Walk", type = "walk") { } } + const list = Object.keys(map).sort().map(k => map[k]); + const firstLon = list.find(e => !!e.longitude)?.longitude; + const firstLat = list.find(e => !!e.latitude)?.latitude; + const firstAlt = list.find(e => !!e.altitude)?.altitude; + const out = { date: dateStr, contents: [{rawActivity: { @@ -49,13 +54,13 @@ export function generateTrimlogInput(filename, name = "Walk", type = "walk") { sets: [], effortScale: 1, weight: 0, - measurements: Object.keys(map).sort().map(k => map[k]).map(p => ({ + measurements: list.map(p => ({ seconds: Math.round((new Date(p.time) - date) / 1000), meters: p.distance, pulse: p.pulse, - longitude: p.longitude, - latitude: p.latitude, - altitude: p.altitude, + longitude: p.longitude || firstLon, + latitude: p.latitude || firstLat, + altitude: p.altitude || firstAlt, })) }}], description: name,