use crate::obis::ObisCode;
use crate::types::CosemDataType;
use super::obis;
use super::profile::reference_profile;
#[derive(Clone, Debug, Default)]
pub struct Channel {
pub channel_id: u8,
pub interface: Vec<u8>,
}
#[derive(Clone, Debug, Default)]
pub struct ChannelList {
channels: Vec<Channel>,
}
impl ChannelList {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, channel: Channel) {
self.channels.push(channel);
}
pub fn len(&self) -> usize {
self.channels.len()
}
pub fn is_empty(&self) -> bool {
self.channels.is_empty()
}
pub fn build(&self) -> crate::classes::profile_generic::ProfileGeneric {
let buffer = self
.channels
.iter()
.map(|c| {
CosemDataType::Structure(vec![
CosemDataType::Unsigned(c.channel_id),
CosemDataType::OctetString(c.interface.clone()),
])
})
.collect();
let columns = [ObisCode::new(0, 0, 94, 7, 130, 1), ObisCode::new(0, 0, 94, 7, 130, 2)];
reference_profile(obis::channel_list(), &columns, buffer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interface::InterfaceClass;
#[test]
fn channel_list_builds_profile() {
let mut list = ChannelList::new();
list.add(Channel { channel_id: 1, interface: b"RS485_1:9600".to_vec() });
assert_eq!(list.len(), 1);
let profile = list.build();
assert_eq!(profile.class_id(), 7);
assert_eq!(profile.version(), 1);
assert_eq!(profile.logical_name(), &obis::channel_list());
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[0], CosemDataType::Unsigned(1));
assert_eq!(cols[1], CosemDataType::OctetString(b"RS485_1:9600".to_vec()));
let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
assert_eq!(caps.len(), 2);
}
}