Skip to content

Commit 6918445

Browse files
committed
Confirm components are batteries when creating a battery pool
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 215b049 commit 6918445

5 files changed

Lines changed: 32 additions & 13 deletions

File tree

examples/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<(), Error> {
2323
)
2424
.await?;
2525

26-
let mut battery_pool = microgrid.battery_pool(None);
26+
let mut battery_pool = microgrid.battery_pool(None)?;
2727
let mut bounds_rx = battery_pool.power_bounds();
2828

2929
while let Ok(bounds) = bounds_rx.recv().await {

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ErrorKind!(
5959
(ChronoError, chrono_error),
6060
(DroppedUnusedFormulas, dropped_unused_formulas),
6161
(FormulaEngineError, formula_engine_error),
62+
(InvalidComponent, invalid_component),
6263
(Internal, internal),
6364
(APIServerError, api_server_error),
6465
);

src/microgrid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl Microgrid {
5858
self.logical_meter.clone()
5959
}
6060

61-
pub fn battery_pool(&self, component_ids: Option<Vec<u64>>) -> BatteryPool {
62-
BatteryPool::new(
61+
pub fn battery_pool(&self, component_ids: Option<Vec<u64>>) -> Result<BatteryPool, Error> {
62+
BatteryPool::try_new(
6363
component_ids.map(|ids| ids.into_iter().collect()),
6464
self.client.clone(),
6565
self.logical_meter.clone(),

src/microgrid/battery_pool.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,48 @@ pub struct BatteryPool {
3636
impl BatteryPool {
3737
/// Creates a new `BatteryPool` instance with the given component IDs,
3838
/// client and logical meter handles.
39-
pub(crate) fn new(
39+
pub(crate) fn try_new(
4040
component_ids: Option<BTreeSet<u64>>,
4141
client: MicrogridClientHandle,
4242
logical_meter: LogicalMeterHandle,
43-
) -> Self {
44-
Self {
43+
) -> Result<Self, Error> {
44+
let this = Self {
4545
component_ids,
4646
client,
4747
logical_meter,
4848
snapshot_tx: None,
4949
bounds_tx: None,
50+
};
51+
if let Some(ids) = &this.component_ids {
52+
if ids.is_empty() {
53+
let e = "component_ids cannot be an empty set".to_string();
54+
tracing::error!("{e}");
55+
return Err(Error::invalid_component(e));
56+
}
57+
// Validate that all provided IDs correspond to batteries in the graph.
58+
if !ids.is_subset(&this.get_all_battery_ids()) {
59+
let e = format!("All component_ids {:?} must be batteries.", ids);
60+
tracing::error!("{e}");
61+
return Err(Error::invalid_component(e));
62+
}
5063
}
64+
Ok(this)
65+
}
66+
67+
fn get_all_battery_ids(&self) -> BTreeSet<u64> {
68+
self.logical_meter
69+
.graph()
70+
.components()
71+
.filter(|c| c.category() == ElectricalComponentCategory::Battery)
72+
.map(|c| c.id)
73+
.collect()
5174
}
5275

5376
pub(crate) fn get_battery_ids(&self) -> BTreeSet<u64> {
5477
if let Some(ids) = &self.component_ids {
5578
ids.clone()
5679
} else {
57-
self.logical_meter
58-
.graph()
59-
.components()
60-
.filter(|c| c.category() == ElectricalComponentCategory::Battery)
61-
.map(|c| c.id)
62-
.collect()
80+
self.get_all_battery_ids()
6381
}
6482
}
6583

src/microgrid/telemetry_tracker/battery_pool_telemetry_tracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ mod tests {
242242
)
243243
.await
244244
.unwrap();
245-
BatteryPool::new(None, client, lm)
245+
BatteryPool::try_new(None, client, lm).unwrap()
246246
}
247247

248248
/// Drains `rx` for up to `steps` * 100ms of simulated time, returning the

0 commit comments

Comments
 (0)