1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2020 Timo Saarinen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use super::*;

/// RMC - position, velocity, and time (Recommended Minimum sentence C)
#[derive(Clone, Debug, PartialEq)]
pub struct RmcData {
    /// Navigation system
    pub source: NavigationSystem,

    /// Fix datetime based on HHMMSS and DDMMYY
    pub timestamp: Option<DateTime<Utc>>,
    
    /// Status: true = active, false = void.
    pub status_active: Option<bool>,
    
    /// Latitude in degrees    
    pub latitude: Option<f64>,

    /// Longitude in degrees    
    pub longitude: Option<f64>,
    
    /// Speed over ground in knots
    pub sog_knots: Option<f64>,
    
    /// Track angle in degrees (True)
    pub bearing: Option<f64>,
    
    /// Magnetic variation in degrees
    pub variation: Option<f64>
}

impl LatLon for RmcData {
    fn latitude(&self) -> Option<f64> {
        self.latitude
    }

    fn longitude(&self) -> Option<f64> {
        self.longitude
    }
}

// -------------------------------------------------------------------------------------------------

#[doc(hidden)]
/// xxRMC: Recommended minimum specific GPS/Transit data
pub fn handle(sentence: &str, nav_system: NavigationSystem) -> Result<ParsedSentence, ParseError> {
    let split: Vec<&str> = sentence.split(',').collect();
    
    return Ok(ParsedSentence::Rmc(RmcData{
        source:             nav_system,
        timestamp:          parse_yymmdd_hhmmss(split.get(9).unwrap_or(&""), 
                                                split.get(1).unwrap_or(&"")).ok(),
        status_active:      {
            let s = split.get(2).unwrap_or(&"");
            match s {
                &"A" => Some(true),
                &"V" => Some(false),
                &"" => None,
                _ => { return Err(format!("Invalid RMC navigation receiver status: {}", s).into()); }
            }
        },
        latitude:           parse_latitude_ddmm_mmm(split.get(3).unwrap_or(&""), 
                                                    split.get(4).unwrap_or(&""))?,
        longitude:          parse_longitude_dddmm_mmm(split.get(5).unwrap_or(&""), 
                                                      split.get(6).unwrap_or(&""))?,
        sog_knots:        pick_number_field(&split, 7)?,
        bearing:            pick_number_field(&split, 8)?,
        variation: {
            if let Some(val) = pick_number_field::<f64>(&split, 10)? {
                let side = split.get(11).unwrap_or(&"");
                match side {
                    &"E" => { Some(val) },
                    &"W" => { Some(-val) },
                    _ => { return Err(format!("Invalid RMC variation side: {}", side).into()); },
                }
            } else {
                None
            }
        },
    }));

}

// -------------------------------------------------------------------------------------------------

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_parse_cprmc() {
        let mut p = NmeaParser::new();
        match p.parse_sentence("$GPRMC,225446,A,4916.45,N,12311.12,W,000.5,054.7,191120,020.3,E*67") 
        {
            Ok(ps) => {
                match ps {
                    // The expected result
                    ParsedSentence::Rmc(rmc) => {
                        assert_eq!(rmc.status_active, Some(true));
                        assert_eq!(rmc.timestamp, {
                            Some(Utc.ymd(2020, 11, 19).and_hms(22, 54, 46))
                        });
                        assert_eq!(rmc.sog_knots.unwrap(), 0.5);
                        assert::close(rmc.bearing.unwrap_or(0.0), 54.7, 0.1);
                        assert_eq!(rmc.variation.unwrap(), 20.3);
                    },
                    ParsedSentence::Incomplete => {
                        assert!(false);
                    },
                    _ => {
                        assert!(false);
                    }
                }
            },
            Err(e) => {
                assert_eq!(e.to_string(), "OK");
            }
        }
    }

    #[test]
    fn test_parse_cprmc_empty_fields() {
        let mut p = NmeaParser::new();
        match p.parse_sentence("$GPRMC,225446,A,,,,,,,070809,,*23") {
            Ok(ps) => {
                match ps {
                    // The expected result
                    ParsedSentence::Rmc(rmc) => {
                        assert_eq!(rmc.status_active, Some(true));
                        assert_eq!(rmc.timestamp, {
                            Some(Utc.ymd(2009, 8, 7).and_hms(22, 54, 46))
                        });
                        assert_eq!(rmc.sog_knots, None);
                        assert_eq!(rmc.bearing, None);
                        assert_eq!(rmc.variation, None);
                    },
                    ParsedSentence::Incomplete => {
                        assert!(false);
                    },
                    _ => {
                        assert!(false);
                    }
                }
            },
            Err(e) => {
                assert_eq!(e.to_string(), "OK");
            }
        }
    }
}