Transform into library
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
72
src/lib.rs
Normal file
72
src/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::fs::File;
|
||||
use chrono::{DateTime, Utc};
|
||||
use nmea_parser::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ParseResult {
|
||||
lat: f64,
|
||||
lon: f64,
|
||||
timestamp: DateTime<Utc>,
|
||||
speed: f64
|
||||
}
|
||||
|
||||
pub struct GpsParser {
|
||||
gps_file: String,
|
||||
}
|
||||
|
||||
impl GpsParser {
|
||||
pub fn new(gps_device: &str) -> GpsParser{
|
||||
GpsParser {
|
||||
gps_file: format!("{}",gps_device),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_nmea(&mut self) -> Result<ParseResult, ParseError> {
|
||||
let f = match File::open(&self.gps_file) {
|
||||
Ok(f) => f,
|
||||
Err(error) => panic!("Error: {error:?}"),
|
||||
};
|
||||
let mut reader = BufReader::new(f);
|
||||
let mut line = String::new();
|
||||
let mut parser = NmeaParser::new();
|
||||
let mut res = ParseResult {
|
||||
lat: 0.0,
|
||||
lon: 0.0,
|
||||
timestamp: Utc::now(),
|
||||
speed: 0.0,
|
||||
};
|
||||
|
||||
let mut parsing_complete = false;
|
||||
let mut parsed_gga:bool = false;
|
||||
let mut parsed_vtg:bool = false;
|
||||
while !parsing_complete {
|
||||
let len = match reader.read_line(&mut line) {
|
||||
Ok(len) => len,
|
||||
Err(_) => 0,
|
||||
};
|
||||
if len > 1 {
|
||||
match parser.parse_sentence(&line)? {
|
||||
ParsedMessage::Gga(gga) => {
|
||||
parsed_gga = true;
|
||||
res.lat = gga.latitude.unwrap();
|
||||
res.lon = gga.longitude.unwrap();
|
||||
res.timestamp = gga.timestamp.unwrap();
|
||||
},
|
||||
ParsedMessage::Vtg(vtg) => {
|
||||
parsed_vtg = true;
|
||||
res.speed = vtg.sog_kph.unwrap();
|
||||
},
|
||||
_ => {
|
||||
}
|
||||
}
|
||||
}
|
||||
if parsed_gga && parsed_vtg {
|
||||
parsing_complete = true;
|
||||
}
|
||||
line.clear();
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
}
|
60
src/main.rs
60
src/main.rs
@@ -1,60 +0,0 @@
|
||||
use std::io::prelude::*;
|
||||
use std::io::BufReader;
|
||||
use std::fs::File;
|
||||
use nmea_parser::*;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let f = File::open("/dev/ttyACM0")?;
|
||||
let mut reader = BufReader::new(f);
|
||||
|
||||
let mut line = String::new();
|
||||
|
||||
let mut parser = NmeaParser::new();
|
||||
loop {
|
||||
let len = reader.read_line(&mut line)?;
|
||||
if len > 1 {
|
||||
match parser.parse_sentence(&line) {
|
||||
Ok(msg) => {
|
||||
match msg {
|
||||
ParsedMessage::Gga(gga) => {
|
||||
println!("Source: {}", gga.source);
|
||||
println!("Latitude: {:.3}°", gga.latitude.unwrap());
|
||||
println!("Longitude: {:.3}°", gga.longitude.unwrap());
|
||||
match gga.satellite_count {
|
||||
Some(sat_count) => {
|
||||
println!("Satellite count: {}", sat_count);
|
||||
},
|
||||
None => {
|
||||
println!("No satellites in sight");
|
||||
}
|
||||
}
|
||||
match gga.timestamp {
|
||||
Some(timestamp) => {
|
||||
println!("Timestamp: {}", timestamp);
|
||||
},
|
||||
None => {
|
||||
println!("No timestamp
|
||||
")
|
||||
}
|
||||
}
|
||||
},
|
||||
// ParsedMessage::Gsa(gsa) => {
|
||||
// println!("{}", gsa.mode1_automatic)
|
||||
// }
|
||||
_ => {
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_err) => {}
|
||||
}
|
||||
}
|
||||
line.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// $GNRMC,204607.00,A,5222.24380,N,01351.13504,E,0.103,,210621,,,A*62
|
||||
// $GNVTG,,T,,M,0.103,N,0.191,K,A*36
|
||||
// $GNGGA,204607.00,5222.24380,N,01351.13504,E,1,08,1.29,29.2,M,41.8,M,,*7C
|
||||
// $GNGSA,A,3,04,09,06,03,07,26,,,,,,,1.90,1.29,1.39*1E
|
||||
// $GPGSV,4,1,13,02,40,283,19,03,08,126,08,04,39,073,27,06,44,232,31*77
|
||||
// $GNGLL,5222.24380,N,01351.13504,E,204607.00,A,A*7F
|
Reference in New Issue
Block a user