lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
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
746
747
748
749
750
751
752
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0
//
// S.M.A.R.T. Monitoring
// Predictive disk failure detection with machine learning.

use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use libm;
use spin::Mutex;

/// S.M.A.R.T. attribute types
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SmartAttribute {
    /// Reallocated sectors count
    ReallocatedSectors,
    /// Spin retry count
    SpinRetry,
    /// End-to-end errors
    EndToEndErrors,
    /// Reported uncorrectable errors
    ReportedUncorrectable,
    /// Command timeout
    CommandTimeout,
    /// Current pending sectors
    CurrentPendingSectors,
    /// Offline uncorrectable
    OfflineUncorrectable,
    /// Temperature (Celsius)
    Temperature,
    /// Power-on hours
    PowerOnHours,
    /// Total LBAs written
    TotalLbasWritten,
    /// Total LBAs read
    TotalLbasRead,
    /// UDMA CRC error count
    UdmaCrcErrors,
}

impl SmartAttribute {
    /// Get attribute name
    pub fn name(&self) -> &'static str {
        match self {
            SmartAttribute::ReallocatedSectors => "Reallocated Sectors",
            SmartAttribute::SpinRetry => "Spin Retry",
            SmartAttribute::EndToEndErrors => "End-to-End Errors",
            SmartAttribute::ReportedUncorrectable => "Reported Uncorrectable",
            SmartAttribute::CommandTimeout => "Command Timeout",
            SmartAttribute::CurrentPendingSectors => "Current Pending Sectors",
            SmartAttribute::OfflineUncorrectable => "Offline Uncorrectable",
            SmartAttribute::Temperature => "Temperature",
            SmartAttribute::PowerOnHours => "Power-On Hours",
            SmartAttribute::TotalLbasWritten => "Total LBAs Written",
            SmartAttribute::TotalLbasRead => "Total LBAs Read",
            SmartAttribute::UdmaCrcErrors => "UDMA CRC Errors",
        }
    }

    /// Get critical threshold (failure if exceeded)
    pub fn critical_threshold(&self) -> u64 {
        match self {
            SmartAttribute::ReallocatedSectors => 10,
            SmartAttribute::SpinRetry => 5,
            SmartAttribute::EndToEndErrors => 1,
            SmartAttribute::ReportedUncorrectable => 1,
            SmartAttribute::CommandTimeout => 100,
            SmartAttribute::CurrentPendingSectors => 5,
            SmartAttribute::OfflineUncorrectable => 1,
            SmartAttribute::Temperature => 60, // 60°C
            SmartAttribute::PowerOnHours => 50000,
            SmartAttribute::TotalLbasWritten => u64::MAX,
            SmartAttribute::TotalLbasRead => u64::MAX,
            SmartAttribute::UdmaCrcErrors => 50,
        }
    }

    /// Get weight for ML scoring (0.0-1.0)
    pub fn ml_weight(&self) -> f32 {
        match self {
            SmartAttribute::ReallocatedSectors => 1.0, // Most critical
            SmartAttribute::CurrentPendingSectors => 0.9,
            SmartAttribute::ReportedUncorrectable => 0.85,
            SmartAttribute::OfflineUncorrectable => 0.8,
            SmartAttribute::EndToEndErrors => 0.75,
            SmartAttribute::SpinRetry => 0.7,
            SmartAttribute::CommandTimeout => 0.6,
            SmartAttribute::UdmaCrcErrors => 0.5,
            SmartAttribute::Temperature => 0.3,
            SmartAttribute::PowerOnHours => 0.2,
            SmartAttribute::TotalLbasWritten => 0.1,
            SmartAttribute::TotalLbasRead => 0.1,
        }
    }
}

/// Disk health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DiskHealth {
    /// Disk is healthy
    Healthy,
    /// Warning - degradation detected
    Warning,
    /// Critical - failure imminent
    Critical,
    /// Failed - disk unusable
    Failed,
}

impl DiskHealth {
    /// Get health name
    pub fn name(&self) -> &'static str {
        match self {
            DiskHealth::Healthy => "Healthy",
            DiskHealth::Warning => "Warning",
            DiskHealth::Critical => "Critical",
            DiskHealth::Failed => "Failed",
        }
    }

    /// Get color code for display
    pub fn color(&self) -> &'static str {
        match self {
            DiskHealth::Healthy => "Green",
            DiskHealth::Warning => "Yellow",
            DiskHealth::Critical => "Orange",
            DiskHealth::Failed => "Red",
        }
    }
}

/// S.M.A.R.T. attribute value with history
#[derive(Debug, Clone)]
pub struct AttributeValue {
    /// Current value
    pub current: u64,
    /// Worst value seen
    pub worst: u64,
    /// Threshold
    pub threshold: u64,
    /// Historical values (for trend analysis)
    pub history: Vec<u64>,
    /// Average value
    pub average: f64,
    /// Variance
    pub variance: f64,
    /// Sample count
    pub sample_count: u64,
}

impl AttributeValue {
    /// Create new attribute value
    pub fn new(value: u64, threshold: u64) -> Self {
        Self {
            current: value,
            worst: value,
            threshold,
            history: Vec::new(),
            average: value as f64,
            variance: 0.0,
            sample_count: 1,
        }
    }

    /// Update value using Welford's online algorithm
    pub fn update(&mut self, value: u64) {
        self.current = value;
        self.worst = self.worst.max(value);

        // Add to history (keep last 100 samples)
        self.history.push(value);
        if self.history.len() > 100 {
            self.history.remove(0);
        }

        // Update running statistics
        self.sample_count += 1;
        let n = self.sample_count as f64;
        let delta = value as f64 - self.average;
        self.average += delta / n;
        let delta2 = value as f64 - self.average;
        self.variance += delta * delta2;
    }

    /// Get standard deviation
    pub fn stddev(&self) -> f64 {
        if self.sample_count < 2 {
            return 0.0;
        }
        libm::sqrt(self.variance / (self.sample_count - 1) as f64)
    }

    /// Calculate trend (slope of linear regression)
    pub fn trend(&self) -> f32 {
        if self.history.len() < 2 {
            return 0.0;
        }

        let n = self.history.len() as f32;
        let mut sum_x = 0.0;
        let mut sum_y = 0.0;
        let mut sum_xy = 0.0;
        let mut sum_x2 = 0.0;

        for (i, &value) in self.history.iter().enumerate() {
            let x = i as f32;
            let y = value as f32;
            sum_x += x;
            sum_y += y;
            sum_xy += x * y;
            sum_x2 += x * x;
        }

        // Slope = (n*sum_xy - sum_x*sum_y) / (n*sum_x2 - sum_x^2)
        let numerator = n * sum_xy - sum_x * sum_y;
        let denominator = n * sum_x2 - sum_x * sum_x;

        if denominator.abs() < 0.001 {
            return 0.0;
        }

        numerator / denominator
    }

    /// Check if value is anomalous (>3 sigma)
    pub fn is_anomalous(&self) -> bool {
        if self.sample_count < 10 {
            return false;
        }

        let sigma = self.stddev();
        let z_score = (self.current as f64 - self.average).abs() / sigma.max(1.0);
        z_score > 3.0
    }

    /// Check if value exceeds threshold
    pub fn exceeds_threshold(&self) -> bool {
        self.current > self.threshold
    }
}

/// S.M.A.R.T. data for a disk
#[derive(Debug, Clone)]
pub struct SmartData {
    /// Disk ID
    pub disk_id: u64,
    /// S.M.A.R.T. attributes
    pub attributes: BTreeMap<SmartAttribute, AttributeValue>,
    /// Last update timestamp
    pub last_update: u64,
    /// Health status
    pub health: DiskHealth,
    /// ML failure probability (0.0-1.0)
    pub failure_probability: f32,
}

impl SmartData {
    /// Create new S.M.A.R.T. data
    pub fn new(disk_id: u64) -> Self {
        Self {
            disk_id,
            attributes: BTreeMap::new(),
            last_update: 0,
            health: DiskHealth::Healthy,
            failure_probability: 0.0,
        }
    }

    /// Update attribute
    pub fn update_attribute(&mut self, attr: SmartAttribute, value: u64, timestamp: u64) {
        self.last_update = timestamp;

        let threshold = attr.critical_threshold();

        if let Some(attr_value) = self.attributes.get_mut(&attr) {
            attr_value.update(value);
        } else {
            self.attributes
                .insert(attr, AttributeValue::new(value, threshold));
        }

        // Recalculate health
        self.update_health();
    }

    /// Update health status using ML
    fn update_health(&mut self) {
        let mut score = 0.0f32;
        let mut total_weight = 0.0f32;

        for (attr, value) in &self.attributes {
            let weight = attr.ml_weight();
            total_weight += weight;

            // Factor 1: Threshold violation
            if value.exceeds_threshold() {
                score += weight * 1.0;
            }

            // Factor 2: Anomaly detection
            if value.is_anomalous() {
                score += weight * 0.5;
            }

            // Factor 3: Negative trend (worsening)
            let trend = value.trend();
            if trend > 0.1 {
                score += weight * 0.3;
            }
        }

        // Normalize score
        if total_weight > 0.0 {
            self.failure_probability = score / total_weight;
        } else {
            self.failure_probability = 0.0;
        }

        // Determine health status
        self.health = if self.failure_probability >= 0.8 {
            DiskHealth::Failed
        } else if self.failure_probability >= 0.5 {
            DiskHealth::Critical
        } else if self.failure_probability >= 0.2 {
            DiskHealth::Warning
        } else {
            DiskHealth::Healthy
        };
    }

    /// Get critical attributes
    pub fn critical_attributes(&self) -> Vec<SmartAttribute> {
        self.attributes
            .iter()
            .filter(|(_, v)| v.exceeds_threshold())
            .map(|(k, _)| *k)
            .collect()
    }

    /// Get anomalous attributes
    pub fn anomalous_attributes(&self) -> Vec<SmartAttribute> {
        self.attributes
            .iter()
            .filter(|(_, v)| v.is_anomalous())
            .map(|(k, _)| *k)
            .collect()
    }
}

/// S.M.A.R.T. monitoring statistics
#[derive(Debug, Clone, Default)]
pub struct SmartStats {
    /// Total disks monitored
    pub total_disks: u64,
    /// Healthy disks
    pub healthy_disks: u64,
    /// Warning disks
    pub warning_disks: u64,
    /// Critical disks
    pub critical_disks: u64,
    /// Failed disks
    pub failed_disks: u64,
    /// Predicted failures
    pub predicted_failures: u64,
}

lazy_static! {
    /// Global S.M.A.R.T. monitor
    static ref SMART_MONITOR: Mutex<SmartMonitor> = Mutex::new(SmartMonitor::new());
}

/// S.M.A.R.T. monitor
pub struct SmartMonitor {
    /// Disk data
    disks: BTreeMap<u64, SmartData>,
    /// Statistics
    stats: SmartStats,
    /// Monitoring interval (ms)
    monitor_interval: u64,
    /// Last monitoring time
    last_monitor: u64,
}

impl Default for SmartMonitor {
    fn default() -> Self {
        Self::new()
    }
}

impl SmartMonitor {
    /// Create new S.M.A.R.T. monitor
    pub fn new() -> Self {
        Self {
            disks: BTreeMap::new(),
            stats: SmartStats::default(),
            monitor_interval: 60_000, // 1 minute
            last_monitor: 0,
        }
    }

    /// Register disk for monitoring
    pub fn register_disk(&mut self, disk_id: u64) {
        let data = SmartData::new(disk_id);
        self.disks.insert(disk_id, data);
        self.stats.total_disks += 1;
        self.stats.healthy_disks += 1;

        crate::lcpfs_println!("[ SMART ] Registered disk {} for monitoring", disk_id);
    }

    /// Update disk attribute
    pub fn update_attribute(
        &mut self,
        disk_id: u64,
        attr: SmartAttribute,
        value: u64,
        timestamp: u64,
    ) -> Result<(), &'static str> {
        let data = self.disks.get_mut(&disk_id).ok_or("Disk not found")?;

        let old_health = data.health;
        data.update_attribute(attr, value, timestamp);
        let new_health = data.health;
        let failure_prob = data.failure_probability;

        // Update statistics if health changed
        if old_health != new_health {
            self.update_health_stats(old_health, new_health);

            crate::lcpfs_println!(
                "[ SMART ] Disk {} health changed: {} -> {} (failure prob: {:.1}%)",
                disk_id,
                old_health.name(),
                new_health.name(),
                failure_prob * 100.0
            );

            if new_health == DiskHealth::Critical || new_health == DiskHealth::Failed {
                self.stats.predicted_failures += 1;
            }
        }

        Ok(())
    }

    /// Update health statistics
    fn update_health_stats(&mut self, old: DiskHealth, new: DiskHealth) {
        // Decrement old count
        match old {
            DiskHealth::Healthy => self.stats.healthy_disks -= 1,
            DiskHealth::Warning => self.stats.warning_disks -= 1,
            DiskHealth::Critical => self.stats.critical_disks -= 1,
            DiskHealth::Failed => self.stats.failed_disks -= 1,
        }

        // Increment new count
        match new {
            DiskHealth::Healthy => self.stats.healthy_disks += 1,
            DiskHealth::Warning => self.stats.warning_disks += 1,
            DiskHealth::Critical => self.stats.critical_disks += 1,
            DiskHealth::Failed => self.stats.failed_disks += 1,
        }
    }

    /// Get disk health
    pub fn get_health(&self, disk_id: u64) -> Option<DiskHealth> {
        self.disks.get(&disk_id).map(|d| d.health)
    }

    /// Get disks by health status
    pub fn disks_by_health(&self, health: DiskHealth) -> Vec<u64> {
        self.disks
            .iter()
            .filter(|(_, d)| d.health == health)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Run monitoring cycle
    pub fn monitor(&mut self, current_time: u64) {
        if current_time < self.last_monitor + self.monitor_interval {
            return;
        }

        self.last_monitor = current_time;

        // Check for stale data (no update in 10 minutes)
        for (disk_id, data) in &self.disks {
            if current_time > data.last_update + 600_000 {
                crate::lcpfs_println!(
                    "[ SMART ] WARNING: No S.M.A.R.T. update for disk {} in 10 minutes",
                    disk_id
                );
            }
        }
    }

    /// Get statistics
    pub fn stats(&self) -> SmartStats {
        self.stats.clone()
    }

    /// Get disk data
    pub fn get_disk_data(&self, disk_id: u64) -> Option<SmartData> {
        self.disks.get(&disk_id).cloned()
    }
}

/// Global S.M.A.R.T. operations
pub struct Smart;

impl Smart {
    /// Register disk
    pub fn register_disk(disk_id: u64) {
        let mut mon = SMART_MONITOR.lock();
        mon.register_disk(disk_id);
    }

    /// Update attribute
    pub fn update_attribute(
        disk_id: u64,
        attr: SmartAttribute,
        value: u64,
        timestamp: u64,
    ) -> Result<(), &'static str> {
        let mut mon = SMART_MONITOR.lock();
        mon.update_attribute(disk_id, attr, value, timestamp)
    }

    /// Get health
    pub fn get_health(disk_id: u64) -> Option<DiskHealth> {
        let mon = SMART_MONITOR.lock();
        mon.get_health(disk_id)
    }

    /// Monitor
    pub fn monitor(current_time: u64) {
        let mut mon = SMART_MONITOR.lock();
        mon.monitor(current_time);
    }

    /// Get statistics
    pub fn stats() -> SmartStats {
        let mon = SMART_MONITOR.lock();
        mon.stats()
    }

    /// Get SMART data for a specific disk
    pub fn get_disk_data(disk_id: u64) -> Option<SmartData> {
        let mon = SMART_MONITOR.lock();
        mon.get_disk_data(disk_id)
    }
}

/// Get SMART data for a disk (convenience function)
pub fn get_smart_data(disk_id: u64) -> Option<SmartData> {
    Smart::get_disk_data(disk_id)
}

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

    #[test]
    fn test_attribute_thresholds() {
        assert_eq!(SmartAttribute::ReallocatedSectors.critical_threshold(), 10);
        assert!(SmartAttribute::Temperature.critical_threshold() < 100);
    }

    #[test]
    fn test_attribute_weights() {
        let weight1 = SmartAttribute::ReallocatedSectors.ml_weight();
        let weight2 = SmartAttribute::Temperature.ml_weight();

        assert!(weight1 > weight2); // Reallocated sectors more critical
    }

    #[test]
    fn test_attribute_value_update() {
        let mut attr = AttributeValue::new(0, 10);

        attr.update(5);
        assert_eq!(attr.current, 5);
        assert_eq!(attr.worst, 5);

        attr.update(2);
        assert_eq!(attr.current, 2);
        assert_eq!(attr.worst, 5); // Worst stays at 5
    }

    #[test]
    fn test_welford_statistics() {
        let mut attr = AttributeValue::new(10, 100);

        for &val in &[12, 11, 13, 9, 10] {
            attr.update(val);
        }

        // Mean should be around 11
        assert!((attr.average - 11.0).abs() < 1.0);

        let stddev = attr.stddev();
        assert!(stddev > 0.0 && stddev < 3.0);
    }

    #[test]
    fn test_trend_detection() {
        let mut attr = AttributeValue::new(0, 100);

        // Increasing trend
        for i in 1..=10 {
            attr.update(i * 5);
        }

        let trend = attr.trend();
        assert!(trend > 0.0); // Positive trend
    }

    #[test]
    fn test_anomaly_detection() {
        let mut attr = AttributeValue::new(10, 100);

        // Establish baseline
        for _ in 0..20 {
            attr.update(10);
        }

        // Normal value
        assert!(!attr.is_anomalous());

        // Anomalous value (far from mean)
        attr.update(100);
        assert!(attr.is_anomalous());
    }

    #[test]
    fn test_threshold_checking() {
        let attr = AttributeValue::new(15, 10);
        assert!(attr.exceeds_threshold());

        let attr2 = AttributeValue::new(5, 10);
        assert!(!attr2.exceeds_threshold());
    }

    #[test]
    fn test_smart_data_creation() {
        let data = SmartData::new(1);

        assert_eq!(data.disk_id, 1);
        assert_eq!(data.health, DiskHealth::Healthy);
        assert_eq!(data.failure_probability, 0.0);
    }

    #[test]
    fn test_health_status_update() {
        let mut data = SmartData::new(1);

        // Add critical attribute exceeding threshold
        data.update_attribute(SmartAttribute::ReallocatedSectors, 20, 1000);

        assert_ne!(data.health, DiskHealth::Healthy);
        assert!(data.failure_probability > 0.0);
    }

    #[test]
    fn test_critical_attributes() {
        let mut data = SmartData::new(1);

        data.update_attribute(SmartAttribute::ReallocatedSectors, 15, 1000);
        data.update_attribute(SmartAttribute::Temperature, 45, 1000);

        let critical = data.critical_attributes();
        assert_eq!(critical.len(), 1);
        assert_eq!(critical[0], SmartAttribute::ReallocatedSectors);
    }

    #[test]
    fn test_monitor_registration() {
        let mut mon = SmartMonitor::new();

        mon.register_disk(1);
        mon.register_disk(2);

        assert_eq!(mon.stats.total_disks, 2);
        assert_eq!(mon.stats.healthy_disks, 2);
    }

    #[test]
    fn test_health_state_transitions() {
        let mut mon = SmartMonitor::new();
        mon.register_disk(1);

        // Update to warning state
        mon.update_attribute(1, SmartAttribute::ReallocatedSectors, 8, 1000)
            .expect("test: operation should succeed");

        let stats = mon.stats();
        // Health may be Warning or still Healthy depending on ML scoring
        assert!(stats.healthy_disks + stats.warning_disks == 1);
    }

    #[test]
    fn test_disks_by_health() {
        let mut mon = SmartMonitor::new();

        mon.register_disk(1);
        mon.register_disk(2);

        let healthy = mon.disks_by_health(DiskHealth::Healthy);
        assert_eq!(healthy.len(), 2);
    }

    #[test]
    fn test_failure_prediction() {
        let mut mon = SmartMonitor::new();
        mon.register_disk(1);

        // Simulate disk degradation
        mon.update_attribute(1, SmartAttribute::ReallocatedSectors, 15, 1000)
            .expect("test: operation should succeed");
        mon.update_attribute(1, SmartAttribute::CurrentPendingSectors, 10, 2000)
            .expect("test: operation should succeed");

        let data = mon
            .get_disk_data(1)
            .expect("test: operation should succeed");
        assert!(data.failure_probability > 0.3);

        if data.health == DiskHealth::Critical || data.health == DiskHealth::Failed {
            assert!(mon.stats.predicted_failures > 0);
        }
    }

    #[test]
    fn test_monitoring_interval() {
        let mut mon = SmartMonitor::new();
        mon.register_disk(1);

        mon.update_attribute(1, SmartAttribute::Temperature, 40, 1000)
            .expect("test: operation should succeed");

        // First monitor call
        mon.monitor(1000);

        // Too soon - should skip
        mon.monitor(2000);

        // After interval - should run
        mon.monitor(70_000);
    }
}