|
14 | 14 | import logging |
15 | 15 | from datetime import datetime, timezone |
16 | 16 | from queue import Queue |
17 | | -from typing import Any, List, Optional, Set |
| 17 | +from typing import List, Optional, Set |
18 | 18 |
|
19 | 19 | from frequenz.channels import Bidirectional, Broadcast, Receiver, Sender |
20 | 20 |
|
21 | 21 | from frequenz.sdk import microgrid |
22 | | -from frequenz.sdk._data_handling import TimeSeriesEntry |
23 | | -from frequenz.sdk._data_ingestion import MicrogridData |
24 | | -from frequenz.sdk._data_ingestion.formula_calculator import FormulaCalculator |
25 | | -from frequenz.sdk.actor import actor |
| 22 | +from frequenz.sdk.actor import ( |
| 23 | + ChannelRegistry, |
| 24 | + ComponentMetricRequest, |
| 25 | + ComponentMetricsResamplingActor, |
| 26 | + DataSourcingActor, |
| 27 | + ResamplerConfig, |
| 28 | + actor, |
| 29 | +) |
26 | 30 | from frequenz.sdk.actor.power_distributing import ( |
27 | 31 | PowerDistributingActor, |
28 | 32 | Request, |
29 | 33 | Result, |
30 | 34 | Success, |
31 | 35 | ) |
32 | 36 | from frequenz.sdk.microgrid.component import Component, ComponentCategory |
| 37 | +from frequenz.sdk.timeseries import Sample |
| 38 | +from frequenz.sdk.timeseries.logical_meter import LogicalMeter |
33 | 39 |
|
34 | 40 | _logger = logging.getLogger(__name__) |
35 | 41 | HOST = "microgrid.sandbox.api.frequenz.io" # it should be the host name. |
@@ -113,7 +119,7 @@ class DataCollectingActor: |
113 | 119 | def __init__( |
114 | 120 | self, |
115 | 121 | request_channel: Sender[List[float]], |
116 | | - active_power_data: Receiver[TimeSeriesEntry[Any]], |
| 122 | + active_power_data: Receiver[Sample], |
117 | 123 | ) -> None: |
118 | 124 | """Create actor instance. |
119 | 125 |
|
@@ -154,27 +160,33 @@ async def run() -> None: |
154 | 160 | ) |
155 | 161 | await microgrid.initialize(HOST, PORT) |
156 | 162 |
|
157 | | - # await initialize(HOST, PORT) # in v0.8.0 |
| 163 | + channel_registry = ChannelRegistry(name="Microgrid Channel Registry") |
158 | 164 |
|
159 | | - # Create MicrogridData |
160 | | - microgrid_data_channels = { |
161 | | - "batteries_active_power": Broadcast[TimeSeriesEntry[Any]]( |
162 | | - "batteries_active_power_formula", resend_latest=True |
163 | | - ), |
164 | | - } |
| 165 | + data_source_request_channel = Broadcast[ComponentMetricRequest]( |
| 166 | + "Data Source Request Channel" |
| 167 | + ) |
165 | 168 |
|
166 | | - formula_calculator = FormulaCalculator(microgrid.get().component_graph) |
167 | | - microgrid_data = MicrogridData( |
168 | | - microgrid_client=microgrid.get().api_client, |
169 | | - # microgrid_client=microgrid_api.microgrid_api, # in v0.8.0 |
170 | | - component_graph=microgrid.get().component_graph, |
171 | | - outputs={ |
172 | | - key: channel.new_sender() |
173 | | - for key, channel in microgrid_data_channels.items() |
174 | | - }, |
175 | | - formula_calculator=formula_calculator, |
| 169 | + resampling_actor_request_channel = Broadcast[ComponentMetricRequest]( |
| 170 | + "Resampling Actor Request Channel" |
176 | 171 | ) |
177 | 172 |
|
| 173 | + _ds_actor = DataSourcingActor( |
| 174 | + request_receiver=data_source_request_channel.new_receiver(), |
| 175 | + registry=channel_registry, |
| 176 | + ) |
| 177 | + |
| 178 | + _resampling_actor = ComponentMetricsResamplingActor( |
| 179 | + channel_registry=channel_registry, |
| 180 | + data_sourcing_request_sender=data_source_request_channel.new_sender(), |
| 181 | + resampling_request_receiver=resampling_actor_request_channel.new_receiver(), |
| 182 | + config=ResamplerConfig(resampling_period_s=1.0), |
| 183 | + ) |
| 184 | + |
| 185 | + logical_meter = LogicalMeter( |
| 186 | + channel_registry, |
| 187 | + resampling_actor_request_channel.new_sender(), |
| 188 | + microgrid.get().component_graph, |
| 189 | + ) |
178 | 190 | sending_actor_id: str = "SendingActor" |
179 | 191 | # Bidirectional channel is used for one sender - one receiver communication |
180 | 192 | power_distributor_channels = { |
@@ -212,15 +224,12 @@ async def run() -> None: |
212 | 224 |
|
213 | 225 | client_actor = DataCollectingActor( |
214 | 226 | request_channel=power_dist_req_chan.new_sender(), |
215 | | - active_power_data=microgrid_data_channels[ |
216 | | - "batteries_active_power" |
217 | | - ].new_receiver(name="DecisionMakingActor"), |
| 227 | + active_power_data=await logical_meter.grid_power(), |
218 | 228 | ) |
219 | 229 |
|
220 | 230 | # pylint: disable=no-member |
221 | 231 | await service_actor.join() # type: ignore[attr-defined] |
222 | 232 | await client_actor.join() # type: ignore[attr-defined] |
223 | | - await microgrid_data.join() # type: ignore[attr-defined] |
224 | 233 | await power_distributor.join() # type: ignore[attr-defined] |
225 | 234 |
|
226 | 235 |
|
|
0 commit comments