Skip to main content

spodes_rs/spodus/
status.rs

1//! Meter status table (СТО 34.01-5.1-013-2023, §10.8, `0.0.94.7.134.255`).
2//!
3//! Per-meter status snapshot, exposed as a `ProfileGeneric` (IC 7, v1) with the
4//! Table-7 columns: meter uid, last successful/attempted session times, current
5//! relay state/mode and power limit, the last read meter time, the fix times of
6//! those values and the load-profile period.
7
8use crate::classes::profile_generic::ProfileGeneric;
9use crate::obis::ObisCode;
10use crate::types::CosemDataType;
11
12use super::obis;
13use super::profile::reference_profile;
14
15/// One meter's status row (§10.8, Table 7).
16#[derive(Clone, Debug, Default)]
17pub struct MeterStatus {
18    /// Unique meter identifier.
19    pub meter_uid: Vec<u8>,
20    /// Time of the last successful session (date-time octets).
21    pub last_success: Vec<u8>,
22    /// Time of the last session attempt (date-time octets).
23    pub last_attempt: Vec<u8>,
24    /// Current relay state.
25    pub relay_state: u8,
26    /// Current relay control mode.
27    pub relay_mode: u8,
28    /// Current power limit.
29    pub power_limit: u8,
30    /// Last read meter time (date-time octets).
31    pub last_meter_time: Vec<u8>,
32    /// Relay-state fix time (date-time octets).
33    pub relay_state_time: Vec<u8>,
34    /// Relay-mode fix time (date-time octets).
35    pub relay_mode_time: Vec<u8>,
36    /// Power-limit fix time (date-time octets).
37    pub power_limit_time: Vec<u8>,
38    /// Current-time fix time (date-time octets).
39    pub current_time_fix: Vec<u8>,
40    /// Load-profile period.
41    pub load_profile_period: u16,
42}
43
44impl MeterStatus {
45    fn to_row(&self) -> CosemDataType {
46        CosemDataType::Structure(vec![
47            CosemDataType::OctetString(self.meter_uid.clone()),
48            CosemDataType::DateTime(self.last_success.clone()),
49            CosemDataType::DateTime(self.last_attempt.clone()),
50            CosemDataType::Unsigned(self.relay_state),
51            CosemDataType::Unsigned(self.relay_mode),
52            CosemDataType::Unsigned(self.power_limit),
53            CosemDataType::DateTime(self.last_meter_time.clone()),
54            CosemDataType::DateTime(self.relay_state_time.clone()),
55            CosemDataType::DateTime(self.relay_mode_time.clone()),
56            CosemDataType::DateTime(self.power_limit_time.clone()),
57            CosemDataType::DateTime(self.current_time_fix.clone()),
58            CosemDataType::LongUnsigned(self.load_profile_period),
59        ])
60    }
61}
62
63/// The meter status table (§10.8).
64#[derive(Clone, Debug, Default)]
65pub struct MeterStatusTable {
66    rows: Vec<MeterStatus>,
67}
68
69impl MeterStatusTable {
70    /// Creates an empty table.
71    pub fn new() -> Self {
72        Self::default()
73    }
74
75    /// Adds a meter status row.
76    pub fn add(&mut self, status: MeterStatus) {
77        self.rows.push(status);
78    }
79
80    /// Builds the COSEM `ProfileGeneric` (IC 7, v1) object (§10.8).
81    pub fn build(&self) -> ProfileGeneric {
82        let buffer = self.rows.iter().map(MeterStatus::to_row).collect();
83        let columns = [
84            ObisCode::new(0, 0, 94, 7, 128, 10),
85            ObisCode::new(0, 0, 94, 7, 134, 1),
86            ObisCode::new(0, 0, 94, 7, 134, 2),
87            ObisCode::new(0, 0, 94, 7, 134, 3),
88            ObisCode::new(0, 0, 94, 7, 134, 4),
89            ObisCode::new(0, 0, 94, 7, 134, 5),
90            ObisCode::new(0, 0, 94, 7, 134, 6),
91            ObisCode::new(0, 0, 94, 7, 134, 7),
92            ObisCode::new(0, 0, 94, 7, 134, 8),
93            ObisCode::new(0, 0, 94, 7, 134, 9),
94            ObisCode::new(0, 0, 94, 7, 134, 10),
95            ObisCode::new(0, 0, 94, 7, 134, 11),
96        ];
97        reference_profile(obis::meter_status_table(), &columns, buffer)
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104    use crate::interface::InterfaceClass;
105
106    #[test]
107    fn meter_status_table_builds_profile() {
108        let mut table = MeterStatusTable::new();
109        table.add(MeterStatus {
110            meter_uid: b"SIT12260004".to_vec(),
111            relay_state: 1,
112            power_limit: 15,
113            load_profile_period: 900,
114            ..Default::default()
115        });
116        let profile = table.build();
117        assert_eq!(profile.class_id(), 7);
118        assert_eq!(profile.logical_name(), &obis::meter_status_table());
119
120        let attrs = profile.attributes();
121        let CosemDataType::Array(rows) = &attrs[1].1 else { panic!("buffer array") };
122        let CosemDataType::Structure(cols) = &rows[0] else { panic!("row structure") };
123        assert_eq!(cols.len(), 12);
124        assert_eq!(cols[3], CosemDataType::Unsigned(1));
125        assert_eq!(cols[11], CosemDataType::LongUnsigned(900));
126        let CosemDataType::Array(caps) = &attrs[2].1 else { panic!("capture array") };
127        assert_eq!(caps.len(), 12);
128    }
129}