spodes_rs/spodus/
channels.rs1use crate::obis::ObisCode;
8use crate::types::CosemDataType;
9
10use super::obis;
11use super::profile::reference_profile;
12
13#[derive(Clone, Debug, Default)]
15pub struct Channel {
16 pub channel_id: u8,
18 pub interface: Vec<u8>,
20}
21
22#[derive(Clone, Debug, Default)]
24pub struct ChannelList {
25 channels: Vec<Channel>,
26}
27
28impl ChannelList {
29 pub fn new() -> Self {
31 Self::default()
32 }
33
34 pub fn add(&mut self, channel: Channel) {
36 self.channels.push(channel);
37 }
38
39 pub fn len(&self) -> usize {
41 self.channels.len()
42 }
43
44 pub fn is_empty(&self) -> bool {
46 self.channels.is_empty()
47 }
48
49 pub fn build(&self) -> crate::classes::profile_generic::ProfileGeneric {
51 let buffer = self
52 .channels
53 .iter()
54 .map(|c| {
55 CosemDataType::Structure(vec![
56 CosemDataType::Unsigned(c.channel_id),
57 CosemDataType::OctetString(c.interface.clone()),
58 ])
59 })
60 .collect();
61 let columns = [ObisCode::new(0, 0, 94, 7, 130, 1), ObisCode::new(0, 0, 94, 7, 130, 2)];
62 reference_profile(obis::channel_list(), &columns, buffer)
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69 use crate::interface::InterfaceClass;
70
71 #[test]
72 fn channel_list_builds_profile() {
73 let mut list = ChannelList::new();
74 list.add(Channel { channel_id: 1, interface: b"RS485_1:9600".to_vec() });
75 assert_eq!(list.len(), 1);
76
77 let profile = list.build();
78 assert_eq!(profile.class_id(), 7);
79 assert_eq!(profile.version(), 1);
80 assert_eq!(profile.logical_name(), &obis::channel_list());
81
82 let attrs = profile.attributes();
83 let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
84 let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
85 assert_eq!(cols[0], CosemDataType::Unsigned(1));
86 assert_eq!(cols[1], CosemDataType::OctetString(b"RS485_1:9600".to_vec()));
87 let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
88 assert_eq!(caps.len(), 2);
89 }
90}