Skip to content

Commit 3990011

Browse files
authored
Improve examples (#85)
Make a few minor improvements in channel names and other coding style and rename the files because the current names are pretty redundant and in some cases not very representative about what the example is about.
2 parents 0719190 + a982f89 commit 3990011

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ async def run() -> None:
195195
)
196196

197197
# Channel to communicate between actors.
198-
request_channel = Broadcast[List[float]]("RequestChannel", resend_latest=True)
198+
power_dist_req_chan = Broadcast[List[float]](
199+
"power-distribing-req", resend_latest=True
200+
)
199201

200202
# You should get components from ComponentGraph, not from the api.
201203
# It is faster and and non blocking approach.
@@ -205,15 +207,15 @@ async def run() -> None:
205207
)
206208

207209
service_actor = DecisionMakingActor(
208-
power_channel=request_channel.new_receiver(),
210+
power_channel=power_dist_req_chan.new_receiver(),
209211
power_distributor_handle=power_distributor_channels[
210212
sending_actor_id
211213
].client_handle,
212214
batteries={battery.component_id for battery in batteries},
213215
)
214216

215217
client_actor = DataCollectingActor(
216-
request_channel=request_channel.new_sender(),
218+
request_channel=power_dist_req_chan.new_sender(),
217219
active_power_data=microgrid_data_channels[
218220
"batteries_active_power"
219221
].new_receiver(name="DecisionMakingActor"),
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ async def run() -> None: # pylint: disable=too-many-locals
2525
"""Run main functions that initializes and creates everything."""
2626
await microgrid.initialize(HOST, PORT)
2727

28-
channel_registry = ChannelRegistry(name="Microgrid Channel Registry")
28+
channel_registry = ChannelRegistry(name="data-registry")
2929

3030
# Create a channels for sending/receiving subscription requests
31-
data_source_request_channel = Broadcast[ComponentMetricRequest](
32-
"Data Source Request Channel"
33-
)
31+
data_source_request_channel = Broadcast[ComponentMetricRequest]("data-source")
3432
data_source_request_sender = data_source_request_channel.new_sender()
3533
data_source_request_receiver = data_source_request_channel.new_receiver()
3634

37-
resampling_actor_request_channel = Broadcast[ComponentMetricRequest](
38-
"Resampling Actor Request Channel"
39-
)
35+
resampling_actor_request_channel = Broadcast[ComponentMetricRequest]("resample")
4036
resampling_actor_request_sender = resampling_actor_request_channel.new_sender()
4137
resampling_actor_request_receiver = resampling_actor_request_channel.new_receiver()
4238

@@ -63,7 +59,7 @@ async def run() -> None: # pylint: disable=too-many-locals
6359
# Create subscription requests for each time series id
6460
subscription_requests = [
6561
ComponentMetricRequest(
66-
namespace="Resampling",
62+
namespace="resampling",
6763
component_id=component_id,
6864
metric_id=ComponentMetricId.SOC,
6965
start_time=None,
@@ -79,18 +75,18 @@ async def run() -> None: # pylint: disable=too-many-locals
7975
]
8076
)
8177

82-
# Store sample receivers for each subscription
83-
sample_receiver = MergeNamed(
78+
# Merge sample receivers for each subscription into one receiver
79+
merged_receiver = MergeNamed(
8480
**{
85-
channel_name: channel_registry.new_receiver(channel_name)
86-
for channel_name in map(
87-
lambda req: req.get_channel_name(), subscription_requests
81+
req.get_channel_name(): channel_registry.new_receiver(
82+
req.get_channel_name()
8883
)
84+
for req in subscription_requests
8985
}
9086
)
9187

92-
async for channel_name, msg in sample_receiver:
93-
print(msg)
88+
async for channel_name, msg in merged_receiver:
89+
print(f"{channel_name}: {msg}")
9490

9591

9692
asyncio.run(run())

0 commit comments

Comments
 (0)