Skip to main content

frequenz_microgrid/
microgrid.rs

1// License: MIT
2// Copyright © 2026 Frequenz Energy-as-a-Service GmbH
3
4//! High-level interface for the Microgrid API.
5
6mod battery_bounds_tracker;
7mod battery_pool;
8pub use battery_pool::BatteryPool;
9
10pub(crate) mod telemetry_tracker;
11
12use crate::{Error, LogicalMeterConfig, LogicalMeterHandle, MicrogridClientHandle};
13
14/// A high-level interface for the Microgrid API.
15pub struct Microgrid {
16    client: MicrogridClientHandle,
17    logical_meter: LogicalMeterHandle,
18}
19
20impl Microgrid {
21    /// Creates a new `Microgrid` instance with the given microgrid API URL and
22    /// logical meter configuration.
23    ///
24    /// Returns an error if the URL is unreachable, or if the component graph
25    /// cannot be created with the given configuration.
26    pub async fn try_new(
27        url: impl Into<String>,
28        config: LogicalMeterConfig,
29    ) -> Result<Self, Error> {
30        let client = MicrogridClientHandle::try_new(url).await?;
31        let logical_meter = LogicalMeterHandle::try_new(client.clone(), config).await?;
32
33        Ok(Microgrid {
34            client,
35            logical_meter,
36        })
37    }
38
39    /// Creates a new `Microgrid` instance from the given client and logical
40    /// meter handles.
41    pub fn new_from_handles(
42        client: MicrogridClientHandle,
43        logical_meter: LogicalMeterHandle,
44    ) -> Self {
45        Microgrid {
46            client,
47            logical_meter,
48        }
49    }
50
51    /// Returns a handle to the Microgrid client.
52    pub fn client(&self) -> MicrogridClientHandle {
53        self.client.clone()
54    }
55
56    /// Returns a handle to the logical meter.
57    pub fn logical_meter(&self) -> LogicalMeterHandle {
58        self.logical_meter.clone()
59    }
60
61    pub fn battery_pool(&self, component_ids: Option<Vec<u64>>) -> Result<BatteryPool, Error> {
62        BatteryPool::try_new(
63            component_ids.map(|ids| ids.into_iter().collect()),
64            self.client.clone(),
65            self.logical_meter.clone(),
66        )
67    }
68}