nmea_parser/gnss/
gll.rs

1/*
2Copyright 2020 Timo Saarinen
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16use super::*;
17
18/// GLL - geographic Position - Latitude/Longitude
19#[derive(Clone, Debug, PartialEq, Serialize)]
20pub struct GllData {
21    /// Navigation system
22    pub source: NavigationSystem,
23
24    /// Latitude in degrees.
25    pub latitude: Option<f64>,
26
27    /// Longitude in degrees.
28    pub longitude: Option<f64>,
29
30    /// UTC of position fix
31    #[serde(with = "json_date_time_utc")]
32    pub timestamp: Option<DateTime<Utc>>,
33
34    /// True = data valid, false = data invalid.
35    pub data_valid: Option<bool>,
36
37    /// FAA mode indicator (NMEA 2.3 and later).
38    pub faa_mode: Option<FaaMode>,
39}
40
41impl LatLon for GllData {
42    fn latitude(&self) -> Option<f64> {
43        self.latitude
44    }
45
46    fn longitude(&self) -> Option<f64> {
47        self.longitude
48    }
49}
50
51// -------------------------------------------------------------------------------------------------
52
53/// xxGLL: Geographic Position, Latitude / Longitude and time.
54pub(crate) fn handle(
55    sentence: &str,
56    nav_system: NavigationSystem,
57) -> Result<ParsedMessage, ParseError> {
58    let now: DateTime<Utc> = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).single().unwrap();
59    let split: Vec<&str> = sentence.split(',').collect();
60
61    Ok(ParsedMessage::Gll(GllData {
62        source: nav_system,
63        latitude: parse_latitude_ddmm_mmm(
64            split.get(1).unwrap_or(&""),
65            split.get(2).unwrap_or(&""),
66        )?,
67        longitude: parse_longitude_dddmm_mmm(
68            split.get(3).unwrap_or(&""),
69            split.get(4).unwrap_or(&""),
70        )?,
71        timestamp: parse_hhmmss(split.get(5).unwrap_or(&""), now).ok(),
72        data_valid: {
73            match *split.get(6).unwrap_or(&"") {
74                "A" => Some(true),
75                "V" => Some(false),
76                _ => None,
77            }
78        },
79        faa_mode: FaaMode::new(split.get(7).unwrap_or(&"")).ok(),
80    }))
81}
82
83// -------------------------------------------------------------------------------------------------
84
85#[cfg(test)]
86mod test {
87    use super::*;
88
89    #[test]
90    fn test_parse_gagll() {
91        let mut p = NmeaParser::new();
92        match p.parse_sentence("$GAGLL,4916.45,N,12311.12,W,225444,A,D*48") {
93            Ok(ps) => {
94                match ps {
95                    // The expected result
96                    ParsedMessage::Gll(gll) => {
97                        assert_eq!(gll.source, NavigationSystem::Galileo);
98                        assert::close(gll.latitude.unwrap_or(0.0), 49.3, 0.1);
99                        assert::close(gll.longitude.unwrap_or(0.0), -123.2, 0.1);
100                        assert_eq!(gll.timestamp, {
101                            Utc.with_ymd_and_hms(2000, 01, 01, 22, 54, 44).single()
102                        });
103                        assert_eq!(gll.data_valid, Some(true));
104                        assert_eq!(gll.faa_mode, Some(FaaMode::Differential));
105                    }
106                    _ => {
107                        assert!(false);
108                    }
109                }
110            }
111            Err(e) => {
112                assert_eq!(e.to_string(), "OK");
113            }
114        }
115    }
116}