Skip to main content

spodes_rs/spodus/
channels.rs

1//! ИВКЭ channel list (СТО 34.01-5.1-013-2023, §10.4, `0.0.94.7.130.255`).
2//!
3//! Reference information about the ИВКЭ's communication channels to meters,
4//! exposed as a `ProfileGeneric` (IC 7, v1) with the Table-5 columns:
5//! `channel_id` and the interface name (e.g. `RS485_1:9600`).
6
7use crate::obis::ObisCode;
8use crate::types::CosemDataType;
9
10use super::obis;
11use super::profile::reference_profile;
12
13/// One channel of the ИВКЭ (§10.4, Table 5).
14#[derive(Clone, Debug, Default)]
15pub struct Channel {
16    /// Channel identifier (`channel_id`).
17    pub channel_id: u8,
18    /// Interface name (e.g. `RS485_1:9600`).
19    pub interface: Vec<u8>,
20}
21
22/// The ИВКЭ channel list (§10.4).
23#[derive(Clone, Debug, Default)]
24pub struct ChannelList {
25    channels: Vec<Channel>,
26}
27
28impl ChannelList {
29    /// Creates an empty channel list.
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Adds a channel.
35    pub fn add(&mut self, channel: Channel) {
36        self.channels.push(channel);
37    }
38
39    /// Number of channels.
40    pub fn len(&self) -> usize {
41        self.channels.len()
42    }
43
44    /// Whether the list is empty.
45    pub fn is_empty(&self) -> bool {
46        self.channels.is_empty()
47    }
48
49    /// Builds the COSEM `ProfileGeneric` (IC 7, v1) object (§10.4).
50    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}