ieee2030-5 0.2.0

IEEE 2030.5 (Smart Energy Profile 2.0) for Rust
Documentation
//! CSIP-AUS v1.2 site limits at the connection point. The application chooses
//! the site and retains independent limits for import, export, generation and load.
use crate::schedule::{self, Draws, Event, Selection};
use alloc::vec::Vec;

pub const NAMESPACE: &str = "https://csipaus.org/ns";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Export,
    Import,
    Generation,
    Load,
}
impl Mode {
    pub const ALL: [Self; 4] = [Self::Export, Self::Import, Self::Generation, Self::Load];

    pub const fn local_name(self) -> &'static str {
        match self {
            Self::Export => "opModExpLimW",
            Self::Import => "opModImpLimW",
            Self::Generation => "opModGenLimW",
            Self::Load => "opModLoadLimW",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ActivePower {
    pub value: i16,
    pub multiplier: i8,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PowerError {
    Fractional,
    Overflow,
    InvalidElement,
}
impl ActivePower {
    /// Convert the schema's decimal quantity exactly, including negative limits.
    pub fn whole_watts(self) -> Result<i64, PowerError> {
        if self.value == 0 {
            return Ok(0);
        }
        let value = i64::from(self.value);
        if self.multiplier >= 0 {
            let factor = 10i64
                .checked_pow(self.multiplier as u32)
                .ok_or(PowerError::Overflow)?;
            value.checked_mul(factor).ok_or(PowerError::Overflow)
        } else {
            let factor = 10i64
                .checked_pow(u32::from(self.multiplier.unsigned_abs()))
                .ok_or(PowerError::Fractional)?;
            if value % factor == 0 {
                Ok(value / factor)
            } else {
                Err(PowerError::Fractional)
            }
        }
    }

    /// Read a qualified limit from a validated Australian DERControlBase.
    pub fn from_control(
        base: crate::model::DERControlBase<'_>,
        mode: Mode,
    ) -> Result<Option<Self>, PowerError> {
        use crate::model::View;
        base.node()
            .children()
            .find(|n| n.has_tag_name((NAMESPACE, mode.local_name())))
            .map(Self::from_node)
            .transpose()
    }

    fn from_node(node: roxmltree::Node<'_, '_>) -> Result<Self, PowerError> {
        fn field<T: core::str::FromStr>(
            node: roxmltree::Node<'_, '_>,
            name: &str,
        ) -> Result<T, PowerError> {
            node.children()
                .find(|n| n.has_tag_name(("urn:ieee:std:2030.5:ns", name)))
                .and_then(|n| crate::codec::element_text(n).trim().parse().ok())
                .ok_or(PowerError::InvalidElement)
        }
        Ok(Self {
            value: field(node, "value")?,
            multiplier: field(node, "multiplier")?,
        })
    }
}

/// Site-scoped mode identity. `connection_point` is the network connection
/// identifier carried by the CSIP-AUS ConnectionPoint resource.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SiteMode<'a> {
    pub connection_point: &'a str,
    pub mode: Mode,
}

/// Evaluate one connection point and mode. Scope the default to the same key.
pub fn effective_limit<'a>(
    events: &'a [Event<SiteMode<'_>, ActivePower>],
    key: SiteMode<'a>,
    now: i64,
    default: Option<(SiteMode<'_>, &'a ActivePower)>,
    max_events: usize,
) -> Result<Selection<'a, ActivePower>, schedule::Error> {
    let default = default.filter(|(k, _)| *k == key).map(|(_, v)| v);
    schedule::select(events, &key, now, default, max_events)
}

/// Project a validated control into one site-limit event per CSIP-AUS limit it
/// carries, keyed by `connection_point`. Base IEEE 2030.5 modes are skipped.
pub fn events<'c>(
    program: crate::model::DERProgram<'_>,
    control: crate::model::DERControl<'_>,
    draws: Draws,
    device_category: u32,
    received: i64,
    connection_point: &'c str,
) -> Result<Vec<Event<SiteMode<'c>, ActivePower>>, schedule::Error> {
    schedule::from_control(program, control, draws, device_category, received)?
        .into_iter()
        .filter_map(|e| {
            let mode = Mode::ALL
                .into_iter()
                .find(|m| e.mode == (NAMESPACE, m.local_name()).into())?;
            Some(
                ActivePower::from_node(e.value)
                    .map(|value| Event {
                        id: e.id,
                        mode: SiteMode {
                            connection_point,
                            mode,
                        },
                        value,
                        primacy: e.primacy,
                        created: e.created,
                        received: e.received,
                        window: e.window,
                        termination: e.termination,
                    })
                    .map_err(|_| schedule::Error::InvalidControl),
            )
        })
        .collect()
}