diff --git a/src/main.rs b/src/main.rs index 0f85acd..d4b0055 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ use clap::Parser; +use fit_file as fit; +use fit_file::{fit_file, FitFieldValue, FitRecordMsg, FitSessionMsg}; use geo_types::{coord, Point}; use gpx::{Gpx, GpxVersion, Track, TrackSegment, Waypoint}; use std::{fs::File, io::BufWriter}; use time::OffsetDateTime; +/// universal Result type Res = Result>; #[derive(Parser)] @@ -14,22 +17,20 @@ struct Args { // FitRecordMsg to gpx Waypoint fn frm_to_gwp(frm: FitRecordMsg) -> Waypoint { - // "Time: {}\tLat: {}\tLon: {}\tAlt: {:.1}m\tDist: {:.3}km\tSpeed: {:.1}km/h\tHR: {}", - // eprintln!("time: {:?}", frm.timestamp); let time = frm.timestamp.unwrap_or(0); let time = OffsetDateTime::from_unix_timestamp(time.into()).ok(); - let lat = fit_file::semicircles_to_degrees(frm.position_lat.unwrap_or(0)); - let lon = fit_file::semicircles_to_degrees(frm.position_long.unwrap_or(0)); + let lat = fit::semicircles_to_degrees(frm.position_lat.unwrap_or(0)); + let lon = fit::semicircles_to_degrees(frm.position_long.unwrap_or(0)); let alt = if let Some(enh_alt) = frm.enhanced_altitude { Some(enh_alt) } else { frm.altitude.map(|alt| alt.into()) } - .map(|alt| alt as f32 / 5. - 500.); + .map(|alt| alt as f32 / 5. - 500.); // m - // let dist = frm.distance.unwrap_or(0) as f32 / 100000.; + // let dist = frm.distance.unwrap_or(0) as f32 / 100000.; // km let speed = if let Some(enh_spd) = frm.enhanced_speed { Some(enh_spd) @@ -37,7 +38,7 @@ fn frm_to_gwp(frm: FitRecordMsg) -> Waypoint { frm.speed.map(|spd| spd.into()) } .map(|spd| spd as f64); - // .map(|spd| spd as f64 / 1000. * 3.6); + // .map(|spd| spd as f64 / 1000. * 3.6); // km/h // let hr = frm // .heart_rate @@ -60,27 +61,22 @@ fn no_lat_lon(frm: &FitRecordMsg) -> bool { frm.position_long.is_none() && frm.position_lat.is_none() } -use fit_file::{fit_file, FitRecordMsg, FitSessionMsg}; - /// Called for each record message as it is processed. fn callback( timestamp: u32, global_message_num: u16, _local_msg_type: u8, _message_index: u16, - fields: Vec, + fields: Vec, data: &mut Context, ) { - if global_message_num == fit_file::GLOBAL_MSG_NUM_DEVICE_INFO { - // let msg = FitDeviceInfoMsg::new(fields); - // println!("{msg:#?}"); - } else if global_message_num == fit_file::GLOBAL_MSG_NUM_SESSION { + if global_message_num == fit::GLOBAL_MSG_NUM_SESSION { let msg = FitSessionMsg::new(fields); let sport_names = fit_file::init_sport_name_map(); let sport_id = msg.sport.unwrap(); println!("Sport: {}", sport_names.get(&sport_id).unwrap()); - } else if global_message_num == fit_file::GLOBAL_MSG_NUM_RECORD { + } else if global_message_num == fit::GLOBAL_MSG_NUM_RECORD { let mut msg = FitRecordMsg::new(fields); data.num_records_processed += 1; @@ -95,30 +91,8 @@ fn callback( data.no_lat_lon_sum += 1; } - // println!( - // "timestamp: {:?}|{:?}|{:?}|{}", - // msg.timestamp, msg.time128, msg.time_from_course, timestamp - // ); let wp = frm_to_gwp(msg); data.track_segment.points.push(wp); - // assert!(msg.timestamp.is_some()); - - // println!("{msg:#?}"); - - // println!( - // // "Timestamp: {} Latitude: {} Longitude: {} Altitude: {} Distance: {} Speed: {} HeartRate: {}", - // "Time: {}\tLat: {}\tLon: {}\tAlt: {:.1}m\tDist: {:.3}km\tSpeed: {:.1}km/h\tHR: {}", - // msg.timestamp.unwrap_or(0), - // fit_file::semicircles_to_degrees(msg.position_lat.unwrap_or(0)), - // fit_file::semicircles_to_degrees(msg.position_long.unwrap_or(0)), - // msg.enhanced_altitude.unwrap_or(0) as f32 / 5. - 500., - // msg.distance.unwrap_or(0) as f32 / 100000., - // msg.enhanced_speed.unwrap_or(0) as f32 / 1000. * 3.6, - // msg.heart_rate - // .map(|hr| hr.checked_add(1)) - // .unwrap_or(Some(0)) - // .unwrap_or(0), - // ); } } @@ -176,33 +150,6 @@ fn fit2gpx(f_in: &str) -> Res<()> { Ok(()) } -// if let MessageType::Record = msg.data.message_type { -// let rec_dat: RecordData = msg.data.clone().into(); -// if rec_dat.no_lat_lon() { -// no_lat_lon_sum += 1; -// // eprintln!("doesn't contain lat/lon data: {:?}", msg.data); -// // continue; -// } -// // eprintln!("{rec_dat:#?}"); -// // Add track point -// let wp: Waypoint = rec_dat.into(); -// // if wp.point().x_y() == (0., 0.) { -// // eprintln!("warn: guess it's invalid: {msg:#?}"); -// // } -// if ongoing_activity { -// track_segment.points.push(wp); -// } else { -// eprintln!("warn: NOT in an activity right now"); -// // std::io::stdin().read_line(&mut String::new())?; -// } -// } else if let MessageType::Activity = msg.data.message_type { -// let start_stop = df_at(&msg.data, 4); -// if let Some(Value::Enum(start_stop)) = start_stop { -// if start_stop == &"start" { -// ongoing_activity = true; -// } else if start_stop == &"stop" { -// ongoing_activity = false; - fn main() { // collecting cli args let args = Args::parse();