flexpool_telemetry/lib.rs
1//! Live telemetry for flexpool.
2//!
3//! Measurements arrive from devices (or the sandbox simulator) as power in kW — generator
4//! convention: feed-in and battery discharge positive — and battery state of charge in percent.
5//! This crate decides their [`Quality`], keeps them in a bounded [`Store`], aggregates them per
6//! quarter hour and sums them per pool. It reads no clock: every function takes `now`.
7//!
8//! Times are Unix seconds (`i64`); values are measurements (`f64` at the API, `f32` in the rings).
9//! Nothing here becomes a market quantity, so no value crosses the quantisation boundary.
10
11mod quality;
12pub mod sim;
13mod store;
14mod summary;
15
16pub use quality::{
17 FUTURE_TOLERANCE_SECS, Flags, Quality, Quantity, Reading, classify, plausible, stale_limit,
18};
19pub use store::{
20 AssetNow, CLOCK_STEP_SECS, Pushed, QUARTER_BYTES, QUARTERS_MAX, QuarterAggregate, RECENT_MAX,
21 RECENT_SECS, Recent, Store,
22};
23pub use summary::{PoolLive, QualityCounts, summarise};
24
25/// Seconds in a quarter hour.
26pub const QUARTER_SECS: i64 = 900;
27
28/// The start of the quarter hour containing `t` (Berlin quarters align with UTC quarters).
29#[must_use]
30pub const fn quarter_start(t: i64) -> i64 {
31 t - t.rem_euclid(QUARTER_SECS)
32}
33
34/// An asset's rated power in kW as a float (for plausibility and simulation only).
35#[must_use]
36pub fn rated_kw(asset: &flexpool_portfolio::Asset) -> f64 {
37 use rust_decimal::prelude::ToPrimitive;
38 asset.rated_kw.value().to_f64().unwrap_or(0.0)
39}
40
41/// A battery's usable energy in kWh as a float, if it has one.
42#[must_use]
43pub fn energy_kwh(asset: &flexpool_portfolio::Asset) -> Option<f64> {
44 use rust_decimal::prelude::ToPrimitive;
45 asset.energy_kwh.and_then(|e| e.value().to_f64())
46}