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>
impl<'a> MeterProjector<'a>
Sourcepub fn new(
contract: &'a Contract,
window: MeterWindow<'_>,
) -> Result<Self, Error>
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?
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: ®ister,
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}Sourcepub fn remaining_input_bytes(&self) -> usize
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.
Sourcepub fn push(&mut self, reading: &MeterReading) -> Result<(), Error>
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?
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: ®ister,
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}Sourcepub fn finish(
self,
closed: &[(OffsetDateTime, u64)],
) -> Result<MeterProjection, Error>
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?
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: ®ister,
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}