-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_sample_distributed_app_without_coord_agent.py
More file actions
227 lines (169 loc) · 8.51 KB
/
run_sample_distributed_app_without_coord_agent.py
File metadata and controls
227 lines (169 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import json
import logging
import sys
import time
from typing import Dict
from cimgraph.data_profile import CIM_PROFILE
from pathlib import Path
import gridappsd_field_bus.field_interface.agents.agents as agents_mod
import gridappsd.topics as t
from gridappsd_field_bus.field_interface.agents import (CoordinatingAgent,
FeederAgent,
SecondaryAreaAgent,
SwitchAreaAgent)
from gridappsd_field_bus.field_interface.context import LocalContext
from gridappsd_field_bus.field_interface.interfaces import MessageBusDefinition
import sample_queries as example
cim_profile = CIM_PROFILE.CIMHUB_2023.value
agents_mod.set_cim_profile(cim_profile=cim_profile, iec61970_301=8)
cim = agents_mod.cim
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('goss').setLevel(logging.ERROR)
logging.getLogger('stomp.py').setLevel(logging.ERROR)
_log = logging.getLogger(__name__)
class SampleCoordinatingAgent(CoordinatingAgent):
def __init__(self, system_message_bus_def, simulation_id=None):
super().__init__(system_message_bus_def, simulation_id)
class SampleFeederAgent(FeederAgent):
def __init__(self,
upstream_message_bus_def: MessageBusDefinition,
downstream_message_bus_def: MessageBusDefinition,
agent_config,
feeder_dict: Dict = None,
simulation_id: str = None):
super().__init__(upstream_message_bus_def, downstream_message_bus_def,
agent_config, feeder_dict, simulation_id)
#TODO remove first four
def on_measurement(self, headers: Dict, message) -> None:
_log.debug(
f"measurement: {self.__class__.__name__}.{headers.get('destination')}"
)
with open("feeder.txt", "a") as fp:
fp.write(json.dumps(message))
#print(message)
def on_upstream_message(self, headers: Dict, message) -> None:
print(f"Received message from upstream message bus: {message}")
def on_downstream_message(self, headers: Dict, message) -> None:
print(f"Received message from downstream message bus: {message}")
def on_request(self, message_bus, headers: Dict, message):
print(f"Received request: {message}")
reply_to = headers['reply-to']
message_bus.send(reply_to, 'this is a reponse')
class SampleSwitchAreaAgent(SwitchAreaAgent):
def __init__(self,
upstream_message_bus_def: MessageBusDefinition,
downstream_message_bus_def: MessageBusDefinition,
agent_config,
switch_area_dict: Dict = None,
simulation_id: str = None):
super().__init__(upstream_message_bus_def, downstream_message_bus_def,
agent_config, switch_area_dict, simulation_id)
def on_measurement(self, headers: Dict, message):
_log.debug(
f"measurement: {self.__class__.__name__}.{headers.get('destination')}"
)
with open("switch_area.txt", "a") as fp:
fp.write(json.dumps(message))
#print(message)
class SampleSecondaryAreaAgent(SecondaryAreaAgent):
def __init__(self,
upstream_message_bus_def: MessageBusDefinition,
downstream_message_bus_def: MessageBusDefinition,
agent_config,
secondary_area_dict: Dict = None,
simulation_id: str = None):
super().__init__(upstream_message_bus_def, downstream_message_bus_def,
agent_config, secondary_area_dict, simulation_id)
def on_measurement(self, headers: Dict, message):
_log.debug(
f"measurement: {self.__class__.__name__}.{headers.get('destination')}"
)
with open("secondary.txt", "a") as fp:
fp.write(json.dumps(message))
def _main():
agent_config = {
"app_id": "sample_app_without_coord_agent",
"description":
"This is a GridAPPS-D sample distribution application agent"
}
simulation_id_path = Path("simulation.id.txt")
if not simulation_id_path.exists():
print(
"Simulation id not written, please execute `python run_simulation.py` before executing this script."
)
sys.exit(0)
simulation_id = simulation_id_path.read_text().strip()
system_message_bus_def = MessageBusDefinition.load(
"config_files_simulated/system-message-bus.yml")
feeder_message_bus_def = MessageBusDefinition.load(
"config_files_simulated/feeder-message-bus.yml")
#TODO: create access control for agents for different layers
feeder_agent = SampleFeederAgent(system_message_bus_def,
feeder_message_bus_def, agent_config,
None, simulation_id)
# create switch area distributed agents
switch_areas = feeder_agent.agent_area_dict['switch_areas']
for sw_index, switch_area in enumerate(switch_areas):
switch_area_message_bus_def = MessageBusDefinition.load(
f"config_files_simulated/switch_area_message_bus_{sw_index}.yml")
print("Creating switch area agent " +
str(switch_area['message_bus_id']))
switch_area_agent = SampleSwitchAreaAgent(feeder_message_bus_def,
switch_area_message_bus_def,
agent_config, None,
simulation_id)
# Get all the attributes of the equipments in the switch area from the model
# EXAMPLE 1 - Get phase, bus info about ACLineSegments
example.get_lines_buses(switch_area_agent.switch_area)
# EXAMPLE 2 - Get all line impedance data
example.get_line_impedances(switch_area_agent.switch_area)
# EXAMPLE 3 - Sort all line impedance by line phase:
example.sort_impedance_by_line(switch_area_agent.switch_area)
# Example 4 - Sort all lines by impedance
example.sort_line_by_impedance(switch_area_agent.switch_area)
# Example 5 - Get TransformerTank impedances
example.get_tank_impedances(switch_area_agent.switch_area)
# Example 6 - Get inverter buses and phases
example.get_inverter_buses(switch_area_agent.switch_area)
# Example 7 - Get load buses and phases
example.get_load_buses(switch_area_agent.switch_area)
service_agent_id = None
response = LocalContext.get_agents(
switch_area_agent.downstream_message_bus)
_log.info(f"Agents: {response}")
for agent_id, agent_details in response.items():
if agent_details['app_id'] == 'local_service':
service_agent_id = agent_id
request_queue = t.field_agent_request_queue(
switch_area_agent.downstream_message_bus.id, service_agent_id)
print(request_queue)
response = switch_area_agent.downstream_message_bus.get_response(
request_queue, {'test': 'test request'})
print(f'Response from service agent: {response}')
# create secondary area distributed agents
for sec_index, secondary_area in enumerate(
switch_area['secondary_areas']):
secondary_area_message_bus_def = MessageBusDefinition.load(
f"config_files_simulated/secondary_area_message_bus_{sw_index}_{sec_index}.yml"
)
print("Creating secondary area agent " +
str(secondary_area['message_bus_id']))
secondary_area_agent = SampleSecondaryAreaAgent(
switch_area_message_bus_def, secondary_area_message_bus_def,
agent_config, None, simulation_id)
if len(secondary_area_agent.secondary_area.addressable_equipment) == 0:
_log.info(f"No addressable equipment in the area {secondary_area_agent.downstream_message_bus.id}. Disconnecting the agent.")
secondary_area_agent.disconnect()
else:
# Example 6 - Get inverter buses and phases
example.get_inverter_buses(secondary_area_agent.secondary_area)
# Example 7 - Get load buses and phases
example.get_load_buses(secondary_area_agent.secondary_area)
while True:
try:
time.sleep(0.1)
except KeyboardInterrupt:
print("Exiting sample")
break
if __name__ == "__main__":
_main()