nmea_parser/ais/
vdm_t11.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*/
16
17use super::*;
18
19/// AIS VDM/VDO type 11: UTC/Date Response
20pub(crate) fn handle(
21    bv: &BitVec,
22    station: Station,
23    own_vessel: bool,
24) -> Result<ParsedMessage, ParseError> {
25    Ok(ParsedMessage::UtcDateResponse(BaseStationReport {
26        own_vessel: { own_vessel },
27        station: { station },
28        mmsi: { pick_u64(bv, 8, 30) as u32 },
29        timestamp: {
30            Some(parse_ymdhs(
31                pick_u64(bv, 38, 14) as i32,
32                pick_u64(bv, 52, 4) as u32,
33                pick_u64(bv, 56, 5) as u32,
34                pick_u64(bv, 61, 5) as u32,
35                pick_u64(bv, 66, 6) as u32,
36                pick_u64(bv, 72, 6) as u32,
37            )?)
38        },
39        high_position_accuracy: { pick_u64(bv, 78, 1) != 0 },
40        latitude: {
41            let lat_raw = pick_i64(bv, 107, 27) as i32;
42            if lat_raw != 0x3412140 {
43                Some((lat_raw as f64) / 600000.0)
44            } else {
45                None
46            }
47        },
48        longitude: {
49            let lon_raw = pick_i64(bv, 79, 28) as i32;
50            if lon_raw != 0x6791AC0 {
51                Some((lon_raw as f64) / 600000.0)
52            } else {
53                None
54            }
55        },
56        position_fix_type: {
57            let raw = pick_u64(bv, 134, 4) as u8;
58            match raw {
59                0 => None,
60                _ => Some(PositionFixType::new(raw)),
61            }
62        },
63        raim_flag: { pick_u64(bv, 148, 1) != 0 },
64        radio_status: { pick_u64(bv, 149, 19) as u32 },
65    }))
66}
67
68// -------------------------------------------------------------------------------------------------
69
70#[cfg(test)]
71mod test {
72    use super::*;
73
74    #[test]
75    fn test_parse_vdm_type11() {
76        let mut p = NmeaParser::new();
77        match p.parse_sentence("!AIVDM,1,1,,B,;4R33:1uUK2F`q?mOt@@GoQ00000,0*5D,s28089,d-103,T39.44353985,x147521,r08TPHI1,1242958962") {
78            Ok(ps) => {
79                match ps {
80                    // The expected result
81                    ParsedMessage::UtcDateResponse(bsr) => {
82                        assert_eq!(bsr.mmsi, 304137000);
83                        assert_eq!(bsr.timestamp, Utc.with_ymd_and_hms(2009, 5, 22,2, 22, 40).single());
84                        assert!(bsr.high_position_accuracy);
85                        assert::close(bsr.latitude.unwrap_or(0.0), 28.409, 0.001);
86                        assert::close(bsr.longitude.unwrap_or(0.0), -94.407, 0.001);
87                        assert_eq!(bsr.position_fix_type, Some(PositionFixType::GPS));
88                        assert!(!bsr.raim_flag);
89                        assert_eq!(bsr.radio_status, 0);
90                    },
91                    ParsedMessage::Incomplete => {
92                        assert!(false);
93                    },
94                    _ => {
95                        assert!(false);
96                    }
97                }
98            },
99            Err(e) => {
100                assert_eq!(e.to_string(), "OK");
101            }
102        }
103    }
104}