nmea_parser/gnss/
dpt.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/// DPT - Depth of Water
20#[derive(Clone, Debug, PartialEq, Serialize)]
21pub struct DptData {
22    /// Water depth relative to transducer, meters
23    pub depth_relative_to_transducer: Option<f64>,
24
25    /// Offset from transducer, meters positive means distance from transducer to water line negative means distance from transducer to keel
26    pub transducer_offset: Option<f64>,
27}
28
29// -------------------------------------------------------------------------------------------------
30
31/// xxDPT: Depth of Water
32pub(crate) fn handle(sentence: &str) -> Result<ParsedMessage, ParseError> {
33    let split: Vec<&str> = sentence.split(',').collect();
34
35    Ok(ParsedMessage::Dpt(DptData {
36        depth_relative_to_transducer: pick_number_field(&split, 1)?,
37        transducer_offset: pick_number_field(&split, 2)?,
38    }))
39}
40
41// -------------------------------------------------------------------------------------------------
42
43#[cfg(test)]
44mod test {
45    use super::*;
46    use crate::NmeaParser;
47
48    #[test]
49    fn test_parse_dpt() {
50        match NmeaParser::new().parse_sentence("$SDDPT,17.5,0.3*67") {
51            Ok(ps) => match ps {
52                ParsedMessage::Dpt(dpt) => {
53                    assert_eq!(dpt.depth_relative_to_transducer, Some(17.5));
54                    assert_eq!(dpt.transducer_offset, Some(0.3));
55                }
56                ParsedMessage::Incomplete => {
57                    assert!(false);
58                }
59                _ => {
60                    assert!(false);
61                }
62            },
63            Err(e) => {
64                assert_eq!(e.to_string(), "OK");
65            }
66        }
67    }
68}