ogn-parser 0.3.19

OGN message parser for Rust
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use rust_decimal::prelude::*;
use serde::Serialize;
use std::{convert::Infallible, str::FromStr};

use crate::utils::{extract_values, split_value_unit};

#[derive(Debug, PartialEq, Default, Clone, Serialize)]
pub struct StatusComment {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platform: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu_load: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ram_free: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ram_total: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ntp_offset: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ntp_correction: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voltage: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amperage: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cpu_temperature: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visible_senders: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latency: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub senders: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rf_correction_manual: Option<i16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rf_correction_automatic: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub noise: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub senders_signal_quality: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub senders_messages: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub good_senders_signal_quality: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub good_senders: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub good_and_bad_senders: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unparsed: Option<String>,
}

impl FromStr for StatusComment {
    type Err = Infallible;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut status_comment = StatusComment {
            ..Default::default()
        };
        let mut unparsed: Vec<_> = vec![];
        for part in s.split_whitespace() {
            // receiver software version: vX.Y.Z
            // X (major)
            // Y (minor)
            // Z (bugfix)
            if &part[0..1] == "v"
                && part.matches('.').count() == 3
                && status_comment.version.is_none()
            {
                let (first, second) = part
                    .match_indices('.')
                    .nth(2)
                    .map(|(idx, _)| part.split_at(idx))
                    .unwrap();
                status_comment.version = Some(first[1..].into());
                status_comment.platform = Some(second[1..].into());

            // cpu load: CPU:x.x
            // x.x: cpu load as percentage
            } else if part.len() > 4
                && part.starts_with("CPU:")
                && status_comment.cpu_load.is_none()
            {
                if let Ok(cpu_load) = part[4..].parse::<f32>() {
                    status_comment.cpu_load = Decimal::from_f32(cpu_load);
                } else {
                    unparsed.push(part);
                }

            // RAM usage: RAM:x.x/y.yMB
            // x.x: free RAM in MB
            // y.y: total RAM in MB
            } else if part.len() > 6
                && part.starts_with("RAM:")
                && part.ends_with("MB")
                && part.find('/').is_some()
                && status_comment.ram_free.is_none()
            {
                let subpart = &part[4..part.len() - 2];
                let split_point = subpart.find('/').unwrap();
                let (first, second) = subpart.split_at(split_point);
                let ram_free = first.parse::<f32>().ok();
                let ram_total = second[1..].parse::<f32>().ok();
                if ram_free.is_some() && ram_total.is_some() {
                    status_comment.ram_free = ram_free.and_then(Decimal::from_f32);
                    status_comment.ram_total = ram_total.and_then(Decimal::from_f32);
                } else {
                    unparsed.push(part);
                }

            // time synchronisation: NTP:x.xms/y.yppm
            // x.x: NTP offset in [ms]
            // y.y: NTP correction in [ppm]
            } else if part.len() > 6
                && part.starts_with("NTP:")
                && part.find('/').is_some()
                && status_comment.ntp_offset.is_none()
            {
                let subpart = &part[4..part.len() - 3];
                let split_point = subpart.find('/').unwrap();
                let (first, second) = subpart.split_at(split_point);
                let ntp_offset = first[0..first.len() - 2].parse::<f32>().ok();
                let ntp_correction = second[1..].parse::<f32>().ok();
                if ntp_offset.is_some() && ntp_correction.is_some() {
                    status_comment.ntp_offset = ntp_offset.and_then(Decimal::from_f32);
                    status_comment.ntp_correction = ntp_correction.and_then(Decimal::from_f32);
                } else {
                    unparsed.push(part);
                }

            // senders count: x/yAcfts[1h]
            // x: visible senders in the last hour
            // y: total senders in the last hour
            } else if part.len() >= 11
                && part.ends_with("Acfts[1h]")
                && part.find('/').is_some()
                && status_comment.visible_senders.is_none()
            {
                let subpart = &part[0..part.len() - 9];
                let split_point = subpart.find('/').unwrap();
                let (first, second) = subpart.split_at(split_point);
                let visible_senders = first.parse::<u16>().ok();
                let senders = second[1..].parse::<u16>().ok();
                if visible_senders.is_some() && senders.is_some() {
                    status_comment.visible_senders = visible_senders;
                    status_comment.senders = senders;
                } else {
                    unparsed.push(part);
                }

            // latency: Lat:x.xs
            // x.x: latency in [s]
            } else if part.len() > 5
                && part.starts_with("Lat:")
                && part.ends_with("s")
                && status_comment.latency.is_none()
            {
                let latency = part[4..part.len() - 1].parse::<f32>().ok();
                if latency.is_some() {
                    status_comment.latency = latency.and_then(Decimal::from_f32);
                } else {
                    unparsed.push(part);
                }

            // radio frequency informations start with "RF:"
            } else if part.len() >= 11
                && part.starts_with("RF:")
                && status_comment.rf_correction_manual.is_none()
            {
                let values = extract_values(part);
                // short RF format: RF:+x.x/y.yppm/+z.zdB
                // x.x: manual correction in [ppm]
                // y.y: automatic correction in [ppm]
                // z.z: background noise in [dB]
                if values.len() == 3 {
                    let rf_correction_manual = values[0].parse::<i16>().ok();
                    let rf_correction_automatic = values[1].parse::<f32>().ok();
                    let noise = values[2].parse::<f32>().ok();

                    if rf_correction_manual.is_some()
                        && rf_correction_automatic.is_some()
                        && noise.is_some()
                    {
                        status_comment.rf_correction_manual = rf_correction_manual;
                        status_comment.rf_correction_automatic =
                            rf_correction_automatic.and_then(Decimal::from_f32);
                        status_comment.noise = noise.and_then(Decimal::from_f32)
                    } else {
                        unparsed.push(part);
                        continue;
                    }
                // medium RF format: RF:+x.x/y.yppm/+z.zdB/+a.adB@10km[b]
                // a.a: sender signal quality [dB]
                // b: number of messages
                } else if values.len() == 6 {
                    let rf_correction_manual = values[0].parse::<i16>().ok();
                    let rf_correction_automatic = values[1].parse::<f32>().ok();
                    let noise = values[2].parse::<f32>().ok();
                    let senders_signal_quality = values[3].parse::<f32>().ok();
                    let senders_messages = values[5].parse::<u32>().ok();
                    if rf_correction_manual.is_some()
                        && rf_correction_automatic.is_some()
                        && noise.is_some()
                        && senders_signal_quality.is_some()
                        && senders_messages.is_some()
                    {
                        status_comment.rf_correction_manual = rf_correction_manual;
                        status_comment.rf_correction_automatic =
                            rf_correction_automatic.and_then(Decimal::from_f32);
                        status_comment.noise = noise.and_then(Decimal::from_f32);
                        status_comment.senders_signal_quality =
                            senders_signal_quality.and_then(Decimal::from_f32);
                        status_comment.senders_messages = senders_messages;
                    } else {
                        unparsed.push(part);
                        continue;
                    }
                // long RF format: RF:+x.x/y.yppm/+z.zdB/+a.adB@10km[b]/+c.cdB@10km[d/e]
                // c.c: good senders signal quality [dB]
                // d: number of good senders
                // e: number of good and bad senders
                } else if values.len() == 10 {
                    let rf_correction_manual = values[0].parse::<i16>().ok();
                    let rf_correction_automatic = values[1].parse::<f32>().ok();
                    let noise = values[2].parse::<f32>().ok();
                    let senders_signal_quality = values[3].parse::<f32>().ok();
                    let senders_messages = values[5].parse::<u32>().ok();
                    let good_senders_signal_quality = values[6].parse::<f32>().ok();
                    let good_senders = values[8].parse::<u16>().ok();
                    let good_and_bad_senders = values[9].parse::<u16>().ok();
                    if rf_correction_manual.is_some()
                        && rf_correction_automatic.is_some()
                        && noise.is_some()
                        && senders_signal_quality.is_some()
                        && senders_messages.is_some()
                        && good_senders_signal_quality.is_some()
                        && good_senders.is_some()
                        && good_and_bad_senders.is_some()
                    {
                        status_comment.rf_correction_manual = rf_correction_manual;
                        status_comment.rf_correction_automatic =
                            rf_correction_automatic.and_then(Decimal::from_f32);
                        status_comment.noise = noise.and_then(Decimal::from_f32);
                        status_comment.senders_signal_quality =
                            senders_signal_quality.and_then(Decimal::from_f32);
                        status_comment.senders_messages = senders_messages;
                        status_comment.good_senders_signal_quality =
                            good_senders_signal_quality.and_then(Decimal::from_f32);
                        status_comment.good_senders = good_senders;
                        status_comment.good_and_bad_senders = good_and_bad_senders;
                    } else {
                        unparsed.push(part);
                        continue;
                    }
                } else {
                    unparsed.push(part);
                    continue;
                }
            } else if let Some((value, unit)) = split_value_unit(part) {
                // cpu temperature: +x.xC
                // x.x: cpu temperature in [°C]
                if unit == "C" && status_comment.cpu_temperature.is_none() {
                    status_comment.cpu_temperature =
                        value.parse::<f32>().ok().and_then(Decimal::from_f32);
                // voltage: +x.xV
                // x.x: voltage in [V]
                } else if unit == "V" && status_comment.voltage.is_none() {
                    status_comment.voltage = value.parse::<f32>().ok().and_then(Decimal::from_f32);
                // currency: +x.xA
                // x.x: currency in [A]
                } else if unit == "A" && status_comment.amperage.is_none() {
                    status_comment.amperage = value.parse::<f32>().ok().and_then(Decimal::from_f32);
                } else {
                    unparsed.push(part);
                }
            } else {
                unparsed.push(part);
            }
        }
        status_comment.unparsed = if !unparsed.is_empty() {
            Some(unparsed.join(" "))
        } else {
            None
        };

        Ok(status_comment)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sdr() {
        let result = "v0.2.7.RPI-GPU CPU:0.7 RAM:770.2/968.2MB NTP:1.8ms/-3.3ppm +55.7C 7/8Acfts[1h] RF:+54-1.1ppm/-0.16dB/+7.1dB@10km[19481]/+16.8dB@10km[7/13]".parse::<StatusComment>().unwrap();
        assert_eq!(
            result,
            StatusComment {
                version: Some("0.2.7".into()),
                platform: Some("RPI-GPU".into()),
                cpu_load: Decimal::from_f32(0.7),
                ram_free: Decimal::from_f32(770.2),
                ram_total: Decimal::from_f32(968.2),
                ntp_offset: Decimal::from_f32(1.8),
                ntp_correction: Decimal::from_f32(-3.3),
                voltage: None,
                amperage: None,
                cpu_temperature: Decimal::from_f32(55.7),
                visible_senders: Some(7),
                senders: Some(8),
                rf_correction_manual: Some(54),
                rf_correction_automatic: Decimal::from_f32(-1.1),
                noise: Decimal::from_f32(-0.16),
                senders_signal_quality: Decimal::from_f32(7.1),
                senders_messages: Some(19481),
                good_senders_signal_quality: Decimal::from_f32(16.8),
                good_senders: Some(7),
                good_and_bad_senders: Some(13),
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_sdr_different_order() {
        let result = "NTP:1.8ms/-3.3ppm +55.7C CPU:0.7 RAM:770.2/968.2MB 7/8Acfts[1h] RF:+54-1.1ppm/-0.16dB/+7.1dB@10km[19481]/+16.8dB@10km[7/13] v0.2.7.RPI-GPU".parse::<StatusComment>().unwrap();
        assert_eq!(
            result,
            StatusComment {
                version: Some("0.2.7".into()),
                platform: Some("RPI-GPU".into()),
                cpu_load: Decimal::from_f32(0.7),
                ram_free: Decimal::from_f32(770.2),
                ram_total: Decimal::from_f32(968.2),
                ntp_offset: Decimal::from_f32(1.8),
                ntp_correction: Decimal::from_f32(-3.3),
                voltage: None,
                amperage: None,
                cpu_temperature: Decimal::from_f32(55.7),
                visible_senders: Some(7),
                senders: Some(8),
                rf_correction_manual: Some(54),
                rf_correction_automatic: Decimal::from_f32(-1.1),
                noise: Decimal::from_f32(-0.16),
                senders_signal_quality: Decimal::from_f32(7.1),
                senders_messages: Some(19481),
                good_senders_signal_quality: Decimal::from_f32(16.8),
                good_senders: Some(7),
                good_and_bad_senders: Some(13),
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_rf_3() {
        let result = "RF:+29+0.0ppm/+35.22dB".parse::<StatusComment>().unwrap();
        assert_eq!(
            result,
            StatusComment {
                rf_correction_manual: Some(29),
                rf_correction_automatic: Decimal::from_f32(0.0),
                noise: Decimal::from_f32(35.22),
                ..Default::default()
            }
        )
    }

    #[test]
    fn test_rf_6() {
        let result = "RF:+41+56.0ppm/-1.87dB/+0.1dB@10km[1928]"
            .parse::<StatusComment>()
            .unwrap();
        assert_eq!(
            result,
            StatusComment {
                rf_correction_manual: Some(41),
                rf_correction_automatic: Decimal::from_f32(56.0),
                noise: Decimal::from_f32(-1.87),
                senders_signal_quality: Decimal::from_f32(0.1),
                senders_messages: Some(1928),
                ..Default::default()
            }
        )
    }

    #[test]
    fn test_rf_10() {
        let result = "RF:+54-1.1ppm/-0.16dB/+7.1dB@10km[19481]/+16.8dB@10km[7/13]"
            .parse::<StatusComment>()
            .unwrap();
        assert_eq!(
            result,
            StatusComment {
                rf_correction_manual: Some(54),
                rf_correction_automatic: Decimal::from_f32(-1.1),
                noise: Decimal::from_f32(-0.16),
                senders_signal_quality: Decimal::from_f32(7.1),
                senders_messages: Some(19481),
                good_senders_signal_quality: Decimal::from_f32(16.8),
                good_senders: Some(7),
                good_and_bad_senders: Some(13),
                ..Default::default()
            }
        )
    }
}