Skip to main content

emob_session/
lib.rs

1//! Charging sessions: how they were authorised, what the meter said, and how
2//! that divides across the quarter hours the market settles in.
3//!
4//! # What is here
5//!
6//! - [`auth`] — the five paths a session can be authorised by, and why they are
7//!   not interchangeable: AFIR requires a contract-free one at every public
8//!   point, and the signed record states how strongly the driver was
9//!   identified.
10//! - [`meter`] — cumulative register readings with the reason each was taken.
11//!   `Sample.Clock` is the one that can settle a quarter hour without guessing.
12//! - [`split`] — the quarter-hour split, which **conserves energy exactly by
13//!   construction** and records for every slot whether it was measured or
14//!   interpolated.
15//! - [`mispel`] — the first arithmetic larger than one session: a bidirectional
16//!   site's month (and Pauschal year) under the Bundesnetzagentur's `MiSpeL` Festlegung,
17//!   netting the points' quarter hours against the grid meter's.
18//! - [`success`] and [`reliability`] — the charging success rate per day, and
19//!   the month report a second reader can recompute and the public can read.
20//! - [`session`] — the session itself, as a state machine that refuses what the
21//!   protocol forbids and keeps **when** each state was entered, because
22//!   "suspended" is an interval a tariff prices rather than a status light.
23//!
24//! # The one idea worth reading about
25//!
26//! Splitting a session across quarter hours naively — computing each slot
27//! independently — leaves a sum that is a few milliwatt-hours off the total,
28//! and the usual fix shoves the difference into the last slot, misattributing
29//! energy to whoever held it. Instead [`split::into_quarter_hours`] computes
30//! the **cumulative** value at each boundary once and takes differences, so the
31//! sum telescopes and equals the total to the last digit, always:
32//!
33//! ```
34//! use emob_session::{MeterReading, MeterSeries, ReadingContext, split};
35//! use emob_core::{Direction, Energy};
36//! use rust_decimal::Decimal;
37//! use std::str::FromStr;
38//! use time::macros::datetime;
39//!
40//! # let kwh = |s: &str| Energy::from_kwh(Decimal::from_str(s).unwrap()).unwrap();
41//! // 10:01 to 10:22 — the boundary at 10:15 falls two thirds of the way through,
42//! // and 7 kWh times two thirds does not terminate.
43//! let series = MeterSeries::new(Direction::Import, vec![
44//!     MeterReading::new(datetime!(2026-01-02 10:01 +1), kwh("0"), Direction::Import, ReadingContext::TransactionBegin),
45//!     MeterReading::new(datetime!(2026-01-02 10:22 +1), kwh("7"), Direction::Import, ReadingContext::TransactionEnd),
46//! ])?;
47//!
48//! let split = split::into_quarter_hours(&series)?;
49//! assert!(split.conserves());   // exactly 7 kWh, across two slots
50//! assert!(!split.fully_measured());  // …and it says it had to interpolate
51//! # Ok::<(), Box<dyn std::error::Error>>(())
52//! ```
53//!
54//! # No I/O, no clock
55//!
56//! Nothing here reads a clock, a socket or a file. Every instant is an
57//! argument, so a session from two years ago splits today exactly as it split
58//! then.
59
60#![forbid(unsafe_code)]
61#![warn(missing_docs, clippy::pedantic)]
62#![allow(clippy::module_name_repetitions)]
63
64pub mod auth;
65pub mod meter;
66pub mod mispel;
67pub mod reliability;
68pub mod session;
69pub mod split;
70pub mod success;
71
72pub use auth::{AuthError, AuthPath, Authorization, Subject, TokenRef};
73pub use meter::{MeterError, MeterReading, MeterSeries, ReadingContext};
74pub use session::{EndReason, Session, SessionError, SessionState, StateChange};
75pub use split::{Idle, Provenance, SessionSplit, Slot, SplitError};
76// The settlement grid is market vocabulary and lives in `emob-core`; it is
77// re-exported because every consumer of a `Slot` needs it in the same breath.
78pub use emob_core::QuarterHour;