|
14 | 14 | from ...microgrid.component import ComponentCategory, ComponentMetricId, InverterType |
15 | 15 | from ...timeseries import Energy, Percentage, Sample, Temperature |
16 | 16 | from ._component_metrics import ComponentMetricsData |
17 | | -from ._result_types import Bound, PowerMetrics |
| 17 | +from ._result_types import Bound, PowerMetrics, TemperatureMetrics |
18 | 18 |
|
19 | 19 | _logger = logging.getLogger(__name__) |
20 | 20 | _MIN_TIMESTAMP = datetime.min.replace(tzinfo=timezone.utc) |
@@ -59,7 +59,7 @@ def battery_inverter_mapping(batteries: Iterable[int]) -> dict[int, int]: |
59 | 59 |
|
60 | 60 | # Formula output types class have no common interface |
61 | 61 | # Print all possible types here. |
62 | | -T = TypeVar("T", Sample[Percentage], Sample[Energy], PowerMetrics) |
| 62 | +T = TypeVar("T", Sample[Percentage], Sample[Energy], PowerMetrics, TemperatureMetrics) |
63 | 63 |
|
64 | 64 |
|
65 | 65 | class MetricCalculator(ABC, Generic[T]): |
@@ -234,6 +234,99 @@ def calculate( |
234 | 234 | ) |
235 | 235 |
|
236 | 236 |
|
| 237 | +class TemperatureCalculator(MetricCalculator[TemperatureMetrics]): |
| 238 | + """Define how to calculate temperature metrics.""" |
| 239 | + |
| 240 | + def __init__(self, batteries: Set[int]) -> None: |
| 241 | + """Create class instance. |
| 242 | +
|
| 243 | + Args: |
| 244 | + batteries: What batteries should be used for calculation. |
| 245 | + """ |
| 246 | + super().__init__(batteries) |
| 247 | + |
| 248 | + self._metrics = [ |
| 249 | + ComponentMetricId.TEMPERATURE_MAX, |
| 250 | + ] |
| 251 | + |
| 252 | + @classmethod |
| 253 | + def name(cls) -> str: |
| 254 | + """Return name of the calculator. |
| 255 | +
|
| 256 | + Returns: |
| 257 | + Name of the calculator |
| 258 | + """ |
| 259 | + return "temperature" |
| 260 | + |
| 261 | + @property |
| 262 | + def battery_metrics(self) -> Mapping[int, list[ComponentMetricId]]: |
| 263 | + """Return what metrics are needed for each battery. |
| 264 | +
|
| 265 | + Returns: |
| 266 | + Map between battery id and set of required metrics id. |
| 267 | + """ |
| 268 | + return {bid: self._metrics for bid in self._batteries} |
| 269 | + |
| 270 | + @property |
| 271 | + def inverter_metrics(self) -> Mapping[int, list[ComponentMetricId]]: |
| 272 | + """Return what metrics are needed for each inverter. |
| 273 | +
|
| 274 | + Returns: |
| 275 | + Map between inverter id and set of required metrics id. |
| 276 | + """ |
| 277 | + return {} |
| 278 | + |
| 279 | + def calculate( |
| 280 | + self, |
| 281 | + metrics_data: dict[int, ComponentMetricsData], |
| 282 | + working_batteries: set[int], |
| 283 | + ) -> TemperatureMetrics | None: |
| 284 | + """Aggregate the metrics_data and calculate high level metric for temperature. |
| 285 | +
|
| 286 | + Missing components will be ignored. Formula will be calculated for all |
| 287 | + working batteries that are in metrics_data. |
| 288 | +
|
| 289 | + Args: |
| 290 | + metrics_data: Components metrics data, that should be used to calculate the |
| 291 | + result. |
| 292 | + working_batteries: working batteries. These batteries will be used |
| 293 | + to calculate the result. It should be subset of the batteries given in a |
| 294 | + constructor. |
| 295 | +
|
| 296 | + Returns: |
| 297 | + High level metric calculated from the given metrics. |
| 298 | + Return None if there are no component metrics. |
| 299 | + """ |
| 300 | + timestamp = _MIN_TIMESTAMP |
| 301 | + temperature_sum: float = 0 |
| 302 | + temperature_min: float = float("inf") |
| 303 | + temperature_max: float = float("-inf") |
| 304 | + temperature_count: int = 0 |
| 305 | + for battery_id in working_batteries: |
| 306 | + if battery_id not in metrics_data: |
| 307 | + continue |
| 308 | + metrics = metrics_data[battery_id] |
| 309 | + temperature = metrics.get(ComponentMetricId.TEMPERATURE_MAX) |
| 310 | + if temperature is None: |
| 311 | + continue |
| 312 | + timestamp = max(timestamp, metrics.timestamp) |
| 313 | + temperature_sum += temperature |
| 314 | + temperature_min = min(temperature_min, temperature) |
| 315 | + temperature_max = max(temperature_max, temperature) |
| 316 | + temperature_count += 1 |
| 317 | + if timestamp == _MIN_TIMESTAMP: |
| 318 | + return None |
| 319 | + |
| 320 | + temperature_avg = temperature_sum / temperature_count |
| 321 | + |
| 322 | + return TemperatureMetrics( |
| 323 | + timestamp=timestamp, |
| 324 | + min=Temperature.from_celsius(value=temperature_min), |
| 325 | + avg=Temperature.from_celsius(value=temperature_avg), |
| 326 | + max=Temperature.from_celsius(value=temperature_max), |
| 327 | + ) |
| 328 | + |
| 329 | + |
237 | 330 | class SoCCalculator(MetricCalculator[Sample[Percentage]]): |
238 | 331 | """Define how to calculate SoC metrics.""" |
239 | 332 |
|
|
0 commit comments