Skip to content

Commit

Permalink
fix(code): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeromos Kovacs committed Jul 5, 2024
1 parent 593bd6e commit ed2ae3a
Showing 1 changed file with 11 additions and 64 deletions.
75 changes: 11 additions & 64 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<T> = Result<T, Box<dyn std::error::Error>>;

#[derive(Parser)]
Expand All @@ -14,30 +17,28 @@ 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)
} else {
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
Expand All @@ -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<fit_file::FitFieldValue>,
fields: Vec<FitFieldValue>,
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;
Expand All @@ -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),
// );
}
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ed2ae3a

Please sign in to comment.