use crate::seq::{Ctx, Element, Seq, sealed};
use crate::tl::{Reader, Writer};
use crate::value::{Quantity, SmlValue, Time, Unit};
use crate::{Error, NameList, Obis, Result, ServerId};
pub type ProfileColumns<'a> = Seq<'a, ProfileColumn>;
pub type ProfilePeriods<'a> = Seq<'a, ProfilePeriod<'a>>;
pub type ProfileValues<'a> = Seq<'a, ProfileValue<'a>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct GetProfilePackResponse<'a> {
pub server_id: ServerId<'a>,
pub act_time: Time,
pub reg_period: u32,
pub tree_path: NameList<'a>,
pub columns: ProfileColumns<'a>,
pub periods: ProfilePeriods<'a>,
pub rawdata: Option<&'a [u8]>,
pub profile_signature: Option<&'a [u8]>,
}
impl<'a> GetProfilePackResponse<'a> {
pub fn quantity(&self, period: &ProfilePeriod<'_>, obis: Obis) -> Option<Quantity> {
let index = self.columns.iter().position(|c| c.is_ok_and(|c| c.obis == obis))?;
let column = self.columns.iter().nth(index)?.ok()?;
let value = period.values.iter().nth(index)?.ok()?;
let mantissa = value.value.as_integer()?;
Some(Quantity { mantissa, exponent: column.scaler, unit: Some(column.unit) })
}
pub(crate) fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
r.list(8)?;
let response = Self {
server_id: ServerId::new(r.octet_string()?),
act_time: Time::read(r, ctx.options)?,
reg_period: u32::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?,
tree_path: Seq::read(r, ctx)?,
columns: Seq::read(r, ctx)?,
periods: Seq::read(r, ctx)?,
rawdata: r.opt_octet_string()?,
profile_signature: r.opt_octet_string()?,
};
for period in &response.periods {
let found = period?.values.len();
if found != response.columns.len() {
return Err(Error::ListLength { expected: response.columns.len(), found });
}
}
Ok(response)
}
pub(crate) fn write(&self, w: &mut Writer<'_>) -> Result<()> {
w.list(8)?;
w.octet_string(self.server_id.as_bytes())?;
self.act_time.write(w)?;
w.unsigned(u64::from(self.reg_period), 4)?;
self.tree_path.write(w)?;
self.columns.write(w)?;
self.periods.write(w)?;
w.opt_octet_string(self.rawdata)?;
w.opt_octet_string(self.profile_signature)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ProfileColumn {
pub obis: Obis,
pub unit: Unit,
pub scaler: i8,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProfilePeriod<'a> {
pub val_time: Time,
pub status: u64,
pub values: ProfileValues<'a>,
pub period_signature: Option<&'a [u8]>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ProfileValue<'a> {
pub value: SmlValue<'a>,
pub value_signature: Option<&'a [u8]>,
}
impl<'a> ProfileValue<'a> {
pub const fn new(value: SmlValue<'a>) -> Self {
Self { value, value_signature: None }
}
}
impl sealed::Sealed for ProfileColumn {}
impl sealed::Sealed for ProfilePeriod<'_> {}
impl sealed::Sealed for ProfileValue<'_> {}
impl<'a> Element<'a> for ProfileColumn {
fn read(r: &mut Reader<'a>, _: Ctx) -> Result<Self> {
r.list(3)?;
let obis = Obis::from_bytes(r.octet_string()?)?;
let unit = Unit(u8::try_from(r.unsigned()?).map_err(|_| Error::ValueOutOfRange)?);
let scaler = i8::try_from(r.integer()?).map_err(|_| Error::ValueOutOfRange)?;
Ok(Self { obis, unit, scaler })
}
fn write(&self, w: &mut Writer<'_>) -> Result<()> {
w.list(3)?;
w.octet_string(&self.obis.0)?;
w.unsigned(u64::from(self.unit.0), 1)?;
w.integer(i64::from(self.scaler), 1)
}
}
impl<'a> Element<'a> for ProfilePeriod<'a> {
fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
r.list(4)?;
Ok(Self {
val_time: Time::read(r, ctx.options)?,
status: r.unsigned()?,
values: Seq::read(r, ctx)?,
period_signature: r.opt_octet_string()?,
})
}
fn write(&self, w: &mut Writer<'_>) -> Result<()> {
w.list(4)?;
self.val_time.write(w)?;
w.unsigned(self.status, 8)?;
self.values.write(w)?;
w.opt_octet_string(self.period_signature)
}
}
impl<'a> Element<'a> for ProfileValue<'a> {
fn read(r: &mut Reader<'a>, ctx: Ctx) -> Result<Self> {
r.list(2)?;
let value = SmlValue::read(r, ctx.options)?.0;
Ok(Self { value, value_signature: r.opt_octet_string()? })
}
fn write(&self, w: &mut Writer<'_>) -> Result<()> {
w.list(2)?;
self.value.write(w)?;
w.opt_octet_string(self.value_signature)
}
}