Skip to content

Commit f6cdd67

Browse files
committed
Rewrite README and use it as the crate-level docs
Also include some documentation fixes. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 823f3a3 commit f6cdd67

4 files changed

Lines changed: 121 additions & 12 deletions

File tree

README.md

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,125 @@
33
[<img alt="docs.rs" src="https://img.shields.io/docsrs/frequenz-microgrid">](https://docs.rs/frequenz-microgrid)
44
[<img alt="Crates.io" src="https://img.shields.io/crates/v/frequenz-microgrid">](https://crates.io/crates/frequenz-microgrid)
55

6-
High-level interface for Rust to the Frequenz Microgrid API.
6+
High-level Rust interface for the Frequenz Microgrid API.
77

8-
## Documentation
8+
The crate connects to a Microgrid API server, builds a [`ComponentGraph`]
9+
from the live topology, and exposes typed, formula-driven streams of
10+
microgrid metrics — grid power, battery state-of-charge, PV reactive
11+
power, consumer current, and so on — without requiring callers to write
12+
the per-component formulas by hand.
913

10-
For more information, please visit the [documentation
11-
website](https://docs.rs/frequenz-microgrid).
14+
Support for controlling components is coming soon.
15+
16+
## Quick start
17+
18+
```toml
19+
[dependencies]
20+
frequenz-microgrid = "0.4"
21+
chrono = "0.4"
22+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
23+
```
24+
25+
Stream the grid's active power once per second:
26+
27+
```rust , ignore
28+
use chrono::TimeDelta;
29+
use frequenz_microgrid::{Error, LogicalMeterConfig, Microgrid, metric};
30+
31+
#[tokio::main]
32+
async fn main() -> Result<(), Error> {
33+
let microgrid = Microgrid::try_new(
34+
"http://[::1]:8800",
35+
LogicalMeterConfig::new(TimeDelta::try_seconds(1).unwrap()),
36+
)
37+
.await?;
38+
39+
let mut grid = microgrid
40+
.logical_meter()
41+
.grid::<metric::AcPowerActive>()?
42+
.subscribe()
43+
.await?;
44+
45+
while let Ok(sample) = grid.recv().await {
46+
println!("{:?}: {:?}", sample.timestamp(), sample.value());
47+
}
48+
Ok(())
49+
}
50+
```
51+
52+
[`Microgrid::try_new`] blocks (with retries) until the server is
53+
reachable and returns a graph that builds successfully, so applications
54+
can start before their backing service is ready.
55+
56+
## Testing with the in-crate mock
57+
58+
[`frequenz_microgrid::test_utils`][`client::test_utils`] ships a
59+
[`MockMicrogridApiClient`] (plus [`MockComponent`] and
60+
[`TokioSyncedClock`] helpers) for downstream tests. Enable it as a
61+
dev-dependency feature:
62+
63+
```toml
64+
[dev-dependencies]
65+
frequenz-microgrid = { version = "0.4", features = ["test-utils"] }
66+
```
67+
68+
## What's included
69+
70+
- [`Microgrid`] / [`LogicalMeterHandle`]: typed formulas for [`grid`],
71+
[`battery`], [`pv`], [`chp`], [`ev_charger`], [`consumer`],
72+
[`producer`], and individual [`component`]s, parametrised over a
73+
metric in [`metric`].
74+
- [`BatteryPool`]: aggregated active-power bounds and state-of-charge
75+
for one or more batteries.
76+
- [`MicrogridClientHandle`]: cloneable low-level gRPC handle with
77+
per-stream automatic reconnect.
78+
- [`quantity`]: [`Power`], [`Current`], [`Voltage`], [`ReactivePower`],
79+
[`Energy`], [`Frequency`], [`Percentage`]. Unit conversions are
80+
explicit at every API surface.
81+
82+
## Configuring the underlying graph
83+
84+
[`LogicalMeterConfig::with_component_graph_config`] forwards a
85+
[`ComponentGraphConfig`] to the
86+
[`frequenz-microgrid-component-graph`](https://docs.rs/frequenz-microgrid-component-graph)
87+
builder, exposing knobs like
88+
[`prefer_meters_in_component_formulas`],
89+
[`include_phantom_loads_in_consumer_formula`], and per-formula
90+
overrides. If not set, the graph crate's `Default::default()` is used.
1291

1392
## Contributing
1493

15-
If you want to know how to build this project and contribute to it, please
16-
check out the [Contributing Guide](CONTRIBUTING.md).
94+
See the [Contributing Guide](https://github.com/frequenz-floss/frequenz-microgrid-rs/blob/HEAD/CONTRIBUTING.md).
95+
96+
[`ComponentGraph`]: https://docs.rs/frequenz-microgrid-component-graph/latest/frequenz_microgrid_component_graph/struct.ComponentGraph.html
97+
[`ComponentGraphConfig`]: https://docs.rs/frequenz-microgrid-component-graph/latest/frequenz_microgrid_component_graph/struct.ComponentGraphConfig.html
98+
[`prefer_meters_in_component_formulas`]: https://docs.rs/frequenz-microgrid-component-graph/latest/frequenz_microgrid_component_graph/struct.ComponentGraphConfigBuilder.html#method.prefer_meters_in_component_formulas
99+
[`include_phantom_loads_in_consumer_formula`]: https://docs.rs/frequenz-microgrid-component-graph/latest/frequenz_microgrid_component_graph/struct.ComponentGraphConfigBuilder.html#method.include_phantom_loads_in_consumer_formula
100+
[`Microgrid`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.Microgrid.html
101+
[`Microgrid::try_new`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.Microgrid.html#method.try_new
102+
[`LogicalMeterHandle`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html
103+
[`LogicalMeterConfig`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterConfig.html
104+
[`LogicalMeterConfig::with_component_graph_config`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterConfig.html#method.with_component_graph_config
105+
[`BatteryPool`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.BatteryPool.html
106+
[`MicrogridClientHandle`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.MicrogridClientHandle.html
107+
[`grid`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.grid
108+
[`battery`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.battery
109+
[`pv`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.pv
110+
[`chp`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.chp
111+
[`ev_charger`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.ev_charger
112+
[`consumer`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.consumer
113+
[`producer`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.producer
114+
[`component`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/struct.LogicalMeterHandle.html#method.component
115+
[`metric`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/metric/index.html
116+
[`quantity`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/index.html
117+
[`Power`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Power.html
118+
[`Current`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Current.html
119+
[`Voltage`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Voltage.html
120+
[`ReactivePower`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.ReactivePower.html
121+
[`Energy`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Energy.html
122+
[`Frequency`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Frequency.html
123+
[`Percentage`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/quantity/struct.Percentage.html
124+
[`client::test_utils`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/client/test_utils/index.html
125+
[`MockMicrogridApiClient`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/client/test_utils/struct.MockMicrogridApiClient.html
126+
[`MockComponent`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/client/test_utils/struct.MockComponent.html
127+
[`TokioSyncedClock`]: https://docs.rs/frequenz-microgrid/latest/frequenz_microgrid/client/test_utils/struct.TokioSyncedClock.html

src/client/microgrid_client_handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ impl MicrogridClientHandle {
131131
///
132132
/// The direction of a connection is always away from the grid endpoint,
133133
/// i.e. aligned with the direction of positive current according to the
134-
/// passive sign convention:
135-
/// https://en.wikipedia.org/wiki/Passive_sign_convention
134+
/// [passive sign
135+
/// convention](https://en.wikipedia.org/wiki/Passive_sign_convention).
136136
///
137137
/// If provided, the `start` and `end` filters have an `AND` relationship
138138
/// between each other, meaning that they are applied serially, but an `OR`

src/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ ErrorKind!(
6464
(APIServerError, api_server_error),
6565
);
6666

67-
/// An error that can occur during the creation or traversal of a
68-
/// [ComponentGraph][crate::ComponentGraph].
67+
/// An error that occurred in `frequenz_microgrid`.
6968
#[derive(Debug, Clone, PartialEq)]
7069
pub struct Error {
7170
kind: ErrorKind,

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// License: MIT
22
// Copyright © 2025 Frequenz Energy-as-a-Service GmbH
33

4-
//! High-level interface for the Microgrid API.
5-
4+
#![doc = include_str!("../README.md")]
65
#![cfg_attr(
76
not(test),
87
deny(

0 commit comments

Comments
 (0)