Skip to main content

smgw_lmn/
lib.rs

1#![no_std]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4
5pub mod dlms;
6#[cfg(feature = "mbus")]
7pub mod mbus;
8#[cfg(feature = "sml")]
9pub mod sml;
10
11#[cfg(any(feature = "loopback-client", feature = "loopback-meter"))]
12pub mod simulator;
13
14/// Counts for one fully committed import.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub struct Counts {
17    pub inserted: usize,
18    pub duplicates: usize,
19}
20
21/// The slot a pushed reading fills, timed by the gateway's trusted clock.
22///
23/// Push meters have no calendar clock. The host assigns the first reading it
24/// receives at or after a registration instant to that instant's slot.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub struct Slot {
27    /// Registration instant on the destination's grid.
28    pub reference_utc: i64,
29    /// Gateway reception time of the telegram.
30    pub received_utc: i64,
31}
32
33impl Slot {
34    /// `true` when `reference ≤ received < reference + period`.
35    pub fn contains_reception(self, period_seconds: i64) -> bool {
36        let offset = i128::from(self.received_utc) - i128::from(self.reference_utc);
37        (0..i128::from(period_seconds)).contains(&offset)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::Slot;
44
45    #[test]
46    fn a_slot_owns_receptions_from_its_instant_until_the_next() {
47        let slot = |received_utc| Slot {
48            reference_utc: 900,
49            received_utc,
50        };
51        assert!(!slot(899).contains_reception(900));
52        assert!(slot(900).contains_reception(900));
53        assert!(slot(1799).contains_reception(900));
54        assert!(!slot(1800).contains_reception(900));
55        let extreme = Slot {
56            reference_utc: i64::MAX,
57            received_utc: i64::MIN,
58        };
59        assert!(!extreme.contains_reception(900));
60    }
61}