Build function that waits for one location, returns it, and exits. #4
+86
-77
@@ -1,23 +1,92 @@
|
||||
use chrono::Local;
|
||||
use clap::Parser;
|
||||
use nmea::Nmea;
|
||||
use nmea::SentenceType;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::todo;
|
||||
use clap::Parser;
|
||||
use chrono::Local;
|
||||
|
||||
/// Command-line arguments using Clap.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about = "A tool for echoing latitude and longitude from a GPS USB dongle.")]
|
||||
#[command(
|
||||
author,
|
||||
version,
|
||||
about = "A tool for echoing latitude and longitude from a GPS USB dongle."
|
||||
)]
|
||||
struct Cli {
|
||||
|
||||
/// COM port
|
||||
#[arg(short = 'c', long)]
|
||||
com: Option<String>,
|
||||
|
||||
/// Format
|
||||
#[arg(short = 'f', long, default_value = "csv")]
|
||||
format: Option<String>,
|
||||
}
|
||||
|
||||
// Function to get the location
|
||||
fn get_loc(port:String, fmt:String) -> String {
|
||||
|
||||
let mut nmea = Nmea::default();
|
||||
|
||||
// Harcode port for dev
|
||||
//let port_name = "/dev/tty.usbmodem1301";
|
||||
let port_name = port;
|
||||
|
||||
//fn read_loc(com: &str) -> &str {
|
||||
//println!("Reading from {port_name}");
|
||||
|
||||
let port = serialport::new(port_name, 9600)
|
||||
.timeout(Duration::from_millis(1000))
|
||||
.open();
|
||||
|
||||
if let Ok(mut serial) = port {
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
loop {
|
||||
match serial.read(&mut buf) {
|
||||
Ok(n) => {
|
||||
let data = String::from_utf8_lossy(&buf[..n]);
|
||||
|
||||
for line in data.lines() {
|
||||
// Check sentence type
|
||||
let sentence_type = match nmea.parse(line) {
|
||||
Ok(sentence_type) => sentence_type,
|
||||
Err(_) => {
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
// Only keep the GLL sentences
|
||||
if sentence_type == SentenceType::GLL {
|
||||
let lat = match nmea.latitude {
|
||||
Some(lat) => lat,
|
||||
None => -9999.25,
|
||||
};
|
||||
let lon = match nmea.longitude {
|
||||
Some(lon) => lon,
|
||||
None => -9999.25,
|
||||
};
|
||||
let date = Local::now();
|
||||
//println!("{}, {lat}, {lon}", date.format("%Y-%m-%d %H:%M:%S"));
|
||||
if fmt == "csv" {
|
||||
return format!("{},{:.8},{:.8}", date.format("%Y-%m-%d %H:%M:%S"), lat, lon);
|
||||
} else if fmt == "ssv" {
|
||||
return format!("\"{}\" {:.8} {:.8}", date.format("%Y-%m-%d %H:%M:%S"), lat, lon);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {} // lol that I can just not put a return in?
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return format!("wut");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
// Arguments
|
||||
let cli = Cli::parse();
|
||||
|
||||
let com = match cli.com {
|
||||
@@ -34,77 +103,17 @@ fn main() {
|
||||
} //todo!()
|
||||
};
|
||||
|
||||
//fn read_loc(com: &str) -> &str {
|
||||
println!("Reading from {com:?}");
|
||||
|
||||
// Infinite loop until I add controls
|
||||
//loop {
|
||||
let mut nmea = Nmea::default();
|
||||
|
||||
// Harcode port for dev
|
||||
//let port_name = "/dev/tty.usbmodem1301";
|
||||
let port_name = com.clone();
|
||||
// let port1_name = com.clone();
|
||||
|
||||
// let mut port1 = serialport::new(port1_name, 115_200)
|
||||
// .timeout(Duration::from_millis(10000))
|
||||
// .open()
|
||||
// .expect("Failed to open port");
|
||||
// let mut serial_buf: Vec<u8> = vec![0; 32];
|
||||
// port1.read(serial_buf.as_mut_slice()).expect("Found no data!");
|
||||
// dbg!(serial_buf);
|
||||
|
||||
|
||||
|
||||
// Thread for GPS streaming
|
||||
//thread::spawn(move || {
|
||||
let port = serialport::new(port_name, 9600)
|
||||
.timeout(Duration::from_millis(1000))
|
||||
.open();
|
||||
|
||||
|
||||
if let Ok(mut serial) = port {
|
||||
let mut buf = [0u8; 1024];
|
||||
|
||||
loop {
|
||||
match serial.read(&mut buf) {
|
||||
Ok(n) => {
|
||||
let data = String::from_utf8_lossy(&buf[..n]);
|
||||
|
||||
for line in data.lines() {
|
||||
|
||||
// Check sentence type
|
||||
let sentence_type = match nmea.parse(line) {
|
||||
Ok(sentence_type) => sentence_type,
|
||||
Err(_) => {
|
||||
break;
|
||||
//return;
|
||||
}
|
||||
};
|
||||
|
||||
// Only keep the GLL sentences
|
||||
if sentence_type == SentenceType::GLL {
|
||||
let lat = match nmea.latitude {
|
||||
Some(lat) => lat,
|
||||
None => -9999.25
|
||||
} ;
|
||||
let lon = match nmea.longitude {
|
||||
Some(lon) => lon,
|
||||
None => -9999.25
|
||||
} ;
|
||||
let date = Local::now();
|
||||
println!("{}, {lat}, {lon}", date.format("%Y-%m-%d %H:%M:%S"));
|
||||
let fmt = match cli.format {
|
||||
Some(fmt) => fmt,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {}, // lol that I can just not put a return in?
|
||||
}
|
||||
} //todo!()
|
||||
};
|
||||
|
||||
|
||||
// Run the function
|
||||
let result = get_loc(com, fmt);
|
||||
println!("{result}");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//});
|
||||
}
|
||||
//}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user