crdtosphere 0.1.0

Universal embedded CRDTs for distributed coordination across automotive, robotics, IoT, and industrial applications
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
//! Sensor Network for IoT Systems
//!
//! This module implements CRDTs for distributed IoT sensor data coordination,
//! enabling aggregation and synchronization of sensor readings across networks.

use crate::clock::CompactTimestamp;
use crate::error::{CRDTError, CRDTResult};
use crate::memory::{MemoryConfig, NodeId};
use crate::traits::{BoundedCRDT, CRDT, RealTimeCRDT};

/// IoT sensor types
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum SensorType {
    /// Temperature sensor
    Temperature = 1,
    /// Humidity sensor
    Humidity = 2,
    /// Pressure sensor
    Pressure = 3,
    /// Light/illuminance sensor
    Light = 4,
    /// Motion/PIR sensor
    Motion = 5,
    /// Air quality sensor
    AirQuality = 6,
    /// Sound/noise level sensor
    Sound = 7,
    /// Proximity sensor
    Proximity = 8,
    /// Accelerometer
    Accelerometer = 9,
    /// GPS/location sensor
    GPS = 10,
    /// Generic analog sensor
    Analog = 11,
    /// Generic digital sensor
    Digital = 12,
}

impl SensorType {
    /// Returns true if this sensor type provides continuous readings
    pub fn is_continuous(&self) -> bool {
        matches!(
            self,
            SensorType::Temperature
                | SensorType::Humidity
                | SensorType::Pressure
                | SensorType::Light
                | SensorType::AirQuality
                | SensorType::Sound
                | SensorType::Analog
        )
    }

    /// Returns true if this sensor type provides event-based readings
    pub fn is_event_based(&self) -> bool {
        matches!(
            self,
            SensorType::Motion | SensorType::Proximity | SensorType::Digital
        )
    }

    /// Returns typical update interval in milliseconds
    pub fn typical_interval_ms(&self) -> u32 {
        match self {
            SensorType::Temperature | SensorType::Humidity => 30000, // 30 seconds
            SensorType::Pressure => 60000,                           // 1 minute
            SensorType::Light => 10000,                              // 10 seconds
            SensorType::Motion => 1000,                              // 1 second (when active)
            SensorType::AirQuality => 60000,                         // 1 minute
            SensorType::Sound => 5000,                               // 5 seconds
            SensorType::Proximity => 500,                            // 500ms
            SensorType::Accelerometer => 100,                        // 100ms
            SensorType::GPS => 30000,                                // 30 seconds
            SensorType::Analog => 5000,                              // 5 seconds
            SensorType::Digital => 1000,                             // 1 second
        }
    }
}

/// Reading quality indicators
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum ReadingQuality {
    /// Poor quality reading (high noise, low confidence)
    Poor = 1,
    /// Fair quality reading
    Fair = 2,
    /// Good quality reading
    Good = 3,
    /// Excellent quality reading (low noise, high confidence)
    Excellent = 4,
}

impl ReadingQuality {
    /// Returns confidence weight for this quality level
    pub fn confidence_weight(&self) -> f32 {
        match self {
            ReadingQuality::Poor => 0.25,
            ReadingQuality::Fair => 0.5,
            ReadingQuality::Good => 0.75,
            ReadingQuality::Excellent => 1.0,
        }
    }

    /// Returns true if this quality is acceptable for critical decisions
    pub fn is_acceptable(&self) -> bool {
        matches!(self, ReadingQuality::Good | ReadingQuality::Excellent)
    }
}

/// Individual sensor reading
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SensorReading {
    /// Sensor that produced this reading
    pub sensor_id: NodeId,
    /// Type of sensor
    pub sensor_type: SensorType,
    /// Reading value (scaled and encoded as i32)
    pub value: i32,
    /// Reading quality
    pub quality: ReadingQuality,
    /// Timestamp when reading was taken
    pub timestamp: CompactTimestamp,
    /// Location/zone identifier (optional)
    pub location_id: u16,
    /// Battery level of sensor (0-255)
    pub battery_level: u8,
    /// Signal strength when reading was transmitted (0-255)
    pub signal_strength: u8,
}

impl SensorReading {
    /// Creates a new sensor reading
    pub fn new(
        sensor_id: NodeId,
        sensor_type: SensorType,
        value: i32,
        quality: ReadingQuality,
        timestamp: u64,
        location_id: u16,
    ) -> Self {
        Self {
            sensor_id,
            sensor_type,
            value,
            quality,
            timestamp: CompactTimestamp::new(timestamp),
            location_id,
            battery_level: 255,   // Unknown/AC powered
            signal_strength: 255, // Unknown/wired
        }
    }

    /// Creates a reading with battery and signal info
    pub fn with_vitals(
        sensor_id: NodeId,
        sensor_type: SensorType,
        value: i32,
        quality: ReadingQuality,
        timestamp: u64,
        location_id: u16,
        battery_level: u8,
        signal_strength: u8,
    ) -> Self {
        Self {
            sensor_id,
            sensor_type,
            value,
            quality,
            timestamp: CompactTimestamp::new(timestamp),
            location_id,
            battery_level,
            signal_strength,
        }
    }

    /// Returns true if this reading should override another
    pub fn should_override(&self, other: &SensorReading) -> bool {
        // Same sensor - newer timestamp wins
        if self.sensor_id == other.sensor_id {
            return self.timestamp > other.timestamp;
        }

        // Different sensors - higher quality wins, then newer timestamp
        if self.quality > other.quality {
            return true;
        }

        if self.quality == other.quality {
            return self.timestamp > other.timestamp;
        }

        false
    }

    /// Returns true if reading is stale
    pub fn is_stale(&self, current_time: u64, max_age_ms: u64) -> bool {
        current_time > self.timestamp.as_u64() + max_age_ms
    }

    /// Returns weighted value based on quality
    pub fn weighted_value(&self) -> f32 {
        self.value as f32 * self.quality.confidence_weight()
    }
}

/// IoT Sensor Network CRDT
///
/// This CRDT manages distributed sensor data coordination across IoT networks,
/// enabling aggregation and synchronization of sensor readings.
///
/// # Type Parameters
/// - `C`: Memory configuration
///
/// # Features
/// - Multi-sensor data aggregation
/// - Quality-based reading prioritization
/// - Location-based sensor grouping
/// - Automatic stale data cleanup
///
/// # Example
/// ```rust
/// use crdtosphere::prelude::*;
/// use crdtosphere::iot::{SensorNetwork, SensorReading, SensorType, ReadingQuality};
///
/// // Create sensor network
/// let mut network = SensorNetwork::<DefaultConfig>::new(1); // Gateway ID 1
///
/// // Add temperature reading
/// network.add_reading(
///     42,                        // sensor ID
///     SensorType::Temperature,
///     2350,                      // 23.5°C (scaled by 100)
///     ReadingQuality::Good,
///     1000,                      // timestamp
///     1                          // location ID
/// )?;
///
/// // Get latest temperature readings
/// let temp_readings = network.readings_by_type(SensorType::Temperature);
/// # Ok::<(), crdtosphere::error::CRDTError>(())
/// ```
#[derive(Debug, Clone)]
pub struct SensorNetwork<C: MemoryConfig> {
    /// Array of sensor readings
    readings: [Option<SensorReading>; 128], // Support up to 128 readings
    /// Number of readings currently stored
    reading_count: usize,
    /// This gateway's ID
    local_gateway_id: NodeId,
    /// Last update timestamp
    last_update: CompactTimestamp,
    /// Phantom data for memory config
    _phantom: core::marker::PhantomData<C>,
}

impl<C: MemoryConfig> SensorNetwork<C> {
    /// Creates a new sensor network
    ///
    /// # Arguments
    /// * `gateway_id` - The ID of this gateway/controller
    ///
    /// # Returns
    /// A new sensor network CRDT
    pub fn new(gateway_id: NodeId) -> Self {
        Self {
            readings: [const { None }; 128],
            reading_count: 0,
            local_gateway_id: gateway_id,
            last_update: CompactTimestamp::new(0),
            _phantom: core::marker::PhantomData,
        }
    }

    /// Adds a sensor reading
    ///
    /// # Arguments
    /// * `sensor_id` - Sensor identifier
    /// * `sensor_type` - Type of sensor
    /// * `value` - Reading value
    /// * `quality` - Reading quality
    /// * `timestamp` - Reading timestamp
    /// * `location_id` - Location/zone identifier
    ///
    /// # Returns
    /// Ok(()) if successful, error otherwise
    pub fn add_reading(
        &mut self,
        sensor_id: NodeId,
        sensor_type: SensorType,
        value: i32,
        quality: ReadingQuality,
        timestamp: u64,
        location_id: u16,
    ) -> CRDTResult<()> {
        let reading = SensorReading::new(
            sensor_id,
            sensor_type,
            value,
            quality,
            timestamp,
            location_id,
        );
        self.add_sensor_reading(reading)?;
        self.last_update = CompactTimestamp::new(timestamp);
        Ok(())
    }

    /// Adds a sensor reading with battery and signal info
    ///
    /// # Arguments
    /// * `sensor_id` - Sensor identifier
    /// * `sensor_type` - Type of sensor
    /// * `value` - Reading value
    /// * `quality` - Reading quality
    /// * `timestamp` - Reading timestamp
    /// * `location_id` - Location/zone identifier
    /// * `battery_level` - Battery level (0-255)
    /// * `signal_strength` - Signal strength (0-255)
    ///
    /// # Returns
    /// Ok(()) if successful, error otherwise
    pub fn add_reading_with_vitals(
        &mut self,
        sensor_id: NodeId,
        sensor_type: SensorType,
        value: i32,
        quality: ReadingQuality,
        timestamp: u64,
        location_id: u16,
        battery_level: u8,
        signal_strength: u8,
    ) -> CRDTResult<()> {
        let reading = SensorReading::with_vitals(
            sensor_id,
            sensor_type,
            value,
            quality,
            timestamp,
            location_id,
            battery_level,
            signal_strength,
        );
        self.add_sensor_reading(reading)?;
        self.last_update = CompactTimestamp::new(timestamp);
        Ok(())
    }

    /// Gets all sensor readings
    ///
    /// # Returns
    /// Iterator over sensor readings
    pub fn all_readings(&self) -> impl Iterator<Item = &SensorReading> {
        self.readings.iter().filter_map(|r| r.as_ref())
    }

    /// Gets readings by sensor type
    ///
    /// # Arguments
    /// * `sensor_type` - Type of sensor to filter by
    ///
    /// # Returns
    /// Iterator over readings of the specified type
    pub fn readings_by_type(
        &self,
        sensor_type: SensorType,
    ) -> impl Iterator<Item = &SensorReading> {
        self.all_readings()
            .filter(move |r| r.sensor_type == sensor_type)
    }

    /// Gets readings by location
    ///
    /// # Arguments
    /// * `location_id` - Location to filter by
    ///
    /// # Returns
    /// Iterator over readings from the specified location
    pub fn readings_by_location(&self, location_id: u16) -> impl Iterator<Item = &SensorReading> {
        self.all_readings()
            .filter(move |r| r.location_id == location_id)
    }

    /// Gets readings by sensor ID
    ///
    /// # Arguments
    /// * `sensor_id` - Sensor to filter by
    ///
    /// # Returns
    /// Iterator over readings from the specified sensor
    pub fn readings_by_sensor(&self, sensor_id: NodeId) -> impl Iterator<Item = &SensorReading> {
        self.all_readings()
            .filter(move |r| r.sensor_id == sensor_id)
    }

    /// Gets readings with acceptable quality
    ///
    /// # Returns
    /// Iterator over high-quality readings
    pub fn quality_readings(&self) -> impl Iterator<Item = &SensorReading> {
        self.all_readings().filter(|r| r.quality.is_acceptable())
    }

    /// Gets latest reading for a sensor type and location
    ///
    /// # Arguments
    /// * `sensor_type` - Type of sensor
    /// * `location_id` - Location identifier
    ///
    /// # Returns
    /// Latest reading if available
    pub fn latest_reading(
        &self,
        sensor_type: SensorType,
        location_id: u16,
    ) -> Option<&SensorReading> {
        self.readings_by_type(sensor_type)
            .filter(|r| r.location_id == location_id)
            .max_by_key(|r| r.timestamp.as_u64())
    }

    /// Calculates average value for a sensor type and location
    ///
    /// # Arguments
    /// * `sensor_type` - Type of sensor
    /// * `location_id` - Location identifier
    /// * `max_age_ms` - Maximum age of readings to include
    /// * `current_time` - Current timestamp
    ///
    /// # Returns
    /// Average value if readings are available
    pub fn average_value(
        &self,
        sensor_type: SensorType,
        location_id: u16,
        max_age_ms: u64,
        current_time: u64,
    ) -> Option<f32> {
        let mut sum = 0.0f32;
        let mut weight_sum = 0.0f32;
        let mut count = 0;

        for reading in self
            .readings_by_type(sensor_type)
            .filter(|r| r.location_id == location_id)
            .filter(|r| !r.is_stale(current_time, max_age_ms))
            .filter(|r| r.quality.is_acceptable())
        {
            sum += reading.weighted_value();
            weight_sum += reading.quality.confidence_weight();
            count += 1;
        }

        if count > 0 && weight_sum > 0.0 {
            Some(sum / weight_sum)
        } else {
            None
        }
    }

    /// Gets sensors with low battery
    ///
    /// # Arguments
    /// * `threshold` - Battery level threshold (0-255)
    ///
    /// # Returns
    /// Iterator over readings from sensors with low battery
    pub fn low_battery_sensors(&self, threshold: u8) -> impl Iterator<Item = &SensorReading> {
        self.all_readings()
            .filter(move |r| r.battery_level < threshold && r.battery_level < 255)
    }

    /// Gets sensors with weak signal
    ///
    /// # Arguments
    /// * `threshold` - Signal strength threshold (0-255)
    ///
    /// # Returns
    /// Iterator over readings from sensors with weak signal
    pub fn weak_signal_sensors(&self, threshold: u8) -> impl Iterator<Item = &SensorReading> {
        self.all_readings()
            .filter(move |r| r.signal_strength < threshold && r.signal_strength < 255)
    }

    /// Gets the number of readings
    ///
    /// # Returns
    /// Number of readings
    pub fn reading_count(&self) -> usize {
        self.reading_count
    }

    /// Removes stale readings
    ///
    /// # Arguments
    /// * `current_time` - Current timestamp
    /// * `max_age_ms` - Maximum age in milliseconds
    ///
    /// # Returns
    /// Number of readings removed
    pub fn cleanup_stale_readings(&mut self, current_time: u64, max_age_ms: u64) -> usize {
        let mut removed = 0;

        for i in 0..128 {
            if let Some(reading) = &self.readings[i] {
                if reading.is_stale(current_time, max_age_ms) {
                    self.readings[i] = None;
                    self.reading_count -= 1;
                    removed += 1;
                }
            }
        }

        // Compact the array
        self.compact_readings();

        removed
    }

    /// Adds a sensor reading to the network
    fn add_sensor_reading(&mut self, reading: SensorReading) -> CRDTResult<()> {
        // Check for existing reading from same sensor
        for i in 0..128 {
            if let Some(ref mut existing) = self.readings[i] {
                if existing.sensor_id == reading.sensor_id
                    && existing.sensor_type == reading.sensor_type
                {
                    // Update if new reading should override
                    if reading.should_override(existing) {
                        *existing = reading;
                    }
                    return Ok(());
                }
            } else {
                // Empty slot - add new reading
                self.readings[i] = Some(reading);
                self.reading_count += 1;
                return Ok(());
            }
        }

        // If no space, try to replace oldest poor quality reading
        self.make_space_for_reading(reading)
    }

    /// Makes space for a new reading by replacing old poor quality readings
    fn make_space_for_reading(&mut self, new_reading: SensorReading) -> CRDTResult<()> {
        // Find oldest poor quality reading to replace
        let mut oldest_idx = None;
        let mut oldest_time = u64::MAX;

        for (i, reading_opt) in self.readings.iter().enumerate() {
            if let Some(reading) = reading_opt {
                if reading.quality <= ReadingQuality::Fair
                    && reading.timestamp.as_u64() < oldest_time
                {
                    oldest_time = reading.timestamp.as_u64();
                    oldest_idx = Some(i);
                }
            }
        }

        if let Some(idx) = oldest_idx {
            self.readings[idx] = Some(new_reading);
            Ok(())
        } else {
            Err(CRDTError::BufferOverflow)
        }
    }

    /// Compacts the readings array
    fn compact_readings(&mut self) {
        let mut write_idx = 0;

        for read_idx in 0..128 {
            if let Some(reading) = self.readings[read_idx] {
                if write_idx != read_idx {
                    self.readings[write_idx] = Some(reading);
                    self.readings[read_idx] = None;
                }
                write_idx += 1;
            }
        }
    }

    /// Validates sensor network data
    ///
    /// # Returns
    /// Ok(()) if valid, error otherwise
    pub fn validate_network(&self) -> CRDTResult<()> {
        // Check sensor IDs are valid
        for reading in self.all_readings() {
            if reading.sensor_id as usize >= C::MAX_NODES {
                return Err(CRDTError::InvalidNodeId);
            }
        }

        Ok(())
    }
}

impl<C: MemoryConfig> CRDT<C> for SensorNetwork<C> {
    type Error = CRDTError;

    fn merge(&mut self, other: &Self) -> CRDTResult<()> {
        // Merge all readings from other
        for reading in other.all_readings() {
            self.add_sensor_reading(*reading)?;
        }

        // Update timestamp to latest
        if other.last_update > self.last_update {
            self.last_update = other.last_update;
        }

        Ok(())
    }

    fn eq(&self, other: &Self) -> bool {
        if self.reading_count != other.reading_count {
            return false;
        }

        // Check that all readings match
        for reading in self.all_readings() {
            let mut found = false;
            for other_reading in other.all_readings() {
                if reading.sensor_id == other_reading.sensor_id
                    && reading.timestamp == other_reading.timestamp
                    && reading == other_reading
                {
                    found = true;
                    break;
                }
            }
            if !found {
                return false;
            }
        }

        true
    }

    fn size_bytes(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn validate(&self) -> CRDTResult<()> {
        self.validate_network()
    }

    fn state_hash(&self) -> u32 {
        let mut hash = self.local_gateway_id as u32;
        for reading in self.all_readings() {
            hash ^= (reading.sensor_id as u32)
                ^ (reading.timestamp.as_u64() as u32)
                ^ (reading.value as u32);
        }
        hash ^= self.reading_count as u32;
        hash
    }

    fn can_merge(&self, _other: &Self) -> bool {
        // Can always merge sensor networks (space is made by removing old readings)
        true
    }
}

impl<C: MemoryConfig> BoundedCRDT<C> for SensorNetwork<C> {
    const MAX_SIZE_BYTES: usize = core::mem::size_of::<Self>();
    const MAX_ELEMENTS: usize = 128; // Maximum readings

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn element_count(&self) -> usize {
        self.reading_count
    }

    fn compact(&mut self) -> CRDTResult<usize> {
        self.compact_readings();
        Ok(0)
    }

    fn can_add_element(&self) -> bool {
        self.reading_count < Self::MAX_ELEMENTS
    }
}

impl<C: MemoryConfig> RealTimeCRDT<C> for SensorNetwork<C> {
    const MAX_MERGE_CYCLES: u32 = 300; // Bounded by number of readings
    const MAX_VALIDATE_CYCLES: u32 = 150;
    const MAX_SERIALIZE_CYCLES: u32 = 200;

    fn merge_bounded(&mut self, other: &Self) -> CRDTResult<()> {
        // Sensor network merge is bounded
        self.merge(other)
    }

    fn validate_bounded(&self) -> CRDTResult<()> {
        // Validation is bounded
        self.validate()
    }

    fn remaining_budget(&self) -> Option<u32> {
        // For IoT systems, we don't track budget
        None
    }

    fn set_budget(&mut self, _cycles: u32) {
        // For IoT systems, we don't limit budget
    }
}

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

    #[test]
    fn test_sensor_type_properties() {
        assert!(SensorType::Temperature.is_continuous());
        assert!(!SensorType::Temperature.is_event_based());

        assert!(SensorType::Motion.is_event_based());
        assert!(!SensorType::Motion.is_continuous());

        assert!(SensorType::Temperature.typical_interval_ms() > 1000);
    }

    #[test]
    fn test_reading_quality_properties() {
        assert!(
            ReadingQuality::Excellent.confidence_weight()
                > ReadingQuality::Poor.confidence_weight()
        );
        assert!(ReadingQuality::Good.is_acceptable());
        assert!(!ReadingQuality::Poor.is_acceptable());
    }

    #[test]
    fn test_sensor_reading_creation() {
        let reading = SensorReading::new(
            42,
            SensorType::Temperature,
            2350, // 23.5°C
            ReadingQuality::Good,
            1000,
            1,
        );

        assert_eq!(reading.sensor_id, 42);
        assert_eq!(reading.sensor_type, SensorType::Temperature);
        assert_eq!(reading.value, 2350);
        assert_eq!(reading.quality, ReadingQuality::Good);
        assert_eq!(reading.location_id, 1);
    }

    #[test]
    fn test_sensor_reading_override() {
        let reading1 = SensorReading::new(
            42,
            SensorType::Temperature,
            2300,
            ReadingQuality::Good,
            1000,
            1,
        );
        let reading2 = SensorReading::new(
            42,
            SensorType::Temperature,
            2350,
            ReadingQuality::Good,
            1001,
            1,
        );
        let reading3 = SensorReading::new(
            43,
            SensorType::Temperature,
            2400,
            ReadingQuality::Excellent,
            999,
            1,
        );

        assert!(reading2.should_override(&reading1)); // Same sensor, newer timestamp
        assert!(reading3.should_override(&reading1)); // Different sensor, higher quality
        assert!(!reading1.should_override(&reading2)); // Same sensor, older timestamp
    }

    #[test]
    fn test_sensor_network_creation() {
        let network = SensorNetwork::<DefaultConfig>::new(1);

        assert_eq!(network.reading_count(), 0);
        assert_eq!(network.local_gateway_id, 1);
    }

    #[test]
    fn test_sensor_reading_addition() {
        let mut network = SensorNetwork::<DefaultConfig>::new(1);

        // Add temperature reading
        network
            .add_reading(
                42,
                SensorType::Temperature,
                2350,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();

        assert_eq!(network.reading_count(), 1);

        // Add humidity reading
        network
            .add_reading_with_vitals(
                43,
                SensorType::Humidity,
                6500, // 65%
                ReadingQuality::Excellent,
                1001,
                1,
                80,  // Battery level
                200, // Signal strength
            )
            .unwrap();

        assert_eq!(network.reading_count(), 2);
    }

    #[test]
    fn test_sensor_network_queries() {
        let mut network = SensorNetwork::<DefaultConfig>::new(1);

        // Add multiple readings
        network
            .add_reading(
                42,
                SensorType::Temperature,
                2350,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();
        network
            .add_reading(
                43,
                SensorType::Temperature,
                2400,
                ReadingQuality::Excellent,
                1001,
                2,
            )
            .unwrap();
        network
            .add_reading(
                44,
                SensorType::Humidity,
                6500,
                ReadingQuality::Good,
                1002,
                1,
            )
            .unwrap();
        network
            .add_reading_with_vitals(
                45,
                SensorType::Motion,
                1,
                ReadingQuality::Fair,
                1003,
                1,
                20,
                100,
            )
            .unwrap();

        // Test queries
        assert_eq!(network.readings_by_type(SensorType::Temperature).count(), 2);
        assert_eq!(network.readings_by_location(1).count(), 3);
        assert_eq!(network.readings_by_sensor(42).count(), 1);
        assert_eq!(network.quality_readings().count(), 3); // Good and Excellent only

        // Test latest reading
        let latest_temp = network.latest_reading(SensorType::Temperature, 2).unwrap();
        assert_eq!(latest_temp.sensor_id, 43);

        // Test low battery sensors
        assert_eq!(network.low_battery_sensors(50).count(), 1); // Sensor 45 has 20% battery
    }

    #[test]
    fn test_sensor_network_average() {
        let mut network = SensorNetwork::<DefaultConfig>::new(1);

        // Add temperature readings from same location
        network
            .add_reading(
                42,
                SensorType::Temperature,
                2300,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();
        network
            .add_reading(
                43,
                SensorType::Temperature,
                2400,
                ReadingQuality::Excellent,
                1001,
                1,
            )
            .unwrap();
        network
            .add_reading(
                44,
                SensorType::Temperature,
                2350,
                ReadingQuality::Good,
                1002,
                1,
            )
            .unwrap();

        // Calculate average (should be weighted by quality)
        let avg = network
            .average_value(SensorType::Temperature, 1, 10000, 2000)
            .unwrap();

        // Expected calculation:
        // Reading 1: 2300 * 0.75 = 1725
        // Reading 2: 2400 * 1.0 = 2400
        // Reading 3: 2350 * 0.75 = 1762.5
        // Sum = 5887.5, Weight sum = 2.5
        // Average = 5887.5 / 2.5 = 2355
        assert!((avg - 2355.0).abs() < 1.0);
    }

    #[test]
    fn test_sensor_network_merge() {
        let mut network1 = SensorNetwork::<DefaultConfig>::new(1);
        let mut network2 = SensorNetwork::<DefaultConfig>::new(2);

        // Add different readings to each network
        network1
            .add_reading(
                42,
                SensorType::Temperature,
                2300,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();
        network2
            .add_reading(
                43,
                SensorType::Humidity,
                6500,
                ReadingQuality::Excellent,
                1001,
                1,
            )
            .unwrap();

        // Merge
        network1.merge(&network2).unwrap();

        // Should have both readings
        assert_eq!(network1.reading_count(), 2);
        assert_eq!(
            network1.readings_by_type(SensorType::Temperature).count(),
            1
        );
        assert_eq!(network1.readings_by_type(SensorType::Humidity).count(), 1);
    }

    #[test]
    fn test_stale_reading_cleanup() {
        let mut network = SensorNetwork::<DefaultConfig>::new(1);

        // Add reading
        network
            .add_reading(
                42,
                SensorType::Temperature,
                2300,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();
        assert_eq!(network.reading_count(), 1);

        // Clean up after timeout
        let removed = network.cleanup_stale_readings(10000, 5000); // 5 second timeout
        assert_eq!(removed, 1);
        assert_eq!(network.reading_count(), 0);
    }

    #[test]
    fn test_bounded_crdt_implementation() {
        let mut network = SensorNetwork::<DefaultConfig>::new(1);

        assert_eq!(network.element_count(), 0);
        assert!(network.can_add_element());

        network
            .add_reading(
                42,
                SensorType::Temperature,
                2300,
                ReadingQuality::Good,
                1000,
                1,
            )
            .unwrap();
        assert_eq!(network.element_count(), 1);
        assert!(network.memory_usage() > 0);
    }

    #[test]
    fn test_real_time_crdt_implementation() {
        let mut network1 = SensorNetwork::<DefaultConfig>::new(1);
        let network2 = SensorNetwork::<DefaultConfig>::new(2);

        assert!(network1.merge_bounded(&network2).is_ok());
        assert!(network1.validate_bounded().is_ok());
    }
}