Skip to main content

spodes_rs/spodus/
records.rs

1//! ИВКЭ record journals (СТО 34.01-5.1-013-2023, §10.10 and §10.11).
2//!
3//! * The **object-correction journal** (§10.10, `0.0.94.7.136.255`) records the
4//!   last correction time of the ИВКЭ configuration objects.
5//! * The **numeric meter journal** (§10.11, `0.0.94.7.137.255`) holds meter
6//!   readings disaggregated one value per row.
7//!
8//! Both are `ProfileGeneric` (IC 7, v1) objects.
9
10use crate::classes::profile_generic::ProfileGeneric;
11use crate::obis::ObisCode;
12use crate::types::CosemDataType;
13
14use super::obis;
15use super::profile::reference_profile;
16
17/// One object-correction record (§10.10, Table 9).
18#[derive(Clone, Debug, Default)]
19pub struct CorrectionRecord {
20    /// OBIS code of the corrected object.
21    pub object_obis: Vec<u8>,
22    /// Last correction time (date-time octets).
23    pub time: Vec<u8>,
24}
25
26/// The object-correction journal (§10.10).
27#[derive(Clone, Debug, Default)]
28pub struct CorrectionJournal {
29    records: Vec<CorrectionRecord>,
30}
31
32impl CorrectionJournal {
33    /// Creates an empty journal.
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Records a correction.
39    pub fn record(&mut self, record: CorrectionRecord) {
40        self.records.push(record);
41    }
42
43    /// Builds the COSEM `ProfileGeneric` (IC 7, v1) object (§10.10).
44    pub fn build(&self) -> ProfileGeneric {
45        let buffer = self
46            .records
47            .iter()
48            .map(|r| {
49                CosemDataType::Structure(vec![
50                    CosemDataType::OctetString(r.object_obis.clone()),
51                    CosemDataType::DateTime(r.time.clone()),
52                ])
53            })
54            .collect();
55        let columns = [ObisCode::new(0, 0, 94, 7, 136, 0), ObisCode::new(0, 0, 94, 7, 136, 1)];
56        reference_profile(obis::object_correction_journal(), &columns, buffer)
57    }
58}
59
60/// One numeric-journal record (§10.11, Table 10). A single disaggregated meter
61/// reading with the metadata needed to relate it to its source journal row.
62#[derive(Clone, Debug)]
63pub struct NumericRecord {
64    /// Meter identifier.
65    pub meter_id: Vec<u8>,
66    /// OBIS of the source journal in the meter.
67    pub journal_obis: Vec<u8>,
68    /// OBIS of the reading.
69    pub reading_obis: Vec<u8>,
70    /// Attribute number.
71    pub attribute: u8,
72    /// Fix time in the meter (date-time octets).
73    pub meter_time: Vec<u8>,
74    /// The reading value (CHOICE — any type).
75    pub value: CosemDataType,
76    /// Fix time in the ИВКЭ (date-time octets).
77    pub ivke_time: Vec<u8>,
78}
79
80/// The numeric meter journal (§10.11).
81#[derive(Clone, Debug, Default)]
82pub struct NumericJournal {
83    records: Vec<NumericRecord>,
84}
85
86impl NumericJournal {
87    /// Creates an empty journal.
88    pub fn new() -> Self {
89        Self::default()
90    }
91
92    /// Records a disaggregated meter reading.
93    pub fn record(&mut self, record: NumericRecord) {
94        self.records.push(record);
95    }
96
97    /// Builds the COSEM `ProfileGeneric` (IC 7, v1) object (§10.11).
98    pub fn build(&self) -> ProfileGeneric {
99        let buffer = self
100            .records
101            .iter()
102            .map(|r| {
103                CosemDataType::Structure(vec![
104                    CosemDataType::OctetString(r.meter_id.clone()),
105                    CosemDataType::OctetString(r.journal_obis.clone()),
106                    CosemDataType::OctetString(r.reading_obis.clone()),
107                    CosemDataType::Unsigned(r.attribute),
108                    CosemDataType::DateTime(r.meter_time.clone()),
109                    r.value.clone(),
110                    CosemDataType::DateTime(r.ivke_time.clone()),
111                ])
112            })
113            .collect();
114        let columns = [
115            ObisCode::new(0, 0, 94, 7, 128, 10),
116            ObisCode::new(0, 0, 94, 7, 137, 1),
117            ObisCode::new(0, 0, 94, 7, 137, 2),
118            ObisCode::new(0, 0, 94, 7, 137, 3),
119            ObisCode::new(0, 0, 94, 7, 137, 4),
120            ObisCode::new(0, 0, 94, 7, 137, 5),
121            ObisCode::new(0, 0, 94, 7, 137, 6),
122        ];
123        reference_profile(obis::numeric_meter_journal(), &columns, buffer)
124    }
125}
126
127/// `transmission status` values of an incoming-event row (§8.5.10, Table 4).
128pub mod transmission_status {
129    /// Waiting to be sent to the head-end.
130    pub const WAITING: u8 = 0;
131    /// Sent but not confirmed.
132    pub const SENT_UNCONFIRMED: u8 = 1;
133    /// Sent and confirmed.
134    pub const SENT_CONFIRMED: u8 = 2;
135    /// Transmission disabled.
136    pub const DISABLED: u8 = 3;
137}
138
139/// One incoming push-event row (§8.5.10, Table 4).
140#[derive(Clone, Debug, Default)]
141pub struct IncomingEvent {
142    /// Meter identifier.
143    pub meter_id: Vec<u8>,
144    /// Meter model.
145    pub meter_model: Vec<u8>,
146    /// Fix time in the ИВКЭ (date-time octets).
147    pub ivke_time: Vec<u8>,
148    /// Event time in the meter (date-time octets).
149    pub meter_time: Vec<u8>,
150    /// Field number within the meter's event-journal OBIS.
151    pub journal_field: u8,
152    /// Event code.
153    pub code: u16,
154    /// Transmission status (see [`transmission_status`]).
155    pub status: u8,
156}
157
158/// The incoming push-events table (§8.5.10, `0.0.94.7.140.255`).
159#[derive(Clone, Debug, Default)]
160pub struct IncomingEventsTable {
161    rows: Vec<IncomingEvent>,
162}
163
164impl IncomingEventsTable {
165    /// Creates an empty table.
166    pub fn new() -> Self {
167        Self::default()
168    }
169
170    /// Records an incoming event.
171    pub fn record(&mut self, event: IncomingEvent) {
172        self.rows.push(event);
173    }
174
175    /// Builds the COSEM `ProfileGeneric` (IC 7, v1) object (§8.5.10).
176    pub fn build(&self) -> ProfileGeneric {
177        let buffer = self
178            .rows
179            .iter()
180            .map(|e| {
181                CosemDataType::Structure(vec![
182                    CosemDataType::OctetString(e.meter_id.clone()),
183                    CosemDataType::OctetString(e.meter_model.clone()),
184                    CosemDataType::DateTime(e.ivke_time.clone()),
185                    CosemDataType::DateTime(e.meter_time.clone()),
186                    CosemDataType::Unsigned(e.journal_field),
187                    CosemDataType::LongUnsigned(e.code),
188                    CosemDataType::Unsigned(e.status),
189                ])
190            })
191            .collect();
192        let columns = [
193            ObisCode::new(0, 0, 94, 7, 128, 10),
194            ObisCode::new(0, 0, 94, 7, 140, 2),
195            ObisCode::new(0, 0, 94, 7, 140, 3),
196            ObisCode::new(0, 0, 94, 7, 140, 4),
197            ObisCode::new(0, 0, 94, 7, 140, 5),
198            ObisCode::new(0, 0, 94, 7, 140, 6),
199            ObisCode::new(0, 0, 94, 7, 140, 7),
200        ];
201        reference_profile(obis::incoming_events_table(), &columns, buffer)
202    }
203}
204
205#[cfg(test)]
206mod tests {
207    use super::*;
208    use crate::interface::InterfaceClass;
209
210    #[test]
211    fn correction_journal_builds_profile() {
212        let mut journal = CorrectionJournal::new();
213        journal.record(CorrectionRecord {
214            object_obis: obis::meter_list().to_bytes(),
215            time: vec![0x07, 0xE6, 0x07, 0x04],
216        });
217        let profile = journal.build();
218        assert_eq!(profile.logical_name(), &obis::object_correction_journal());
219        let attrs = profile.attributes();
220        let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
221        let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
222        assert_eq!(cols.len(), 2);
223        let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
224        assert_eq!(caps.len(), 2);
225    }
226
227    #[test]
228    fn numeric_journal_builds_profile() {
229        let mut journal = NumericJournal::new();
230        journal.record(NumericRecord {
231            meter_id: b"SIT12260004".to_vec(),
232            journal_obis: vec![1, 0, 98, 2, 0, 255],
233            reading_obis: vec![1, 0, 1, 8, 1, 255],
234            attribute: 2,
235            meter_time: vec![0x07, 0xE6, 0x07, 0x04],
236            value: CosemDataType::DoubleLongUnsigned(123_456),
237            ivke_time: vec![0x07, 0xE6, 0x07, 0x04],
238        });
239        let profile = journal.build();
240        assert_eq!(profile.logical_name(), &obis::numeric_meter_journal());
241        let attrs = profile.attributes();
242        let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
243        let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
244        assert_eq!(cols.len(), 7);
245        assert_eq!(cols[5], CosemDataType::DoubleLongUnsigned(123_456));
246        let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
247        assert_eq!(caps.len(), 7);
248    }
249
250    #[test]
251    fn incoming_events_table_builds_profile() {
252        let mut table = IncomingEventsTable::new();
253        table.record(IncomingEvent {
254            meter_id: b"SIT12260004".to_vec(),
255            code: 0x1C,
256            status: transmission_status::WAITING,
257            ..Default::default()
258        });
259        let profile = table.build();
260        assert_eq!(profile.logical_name(), &obis::incoming_events_table());
261        let attrs = profile.attributes();
262        let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
263        let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
264        assert_eq!(cols.len(), 7);
265        assert_eq!(cols[5], CosemDataType::LongUnsigned(0x1C));
266        let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
267        assert_eq!(caps.len(), 7);
268    }
269}