Temperature from FIT/GPX in the profile chart, splits and summary
CI / check (push) Successful in 24s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-16 14:59:55 +01:00
1 parent e3f08f2267
commit ebd4852e98
5 files changed
+27 -12

No files matched your search

+7 -3
View File
@@ -23,6 +23,7 @@ module Maptool
FIELD_CADENCE = 4 FIELD_CADENCE = 4
FIELD_SPEED = 6 FIELD_SPEED = 6
FIELD_POWER = 7 FIELD_POWER = 7
FIELD_TEMPERATURE = 13
FIELD_TIMESTAMP = 253 FIELD_TIMESTAMP = 253
FIELD_ENHANCED_ALTITUDE = 78 FIELD_ENHANCED_ALTITUDE = 78
SPORT_FIELD_BY_MESSAGE = { SPORT_MESSAGE_NUM => 0, SESSION_MESSAGE_NUM => 5 }.freeze SPORT_FIELD_BY_MESSAGE = { SPORT_MESSAGE_NUM => 0, SESSION_MESSAGE_NUM => 5 }.freeze
@@ -61,7 +62,7 @@ module Maptool
def self.track_feature(result) def self.track_feature(result)
coordinates = result.records.map { |r| r[:coordinate] } coordinates = result.records.map { |r| r[:coordinate] }
extras = result.records.map { |r| { speed_mps: r[:speed_mps], cadence_rpm: r[:cadence], heart_rate_bpm: r[:heart_rate], power_w: r[:power] } } extras = result.records.map { |r| { speed_mps: r[:speed_mps], cadence_rpm: r[:cadence], heart_rate_bpm: r[:heart_rate], power_w: r[:power], temperature_c: r[:temperature] } }
{ {
"type" => "Feature", "type" => "Feature",
@@ -171,7 +172,7 @@ module Maptool
# in the message (temperature, ...) is walked past via its # in the message (temperature, ...) is walked past via its
# declared size without being interpreted. # declared size without being interpreted.
def self.extract_record(data, offset, definition) def self.extract_record(data, offset, definition)
lat = lon = altitude = timestamp = heart_rate = cadence = speed = power = nil lat = lon = altitude = timestamp = heart_rate = cadence = speed = power = temperature = nil
pos = offset pos = offset
definition.fields.each do |field_num, size| definition.fields.each do |field_num, size|
@@ -192,6 +193,8 @@ module Maptool
speed = unpack_int(data, pos, size, definition.little_endian, signed: false) speed = unpack_int(data, pos, size, definition.little_endian, signed: false)
when FIELD_POWER when FIELD_POWER
power = unpack_int(data, pos, size, definition.little_endian, signed: false) power = unpack_int(data, pos, size, definition.little_endian, signed: false)
when FIELD_TEMPERATURE
temperature = unpack_int(data, pos, size, definition.little_endian, signed: true)
end end
pos += size pos += size
end end
@@ -205,8 +208,9 @@ module Maptool
cadence = nil if cadence.nil? || invalid?(cadence, 1) cadence = nil if cadence.nil? || invalid?(cadence, 1)
speed_mps = speed && !invalid?(speed, 2) ? speed / 1000.0 : nil speed_mps = speed && !invalid?(speed, 2) ? speed / 1000.0 : nil
power = nil if power.nil? || invalid?(power, 2) power = nil if power.nil? || invalid?(power, 2)
temperature = nil if temperature.nil? || invalid?(temperature, 1)
{ coordinate: coordinate, time: time, heart_rate: heart_rate, cadence: cadence, speed_mps: speed_mps, power: power } { coordinate: coordinate, time: time, heart_rate: heart_rate, cadence: cadence, speed_mps: speed_mps, power: power, temperature: temperature }
end end
private_class_method :extract_record private_class_method :extract_record
+2 -1
View File
@@ -231,7 +231,7 @@ module Maptool
end end
private_class_method :process_point private_class_method :process_point
# Heart rate/cadence/speed/power live in a device-specific <extensions> block # Heart rate/cadence/speed/power/temperature live in a device-specific <extensions> block
# (usually Garmin's own TrackPointExtension) — matched by local element # (usually Garmin's own TrackPointExtension) — matched by local element
# name only (`Element#name` already strips the namespace prefix), so # name only (`Element#name` already strips the namespace prefix), so
# this doesn't care which extension schema a given file actually # this doesn't care which extension schema a given file actually
@@ -259,6 +259,7 @@ module Maptool
when "cad" then values[:cadence_rpm] = child.text&.strip&.to_i when "cad" then values[:cadence_rpm] = child.text&.strip&.to_i
when "speed" then values[:speed_mps] = child.text&.strip&.to_f when "speed" then values[:speed_mps] = child.text&.strip&.to_f
when "power", "PowerInWatts" then values[:power_w] = child.text&.strip&.to_i when "power", "PowerInWatts" then values[:power_w] = child.text&.strip&.to_i
when "atemp", "temp" then values[:temperature_c] = child.text&.strip&.to_f
end end
collect_extension_values(child, values) collect_extension_values(child, values)
end end
+2 -1
View File
@@ -31,7 +31,7 @@ module Maptool
# (computed the same way for every format, rather than trusting a # (computed the same way for every format, rather than trusting a
# device's own odometer field — keeps a GPX and a FIT file directly # device's own odometer field — keeps a GPX and a FIT file directly
# comparable), elapsed time since the first known timestamp, elevation, # comparable), elapsed time since the first known timestamp, elevation,
# and whatever per-point extras (speed/cadence/heart rate/power) the caller # and whatever per-point extras (speed/cadence/heart rate/power/temperature) the caller
# has — nil wherever a format/file doesn't carry it. # has — nil wherever a format/file doesn't carry it.
# `extras`: array of hashes parallel to `coordinates`, or shorter/empty. # `extras`: array of hashes parallel to `coordinates`, or shorter/empty.
# `times`: array of Time (or nil) parallel to `coordinates`, or []. # `times`: array of Time (or nil) parallel to `coordinates`, or [].
@@ -54,6 +54,7 @@ module Maptool
"cadence_rpm" => extra[:cadence_rpm], "cadence_rpm" => extra[:cadence_rpm],
"heart_rate_bpm" => extra[:heart_rate_bpm], "heart_rate_bpm" => extra[:heart_rate_bpm],
"power_w" => extra[:power_w], "power_w" => extra[:power_w],
"temperature_c" => extra[:temperature_c],
"lon" => lon, "lon" => lon,
"lat" => lat, "lat" => lat,
} }
+10 -3
View File
@@ -24,6 +24,7 @@ const SERIES = [
{ key: "cadence_rpm", label: "Cadence", unit: "rpm", color: "#eda100", height: 70, area: false }, { key: "cadence_rpm", label: "Cadence", unit: "rpm", color: "#eda100", height: 70, area: false },
{ key: "heart_rate_bpm", label: "Heart rate", unit: "bpm", color: "#e34948", height: 70, area: false }, { key: "heart_rate_bpm", label: "Heart rate", unit: "bpm", color: "#e34948", height: 70, area: false },
{ key: "power_w", label: "Power", unit: "W", color: "#7b4fd6", height: 70, area: false }, { key: "power_w", label: "Power", unit: "W", color: "#7b4fd6", height: 70, area: false },
{ key: "temperature_c", label: "Temperature", unit: "°C", color: "#d9730d", height: 70, area: false },
]; ];
const BEST_DISTANCES_KM = [1, 5, 10, 21.0975, 42.195, 100]; const BEST_DISTANCES_KM = [1, 5, 10, 21.0975, 42.195, 100];
const BEST_POWER_SECS = [5, 60, 300, 1200, 3600]; const BEST_POWER_SECS = [5, 60, 300, 1200, 3600];
@@ -53,6 +54,11 @@ export class TrackProfilePanel {
this._title.textContent = properties.name || "Track"; this._title.textContent = properties.name || "Track";
this._stats.textContent = statsText(properties); this._stats.textContent = statsText(properties);
const temperatures = this._profile.map((p) => p.temperature_c).filter((t) => typeof t === "number");
if (temperatures.length) {
const avg = temperatures.reduce((a, b) => a + b, 0) / temperatures.length;
this._stats.textContent += ` · ${Math.round(avg)} °C (${Math.round(Math.min(...temperatures))}–${Math.round(Math.max(...temperatures))})`;
}
this._chartsContainer.innerHTML = ""; this._chartsContainer.innerHTML = "";
this._tooltip = document.createElement("div"); this._tooltip = document.createElement("div");
@@ -258,15 +264,16 @@ function statsTables(profile, activityType) {
const ride = /cycl|bik|ride/i.test(activityType || ""); const ride = /cycl|bik|ride/i.test(activityType || "");
const hasHr = profile.some((p) => typeof p.heart_rate_bpm === "number"); const hasHr = profile.some((p) => typeof p.heart_rate_bpm === "number");
const hasPower = profile.some((p) => typeof p.power_w === "number"); const hasPower = profile.some((p) => typeof p.power_w === "number");
const hasTemp = profile.some((p) => typeof p.temperature_c === "number");
const pace = (secs, meters) => (ride ? `${((meters / secs) * 3.6).toFixed(1)} km/h` : `${formatPace(secs / (meters / 1000))} /km`); const pace = (secs, meters) => (ride ? `${((meters / secs) * 3.6).toFixed(1)} km/h` : `${formatPace(secs / (meters / 1000))} /km`);
let html = ""; let html = "";
if (timed.length > 1) { if (timed.length > 1) {
const step = ride || total > 50000 ? 5000 : 1000; const step = ride || total > 50000 ? 5000 : 1000;
const rows = splits(timed, step).map( const rows = splits(timed, step).map(
(s) => `<tr><td>${(s.to / 1000).toFixed(s.to % 1000 ? 1 : 0)} km</td><td>${formatDuration(s.secs)}</td><td>${pace(s.secs, s.meters)}</td><td>↑ ${Math.round(s.gain)} m</td><td>↓ ${Math.round(s.loss)} m</td>${hasHr ? `<td>${s.hr ? `${Math.round(s.hr)} bpm` : ""}</td>` : ""}${hasPower ? `<td>${s.power ? `${Math.round(s.power)} W` : ""}</td>` : ""}</tr>`, (s) => `<tr><td>${(s.to / 1000).toFixed(s.to % 1000 ? 1 : 0)} km</td><td>${formatDuration(s.secs)}</td><td>${pace(s.secs, s.meters)}</td><td>↑ ${Math.round(s.gain)} m</td><td>↓ ${Math.round(s.loss)} m</td>${hasHr ? `<td>${s.hr ? `${Math.round(s.hr)} bpm` : ""}</td>` : ""}${hasPower ? `<td>${s.power ? `${Math.round(s.power)} W` : ""}</td>` : ""}${hasTemp ? `<td>${s.temp != null ? `${Math.round(s.temp)} °C` : ""}</td>` : ""}</tr>`,
); );
html += `<details class="profile-stats"><summary>Splits (${step / 1000} km)</summary><table><thead><tr><th>At</th><th>Time</th><th>${ride ? "Speed" : "Pace"}</th><th>Ascent</th><th>Descent</th>${hasHr ? "<th>Avg HR</th>" : ""}${hasPower ? "<th>Avg power</th>" : ""}</tr></thead><tbody>${rows.join("")}</tbody></table></details>`; html += `<details class="profile-stats"><summary>Splits (${step / 1000} km)</summary><table><thead><tr><th>At</th><th>Time</th><th>${ride ? "Speed" : "Pace"}</th><th>Ascent</th><th>Descent</th>${hasHr ? "<th>Avg HR</th>" : ""}${hasPower ? "<th>Avg power</th>" : ""}${hasTemp ? "<th>Avg temp</th>" : ""}</tr></thead><tbody>${rows.join("")}</tbody></table></details>`;
} }
const best = []; const best = [];
@@ -306,7 +313,7 @@ function splits(profile, step) {
}); });
const meters = profile[i].distance_meters - profile[start].distance_meters; const meters = profile[i].distance_meters - profile[start].distance_meters;
const secs = profile[i].elapsed_secs - profile[start].elapsed_secs; const secs = profile[i].elapsed_secs - profile[start].elapsed_secs;
if (meters > 0 && secs > 0) out.push({ to: profile[i].distance_meters, meters, secs, gain, loss, hr: average(part, "heart_rate_bpm"), power: average(part, "power_w") }); if (meters > 0 && secs > 0) out.push({ to: profile[i].distance_meters, meters, secs, gain, loss, hr: average(part, "heart_rate_bpm"), power: average(part, "power_w"), temp: average(part, "temperature_c") });
start = i; start = i;
} }
return out; return out;
+6 -4
View File
@@ -72,16 +72,16 @@ class FitTest < Minitest::Test
body << [0b00000001].pack("C") << [sport].pack("C") body << [0b00000001].pack("C") << [sport].pack("C")
end end
record_definition = [0, 0].pack("CC") + [20].pack("S<") + [6].pack("C") + record_definition = [0, 0].pack("CC") + [20].pack("S<") + [7].pack("C") +
[0, 4, 0x85].pack("C3") + [1, 4, 0x85].pack("C3") + [0, 4, 0x85].pack("C3") + [1, 4, 0x85].pack("C3") +
[3, 1, 0x02].pack("C3") + [4, 1, 0x02].pack("C3") + [6, 2, 0x84].pack("C3") + [7, 2, 0x84].pack("C3") [3, 1, 0x02].pack("C3") + [4, 1, 0x02].pack("C3") + [6, 2, 0x84].pack("C3") + [7, 2, 0x84].pack("C3") + [13, 1, 0x01].pack("C3")
body << [0b01000000].pack("C") << record_definition body << [0b01000000].pack("C") << record_definition
records.each do |r| records.each do |r|
body << [0b00000000].pack("C") << le32(r[:lat]) << le32(r[:lon]) body << [0b00000000].pack("C") << le32(r[:lat]) << le32(r[:lon])
body << [r[:heart_rate] || 0xFF].pack("C") << [r[:cadence] || 0xFF].pack("C") body << [r[:heart_rate] || 0xFF].pack("C") << [r[:cadence] || 0xFF].pack("C")
speed_raw = r[:speed_mps] ? (r[:speed_mps] * 1000).round : 0xFFFF speed_raw = r[:speed_mps] ? (r[:speed_mps] * 1000).round : 0xFFFF
body << [speed_raw].pack("S<") << [r[:power] || 0xFFFF].pack("S<") body << [speed_raw].pack("S<") << [r[:power] || 0xFFFF].pack("S<") << [r[:temperature] || 0x7F].pack("c")
end end
header = [12, 16, 0, 0].pack("C2S<") + le32(body.bytesize) + ".FIT" header = [12, 16, 0, 0].pack("C2S<") + le32(body.bytesize) + ".FIT"
@@ -110,7 +110,7 @@ class FitTest < Minitest::Test
def test_profile_carries_heart_rate_cadence_and_speed_treating_0xff_as_absent def test_profile_carries_heart_rate_cadence_and_speed_treating_0xff_as_absent
path = write_activity_fit([ path = write_activity_fit([
{ lat: 100_000_000, lon: 200_000_000, heart_rate: 140, cadence: 80, speed_mps: 3.5, power: 250 }, { lat: 100_000_000, lon: 200_000_000, heart_rate: 140, cadence: 80, speed_mps: 3.5, power: 250, temperature: -4 },
{ lat: 100_000_100, lon: 200_000_100 }, # no extras -> 0xFF/0xFFFF sentinels { lat: 100_000_100, lon: 200_000_100 }, # no extras -> 0xFF/0xFFFF sentinels
]) ])
@@ -121,6 +121,8 @@ class FitTest < Minitest::Test
assert_in_delta 3.5, profile[0]["speed_mps"] assert_in_delta 3.5, profile[0]["speed_mps"]
assert_equal 250, profile[0]["power_w"] assert_equal 250, profile[0]["power_w"]
assert_nil profile[1]["power_w"] assert_nil profile[1]["power_w"]
assert_equal(-4, profile[0]["temperature_c"])
assert_nil profile[1]["temperature_c"]
assert_nil profile[1]["heart_rate_bpm"] assert_nil profile[1]["heart_rate_bpm"]
assert_nil profile[1]["speed_mps"] assert_nil profile[1]["speed_mps"]
end end