aranet-core 0.2.0

Core BLE library 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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Data validation and bounds checking for sensor readings.
//!
//! This module provides validation utilities to detect anomalous readings
//! and flag potential sensor issues.
//!
//! # Example
//!
//! ```
//! use aranet_core::ReadingValidator;
//! use aranet_core::validation::ValidatorConfig;
//! use aranet_types::{CurrentReading, Status};
//!
//! // Create a validator with default config
//! let validator = ReadingValidator::default();
//!
//! // Create a reading to validate
//! let reading = CurrentReading {
//!     co2: 800,
//!     temperature: 22.5,
//!     pressure: 1013.0,
//!     humidity: 45,
//!     battery: 85,
//!     status: Status::Green,
//!     interval: 300,
//!     age: 60,
//!     captured_at: None,
//!     radon: None,
//!     radiation_rate: None,
//!     radiation_total: None,
//!     radon_avg_24h: None,
//!     radon_avg_7d: None,
//!     radon_avg_30d: None,
//! };
//!
//! let result = validator.validate(&reading);
//! assert!(result.is_valid);
//! assert!(!result.has_warnings());
//! ```

use serde::{Deserialize, Serialize};

use aranet_types::{CurrentReading, DeviceType};

/// Warning types for validation issues.
///
/// This enum is marked `#[non_exhaustive]` to allow adding new warning types
/// in future versions without breaking downstream code.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ValidationWarning {
    /// CO2 reading is below minimum expected value.
    Co2TooLow { value: u16, min: u16 },
    /// CO2 reading is above maximum expected value.
    Co2TooHigh { value: u16, max: u16 },
    /// Temperature is below minimum expected value.
    TemperatureTooLow { value: f32, min: f32 },
    /// Temperature is above maximum expected value.
    TemperatureTooHigh { value: f32, max: f32 },
    /// Pressure is below minimum expected value.
    PressureTooLow { value: f32, min: f32 },
    /// Pressure is above maximum expected value.
    PressureTooHigh { value: f32, max: f32 },
    /// Humidity is above 100%.
    HumidityOutOfRange { value: u8 },
    /// Battery level is above 100%.
    BatteryOutOfRange { value: u8 },
    /// CO2 is zero which may indicate sensor error.
    Co2Zero,
    /// All values are zero which may indicate communication error.
    AllZeros,
    /// Radon reading is above maximum expected value.
    RadonTooHigh { value: u32, max: u32 },
    /// Radiation rate is above maximum expected value.
    RadiationRateTooHigh { value: f32, max: f32 },
    /// Radiation total is above maximum expected value.
    RadiationTotalTooHigh { value: f64, max: f64 },
}

impl std::fmt::Display for ValidationWarning {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidationWarning::Co2TooLow { value, min } => {
                write!(f, "CO2 {} ppm is below minimum {} ppm", value, min)
            }
            ValidationWarning::Co2TooHigh { value, max } => {
                write!(f, "CO2 {} ppm exceeds maximum {} ppm", value, max)
            }
            ValidationWarning::TemperatureTooLow { value, min } => {
                write!(f, "Temperature {}°C is below minimum {}°C", value, min)
            }
            ValidationWarning::TemperatureTooHigh { value, max } => {
                write!(f, "Temperature {}°C exceeds maximum {}°C", value, max)
            }
            ValidationWarning::PressureTooLow { value, min } => {
                write!(f, "Pressure {} hPa is below minimum {} hPa", value, min)
            }
            ValidationWarning::PressureTooHigh { value, max } => {
                write!(f, "Pressure {} hPa exceeds maximum {} hPa", value, max)
            }
            ValidationWarning::HumidityOutOfRange { value } => {
                write!(f, "Humidity {}% is out of valid range (0-100)", value)
            }
            ValidationWarning::BatteryOutOfRange { value } => {
                write!(f, "Battery {}% is out of valid range (0-100)", value)
            }
            ValidationWarning::Co2Zero => {
                write!(f, "CO2 reading is zero - possible sensor error")
            }
            ValidationWarning::AllZeros => {
                write!(f, "All readings are zero - possible communication error")
            }
            ValidationWarning::RadonTooHigh { value, max } => {
                write!(f, "Radon {} Bq/m³ exceeds maximum {} Bq/m³", value, max)
            }
            ValidationWarning::RadiationRateTooHigh { value, max } => {
                write!(
                    f,
                    "Radiation rate {} µSv/h exceeds maximum {} µSv/h",
                    value, max
                )
            }
            ValidationWarning::RadiationTotalTooHigh { value, max } => {
                write!(
                    f,
                    "Radiation total {} µSv exceeds maximum {} µSv",
                    value, max
                )
            }
        }
    }
}

/// Result of validating a reading.
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether the reading passed validation.
    pub is_valid: bool,
    /// List of warnings (may be non-empty even if valid).
    pub warnings: Vec<ValidationWarning>,
}

impl ValidationResult {
    /// Create a successful validation result with no warnings.
    pub fn valid() -> Self {
        Self {
            is_valid: true,
            warnings: Vec::new(),
        }
    }

    /// Create an invalid result with the given warnings.
    pub fn invalid(warnings: Vec<ValidationWarning>) -> Self {
        Self {
            is_valid: false,
            warnings,
        }
    }

    /// Create a valid result with warnings.
    pub fn valid_with_warnings(warnings: Vec<ValidationWarning>) -> Self {
        Self {
            is_valid: true,
            warnings,
        }
    }

    /// Check if there are any warnings.
    pub fn has_warnings(&self) -> bool {
        !self.warnings.is_empty()
    }
}

/// Configuration for reading validation.
#[derive(Debug, Clone)]
pub struct ValidatorConfig {
    /// Minimum expected CO2 value (ppm).
    pub co2_min: u16,
    /// Maximum expected CO2 value (ppm).
    pub co2_max: u16,
    /// Minimum expected temperature (°C).
    pub temperature_min: f32,
    /// Maximum expected temperature (°C).
    pub temperature_max: f32,
    /// Minimum expected pressure (hPa).
    pub pressure_min: f32,
    /// Maximum expected pressure (hPa).
    pub pressure_max: f32,
    /// Maximum expected radon value (Bq/m³).
    pub radon_max: u32,
    /// Maximum expected radiation rate (µSv/h).
    pub radiation_rate_max: f32,
    /// Maximum expected radiation total (mSv).
    pub radiation_total_max: f64,
    /// Treat CO2 = 0 as an error.
    pub warn_on_zero_co2: bool,
    /// Treat all zeros as an error.
    pub warn_on_all_zeros: bool,
}

impl Default for ValidatorConfig {
    fn default() -> Self {
        Self {
            co2_min: 300,   // Outdoor ambient is ~400 ppm
            co2_max: 10000, // Very high but possible in some scenarios
            temperature_min: -40.0,
            temperature_max: 85.0,
            pressure_min: 300.0,           // Very high altitude
            pressure_max: 1100.0,          // Sea level or below
            radon_max: 1000,               // WHO action level is 100-300 Bq/m³
            radiation_rate_max: 100.0,     // Normal background is ~0.1-0.2 µSv/h
            radiation_total_max: 100000.0, // Reasonable upper bound for accumulated dose
            warn_on_zero_co2: true,
            warn_on_all_zeros: true,
        }
    }
}

impl ValidatorConfig {
    /// Create new validator config with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set minimum CO2 value (ppm).
    #[must_use]
    pub fn co2_min(mut self, min: u16) -> Self {
        self.co2_min = min;
        self
    }

    /// Set maximum CO2 value (ppm).
    #[must_use]
    pub fn co2_max(mut self, max: u16) -> Self {
        self.co2_max = max;
        self
    }

    /// Set CO2 range (min, max).
    #[must_use]
    pub fn co2_range(mut self, min: u16, max: u16) -> Self {
        self.co2_min = min;
        self.co2_max = max;
        self
    }

    /// Set minimum temperature (°C).
    #[must_use]
    pub fn temperature_min(mut self, min: f32) -> Self {
        self.temperature_min = min;
        self
    }

    /// Set maximum temperature (°C).
    #[must_use]
    pub fn temperature_max(mut self, max: f32) -> Self {
        self.temperature_max = max;
        self
    }

    /// Set temperature range (min, max).
    #[must_use]
    pub fn temperature_range(mut self, min: f32, max: f32) -> Self {
        self.temperature_min = min;
        self.temperature_max = max;
        self
    }

    /// Set minimum pressure (hPa).
    #[must_use]
    pub fn pressure_min(mut self, min: f32) -> Self {
        self.pressure_min = min;
        self
    }

    /// Set maximum pressure (hPa).
    #[must_use]
    pub fn pressure_max(mut self, max: f32) -> Self {
        self.pressure_max = max;
        self
    }

    /// Set pressure range (min, max).
    #[must_use]
    pub fn pressure_range(mut self, min: f32, max: f32) -> Self {
        self.pressure_min = min;
        self.pressure_max = max;
        self
    }

    /// Set whether to warn on CO2 = 0.
    #[must_use]
    pub fn warn_on_zero_co2(mut self, warn: bool) -> Self {
        self.warn_on_zero_co2 = warn;
        self
    }

    /// Set whether to warn on all zeros.
    #[must_use]
    pub fn warn_on_all_zeros(mut self, warn: bool) -> Self {
        self.warn_on_all_zeros = warn;
        self
    }

    /// Set maximum radon value (Bq/m³).
    #[must_use]
    pub fn radon_max(mut self, max: u32) -> Self {
        self.radon_max = max;
        self
    }

    /// Set maximum radiation rate (µSv/h).
    #[must_use]
    pub fn radiation_rate_max(mut self, max: f32) -> Self {
        self.radiation_rate_max = max;
        self
    }

    /// Set maximum radiation total (mSv).
    #[must_use]
    pub fn radiation_total_max(mut self, max: f64) -> Self {
        self.radiation_total_max = max;
        self
    }

    /// Create strict validation config (narrow ranges).
    pub fn strict() -> Self {
        Self {
            co2_min: 350,
            co2_max: 5000,
            temperature_min: -10.0,
            temperature_max: 50.0,
            pressure_min: 800.0,
            pressure_max: 1100.0,
            radon_max: 300, // WHO action level
            radiation_rate_max: 10.0,
            radiation_total_max: 10000.0,
            warn_on_zero_co2: true,
            warn_on_all_zeros: true,
        }
    }

    /// Create relaxed validation config (wide ranges).
    pub fn relaxed() -> Self {
        Self {
            co2_min: 0,
            co2_max: 20000,
            temperature_min: -50.0,
            temperature_max: 100.0,
            pressure_min: 200.0,
            pressure_max: 1200.0,
            radon_max: 5000,
            radiation_rate_max: 1000.0,
            radiation_total_max: 1000000.0,
            warn_on_zero_co2: false,
            warn_on_all_zeros: false,
        }
    }

    /// Create validation config optimized for Aranet4 (CO2 sensor).
    ///
    /// Aranet4 measures CO2, temperature, humidity, and pressure.
    /// This preset uses appropriate ranges for indoor air quality monitoring.
    pub fn for_aranet4() -> Self {
        Self {
            co2_min: 300,   // Outdoor ambient is ~400 ppm
            co2_max: 10000, // Aranet4 max range
            temperature_min: -40.0,
            temperature_max: 60.0, // Aranet4 operating range
            pressure_min: 300.0,
            pressure_max: 1100.0,
            radon_max: 0,             // Not applicable
            radiation_rate_max: 0.0,  // Not applicable
            radiation_total_max: 0.0, // Not applicable
            warn_on_zero_co2: true,
            warn_on_all_zeros: true,
        }
    }

    /// Create validation config optimized for Aranet2 (temperature/humidity sensor).
    ///
    /// Aranet2 measures only temperature and humidity.
    /// CO2 and pressure validation is disabled.
    ///
    /// Note: This preset is based on device specifications. Actual testing
    /// with an Aranet2 device may reveal adjustments needed.
    pub fn for_aranet2() -> Self {
        Self {
            co2_min: 0,     // Not applicable - disable CO2 validation
            co2_max: 65535, // Not applicable
            temperature_min: -40.0,
            temperature_max: 60.0,
            pressure_min: 0.0,        // Not applicable
            pressure_max: 2000.0,     // Not applicable
            radon_max: 0,             // Not applicable
            radiation_rate_max: 0.0,  // Not applicable
            radiation_total_max: 0.0, // Not applicable
            warn_on_zero_co2: false,  // CO2 is not measured
            warn_on_all_zeros: false,
        }
    }

    /// Create validation config optimized for AranetRn+ (radon sensor).
    ///
    /// AranetRn+ measures radon, temperature, humidity, and pressure.
    /// CO2 validation is disabled.
    ///
    /// Note: This preset is based on device specifications. Actual testing
    /// with an AranetRn+ device may reveal adjustments needed.
    pub fn for_aranet_radon() -> Self {
        Self {
            co2_min: 0,     // Not applicable
            co2_max: 65535, // Not applicable
            temperature_min: -40.0,
            temperature_max: 60.0,
            pressure_min: 300.0,
            pressure_max: 1100.0,
            radon_max: 1000,          // WHO action level is 100-300 Bq/m³
            radiation_rate_max: 0.0,  // Not applicable
            radiation_total_max: 0.0, // Not applicable
            warn_on_zero_co2: false,
            warn_on_all_zeros: false,
        }
    }

    /// Create validation config optimized for Aranet Radiation sensor.
    ///
    /// Aranet Radiation measures gamma radiation rate and accumulated dose.
    /// CO2 and radon validation is disabled.
    ///
    /// Note: This preset is based on device specifications. Actual testing
    /// with an Aranet Radiation device may reveal adjustments needed.
    pub fn for_aranet_radiation() -> Self {
        Self {
            co2_min: 0,     // Not applicable
            co2_max: 65535, // Not applicable
            temperature_min: -40.0,
            temperature_max: 60.0,
            pressure_min: 300.0,
            pressure_max: 1100.0,
            radon_max: 0,                  // Not applicable
            radiation_rate_max: 100.0,     // Normal background is ~0.1-0.2 µSv/h
            radiation_total_max: 100000.0, // Reasonable upper bound
            warn_on_zero_co2: false,
            warn_on_all_zeros: false,
        }
    }

    /// Create validation config for a specific device type.
    ///
    /// Automatically selects the appropriate preset based on the device type:
    /// - [`DeviceType::Aranet4`] → [`for_aranet4()`](Self::for_aranet4)
    /// - [`DeviceType::Aranet2`] → [`for_aranet2()`](Self::for_aranet2)
    /// - [`DeviceType::AranetRadon`] → [`for_aranet_radon()`](Self::for_aranet_radon)
    /// - [`DeviceType::AranetRadiation`] → [`for_aranet_radiation()`](Self::for_aranet_radiation)
    /// - Unknown types → default config
    ///
    /// # Example
    ///
    /// ```
    /// use aranet_core::validation::ValidatorConfig;
    /// use aranet_types::DeviceType;
    ///
    /// let config = ValidatorConfig::for_device(DeviceType::Aranet4);
    /// assert_eq!(config.co2_max, 10000);
    ///
    /// let config = ValidatorConfig::for_device(DeviceType::AranetRadon);
    /// assert_eq!(config.radon_max, 1000);
    /// ```
    #[must_use]
    pub fn for_device(device_type: DeviceType) -> Self {
        match device_type {
            DeviceType::Aranet4 => Self::for_aranet4(),
            DeviceType::Aranet2 => Self::for_aranet2(),
            DeviceType::AranetRadon => Self::for_aranet_radon(),
            DeviceType::AranetRadiation => Self::for_aranet_radiation(),
            _ => Self::default(),
        }
    }
}

/// Validator for sensor readings.
#[derive(Debug, Clone, Default)]
pub struct ReadingValidator {
    config: ValidatorConfig,
}

impl ReadingValidator {
    /// Create a new validator with the given configuration.
    pub fn new(config: ValidatorConfig) -> Self {
        Self { config }
    }

    /// Get the configuration.
    pub fn config(&self) -> &ValidatorConfig {
        &self.config
    }

    /// Validate a sensor reading.
    pub fn validate(&self, reading: &CurrentReading) -> ValidationResult {
        let mut warnings = Vec::new();

        // Check for all zeros (use approximate comparison for floats)
        if self.config.warn_on_all_zeros
            && reading.co2 == 0
            && reading.temperature.abs() < f32::EPSILON
            && reading.pressure.abs() < f32::EPSILON
            && reading.humidity == 0
        {
            warnings.push(ValidationWarning::AllZeros);
            return ValidationResult::invalid(warnings);
        }

        // Check CO2
        if reading.co2 > 0 {
            if reading.co2 < self.config.co2_min {
                warnings.push(ValidationWarning::Co2TooLow {
                    value: reading.co2,
                    min: self.config.co2_min,
                });
            }
            if reading.co2 > self.config.co2_max {
                warnings.push(ValidationWarning::Co2TooHigh {
                    value: reading.co2,
                    max: self.config.co2_max,
                });
            }
        } else if self.config.warn_on_zero_co2 {
            warnings.push(ValidationWarning::Co2Zero);
        }

        // Check temperature
        if reading.temperature < self.config.temperature_min {
            warnings.push(ValidationWarning::TemperatureTooLow {
                value: reading.temperature,
                min: self.config.temperature_min,
            });
        }
        if reading.temperature > self.config.temperature_max {
            warnings.push(ValidationWarning::TemperatureTooHigh {
                value: reading.temperature,
                max: self.config.temperature_max,
            });
        }

        // Check pressure (skip if 0, might be Aranet2)
        if reading.pressure > 0.0 {
            if reading.pressure < self.config.pressure_min {
                warnings.push(ValidationWarning::PressureTooLow {
                    value: reading.pressure,
                    min: self.config.pressure_min,
                });
            }
            if reading.pressure > self.config.pressure_max {
                warnings.push(ValidationWarning::PressureTooHigh {
                    value: reading.pressure,
                    max: self.config.pressure_max,
                });
            }
        }

        // Check humidity
        if reading.humidity > 100 {
            warnings.push(ValidationWarning::HumidityOutOfRange {
                value: reading.humidity,
            });
        }

        // Check battery
        if reading.battery > 100 {
            warnings.push(ValidationWarning::BatteryOutOfRange {
                value: reading.battery,
            });
        }

        // Check radon (if present)
        if let Some(radon) = reading.radon
            && radon > self.config.radon_max
        {
            warnings.push(ValidationWarning::RadonTooHigh {
                value: radon,
                max: self.config.radon_max,
            });
        }

        // Check radiation rate (if present)
        if let Some(rate) = reading.radiation_rate
            && rate > self.config.radiation_rate_max
        {
            warnings.push(ValidationWarning::RadiationRateTooHigh {
                value: rate,
                max: self.config.radiation_rate_max,
            });
        }

        // Check radiation total (if present)
        if let Some(total) = reading.radiation_total
            && total > self.config.radiation_total_max
        {
            warnings.push(ValidationWarning::RadiationTotalTooHigh {
                value: total,
                max: self.config.radiation_total_max,
            });
        }

        if warnings.is_empty() {
            ValidationResult::valid()
        } else {
            // Determine if any warnings are critical
            let has_critical = warnings.iter().any(|w| {
                matches!(
                    w,
                    ValidationWarning::AllZeros
                        | ValidationWarning::Co2TooHigh { .. }
                        | ValidationWarning::TemperatureTooHigh { .. }
                        | ValidationWarning::RadonTooHigh { .. }
                        | ValidationWarning::RadiationRateTooHigh { .. }
                )
            });

            if has_critical {
                ValidationResult::invalid(warnings)
            } else {
                ValidationResult::valid_with_warnings(warnings)
            }
        }
    }

    /// Quick check if a CO2 value is within expected range.
    pub fn is_co2_valid(&self, co2: u16) -> bool {
        co2 >= self.config.co2_min && co2 <= self.config.co2_max
    }

    /// Quick check if a temperature value is within expected range.
    pub fn is_temperature_valid(&self, temp: f32) -> bool {
        temp >= self.config.temperature_min && temp <= self.config.temperature_max
    }
}

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

    fn make_reading(co2: u16, temp: f32, pressure: f32, humidity: u8) -> CurrentReading {
        CurrentReading {
            co2,
            temperature: temp,
            pressure,
            humidity,
            battery: 80,
            status: Status::Green,
            interval: 300,
            age: 60,
            captured_at: None,
            radon: None,
            radiation_rate: None,
            radiation_total: None,
            radon_avg_24h: None,
            radon_avg_7d: None,
            radon_avg_30d: None,
        }
    }

    #[test]
    fn test_valid_reading() {
        let validator = ReadingValidator::default();
        let reading = make_reading(800, 22.5, 1013.2, 50);
        let result = validator.validate(&reading);
        assert!(result.is_valid);
        assert!(result.warnings.is_empty());
    }

    #[test]
    fn test_co2_too_high() {
        let validator = ReadingValidator::default();
        let reading = make_reading(15000, 22.5, 1013.2, 50);
        let result = validator.validate(&reading);
        assert!(!result.is_valid);
        assert!(
            result
                .warnings
                .iter()
                .any(|w| matches!(w, ValidationWarning::Co2TooHigh { .. }))
        );
    }

    #[test]
    fn test_all_zeros() {
        let validator = ReadingValidator::default();
        let reading = make_reading(0, 0.0, 0.0, 0);
        let result = validator.validate(&reading);
        assert!(!result.is_valid);
        assert!(
            result
                .warnings
                .iter()
                .any(|w| matches!(w, ValidationWarning::AllZeros))
        );
    }

    #[test]
    fn test_humidity_out_of_range() {
        let validator = ReadingValidator::default();
        let reading = make_reading(800, 22.5, 1013.2, 150);
        let result = validator.validate(&reading);
        assert!(result.has_warnings());
        assert!(
            result
                .warnings
                .iter()
                .any(|w| matches!(w, ValidationWarning::HumidityOutOfRange { .. }))
        );
    }

    #[test]
    fn test_for_device_aranet4() {
        let config = ValidatorConfig::for_device(DeviceType::Aranet4);
        assert_eq!(config.co2_min, 300);
        assert_eq!(config.co2_max, 10000);
        assert!(config.warn_on_zero_co2);
    }

    #[test]
    fn test_for_device_aranet2() {
        let config = ValidatorConfig::for_device(DeviceType::Aranet2);
        assert_eq!(config.co2_min, 0); // CO2 validation disabled
        assert!(!config.warn_on_zero_co2);
    }

    #[test]
    fn test_for_device_aranet_radon() {
        let config = ValidatorConfig::for_device(DeviceType::AranetRadon);
        assert_eq!(config.radon_max, 1000);
        assert!(!config.warn_on_zero_co2);
    }

    #[test]
    fn test_for_device_aranet_radiation() {
        let config = ValidatorConfig::for_device(DeviceType::AranetRadiation);
        assert_eq!(config.radiation_rate_max, 100.0);
        assert_eq!(config.radiation_total_max, 100000.0);
        assert!(!config.warn_on_zero_co2);
    }
}