1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! The domain core: deterministic, I/O-free, and shared by the VTN, the VEN and the tests.
//!
//! Four things live here, and they are the parts of OpenADR that carry real semantics rather than
//! transport: [`IntervalExpander`] resolves an event's declared timing into absolute windows,
//! [`Timeline`] merges concurrent events by priority, [`Access`] decides object privacy, and
//! [`ReportSchedule`] says when a VEN owes a report.
//!
//! Everything takes its time from an injected [`Clock`], so a scenario that fails in production can
//! be replayed exactly in a test and nothing here needs to sleep. That, and having no I/O, is what
//! makes the layer `no_std`-capable.
//!
//! # Converting events and reports
//!
//! Capacity limits with their unit, and usage as an absolute series, exact throughout:
//!
//! ```
//! use openadr::core::IntervalExpander;
//! use openadr::model::{Event, Report};
//!
//! # let event: Event = serde_json::from_str(r#"{"id": "e1", "programID": "p1",
//! # "createdDateTime": "2026-03-13T12:00:00Z", "modificationDateTime": "2026-03-13T12:00:00Z",
//! # "payloadDescriptors": [{"payloadType": "IMPORT_CAPACITY_LIMIT", "units": "KW"}],
//! # "intervalPeriod": {"start": "2026-03-14T00:00:00Z", "duration": "PT1H"},
//! # "intervals": [{"id": 0, "payloads": [{"type": "IMPORT_CAPACITY_LIMIT", "values": [7.56]}]},
//! # {"id": 1, "payloads": [{"type": "IMPORT_CAPACITY_LIMIT", "values": [5]}]}]}"#)?;
//! # let report: Report = serde_json::from_str(r#"{"id": "r1", "eventID": "e1", "clientName": "ven-1",
//! # "createdDateTime": "2026-03-14T02:00:00Z", "modificationDateTime": "2026-03-14T02:00:00Z",
//! # "resources": [{"resourceName": "meter-1", "intervals": [
//! # {"id": 1, "payloads": [{"type": "USAGE", "values": [4.2]}]}]}]}"#)?;
//! let expander = IntervalExpander::at("2026-03-14T03:00:00Z".parse()?);
//!
//! // Limits: one window per interval, the unit from the event's descriptor.
//! let unit = event.content.payload_descriptor("IMPORT_CAPACITY_LIMIT").and_then(|d| d.units.as_ref());
//! for window in expander.expand(&event.content)? {
//! if let Some(limit) = window.payload("IMPORT_CAPACITY_LIMIT").and_then(|p| p.values[0].as_decimal()) {
//! println!("{}..{:?}: {limit} {unit:?}", window.start, window.end);
//! }
//! }
//!
//! // Usage: these intervals carry only the event's ids, so pass the event to place them.
//! for resource in &report.content.resources {
//! for window in expander.expand_report(resource, Some(&event.content))? {
//! let kwh = window.payload("USAGE").and_then(|p| p.values[0].as_decimal());
//! assert_eq!(window.start.to_string(), "2026-03-14T01:00:00Z");
//! assert_eq!(kwh, Some("4.2".parse()?));
//! }
//! }
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! Guide: <https://hupe1980.github.io/openadr/docs/domain-core/>.
use crateTimestamp;
pub use ;
pub use ;
pub use ;
pub use ;
/// A source of the current time.
///
/// Everything time-dependent in this crate goes through this trait. Tests use [`FixedClock`]; the
/// VTN uses [`SystemClock`].
/// Reads the host clock.
;
/// A clock frozen at one instant.
;