|
3 | 3 | [<img alt="docs.rs" src="https://img.shields.io/docsrs/frequenz-microgrid">](https://docs.rs/frequenz-microgrid) |
4 | 4 | [<img alt="Crates.io" src="https://img.shields.io/crates/v/frequenz-microgrid">](https://crates.io/crates/frequenz-microgrid) |
5 | 5 |
|
6 | | -High-level interface for Rust to the Frequenz Microgrid API. |
| 6 | +High-level Rust interface for the Frequenz Microgrid API. |
7 | 7 |
|
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. |
9 | 13 |
|
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. |
12 | 91 |
|
13 | 92 | ## Contributing |
14 | 93 |
|
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 |
0 commit comments