|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2022 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""The logical component for calculating high level producer metrics for a microgrid.""" |
| 5 | + |
| 6 | +import uuid |
| 7 | + |
| 8 | +from frequenz.channels import Sender |
| 9 | + |
| 10 | +from ..actor import ChannelRegistry, ComponentMetricRequest |
| 11 | +from ._quantities import Power |
| 12 | +from .formula_engine import FormulaEngine |
| 13 | +from .formula_engine._formula_engine_pool import FormulaEnginePool |
| 14 | +from .formula_engine._formula_generators import ProducerPowerFormula |
| 15 | + |
| 16 | + |
| 17 | +class Producer: |
| 18 | + """Calculate high level producer metrics in a microgrid. |
| 19 | +
|
| 20 | + Under normal circumstances this is expected to correspond to the gross |
| 21 | + production of the sites active parts excluding EV chargers and batteries. |
| 22 | +
|
| 23 | + Producer provides methods for fetching power values from different points |
| 24 | + in the microgrid. These methods return `FormulaReceiver` objects, which can |
| 25 | + be used like normal `Receiver`s, but can also be composed to form |
| 26 | + higher-order formula streams. |
| 27 | +
|
| 28 | + !!! note |
| 29 | + `Producer` instances are not meant to be created directly by users. |
| 30 | + Use the [`microgrid.producer`][frequenz.sdk.microgrid.producer] method |
| 31 | + for creating `Producer` instances. |
| 32 | +
|
| 33 | + Example: |
| 34 | + ```python |
| 35 | + from datetime import timedelta |
| 36 | +
|
| 37 | + from frequenz.sdk import microgrid |
| 38 | + from frequenz.sdk.timeseries import ResamplerConfig |
| 39 | +
|
| 40 | + await microgrid.initialize( |
| 41 | + "127.0.0.1", |
| 42 | + 50051, |
| 43 | + ResamplerConfig(resampling_period=timedelta(seconds=1.0)) |
| 44 | + ) |
| 45 | +
|
| 46 | + producer = microgrid.producer() |
| 47 | +
|
| 48 | + # Get a receiver for a builtin formula |
| 49 | + producer_power_recv = producer.power.new_receiver() |
| 50 | + async for producer_power_sample in producer_power_recv: |
| 51 | + print(producer_power_sample) |
| 52 | + ``` |
| 53 | + """ |
| 54 | + |
| 55 | + _formula_pool: FormulaEnginePool |
| 56 | + """The formula engine pool to generate producer metrics.""" |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + channel_registry: ChannelRegistry, |
| 61 | + resampler_subscription_sender: Sender[ComponentMetricRequest], |
| 62 | + ) -> None: |
| 63 | + """Initialize the producer formula generator. |
| 64 | +
|
| 65 | + Args: |
| 66 | + channel_registry: The channel registry to use for the producer. |
| 67 | + resampler_subscription_sender: The sender to use for resampler subscriptions. |
| 68 | + """ |
| 69 | + namespace = f"producer-{uuid.uuid4()}" |
| 70 | + self._formula_pool = FormulaEnginePool( |
| 71 | + namespace, |
| 72 | + channel_registry, |
| 73 | + resampler_subscription_sender, |
| 74 | + ) |
| 75 | + |
| 76 | + @property |
| 77 | + def power(self) -> FormulaEngine[Power]: |
| 78 | + """Fetch the producer power for the microgrid. |
| 79 | +
|
| 80 | + This formula produces values that are in the Passive Sign Convention (PSC). |
| 81 | +
|
| 82 | + It will start the formula engine to calculate producer power if it is |
| 83 | + not already running. |
| 84 | +
|
| 85 | + A receiver from the formula engine can be created using the |
| 86 | + `new_receiver` method. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + A FormulaEngine that will calculate and stream producer power. |
| 90 | + """ |
| 91 | + engine = self._formula_pool.from_power_formula_generator( |
| 92 | + "producer_power", |
| 93 | + ProducerPowerFormula, |
| 94 | + ) |
| 95 | + assert isinstance(engine, FormulaEngine) |
| 96 | + return engine |
| 97 | + |
| 98 | + async def stop(self) -> None: |
| 99 | + """Stop all formula engines.""" |
| 100 | + await self._formula_pool.stop() |
0 commit comments