1use crate::seq::{Ctx, Element, Seq, sealed};
37use crate::tl::{Reader, Writer};
38use crate::value::{Quantity, SmlValue, Time, Unit};
39use crate::{Error, NameList, Obis, Result, ServerId};
40
41pub type ProfileColumns<'a> = Seq<'a, ProfileColumn>;
43pub type ProfilePeriods<'a> = Seq<'a, ProfilePeriod<'a>>;
45pub type ProfileValues<'a> = Seq<'a, ProfileValue<'a>>;
47
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub struct GetProfilePackResponse<'a> {
51 pub server_id: ServerId<'a>,
53 pub act_time: Time,
55 pub reg_period: u32,
57 pub tree_path: NameList<'a>,
59 pub columns: ProfileColumns<'a>,
61 pub periods: ProfilePeriods<'a>,
63 pub rawdata: Option<&'a [u8]>,
65 pub profile_signature: Option<&'a [u8]>,
67}
68
69impl<'a> GetProfilePackResponse<'a> {
70 pub fn quantity(&self, period: &ProfilePeriod<'_>, obis: Obis) -> Option<Quantity> {
75 let index = self.columns.iter().position(|c| c.is_ok_and(|c| c.obis == obis))?;
76 let column = self.columns.iter().nth(index)?.ok()?;
77 let value = period.values.iter().nth(index)?.ok()?;
78 let mantissa = match value.value {
79 SmlValue::Int(v) => i128::from(v),
80 SmlValue::Uint(v) => i128::from(v),
81 SmlValue::Bool(_) | SmlValue::Bytes(_) | SmlValue::Time(_) => return None,
82 };
83 Some(Quantity { mantissa, exponent: column.scaler, unit: Some(column.unit) })
84 }
85
86 pub(crate) fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
90 r.list(8)?;
91 let response = Self {
92 server_id: ServerId::new(r.octet_string()?),
93 act_time: Time::read(r, ctx.options)?,
94 reg_period: u32::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?,
95 tree_path: Seq::read(r, ctx)?,
96 columns: Seq::read(r, ctx)?,
97 periods: Seq::read(r, ctx)?,
98 rawdata: r.opt_octet_string()?,
99 profile_signature: r.opt_octet_string()?,
100 };
101 for period in &response.periods {
102 let found = period?.values.len();
103 if found != response.columns.len() {
104 return Err(Error::ListLength { expected: response.columns.len(), found });
105 }
106 }
107 Ok(response)
108 }
109
110 pub(crate) fn write(&self, w: &mut Writer<'_>) -> Result<()> {
111 w.list(8)?;
112 w.octet_string(self.server_id.as_bytes())?;
113 self.act_time.write(w)?;
114 w.unsigned(u64::from(self.reg_period), 4)?;
115 self.tree_path.write(w)?;
116 self.columns.write(w)?;
117 self.periods.write(w)?;
118 w.opt_octet_string(self.rawdata)?;
119 w.opt_octet_string(self.profile_signature)
120 }
121}
122
123#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
125pub struct ProfileColumn {
126 pub obis: Obis,
128 pub unit: Unit,
130 pub scaler: i8,
132}
133
134#[derive(Clone, Copy, Debug, PartialEq, Eq)]
136pub struct ProfilePeriod<'a> {
137 pub val_time: Time,
139 pub status: u64,
141 pub values: ProfileValues<'a>,
143 pub period_signature: Option<&'a [u8]>,
145}
146
147#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
149pub struct ProfileValue<'a> {
150 pub value: SmlValue<'a>,
152 pub value_signature: Option<&'a [u8]>,
154}
155
156impl sealed::Sealed for ProfileColumn {}
157impl sealed::Sealed for ProfilePeriod<'_> {}
158impl sealed::Sealed for ProfileValue<'_> {}
159
160impl<'a> Element<'a> for ProfileColumn {
161 fn read(r: &mut Reader<'a>, _: Ctx) -> Result<Self> {
162 r.list(3)?;
163 let obis = Obis::from_bytes(r.octet_string()?)?;
164 let unit = Unit(u8::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?);
165 let scaler = i8::try_from(r.integer()?).map_err(|_| Error::ValueOutOfRange)?;
166 Ok(Self { obis, unit, scaler })
167 }
168
169 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
170 w.list(3)?;
171 w.octet_string(&self.obis.0)?;
172 w.unsigned(u64::from(self.unit.0), 1)?;
173 w.integer(i64::from(self.scaler), 1)
174 }
175}
176
177impl<'a> Element<'a> for ProfilePeriod<'a> {
178 fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
179 r.list(4)?;
180 Ok(Self {
181 val_time: Time::read(r, ctx.options)?,
182 status: r.unsigned()?,
183 values: Seq::read(r, ctx)?,
184 period_signature: r.opt_octet_string()?,
185 })
186 }
187
188 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
189 w.list(4)?;
190 self.val_time.write(w)?;
191 w.unsigned(self.status, 8)?;
192 self.values.write(w)?;
193 w.opt_octet_string(self.period_signature)
194 }
195}
196
197impl<'a> Element<'a> for ProfileValue<'a> {
198 fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
199 r.list(2)?;
200 let value = SmlValue::read(r, ctx.options)?.0;
201 Ok(Self { value, value_signature: r.opt_octet_string()? })
202 }
203
204 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
205 w.list(2)?;
206 self.value.write(w)?;
207 w.opt_octet_string(self.value_signature)
208 }
209}