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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! # openadr — OpenADR 3.1 in Rust
//!
//! An implementation of the [OpenADR 3](https://www.openadr.org/) demand-response protocol: a VTN
//! server, a VEN runtime and a typed client, over a shared wire model and a deterministic domain
//! core — one crate, sliced with Cargo features.
//!
//! OpenADR is how a utility asks flexible load to move. A **VTN** publishes programmes and events;
//! a **VEN** — a charge point, a battery, a heat pump — reads them, acts, and reports back.
//! Narrative documentation, including an introduction to the protocol itself, is at
//! <https://hupe1980.github.io/openadr>.
//!
//! ## Layers
//!
//! | Module | Feature | What |
//! |---|---|---|
//! | [`model`] | always on (`no_std` + `alloc`) | wire types, newtypes with invariants, spec sentinels |
//! | [`schema`] | always on | payload typing from the Alliance enumeration schemas |
//! | [`core`] | always on | interval expansion, timeline, object privacy, report scheduling |
//! | [`client`](https://docs.rs/openadr/latest/openadr/client/) | `client` | business-logic and VEN HTTP clients, role-checked at compile time |
//! | [`ven`](https://docs.rs/openadr/latest/openadr/ven/) | `ven` | the VEN loop: registration, event sync, timeline, report scheduling |
//! | [`conformance`](https://docs.rs/openadr/latest/openadr/conformance/) | `conformance` | a black-box suite that measures **any** VTN against the specification |
//! | [`vtn`](https://docs.rs/openadr/latest/openadr/vtn/) | `vtn` | axum VTN server, storage, authentication, the notification outbox |
//! | [`webhook`](https://docs.rs/openadr/latest/openadr/webhook/) | `webhook-signature` | the webhook signature scheme, both ends of it (`no_std`) |
//! | `mqtt` | `mqtt` | shared MQTT broker transport |
//! | [`discovery`] | always on; `mdns` for the socket | finding a local VTN over mDNS/DNS-SD, both ends |
//!
//! Storage backends are `sqlite` and `postgres`; authentication backends are `internal-auth` and
//! `external-auth`; push transports are `webhook` and `mqtt`. Every flag is read by a `cfg`.
//! A subscriber can enable `webhook-signature` or `mqtt` on its own. TLS roles use the process
//! provider, with a ring fallback when the default `tls-ring` feature is enabled.
//!
//! ## Serving
//!
//! ```no_run
//! # #[cfg(feature = "vtn")]
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! use openadr::vtn::{Vtn, store::MemoryStorage};
//! let vtn = Vtn::builder().storage(MemoryStorage::shared()).build();
//! vtn.serve("0.0.0.0:3000").await?;
//! # Ok(())
//! # }
//! ```
//!
//! `Vtn::builder` is a typestate: `build()` exists only once a storage backend
//! has been given, so a VTN with nowhere to put an event is a compile error. Without an
//! authenticator it serves unauthenticated readers, which is read-only by construction — every
//! write requires a scope, and an anonymous principal holds none.
//!
//! ## Reading a schedule
//!
//! ```no_run
//! use openadr::prelude::*;
//! # fn run(event: &EventRequest) -> Result<(), Box<dyn std::error::Error>> {
//! // Resolve an event's declared timing into absolute windows: inherited periods, the `now`
//! // sentinel, unbounded durations, and multi-value payloads that subdivide their interval.
//! let intervals = IntervalExpander::at(Timestamp::now()).expand(event)?;
//!
//! // Or the whole sequence, including the intervals the event never lists: a tariff that loops
//! // for ever, and a forecast whose structure is implied by its `intervalPeriod` alone.
//! let sequence = IntervalExpander::at(Timestamp::now()).sequence(event)?;
//! # Ok(())
//! # }
//! ```
//!
//! [`core::Timeline`] then merges a programme's concurrent events by priority, splitting the loser
//! *around* the winner so a long price curve resumes when a short curtailment ends.
//! `ven::VenRuntime` is that loop with registration, conditional polling and reporting around it.
//!
//! ## Spec version
//!
//! [`SPEC_VERSION`] is the release this crate is written from. There is no 3.0 compatibility in the
//! wire model — 3.1 is not backwards compatible with it — and peers that bend the schema are served
//! by request/response adapters at the edge instead (see [`model::adapt`]).
//!
//! ## `no_std`
//!
//! `default-features = false` yields a `no_std` + `alloc` build containing [`model`], [`schema`]
//! and [`core`] — the layers an appliance-class VEN needs, and they build for
//! `thumbv7em-none-eabihf` and `wasm32-unknown-unknown`. `no_std` here means no operating system,
//! not no heap: the wire model is `String`s and `Vec`s, so an allocator is assumed.
// Always: the wire model is Strings and Vecs, so an allocator is a requirement rather than a
// choice. `no_std` here means "no operating system", not "no heap".
extern crate alloc;
/// This crate's MQTT 3.1.1 client, shared by the VTN's publisher and the VEN's subscriber.
///
/// Enable `mqtt` to connect directly to a broker. Adding `vtn` or `ven` also enables that
/// role's OpenADR notification binding.
/// One `rustls` decision, shared by everything here that speaks TLS.
pub use ;
/// `std`/`no_std` bridge. Internal, but public so generated code and examples can use it.
/// Everything a typical user wants in scope.
/// The OpenADR specification release this crate is written from.
///
/// Also the single source of truth for `cargo xtask codegen`, which generates the payload table
/// from this version's enumeration files — so the table and the crate cannot describe different
/// releases.
pub const SPEC_VERSION: &str = "3.1.0";
/// Default base path a VTN is mounted under (`/openadr3/3.1.0`).
pub const DEFAULT_BASE_PATH: &str = "/openadr3/3.1.0";