use std::sync::Arc;
use crate::classes::data::Data;
use crate::classes::profile_generic::{ProfileGeneric, ProfileGenericConfig};
use crate::interface::InterfaceClass;
use crate::obis::ObisCode;
use crate::types::attrs::SortMethod;
use crate::types::CosemDataType;
use super::obis;
pub mod exchange_status {
pub const DONE: u8 = 0;
pub const WAITING: u8 = 1;
pub const ACCESS_DENIED: u8 = 2;
pub const NOT_SUPPORTED: u8 = 3;
pub const PARTIAL: u8 = 4;
pub const NOT_FOUND: u8 = 5;
pub const NO_RESPONSE: u8 = 6;
pub const BAD_LINK: u8 = 7;
}
fn column(code: ObisCode) -> (Arc<dyn InterfaceClass + Send + Sync>, u8) {
(Arc::new(Data::new(code, CosemDataType::Null)), 2u8)
}
#[derive(Clone, Debug, Default)]
pub struct ExchangeRecord {
pub task_id: u32,
pub meter_uid: Vec<u8>,
pub start: Vec<u8>,
pub status: u8,
pub end: Vec<u8>,
pub attempts: u8,
}
impl ExchangeRecord {
fn to_entry(&self) -> CosemDataType {
#[allow(clippy::cast_possible_truncation)]
let task_id = self.task_id as u16;
CosemDataType::Structure(vec![
CosemDataType::LongUnsigned(task_id),
CosemDataType::OctetString(self.meter_uid.clone()),
CosemDataType::DateTime(self.start.clone()),
CosemDataType::Unsigned(self.status),
CosemDataType::DateTime(self.end.clone()),
CosemDataType::Unsigned(self.attempts),
])
}
}
#[derive(Clone, Debug, Default)]
pub struct ExchangeStatusJournal {
records: Vec<ExchangeRecord>,
}
impl ExchangeStatusJournal {
pub fn new() -> Self {
Self::default()
}
pub fn append(&mut self, record: ExchangeRecord) {
self.records.push(record);
}
fn columns() -> Vec<(Arc<dyn InterfaceClass + Send + Sync>, u8)> {
vec![
column(ObisCode::new(0, 0, 94, 7, 135, 1)), column(ObisCode::new(0, 0, 94, 7, 128, 10)), column(ObisCode::new(0, 0, 94, 7, 135, 2)), column(ObisCode::new(0, 0, 94, 7, 135, 3)), column(ObisCode::new(0, 0, 94, 7, 135, 4)), column(ObisCode::new(0, 0, 94, 7, 135, 5)), ]
}
pub fn build(&self) -> ProfileGeneric {
let buffer: Vec<CosemDataType> = self.records.iter().map(ExchangeRecord::to_entry).collect();
#[allow(clippy::cast_possible_truncation)]
let entries_in_use = buffer.len() as u32;
ProfileGeneric::new(ProfileGenericConfig {
logical_name: obis::exchange_status_journal(),
version: 1,
buffer,
capture_objects: Self::columns(),
capture_period: 0,
sort_method: SortMethod::Fifo,
sort_object: None,
entries_in_use,
profile_entries: 0,
})
}
}
#[derive(Clone, Debug)]
pub struct EventJournal {
logical_name: ObisCode,
entries: Vec<(Vec<u8>, u16)>,
}
impl EventJournal {
pub fn new(logical_name: ObisCode) -> Self {
EventJournal { logical_name, entries: Vec::new() }
}
pub fn log(&mut self, timestamp: Vec<u8>, code: u16) {
self.entries.push((timestamp, code));
}
pub fn build(&self) -> ProfileGeneric {
let buffer: Vec<CosemDataType> = self
.entries
.iter()
.map(|(ts, code)| {
CosemDataType::Structure(vec![CosemDataType::DateTime(ts.clone()), CosemDataType::LongUnsigned(*code)])
})
.collect();
#[allow(clippy::cast_possible_truncation)]
let entries_in_use = buffer.len() as u32;
let columns = vec![
column(ObisCode::new(0, 0, 1, 0, 0, 255)), column(self.logical_name.clone()), ];
ProfileGeneric::new(ProfileGenericConfig {
logical_name: self.logical_name.clone(),
version: 1,
buffer,
capture_objects: columns,
capture_period: 0,
sort_method: SortMethod::Fifo,
sort_object: None,
entries_in_use,
profile_entries: 0,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exchange_status_journal_builds_profile() {
let mut journal = ExchangeStatusJournal::new();
journal.append(ExchangeRecord {
task_id: 7,
meter_uid: b"SIT12260004".to_vec(),
start: vec![0x07, 0xE6, 0x07, 0x04],
status: exchange_status::DONE,
end: vec![0x07, 0xE6, 0x07, 0x04],
attempts: 1,
});
let profile = journal.build();
assert_eq!(profile.class_id(), 7);
assert_eq!(profile.logical_name(), &obis::exchange_status_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(), 6);
assert_eq!(cols[0], CosemDataType::LongUnsigned(7));
assert_eq!(cols[3], CosemDataType::Unsigned(exchange_status::DONE));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 6);
}
#[test]
fn event_journal_logs_events() {
let mut journal = EventJournal::new(obis::access_control_log());
journal.log(vec![0x07, 0xE6, 0x07, 0x04], 0x0011);
let profile = journal.build();
assert_eq!(profile.logical_name(), &obis::access_control_log());
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);
assert_eq!(cols[1], CosemDataType::LongUnsigned(0x0011));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 2);
}
}