88"""
99
1010import logging
11+ import uuid
1112from collections .abc import Iterable
1213from dataclasses import dataclass
14+ from typing import TYPE_CHECKING
15+
16+ from frequenz .channels import Sender
1317
1418from ..microgrid .component import Component
1519from ..microgrid .component ._component import ComponentCategory
1620from . import Fuse
21+ from .formula_engine ._formula_engine_pool import FormulaEnginePool
22+
23+ if TYPE_CHECKING :
24+ # Break circular import
25+ from ..actor import ChannelRegistry , ComponentMetricRequest
1726
1827_logger = logging .getLogger (__name__ )
1928
@@ -25,15 +34,26 @@ class Grid:
2534 fuse : Fuse
2635 """The fuse protecting the grid connection point."""
2736
37+ _formula_pool : FormulaEnginePool
38+ """The formula engine pool to generate grid metrics."""
39+
2840
2941_GRID : Grid | None = None
3042
3143
32- def initialize (components : Iterable [Component ]) -> None :
44+ def initialize (
45+ components : Iterable [Component ],
46+ channel_registry : ChannelRegistry ,
47+ resampler_subscription_sender : Sender [ComponentMetricRequest ],
48+ ) -> None :
3349 """Initialize the grid connection.
3450
3551 Args:
3652 components: The components in the microgrid.
53+ channel_registry: The channel registry instance shared with the
54+ resampling actor.
55+ resampler_subscription_sender: The sender for sending metric requests
56+ to the resampling actor.
3757
3858 Raises:
3959 RuntimeError: If there is more than 1 grid connection point in the
@@ -67,7 +87,14 @@ def initialize(components: Iterable[Component]) -> None:
6787 if fuse is None :
6888 raise RuntimeError ("Grid fuse is None" )
6989
70- _GRID = Grid (fuse )
90+ namespace = f"grid-{ uuid .uuid4 ()} "
91+ formula_pool = FormulaEnginePool (
92+ namespace ,
93+ channel_registry ,
94+ resampler_subscription_sender ,
95+ )
96+
97+ _GRID = Grid (fuse , formula_pool )
7198
7299
73100def get () -> Grid | None :
0 commit comments