1use crate::classes::profile_generic::ProfileGeneric;
11use crate::obis::ObisCode;
12use crate::types::CosemDataType;
13
14use super::obis;
15use super::profile::reference_profile;
16
17#[derive(Clone, Debug, Default)]
19pub struct CorrectionRecord {
20 pub object_obis: Vec<u8>,
22 pub time: Vec<u8>,
24}
25
26#[derive(Clone, Debug, Default)]
28pub struct CorrectionJournal {
29 records: Vec<CorrectionRecord>,
30}
31
32impl CorrectionJournal {
33 pub fn new() -> Self {
35 Self::default()
36 }
37
38 pub fn record(&mut self, record: CorrectionRecord) {
40 self.records.push(record);
41 }
42
43 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#[derive(Clone, Debug)]
63pub struct NumericRecord {
64 pub meter_id: Vec<u8>,
66 pub journal_obis: Vec<u8>,
68 pub reading_obis: Vec<u8>,
70 pub attribute: u8,
72 pub meter_time: Vec<u8>,
74 pub value: CosemDataType,
76 pub ivke_time: Vec<u8>,
78}
79
80#[derive(Clone, Debug, Default)]
82pub struct NumericJournal {
83 records: Vec<NumericRecord>,
84}
85
86impl NumericJournal {
87 pub fn new() -> Self {
89 Self::default()
90 }
91
92 pub fn record(&mut self, record: NumericRecord) {
94 self.records.push(record);
95 }
96
97 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
127pub mod transmission_status {
129 pub const WAITING: u8 = 0;
131 pub const SENT_UNCONFIRMED: u8 = 1;
133 pub const SENT_CONFIRMED: u8 = 2;
135 pub const DISABLED: u8 = 3;
137}
138
139#[derive(Clone, Debug, Default)]
141pub struct IncomingEvent {
142 pub meter_id: Vec<u8>,
144 pub meter_model: Vec<u8>,
146 pub ivke_time: Vec<u8>,
148 pub meter_time: Vec<u8>,
150 pub journal_field: u8,
152 pub code: u16,
154 pub status: u8,
156}
157
158#[derive(Clone, Debug, Default)]
160pub struct IncomingEventsTable {
161 rows: Vec<IncomingEvent>,
162}
163
164impl IncomingEventsTable {
165 pub fn new() -> Self {
167 Self::default()
168 }
169
170 pub fn record(&mut self, event: IncomingEvent) {
172 self.rows.push(event);
173 }
174
175 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}