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 = value.value.as_integer()?;
79 Some(Quantity { mantissa, exponent: column.scaler, unit: Some(column.unit) })
80 }
81
82 pub(crate) fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
86 r.list(8)?;
87 let response = Self {
88 server_id: ServerId::new(r.octet_string()?),
89 act_time: Time::read(r, ctx.options)?,
90 reg_period: u32::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?,
91 tree_path: Seq::read(r, ctx)?,
92 columns: Seq::read(r, ctx)?,
93 periods: Seq::read(r, ctx)?,
94 rawdata: r.opt_octet_string()?,
95 profile_signature: r.opt_octet_string()?,
96 };
97 for period in &response.periods {
98 let found = period?.values.len();
99 if found != response.columns.len() {
100 return Err(Error::ListLength { expected: response.columns.len(), found });
101 }
102 }
103 Ok(response)
104 }
105
106 pub(crate) fn write(&self, w: &mut Writer<'_>) -> Result<()> {
107 w.list(8)?;
108 w.octet_string(self.server_id.as_bytes())?;
109 self.act_time.write(w)?;
110 w.unsigned(u64::from(self.reg_period), 4)?;
111 self.tree_path.write(w)?;
112 self.columns.write(w)?;
113 self.periods.write(w)?;
114 w.opt_octet_string(self.rawdata)?;
115 w.opt_octet_string(self.profile_signature)
116 }
117}
118
119#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
121pub struct ProfileColumn {
122 pub obis: Obis,
124 pub unit: Unit,
126 pub scaler: i8,
128}
129
130#[derive(Clone, Copy, Debug, PartialEq, Eq)]
132pub struct ProfilePeriod<'a> {
133 pub val_time: Time,
135 pub status: u64,
137 pub values: ProfileValues<'a>,
139 pub period_signature: Option<&'a [u8]>,
141}
142
143#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
145pub struct ProfileValue<'a> {
146 pub value: SmlValue<'a>,
148 pub value_signature: Option<&'a [u8]>,
150}
151
152impl<'a> ProfileValue<'a> {
153 pub const fn new(value: SmlValue<'a>) -> Self {
155 Self { value, value_signature: None }
156 }
157}
158
159impl sealed::Sealed for ProfileColumn {}
160impl sealed::Sealed for ProfilePeriod<'_> {}
161impl sealed::Sealed for ProfileValue<'_> {}
162
163impl<'a> Element<'a> for ProfileColumn {
164 fn read(r: &mut Reader<'a>, _: Ctx) -> Result<Self> {
165 r.list(3)?;
166 let obis = Obis::from_bytes(r.octet_string()?)?;
167 let unit = Unit(u8::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?);
168 let scaler = i8::try_from(r.integer()?).map_err(|_| Error::ValueOutOfRange)?;
169 Ok(Self { obis, unit, scaler })
170 }
171
172 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
173 w.list(3)?;
174 w.octet_string(&self.obis.0)?;
175 w.unsigned(u64::from(self.unit.0), 1)?;
176 w.integer(i64::from(self.scaler), 1)
177 }
178}
179
180impl<'a> Element<'a> for ProfilePeriod<'a> {
181 fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
182 r.list(4)?;
183 Ok(Self {
184 val_time: Time::read(r, ctx.options)?,
185 status: r.unsigned()?,
186 values: Seq::read(r, ctx)?,
187 period_signature: r.opt_octet_string()?,
188 })
189 }
190
191 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
192 w.list(4)?;
193 self.val_time.write(w)?;
194 w.unsigned(self.status, 8)?;
195 self.values.write(w)?;
196 w.opt_octet_string(self.period_signature)
197 }
198}
199
200impl<'a> Element<'a> for ProfileValue<'a> {
201 fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
202 r.list(2)?;
203 let value = SmlValue::read(r, ctx.options)?.0;
204 Ok(Self { value, value_signature: r.opt_octet_string()? })
205 }
206
207 fn write(&self, w: &mut Writer<'_>) -> Result<()> {
208 w.list(2)?;
209 self.value.write(w)?;
210 w.opt_octet_string(self.value_signature)
211 }
212}