irox_carto/
gps.rs

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
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

//!
//! GPS Status Types, Satellite Signal, Fix Type, Dilution of Precision, etc

extern crate alloc;
use alloc::string::ToString;
use core::fmt::{Display, Formatter};

use irox_tools::format;
use irox_tools::options::MaybeFrom;
use irox_units::units::compass::Azimuth;

use crate::coordinate::Elevation;

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct SatelliteSignal {
    pub prn: u8,
    pub azimuth: Azimuth,
    pub elevation: Elevation,
    pub snr: u8,
}

impl Display for SatelliteSignal {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!(
            "PRN: {} Az: {} El: {}, SNR: {}",
            self.prn, self.azimuth, self.elevation, self.snr
        ))
    }
}

/// GPS Fix Type
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub enum GPSFixType {
    #[default]
    Unknown = 0,
    NoFix = 1,
    TwoDim = 2,
    ThreeDim = 3,
}
impl From<i32> for GPSFixType {
    fn from(value: i32) -> Self {
        match value {
            1 => GPSFixType::NoFix,
            2 => GPSFixType::TwoDim,
            3 => GPSFixType::ThreeDim,
            _ => GPSFixType::Unknown,
        }
    }
}
impl From<Option<&str>> for GPSFixType {
    fn from(value: Option<&str>) -> Self {
        if let Some(value) = value {
            if let Ok(value) = value.parse::<i32>() {
                return value.into();
            }
        }
        GPSFixType::Unknown
    }
}

#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Default)]
pub struct DilutionOfPrecision(pub f64);
impl From<f64> for DilutionOfPrecision {
    fn from(value: f64) -> Self {
        DilutionOfPrecision(value)
    }
}
impl From<DilutionOfPrecision> for f64 {
    fn from(value: DilutionOfPrecision) -> Self {
        value.0
    }
}
impl MaybeFrom<Option<f64>> for DilutionOfPrecision {
    fn maybe_from(value: Option<f64>) -> Option<Self> {
        Some(value?.into())
    }
}
impl Display for DilutionOfPrecision {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("{}", self.0))
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct DOPs {
    pub geometric: Option<DilutionOfPrecision>,
    pub horizontal: Option<DilutionOfPrecision>,
    pub position: Option<DilutionOfPrecision>,
    pub time: Option<DilutionOfPrecision>,
    pub vertical: Option<DilutionOfPrecision>,
}

impl DOPs {
    #[must_use]
    pub fn new() -> DOPs {
        Default::default()
    }
}

impl Display for DOPs {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let print = |x: Option<DilutionOfPrecision>| match x {
            Some(x) => format!("{:0.3}", x.0),
            None => "?".to_string(),
        };
        write!(
            f,
            "hdop: {} vdop: {} pdop: {} gdop: {} tdop: {}",
            print(self.horizontal),
            print(self.vertical),
            print(self.position),
            print(self.geometric),
            print(self.time)
        )
    }
}

#[cfg(all(target_os = "windows", feature = "windows"))]
pub mod windows {
    use windows::Devices::Geolocation::Geocoordinate;
    use windows::Foundation::IReference;

    use crate::gps::{DOPs, DilutionOfPrecision};

    impl DOPs {
        pub fn maybe_from(coord: &Geocoordinate) -> Option<DOPs> {
            let Ok(sats) = coord.SatelliteData() else {
                return None;
            };

            let get_dop = |v: IReference<f64>| -> Option<DilutionOfPrecision> {
                v.GetDouble().ok().map(DilutionOfPrecision)
            };
            let geometric = sats.GeometricDilutionOfPrecision().ok().and_then(get_dop);
            let horizontal = sats.HorizontalDilutionOfPrecision().ok().and_then(get_dop);
            let position = sats.PositionDilutionOfPrecision().ok().and_then(get_dop);
            let time = sats.TimeDilutionOfPrecision().ok().and_then(get_dop);
            let vertical = sats.VerticalDilutionOfPrecision().ok().and_then(get_dop);

            Some(DOPs {
                geometric,
                horizontal,
                position,
                time,
                vertical,
            })
        }
    }
}