1use crate::classes::profile_generic::ProfileGeneric;
9use crate::obis::ObisCode;
10use crate::types::CosemDataType;
11
12use super::obis;
13use super::profile::reference_profile;
14
15#[derive(Clone, Debug, Default)]
17pub struct MeterStatus {
18 pub meter_uid: Vec<u8>,
20 pub last_success: Vec<u8>,
22 pub last_attempt: Vec<u8>,
24 pub relay_state: u8,
26 pub relay_mode: u8,
28 pub power_limit: u8,
30 pub last_meter_time: Vec<u8>,
32 pub relay_state_time: Vec<u8>,
34 pub relay_mode_time: Vec<u8>,
36 pub power_limit_time: Vec<u8>,
38 pub current_time_fix: Vec<u8>,
40 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#[derive(Clone, Debug, Default)]
65pub struct MeterStatusTable {
66 rows: Vec<MeterStatus>,
67}
68
69impl MeterStatusTable {
70 pub fn new() -> Self {
72 Self::default()
73 }
74
75 pub fn add(&mut self, status: MeterStatus) {
77 self.rows.push(status);
78 }
79
80 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}