aranet-cli 0.2.0

Command-line interface for Aranet environmental sensors
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Reusable widget components for the TUI.
//!
//! This module provides helper functions for creating styled widget components
//! used throughout the terminal user interface.

use ratatui::prelude::*;

use aranet_core::settings::{DeviceSettings, RadonUnit, TemperatureUnit};
use aranet_types::HistoryRecord;

use super::theme::AppTheme;

/// Convert Celsius to Fahrenheit.
#[inline]
fn celsius_to_fahrenheit(celsius: f32) -> f32 {
    celsius * 9.0 / 5.0 + 32.0
}

/// Convert Bq/m³ to pCi/L (1 Bq/m³ = 0.027 pCi/L).
#[inline]
fn bq_to_pci(bq: u32) -> f32 {
    bq as f32 * 0.027
}

/// Format temperature value based on device settings.
///
/// Uses the device's temperature unit setting if available, otherwise defaults to Celsius.
#[must_use]
pub fn format_temp_for_device(celsius: f32, settings: Option<&DeviceSettings>) -> String {
    let use_fahrenheit = settings
        .map(|s| s.temperature_unit == TemperatureUnit::Fahrenheit)
        .unwrap_or(false);

    if use_fahrenheit {
        format!("{:.1}°F", celsius_to_fahrenheit(celsius))
    } else {
        format!("{:.1}°C", celsius)
    }
}

/// Format radon value based on device settings.
///
/// Uses the device's radon unit setting if available, otherwise defaults to Bq/m³.
#[must_use]
pub fn format_radon_for_device(bq: u32, settings: Option<&DeviceSettings>) -> String {
    let use_pci = settings
        .map(|s| s.radon_unit == RadonUnit::PciL)
        .unwrap_or(false);

    if use_pci {
        format!("{:.2} pCi/L", bq_to_pci(bq))
    } else {
        format!("{} Bq/m³", bq)
    }
}

/// Get the radon unit string based on device settings.
#[must_use]
pub fn radon_unit_for_device(settings: Option<&DeviceSettings>) -> &'static str {
    let use_pci = settings
        .map(|s| s.radon_unit == RadonUnit::PciL)
        .unwrap_or(false);

    if use_pci { "pCi/L" } else { "Bq/m³" }
}

/// Convert radon value for display based on device settings.
///
/// Returns the value converted to the appropriate unit.
#[must_use]
pub fn convert_radon_for_device(bq: u32, settings: Option<&DeviceSettings>) -> f32 {
    let use_pci = settings
        .map(|s| s.radon_unit == RadonUnit::PciL)
        .unwrap_or(false);

    if use_pci { bq_to_pci(bq) } else { bq as f32 }
}

/// Extracts primary sensor values from history records for use in a sparkline widget.
///
/// Returns CO2 for Aranet4, radon for AranetRadon, or attempts both for unknown types.
///
/// # Arguments
///
/// * `history` - Slice of history records
/// * `device_type` - Optional device type to determine which values to extract
///
/// # Returns
///
/// A [`Vec<u64>`] containing the primary sensor values.
/// Returns an empty vector if no valid data is found.
#[must_use]
pub fn sparkline_data(
    history: &[HistoryRecord],
    device_type: Option<aranet_types::DeviceType>,
) -> Vec<u64> {
    use aranet_types::DeviceType;

    match device_type {
        Some(DeviceType::AranetRadon) => {
            // For radon devices, extract radon values
            history
                .iter()
                .filter_map(|record| record.radon)
                .map(u64::from)
                .collect()
        }
        Some(DeviceType::AranetRadiation) => {
            // For radiation devices, extract radiation rate if available
            history
                .iter()
                .filter_map(|record| record.radiation_rate)
                .map(|r| r as u64)
                .collect()
        }
        _ => {
            // For Aranet4 and others, use CO2
            history
                .iter()
                .filter(|record| record.co2 > 0)
                .map(|record| u64::from(record.co2))
                .collect()
        }
    }
}

/// Resample sparkline data to fit a target width.
///
/// If the data has fewer points than the target width, it will be upsampled
/// by repeating values to fill the space. If it has more points, it will
/// be downsampled by averaging values into buckets.
///
/// # Arguments
///
/// * `data` - The original sparkline data
/// * `target_width` - The desired number of data points (typically the screen width)
///
/// # Returns
///
/// A [`Vec<u64>`] with exactly `target_width` data points.
#[must_use]
pub fn resample_sparkline_data(data: &[u64], target_width: usize) -> Vec<u64> {
    if data.is_empty() || target_width == 0 {
        return Vec::new();
    }

    if data.len() == target_width {
        return data.to_vec();
    }

    let mut result = Vec::with_capacity(target_width);

    if data.len() < target_width {
        // Upsample: repeat values to fill the space
        // Use linear interpolation-like approach for smoother visualization
        for i in 0..target_width {
            let src_idx = i * (data.len() - 1) / (target_width - 1).max(1);
            result.push(data[src_idx.min(data.len() - 1)]);
        }
    } else {
        // Downsample: average values into buckets
        let bucket_size = data.len() as f64 / target_width as f64;
        for i in 0..target_width {
            let start = (i as f64 * bucket_size) as usize;
            let end = ((i + 1) as f64 * bucket_size) as usize;
            let end = end.min(data.len());

            if start < end {
                let sum: u64 = data[start..end].iter().sum();
                let avg = sum / (end - start) as u64;
                result.push(avg);
            } else if start < data.len() {
                result.push(data[start]);
            }
        }
    }

    result
}

/// Calculate trend indicator based on current and previous values.
/// Returns (arrow character, color) tuple.
pub fn trend_indicator(
    theme: &AppTheme,
    current: i32,
    previous: i32,
    threshold: i32,
) -> (&'static str, Color) {
    let diff = current - previous;
    if diff > threshold {
        ("", theme.trend_color(diff, threshold))
    } else if diff < -threshold {
        ("", theme.trend_color(diff, threshold))
    } else {
        ("", theme.trend_color(diff, threshold))
    }
}

/// Calculate trend for CO2 readings.
pub fn co2_trend(
    theme: &AppTheme,
    current: u16,
    previous: Option<u16>,
) -> Option<(&'static str, Color)> {
    previous.map(|prev| trend_indicator(theme, current as i32, prev as i32, 20))
}

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

    // ========================================================================
    // celsius_to_fahrenheit tests
    // ========================================================================

    #[test]
    fn test_celsius_to_fahrenheit_freezing() {
        let result = celsius_to_fahrenheit(0.0);
        assert!((result - 32.0).abs() < 0.01);
    }

    #[test]
    fn test_celsius_to_fahrenheit_boiling() {
        let result = celsius_to_fahrenheit(100.0);
        assert!((result - 212.0).abs() < 0.01);
    }

    #[test]
    fn test_celsius_to_fahrenheit_negative() {
        // -40 is where C and F are equal
        let result = celsius_to_fahrenheit(-40.0);
        assert!((result - (-40.0)).abs() < 0.01);
    }

    // ========================================================================
    // bq_to_pci tests
    // ========================================================================

    #[test]
    fn test_bq_to_pci_zero() {
        let result = bq_to_pci(0);
        assert!((result - 0.0).abs() < 0.001);
    }

    #[test]
    fn test_bq_to_pci_100() {
        // 100 Bq/m3 = 2.7 pCi/L
        let result = bq_to_pci(100);
        assert!((result - 2.7).abs() < 0.01);
    }

    // ========================================================================
    // format_temp_for_device tests
    // ========================================================================

    #[test]
    fn test_format_temp_no_settings_defaults_celsius() {
        let result = format_temp_for_device(20.5, None);
        assert_eq!(result, "20.5°C");
    }

    #[test]
    fn test_format_temp_celsius_setting() {
        let settings = DeviceSettings {
            temperature_unit: TemperatureUnit::Celsius,
            ..Default::default()
        };
        let result = format_temp_for_device(20.5, Some(&settings));
        assert_eq!(result, "20.5°C");
    }

    #[test]
    fn test_format_temp_fahrenheit_setting() {
        let settings = DeviceSettings {
            temperature_unit: TemperatureUnit::Fahrenheit,
            ..Default::default()
        };
        let result = format_temp_for_device(20.0, Some(&settings));
        // 20C = 68F
        assert_eq!(result, "68.0°F");
    }

    // ========================================================================
    // format_radon_for_device tests
    // ========================================================================

    #[test]
    fn test_format_radon_no_settings_defaults_bq() {
        let result = format_radon_for_device(150, None);
        assert_eq!(result, "150 Bq/m³");
    }

    #[test]
    fn test_format_radon_bq_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::BqM3,
            ..Default::default()
        };
        let result = format_radon_for_device(150, Some(&settings));
        assert_eq!(result, "150 Bq/m³");
    }

    #[test]
    fn test_format_radon_pci_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::PciL,
            ..Default::default()
        };
        let result = format_radon_for_device(100, Some(&settings));
        // 100 Bq/m3 = 2.70 pCi/L
        assert_eq!(result, "2.70 pCi/L");
    }

    // ========================================================================
    // resample_sparkline_data tests
    // ========================================================================

    #[test]
    fn test_resample_empty_data() {
        let result = resample_sparkline_data(&[], 10);
        assert!(result.is_empty());
    }

    #[test]
    fn test_resample_zero_width() {
        let result = resample_sparkline_data(&[1, 2, 3], 0);
        assert!(result.is_empty());
    }

    #[test]
    fn test_resample_same_size() {
        let data = vec![1, 2, 3, 4, 5];
        let result = resample_sparkline_data(&data, 5);
        assert_eq!(result, data);
    }

    #[test]
    fn test_resample_upsample() {
        let data = vec![100, 200];
        let result = resample_sparkline_data(&data, 4);
        assert_eq!(result.len(), 4);
    }

    #[test]
    fn test_resample_downsample() {
        let data = vec![100, 100, 200, 200];
        let result = resample_sparkline_data(&data, 2);
        assert_eq!(result.len(), 2);
        // Should average buckets
        assert_eq!(result[0], 100);
        assert_eq!(result[1], 200);
    }

    // ========================================================================
    // trend_indicator tests
    // ========================================================================

    #[test]
    fn test_trend_indicator_rising() {
        let theme = AppTheme::dark();
        let (arrow, color) = trend_indicator(&theme, 500, 400, 20);
        assert_eq!(arrow, "");
        assert_eq!(color, theme.trend_rising);
    }

    #[test]
    fn test_trend_indicator_falling() {
        let theme = AppTheme::dark();
        let (arrow, color) = trend_indicator(&theme, 400, 500, 20);
        assert_eq!(arrow, "");
        assert_eq!(color, theme.trend_falling);
    }

    #[test]
    fn test_trend_indicator_stable() {
        let theme = AppTheme::dark();
        let (arrow, color) = trend_indicator(&theme, 500, 505, 20);
        assert_eq!(arrow, "");
        assert_eq!(color, theme.trend_stable);
    }

    // ========================================================================
    // co2_trend tests
    // ========================================================================

    #[test]
    fn test_co2_trend_no_previous() {
        let theme = AppTheme::dark();
        let result = co2_trend(&theme, 800, None);
        assert!(result.is_none());
    }

    #[test]
    fn test_co2_trend_rising() {
        let theme = AppTheme::dark();
        let result = co2_trend(&theme, 850, Some(800));
        assert!(result.is_some());
        let (arrow, color) = result.expect("co2_trend should return Some for rising trend");
        assert_eq!(arrow, "");
        assert_eq!(color, theme.trend_rising);
    }

    #[test]
    fn test_co2_trend_falling() {
        let theme = AppTheme::dark();
        let result = co2_trend(&theme, 750, Some(800));
        assert!(result.is_some());
        let (arrow, color) = result.expect("co2_trend should return Some for falling trend");
        assert_eq!(arrow, "");
        assert_eq!(color, theme.trend_falling);
    }

    #[test]
    fn test_co2_trend_stable() {
        let theme = AppTheme::dark();
        let result = co2_trend(&theme, 805, Some(800));
        assert!(result.is_some());
        let (arrow, _) = result.expect("co2_trend should return Some for stable trend");
        assert_eq!(arrow, "");
    }

    // ========================================================================
    // radon_unit_for_device tests
    // ========================================================================

    #[test]
    fn test_radon_unit_no_settings() {
        let result = radon_unit_for_device(None);
        assert_eq!(result, "Bq/m³");
    }

    #[test]
    fn test_radon_unit_bq_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::BqM3,
            ..Default::default()
        };
        let result = radon_unit_for_device(Some(&settings));
        assert_eq!(result, "Bq/m³");
    }

    #[test]
    fn test_radon_unit_pci_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::PciL,
            ..Default::default()
        };
        let result = radon_unit_for_device(Some(&settings));
        assert_eq!(result, "pCi/L");
    }

    // ========================================================================
    // convert_radon_for_device tests
    // ========================================================================

    #[test]
    fn test_convert_radon_no_settings() {
        let result = convert_radon_for_device(100, None);
        assert_eq!(result, 100.0);
    }

    #[test]
    fn test_convert_radon_bq_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::BqM3,
            ..Default::default()
        };
        let result = convert_radon_for_device(100, Some(&settings));
        assert_eq!(result, 100.0);
    }

    #[test]
    fn test_convert_radon_pci_setting() {
        let settings = DeviceSettings {
            radon_unit: RadonUnit::PciL,
            ..Default::default()
        };
        let result = convert_radon_for_device(100, Some(&settings));
        // 100 Bq/m3 = 2.7 pCi/L
        assert!((result - 2.7).abs() < 0.01);
    }

    // ========================================================================
    // sparkline_data tests
    // ========================================================================

    #[test]
    fn test_sparkline_data_empty() {
        let result = sparkline_data(&[], None);
        assert!(result.is_empty());
    }

    #[test]
    fn test_sparkline_data_aranet4() {
        use aranet_types::DeviceType;
        use time::OffsetDateTime;

        let history = vec![
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 800,
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: None,
                radiation_rate: None,
                radiation_total: None,
            },
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 850,
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: None,
                radiation_rate: None,
                radiation_total: None,
            },
        ];

        let result = sparkline_data(&history, Some(DeviceType::Aranet4));
        assert_eq!(result, vec![800, 850]);
    }

    #[test]
    fn test_sparkline_data_radon() {
        use aranet_types::DeviceType;
        use time::OffsetDateTime;

        let history = vec![
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 0,
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: Some(100),
                radiation_rate: None,
                radiation_total: None,
            },
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 0,
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: Some(150),
                radiation_rate: None,
                radiation_total: None,
            },
        ];

        let result = sparkline_data(&history, Some(DeviceType::AranetRadon));
        assert_eq!(result, vec![100, 150]);
    }

    #[test]
    fn test_sparkline_data_filters_zero_co2() {
        use time::OffsetDateTime;

        let history = vec![
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 0, // Should be filtered out
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: None,
                radiation_rate: None,
                radiation_total: None,
            },
            HistoryRecord {
                timestamp: OffsetDateTime::now_utc(),
                co2: 800,
                temperature: 22.5,
                humidity: 45,
                pressure: 1013.0,
                radon: None,
                radiation_rate: None,
                radiation_total: None,
            },
        ];

        let result = sparkline_data(&history, None);
        assert_eq!(result, vec![800]); // Zero CO2 filtered out
    }
}