nmea_parser/gnss/
mwv.rs

1/*
2Copyright 2021 Linus Eing
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*/
16
17use super::*;
18
19/// MWV - Wind speed and angle
20#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct MwvData {
22    /// wind angle, 0 to 359 degrees
23    pub wind_angle: Option<f64>,
24
25    /// Reference, True/Relative (true = relative, false = true, None = unknown)
26    pub relative: Option<bool>,
27
28    /// Wind speed - knots
29    pub wind_speed_knots: Option<f64>,
30
31    /// Wind speed - km/h
32    pub wind_speed_kmh: Option<f64>,
33}
34
35// -------------------------------------------------------------------------------------------------
36
37/// xxMWV: Wind speed and angle
38
39pub(crate) fn handle(sentence: &str) -> Result<ParsedMessage, ParseError> {
40    let split: Vec<&str> = sentence.split(',').collect();
41
42    Ok(ParsedMessage::Mwv(MwvData {
43        wind_angle: pick_number_field(&split, 1)?,
44        relative: match pick_string_field(&split, 2)
45            .ok_or(ParseError::CorruptedSentence(
46                "pick string for \"relative\" was None".to_string(),
47            ))?
48            .as_str()
49        {
50            "R" => Some(true),
51            "T" => Some(false),
52            _ => None,
53        },
54        wind_speed_knots: match pick_string_field(&split, 4)
55            .ok_or(ParseError::CorruptedSentence(
56                "pick string for \"wind_speed_knots\" was None".to_string(),
57            ))?
58            .as_str()
59        {
60            "N" => pick_number_field(&split, 3)?,
61            "M" => Some(
62                pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
63                    "pick string for \"wind_speed_knots M\" was None".to_string(),
64                ))? * 1.943844,
65            ),
66            "K" => Some(
67                pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
68                    "pick string for \"wind_speed_knots K\" was None".to_string(),
69                ))? * 0.539957,
70            ),
71            _ => None,
72        },
73        wind_speed_kmh: match pick_string_field(&split, 4)
74            .ok_or(ParseError::CorruptedSentence(
75                "pick string for \"wind_speed_kmh\" was None".to_string(),
76            ))?
77            .as_str()
78        {
79            "N" => Some(
80                pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
81                    "pick string for \"wind_speed_kmh N\" was None".to_string(),
82                ))? * 1.852,
83            ),
84            "M" => Some(
85                pick_number_field::<f64>(&split, 3)?.ok_or(ParseError::CorruptedSentence(
86                    "pick string for \"wind_speed_kmh M\" was None".to_string(),
87                ))? * 3.6,
88            ),
89            "K" => pick_number_field(&split, 3)?,
90            _ => None,
91        },
92    }))
93}
94
95// -------------------------------------------------------------------------------------------------
96
97#[cfg(test)]
98mod test {
99    use super::*;
100
101    #[test]
102    fn test_parse_mwv() {
103        match NmeaParser::new().parse_sentence("$WIMWV,295.4,T,33.3,N,A*1C") {
104            Ok(ps) => match ps {
105                ParsedMessage::Mwv(mwv) => {
106                    assert_eq!(mwv.wind_angle, Some(295.4));
107                    assert_eq!(mwv.relative, Some(false));
108                    assert_eq!(mwv.wind_speed_knots, Some(33.3));
109                    assert_eq!(mwv.wind_speed_kmh, Some(33.3 * 1.852));
110                }
111                _ => {
112                    assert!(false);
113                }
114            },
115            Err(e) => {
116                assert_eq!(e.to_string(), "OK");
117            }
118        }
119    }
120}