Skip to main content

project_meter/
project_meter.rs

1//! Project the packaged two-quarter example with the offline Rust API.
2use opcua_kit::NodeId;
3use opcua_kit::commissioning::{
4    meter::{MeterProjector, MeterWindow, OffsetDateTime, parse_meter_reading},
5    parse_contract,
6};
7use std::{
8    error::Error,
9    io::{self, Write},
10};
11use time::{Duration, format_description::well_known::Rfc3339};
12
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}