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 MeterStatus {
pub meter_uid: Vec<u8>,
pub last_success: Vec<u8>,
pub last_attempt: Vec<u8>,
pub relay_state: u8,
pub relay_mode: u8,
pub power_limit: u8,
pub last_meter_time: Vec<u8>,
pub relay_state_time: Vec<u8>,
pub relay_mode_time: Vec<u8>,
pub power_limit_time: Vec<u8>,
pub current_time_fix: Vec<u8>,
pub load_profile_period: u16,
}
impl MeterStatus {
fn to_row(&self) -> CosemDataType {
CosemDataType::Structure(vec![
CosemDataType::OctetString(self.meter_uid.clone()),
CosemDataType::DateTime(self.last_success.clone()),
CosemDataType::DateTime(self.last_attempt.clone()),
CosemDataType::Unsigned(self.relay_state),
CosemDataType::Unsigned(self.relay_mode),
CosemDataType::Unsigned(self.power_limit),
CosemDataType::DateTime(self.last_meter_time.clone()),
CosemDataType::DateTime(self.relay_state_time.clone()),
CosemDataType::DateTime(self.relay_mode_time.clone()),
CosemDataType::DateTime(self.power_limit_time.clone()),
CosemDataType::DateTime(self.current_time_fix.clone()),
CosemDataType::LongUnsigned(self.load_profile_period),
])
}
}
#[derive(Clone, Debug, Default)]
pub struct MeterStatusTable {
rows: Vec<MeterStatus>,
}
impl MeterStatusTable {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, status: MeterStatus) {
self.rows.push(status);
}
pub fn build(&self) -> ProfileGeneric {
let buffer = self.rows.iter().map(MeterStatus::to_row).collect();
let columns = [
ObisCode::new(0, 0, 94, 7, 128, 10),
ObisCode::new(0, 0, 94, 7, 134, 1),
ObisCode::new(0, 0, 94, 7, 134, 2),
ObisCode::new(0, 0, 94, 7, 134, 3),
ObisCode::new(0, 0, 94, 7, 134, 4),
ObisCode::new(0, 0, 94, 7, 134, 5),
ObisCode::new(0, 0, 94, 7, 134, 6),
ObisCode::new(0, 0, 94, 7, 134, 7),
ObisCode::new(0, 0, 94, 7, 134, 8),
ObisCode::new(0, 0, 94, 7, 134, 9),
ObisCode::new(0, 0, 94, 7, 134, 10),
ObisCode::new(0, 0, 94, 7, 134, 11),
];
reference_profile(obis::meter_status_table(), &columns, buffer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interface::InterfaceClass;
#[test]
fn meter_status_table_builds_profile() {
let mut table = MeterStatusTable::new();
table.add(MeterStatus {
meter_uid: b"SIT12260004".to_vec(),
relay_state: 1,
power_limit: 15,
load_profile_period: 900,
..Default::default()
});
let profile = table.build();
assert_eq!(profile.class_id(), 7);
assert_eq!(profile.logical_name(), &obis::meter_status_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(), 12);
assert_eq!(cols[3], CosemDataType::Unsigned(1));
assert_eq!(cols[11], CosemDataType::LongUnsigned(900));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 12);
}
}