Skip to main content

MeterProjector

Struct MeterProjector 

Source
pub struct MeterProjector<'a> { /* private fields */ }
Expand description

A bounded projector for a stream of already validated readings. The projector borrows only the contract. It retains the raw facts needed to detect in-window conflicts and charges every push its compact JSON size plus one newline.

use opcua_kit::commissioning::{Contract, Error, meter::{
    MeterProjection, MeterProjector, MeterReading, MeterWindow, OffsetDateTime,
}};

fn reconcile_input(
    contract: &Contract,
    window: MeterWindow<'_>,
    readings: impl IntoIterator<Item = MeterReading>,
    independent_closes: &[(OffsetDateTime, u64)],
) -> Result<MeterProjection, Error> {
    let mut projector = MeterProjector::new(contract, window)?;
    for reading in readings {
        projector.push(&reading)?;
    }
    projector.finish(independent_closes)
}

Implementations§

Source§

impl<'a> MeterProjector<'a>

Source

pub fn new( contract: &'a Contract, window: MeterWindow<'_>, ) -> Result<Self, Error>

Bind the independently configured close feed and validate the comparison window.

Examples found in repository?
examples/project_meter.rs (line 27)
13fn main() -> Result<(), Box<dyn Error>> {
14    let contract = parse_contract(include_bytes!("../fixtures/meter/contract.json"))?;
15
16    // The independent close feed supplies the meter identity and the UTC quarter starts.
17    let register = NodeId::new("urn:example:meter", "s=GridImportWh")?;
18    let start = OffsetDateTime::parse("2026-10-25T00:45:00Z", &Rfc3339)?;
19    let window = MeterWindow {
20        device: "example-grid-incomer",
21        application_uri: "urn:example:grid-meter",
22        node: &register,
23        start,
24        end: start + Duration::minutes(30),
25        tolerance_wh: 1,
26    };
27    let mut projector = MeterProjector::new(&contract, window)?;
28
29    // In a live driver, push the MeterReading returned by client.read_meter(&contract).
30    for line in include_str!("../fixtures/meter/readings.ndjson").lines() {
31        let reading = parse_meter_reading(&contract, line.as_bytes())?;
32        projector.push(&reading)?;
33    }
34
35    let closes = [(start, 285_000), (start + Duration::minutes(15), 274_999)];
36    let result = projector.finish(&closes)?;
37    let mut output = io::stdout().lock();
38    serde_json::to_writer(&mut output, &result.trace)?;
39    writeln!(output)?;
40    Ok(())
41}
Source

pub fn remaining_input_bytes(&self) -> usize

Remaining typed reading budget in bytes. Compare with MeterReading::compact_line_bytes before pushing. A failed push leaves zero usable bytes.

Source

pub fn push(&mut self, reading: &MeterReading) -> Result<(), Error>

Validate the reading against this contract and add it to the projection. The first error is terminal and is returned by later pushes and Self::finish. The total compact size including one newline per push must fit in 4 MiB.

Examples found in repository?
examples/project_meter.rs (line 32)
13fn main() -> Result<(), Box<dyn Error>> {
14    let contract = parse_contract(include_bytes!("../fixtures/meter/contract.json"))?;
15
16    // The independent close feed supplies the meter identity and the UTC quarter starts.
17    let register = NodeId::new("urn:example:meter", "s=GridImportWh")?;
18    let start = OffsetDateTime::parse("2026-10-25T00:45:00Z", &Rfc3339)?;
19    let window = MeterWindow {
20        device: "example-grid-incomer",
21        application_uri: "urn:example:grid-meter",
22        node: &register,
23        start,
24        end: start + Duration::minutes(30),
25        tolerance_wh: 1,
26    };
27    let mut projector = MeterProjector::new(&contract, window)?;
28
29    // In a live driver, push the MeterReading returned by client.read_meter(&contract).
30    for line in include_str!("../fixtures/meter/readings.ndjson").lines() {
31        let reading = parse_meter_reading(&contract, line.as_bytes())?;
32        projector.push(&reading)?;
33    }
34
35    let closes = [(start, 285_000), (start + Duration::minutes(15), 274_999)];
36    let result = projector.finish(&closes)?;
37    let mut output = io::stdout().lock();
38    serde_json::to_writer(&mut output, &result.trace)?;
39    writeln!(output)?;
40    Ok(())
41}
Source

pub fn finish( self, closed: &[(OffsetDateTime, u64)], ) -> Result<MeterProjection, Error>

Supply the independent closes for the meter bound at construction, then return its sorted trace, omission counts and first exclusion. An empty slice leaves all closes missing. Each pair is the UTC quarter start (or its offset equivalent) and interval energy in Wh. Accepts up to 131,072 entries including duplicates. Identical closes coalesce. The compact trace and its trailing newline must fit in 4 MiB.

Examples found in repository?
examples/project_meter.rs (line 36)
13fn main() -> Result<(), Box<dyn Error>> {
14    let contract = parse_contract(include_bytes!("../fixtures/meter/contract.json"))?;
15
16    // The independent close feed supplies the meter identity and the UTC quarter starts.
17    let register = NodeId::new("urn:example:meter", "s=GridImportWh")?;
18    let start = OffsetDateTime::parse("2026-10-25T00:45:00Z", &Rfc3339)?;
19    let window = MeterWindow {
20        device: "example-grid-incomer",
21        application_uri: "urn:example:grid-meter",
22        node: &register,
23        start,
24        end: start + Duration::minutes(30),
25        tolerance_wh: 1,
26    };
27    let mut projector = MeterProjector::new(&contract, window)?;
28
29    // In a live driver, push the MeterReading returned by client.read_meter(&contract).
30    for line in include_str!("../fixtures/meter/readings.ndjson").lines() {
31        let reading = parse_meter_reading(&contract, line.as_bytes())?;
32        projector.push(&reading)?;
33    }
34
35    let closes = [(start, 285_000), (start + Duration::minutes(15), 274_999)];
36    let result = projector.finish(&closes)?;
37    let mut output = io::stdout().lock();
38    serde_json::to_writer(&mut output, &result.trace)?;
39    writeln!(output)?;
40    Ok(())
41}

Trait Implementations§

Source§

impl<'a> Debug for MeterProjector<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for MeterProjector<'a>

§

impl<'a> Freeze for MeterProjector<'a>

§

impl<'a> Move for MeterProjector<'a>

§

impl<'a> RefUnwindSafe for MeterProjector<'a>

§

impl<'a> Send for MeterProjector<'a>

§

impl<'a> Sync for MeterProjector<'a>

§

impl<'a> Unpin for MeterProjector<'a>

§

impl<'a> UnsafeUnpin for MeterProjector<'a>

§

impl<'a> UnwindSafe for MeterProjector<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.