nmea_parser/gnss/vhw.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/// VHW - Water speed and heading
20#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct VhwData {
22 /// Heading - true
23 pub heading_true: Option<f64>,
24
25 /// Heading - magnetic
26 pub heading_magnetic: Option<f64>,
27
28 /// Velocity relative to water - knots
29 pub speed_through_water_knots: Option<f64>,
30
31 /// Velocity relative to water - km/h
32 pub speed_through_water_kmh: Option<f64>,
33}
34
35// -------------------------------------------------------------------------------------------------
36
37// xxVHW: Water speed and heading
38
39pub(crate) fn handle(sentence: &str) -> Result<ParsedMessage, ParseError> {
40 let split: Vec<&str> = sentence.split(',').collect();
41
42 Ok(ParsedMessage::Vhw(VhwData {
43 heading_true: pick_number_field(&split, 1)?,
44 heading_magnetic: pick_number_field(&split, 3)?,
45 speed_through_water_knots: pick_number_field(&split, 5)?,
46 speed_through_water_kmh: pick_number_field(&split, 7)?,
47 }))
48}
49
50// -------------------------------------------------------------------------------------------------
51
52#[cfg(test)]
53mod test {
54 use super::*;
55
56 #[test]
57 fn test_parse_vhw() {
58 match NmeaParser::new().parse_sentence("$IIVHW,15.0,T,15.0,M,6.3,N,11.8,K*68") {
59 Ok(ps) => match ps {
60 ParsedMessage::Vhw(vhw) => {
61 assert_eq!(vhw.heading_true, Some(15.0));
62 assert_eq!(vhw.heading_magnetic, Some(15.0));
63 assert_eq!(vhw.speed_through_water_knots, Some(6.3));
64 assert_eq!(vhw.speed_through_water_kmh, Some(11.8));
65 }
66 _ => {
67 assert!(false);
68 }
69 },
70 Err(e) => {
71 assert_eq!(e.to_string(), "OK");
72 }
73 }
74 }
75}