Skip to content

Commit a50550d

Browse files
committed
Add boiler-plate for creating BatteryPools to the DataPipeline
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 29aa8e8 commit a50550d

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import annotations
1212

1313
import typing
14+
from collections import abc
1415
from dataclasses import dataclass
1516
from datetime import timedelta
1617

@@ -33,9 +34,11 @@
3334
Request,
3435
Result,
3536
)
37+
from ..timeseries.battery_pool import BatteryPool
3638
from ..timeseries.ev_charger_pool import EVChargerPool
3739
from ..timeseries.logical_meter import LogicalMeter
3840

41+
3942
_REQUEST_RECV_BUFFER_SIZE = 500
4043
"""The maximum number of requests that can be queued in the request receiver.
4144
@@ -78,6 +81,7 @@ def __init__(
7881
self._data_sourcing_actor: _ActorInfo | None = None
7982
self._resampling_actor: _ActorInfo | None = None
8083

84+
self._battery_status_channel = Broadcast["BatteryStatus"]("battery-status")
8185
self._power_distribution_channel = Bidirectional["Request", "Result"](
8286
"Default", "Power Distributing Actor"
8387
)
@@ -86,6 +90,7 @@ def __init__(
8690

8791
self._logical_meter: "LogicalMeter" | None = None
8892
self._ev_charger_pools: dict[frozenset[int], "EVChargerPool"] = {}
93+
self._battery_pools: dict[frozenset[int], "BatteryPool"] = {}
8994

9095
def logical_meter(self) -> LogicalMeter:
9196
"""Return the logical meter instance.
@@ -116,7 +121,7 @@ def ev_charger_pool(
116121
created and returned.
117122
118123
Args:
119-
ev_charger_ids: Optional set of IDs of EV Charger to be managed by the
124+
ev_charger_ids: Optional set of IDs of EV Chargers to be managed by the
120125
EVChargerPool.
121126
122127
Returns:
@@ -137,6 +142,43 @@ def ev_charger_pool(
137142
)
138143
return self._ev_charger_pools[key]
139144

145+
def battery_pool(
146+
self,
147+
battery_ids: abc.Set[int] | None = None,
148+
) -> BatteryPool:
149+
"""Return the corresponding BatteryPool instance for the given ids.
150+
151+
If a BatteryPool instance for the given ids doesn't exist, a new one is created
152+
and returned.
153+
154+
Args:
155+
battery_ids: Optional set of IDs of batteries to be managed by the
156+
BatteryPool.
157+
158+
Returns:
159+
A BatteryPool instance.
160+
"""
161+
from ..timeseries.battery_pool import BatteryPool
162+
163+
if not self._power_distributing_actor:
164+
self._start_power_distributing_actor()
165+
166+
# We use frozenset to make a hashable key from the input set.
167+
key: frozenset[int] = frozenset()
168+
if battery_ids is not None:
169+
key = frozenset(battery_ids)
170+
171+
if key not in self._battery_pools:
172+
self._battery_pools[key] = BatteryPool(
173+
batteries_status_receiver=self._battery_status_channel.new_receiver(),
174+
min_update_interval=timedelta(
175+
seconds=self._resampler_config.resampling_period_s
176+
),
177+
batteries_id=battery_ids,
178+
)
179+
180+
return self._battery_pools[key]
181+
140182
def power_distributing_handle(self) -> Bidirectional.Handle[Request, Result]:
141183
"""Return the handle to the power distributing actor.
142184

0 commit comments

Comments
 (0)