|
10 | 10 |
|
11 | 11 | from __future__ import annotations |
12 | 12 |
|
| 13 | +import logging |
13 | 14 | import sys |
14 | 15 | import typing |
15 | 16 | from collections import abc |
|
44 | 45 | from ..timeseries.logical_meter import LogicalMeter |
45 | 46 | from ..timeseries.producer import Producer |
46 | 47 |
|
| 48 | +_logger = logging.getLogger(__name__) |
| 49 | + |
47 | 50 |
|
48 | 51 | _REQUEST_RECV_BUFFER_SIZE = 500 |
49 | 52 | """The maximum number of requests that can be queued in the request receiver. |
@@ -112,6 +115,13 @@ def __init__( |
112 | 115 | self._frequency_instance: GridFrequency | None = None |
113 | 116 | self._voltage_instance: VoltageStreamer | None = None |
114 | 117 |
|
| 118 | + self._known_pool_keys: set[str] = set() |
| 119 | + """A set of keys for corresponding to created EVChargerPool instances. |
| 120 | +
|
| 121 | + This is used to warn the user if they try to create a new EVChargerPool instance |
| 122 | + for the same set of component IDs, and with the same priority. |
| 123 | + """ |
| 124 | + |
115 | 125 | def frequency(self) -> GridFrequency: |
116 | 126 | """Return the grid frequency measuring point.""" |
117 | 127 | if self._frequency_instance is None: |
@@ -199,6 +209,21 @@ def ev_charger_pool( |
199 | 209 | if ev_charger_ids is not None: |
200 | 210 | ref_store_key = frozenset(ev_charger_ids) |
201 | 211 |
|
| 212 | + pool_key = f"{ref_store_key}-{priority}" |
| 213 | + if pool_key in self._known_pool_keys: |
| 214 | + _logger.warning( |
| 215 | + "An EVChargerPool instance was already created for ev_charger_ids=%s " |
| 216 | + "and priority=%s using `microgrid.ev_charger_pool(...)`." |
| 217 | + "\n Hint: If the multiple instances are created from the same actor, " |
| 218 | + "consider reusing the same instance." |
| 219 | + "\n Hint: If the instances are created from different actors, " |
| 220 | + "consider using different priorities to distinguish them.", |
| 221 | + ev_charger_ids, |
| 222 | + priority, |
| 223 | + ) |
| 224 | + else: |
| 225 | + self._known_pool_keys.add(pool_key) |
| 226 | + |
202 | 227 | if ref_store_key not in self._ev_charger_pool_reference_stores: |
203 | 228 | self._ev_charger_pool_reference_stores[ref_store_key] = ( |
204 | 229 | EVChargerPoolReferenceStore( |
@@ -265,6 +290,21 @@ def battery_pool( |
265 | 290 | if battery_ids is not None: |
266 | 291 | ref_store_key = frozenset(battery_ids) |
267 | 292 |
|
| 293 | + pool_key = f"{ref_store_key}-{priority}" |
| 294 | + if pool_key in self._known_pool_keys: |
| 295 | + _logger.warning( |
| 296 | + "A BatteryPool instance was already created for battery_ids=%s and " |
| 297 | + "priority=%s using `microgrid.battery_pool(...)`." |
| 298 | + "\n Hint: If the multiple instances are created from the same actor, " |
| 299 | + "consider reusing the same instance." |
| 300 | + "\n Hint: If the instances are created from different actors, " |
| 301 | + "consider using different priorities to distinguish them.", |
| 302 | + battery_ids, |
| 303 | + priority, |
| 304 | + ) |
| 305 | + else: |
| 306 | + self._known_pool_keys.add(pool_key) |
| 307 | + |
268 | 308 | if ref_store_key not in self._battery_pool_reference_stores: |
269 | 309 | self._battery_pool_reference_stores[ref_store_key] = ( |
270 | 310 | BatteryPoolReferenceStore( |
|
0 commit comments