use crate::classes::profile_generic::ProfileGeneric;
use crate::obis::ObisCode;
use crate::types::CosemDataType;
use super::obis;
use super::profile::reference_profile;
#[derive(Clone, Debug, Default)]
pub struct CorrectionRecord {
pub object_obis: Vec<u8>,
pub time: Vec<u8>,
}
#[derive(Clone, Debug, Default)]
pub struct CorrectionJournal {
records: Vec<CorrectionRecord>,
}
impl CorrectionJournal {
pub fn new() -> Self {
Self::default()
}
pub fn record(&mut self, record: CorrectionRecord) {
self.records.push(record);
}
pub fn build(&self) -> ProfileGeneric {
let buffer = self
.records
.iter()
.map(|r| {
CosemDataType::Structure(vec![
CosemDataType::OctetString(r.object_obis.clone()),
CosemDataType::DateTime(r.time.clone()),
])
})
.collect();
let columns = [ObisCode::new(0, 0, 94, 7, 136, 0), ObisCode::new(0, 0, 94, 7, 136, 1)];
reference_profile(obis::object_correction_journal(), &columns, buffer)
}
}
#[derive(Clone, Debug)]
pub struct NumericRecord {
pub meter_id: Vec<u8>,
pub journal_obis: Vec<u8>,
pub reading_obis: Vec<u8>,
pub attribute: u8,
pub meter_time: Vec<u8>,
pub value: CosemDataType,
pub ivke_time: Vec<u8>,
}
#[derive(Clone, Debug, Default)]
pub struct NumericJournal {
records: Vec<NumericRecord>,
}
impl NumericJournal {
pub fn new() -> Self {
Self::default()
}
pub fn record(&mut self, record: NumericRecord) {
self.records.push(record);
}
pub fn build(&self) -> ProfileGeneric {
let buffer = self
.records
.iter()
.map(|r| {
CosemDataType::Structure(vec![
CosemDataType::OctetString(r.meter_id.clone()),
CosemDataType::OctetString(r.journal_obis.clone()),
CosemDataType::OctetString(r.reading_obis.clone()),
CosemDataType::Unsigned(r.attribute),
CosemDataType::DateTime(r.meter_time.clone()),
r.value.clone(),
CosemDataType::DateTime(r.ivke_time.clone()),
])
})
.collect();
let columns = [
ObisCode::new(0, 0, 94, 7, 128, 10),
ObisCode::new(0, 0, 94, 7, 137, 1),
ObisCode::new(0, 0, 94, 7, 137, 2),
ObisCode::new(0, 0, 94, 7, 137, 3),
ObisCode::new(0, 0, 94, 7, 137, 4),
ObisCode::new(0, 0, 94, 7, 137, 5),
ObisCode::new(0, 0, 94, 7, 137, 6),
];
reference_profile(obis::numeric_meter_journal(), &columns, buffer)
}
}
pub mod transmission_status {
pub const WAITING: u8 = 0;
pub const SENT_UNCONFIRMED: u8 = 1;
pub const SENT_CONFIRMED: u8 = 2;
pub const DISABLED: u8 = 3;
}
#[derive(Clone, Debug, Default)]
pub struct IncomingEvent {
pub meter_id: Vec<u8>,
pub meter_model: Vec<u8>,
pub ivke_time: Vec<u8>,
pub meter_time: Vec<u8>,
pub journal_field: u8,
pub code: u16,
pub status: u8,
}
#[derive(Clone, Debug, Default)]
pub struct IncomingEventsTable {
rows: Vec<IncomingEvent>,
}
impl IncomingEventsTable {
pub fn new() -> Self {
Self::default()
}
pub fn record(&mut self, event: IncomingEvent) {
self.rows.push(event);
}
pub fn build(&self) -> ProfileGeneric {
let buffer = self
.rows
.iter()
.map(|e| {
CosemDataType::Structure(vec![
CosemDataType::OctetString(e.meter_id.clone()),
CosemDataType::OctetString(e.meter_model.clone()),
CosemDataType::DateTime(e.ivke_time.clone()),
CosemDataType::DateTime(e.meter_time.clone()),
CosemDataType::Unsigned(e.journal_field),
CosemDataType::LongUnsigned(e.code),
CosemDataType::Unsigned(e.status),
])
})
.collect();
let columns = [
ObisCode::new(0, 0, 94, 7, 128, 10),
ObisCode::new(0, 0, 94, 7, 140, 2),
ObisCode::new(0, 0, 94, 7, 140, 3),
ObisCode::new(0, 0, 94, 7, 140, 4),
ObisCode::new(0, 0, 94, 7, 140, 5),
ObisCode::new(0, 0, 94, 7, 140, 6),
ObisCode::new(0, 0, 94, 7, 140, 7),
];
reference_profile(obis::incoming_events_table(), &columns, buffer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interface::InterfaceClass;
#[test]
fn correction_journal_builds_profile() {
let mut journal = CorrectionJournal::new();
journal.record(CorrectionRecord {
object_obis: obis::meter_list().to_bytes(),
time: vec![0x07, 0xE6, 0x07, 0x04],
});
let profile = journal.build();
assert_eq!(profile.logical_name(), &obis::object_correction_journal());
let attrs = profile.attributes();
let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
assert_eq!(cols.len(), 2);
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 2);
}
#[test]
fn numeric_journal_builds_profile() {
let mut journal = NumericJournal::new();
journal.record(NumericRecord {
meter_id: b"SIT12260004".to_vec(),
journal_obis: vec![1, 0, 98, 2, 0, 255],
reading_obis: vec![1, 0, 1, 8, 1, 255],
attribute: 2,
meter_time: vec![0x07, 0xE6, 0x07, 0x04],
value: CosemDataType::DoubleLongUnsigned(123_456),
ivke_time: vec![0x07, 0xE6, 0x07, 0x04],
});
let profile = journal.build();
assert_eq!(profile.logical_name(), &obis::numeric_meter_journal());
let attrs = profile.attributes();
let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
assert_eq!(cols.len(), 7);
assert_eq!(cols[5], CosemDataType::DoubleLongUnsigned(123_456));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 7);
}
#[test]
fn incoming_events_table_builds_profile() {
let mut table = IncomingEventsTable::new();
table.record(IncomingEvent {
meter_id: b"SIT12260004".to_vec(),
code: 0x1C,
status: transmission_status::WAITING,
..Default::default()
});
let profile = table.build();
assert_eq!(profile.logical_name(), &obis::incoming_events_table());
let attrs = profile.attributes();
let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
assert_eq!(cols.len(), 7);
assert_eq!(cols[5], CosemDataType::LongUnsigned(0x1C));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 7);
}
}