use crate::types::{Command, Error, GSAOperationMode, NavigationMode};
#[derive(Debug, Clone, PartialEq)]
pub struct GSA {
pub operation_mode: GSAOperationMode,
pub navigation_mode: NavigationMode,
pub satellites: Vec<Option<u8>>,
pub number_of_satellites: usize,
pub pdop: f64,
pub hdop: f64,
pub vdop: f64,
}
impl Default for GSA {
fn default() -> Self {
Self {
operation_mode: GSAOperationMode::Automatic,
navigation_mode: NavigationMode::NoFix,
satellites: Default::default(),
number_of_satellites: Default::default(),
pdop: Default::default(),
hdop: Default::default(),
vdop: Default::default(),
}
}
}
impl Command<GSA> for GSA {
fn parse_command(&self, command: Vec<String>) -> Result<GSA, crate::types::Error> {
let operation_mode = match GSAOperationMode::from_str(&command[0]) {
Ok(e) => e,
Err(_) => {
return Err(Error::ParseError(format!(
"Invalid operation mode: {}",
command[0]
)))
}
};
let navigation_mode = match NavigationMode::from_str(&command[1]) {
Ok(e) => e,
Err(_) => {
return Err(Error::ParseError(format!(
"Invalid navigation mode: {}",
command[1]
)))
}
};
let satellites: Vec<Option<u8>> = command[2..14]
.iter()
.map(|e| e.parse::<u8>().ok())
.collect();
let number_of_satellites = satellites
.iter()
.filter(|x| x.is_some())
.collect::<Vec<_>>()
.len();
let pdop = command[14].parse()?;
let hdop = command[15].parse()?;
let vdop = command[16].parse()?;
Ok(GSA {
operation_mode,
navigation_mode,
satellites,
number_of_satellites,
pdop,
hdop,
vdop,
})
}
}