Skip to main content

smgw/management/
window.rs

1use super::{Record, invalid};
2use std::io;
3
4/// Three consecutive quarter-hour slots identified by their first UTC second.
5#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
6pub struct ProfileWindow(i64);
7
8impl ProfileWindow {
9    /// Construct a representable, quarter-hour-aligned window.
10    pub fn new(first: i64) -> io::Result<Self> {
11        // COSEM concrete years are 0 through 65534. The final two slots
12        // must also fit before 65535-01-01 (the wildcard year).
13        const FIRST: i64 = -62_167_219_200;
14        const LAST: i64 = 2_005_917_606_900;
15        if first % 900 != 0 || !(FIRST..=LAST).contains(&first) {
16            return Err(invalid("expected three representable quarter-hour slots"));
17        }
18        Ok(Self(first))
19    }
20
21    /// First scheduled UTC second.
22    pub fn first(self) -> i64 {
23        self.0
24    }
25
26    /// Check slot membership, capture order and unique measurement instants.
27    pub fn validate(self, records: &[Record]) -> io::Result<()> {
28        if records.len() > 3
29            || records.iter().any(|r| {
30                !(self.0..=self.0 + 1800).contains(&r.reference_utc) || r.reference_utc % 900 != 0
31            })
32            || records
33                .windows(2)
34                .any(|pair| pair[0].reference_utc >= pair[1].reference_utc)
35            || records.iter().enumerate().any(|(i, r)| {
36                records[..i]
37                    .iter()
38                    .any(|p| p.measured_utc == r.measured_utc)
39            })
40        {
41            return Err(invalid("invalid or duplicate fixture profile slot"));
42        }
43        Ok(())
44    }
45}