Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 43 additions & 44 deletions gridappsd-field-bus-lib/gridappsd/field_interface/agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def __init__(self,
self.description = agent_config['description']
dt = datetime.now()
ts = datetime.timestamp(dt)
self.agent_id = "da_" + self.app_id + "_" + str(int(ts))
if ('context_manager' not in self.app_id):
self.agent_id = "da_" + self.app_id + "_" + str(int(ts))
else:
self.agent_id = downstream_message_bus_def.id+'.context_manager'

self.agent_area_dict = agent_area_dict

if upstream_message_bus_def is not None:
Expand All @@ -87,7 +91,7 @@ def __init__(self,
# self.addressable_equipments = agent_dict['addressable_equipment']
# self.unaddressable_equipments = agent_dict['unaddressable_equipment']

def connect(self):
def _connect(self):
Comment thread
craigpnnl marked this conversation as resolved.

if self.upstream_message_bus is not None:
self.upstream_message_bus.connect()
Expand All @@ -109,6 +113,13 @@ def connect(self):
if ('context_manager' not in self.app_id):
LocalContext.register_agent(self.downstream_message_bus,
self.upstream_message_bus, self)

def disconnect(self):
Comment thread
craigpnnl marked this conversation as resolved.

if self.upstream_message_bus is not None:
self.upstream_message_bus.disconnect()
if self.downstream_message_bus is not None:
self.downstream_message_bus.disconnect()

def subscribe_to_measurement(self):
if self.simulation_id is None:
Expand Down Expand Up @@ -144,18 +155,20 @@ def subscribe_to_messages(self):
self.app_id),
self.on_upstream_message)

_log.debug(
f"Subscribing to message on agents topics: \n {t.field_message_bus_agent_topic(self.downstream_message_bus.id, self.agent_id)} \
\n {t.field_message_bus_agent_topic(self.upstream_message_bus.id, self.agent_id)}"
)
self.downstream_message_bus.subscribe(
t.field_message_bus_agent_topic(self.downstream_message_bus.id,
self.agent_id),
self.on_downstream_message)
self.downstream_message_bus.subscribe(
t.field_message_bus_agent_topic(self.upstream_message_bus.id,
self.agent_id),
self.on_upstream_message)

if ('context_manager' not in self.app_id):
_log.debug(
f"Subscribing to message on agents topics: \n {t.field_message_bus_agent_topic(self.downstream_message_bus.id, self.agent_id)} \
\n {t.field_message_bus_agent_topic(self.upstream_message_bus.id, self.agent_id)}"
)
self.downstream_message_bus.subscribe(
t.field_message_bus_agent_topic(self.downstream_message_bus.id,
self.agent_id),
self.on_downstream_message)
self.upstream_message_bus.subscribe(
t.field_message_bus_agent_topic(self.upstream_message_bus.id,
self.agent_id),
self.on_upstream_message)

def subscribe_to_requests(self):

Expand Down Expand Up @@ -231,22 +244,15 @@ def __init__(self,
agent_config: Dict,
feeder_dict=None,
simulation_id=None):
super(FeederAgent,
self).__init__(upstream_message_bus_def,
super().__init__(upstream_message_bus_def,
downstream_message_bus_def, agent_config,
feeder_dict, simulation_id)
self.feeder_area = None
self.downstream_message_bus_def = downstream_message_bus_def
if self.agent_area_dict is not None:
feeder = cim.Feeder(mRID=self.downstream_message_bus_def.id)
self.feeder_area = DistributedModel(connection=self.connection,
feeder=feeder,
topology=self.agent_area_dict)


def connect(self):
super().connect()
if self.feeder_area is None:
self._connect()

if self.agent_area_dict is not None:
feeder = cim.Feeder(mRID=self.downstream_message_bus_def.id)
self.feeder_area = DistributedModel(connection=self.connection,
feeder=feeder,
Expand All @@ -265,15 +271,10 @@ def __init__(self,
agent_config, switch_area_dict, simulation_id)
self.switch_area = None
self.downstream_message_bus_def = downstream_message_bus_def
if self.agent_area_dict is not None:
self.switch_area = SwitchArea(self.downstream_message_bus_def.id,
self.connection)
self.switch_area.initialize_switch_area(self.agent_area_dict)


def connect(self):
super().connect()
if self.switch_area is None:
self._connect()

if self.agent_area_dict is not None:
self.switch_area = SwitchArea(self.downstream_message_bus_def.id,
self.connection)
self.switch_area.initialize_switch_area(self.agent_area_dict)
Expand All @@ -291,18 +292,17 @@ def __init__(self,
agent_config, secondary_area_dict, simulation_id)
self.secondary_area = None
self.downstream_message_bus_def = downstream_message_bus_def
if self.agent_area_dict is not None:
self.secondary_area = SecondaryArea(self.downstream_message_bus_def.id,
self.connection)
self.secondary_area.initialize_secondary_area(self.agent_area_dict)


def connect(self):
super().connect()
if self.secondary_area is None:
self._connect()

if self.agent_area_dict is not None:
if len(self.agent_area_dict['addressable_equipment']) == 0:
_log.warn(f"No addressable equipment in the secondary area with down stream message bus id: {self.downstream_message_bus.id}.")

self.secondary_area = SecondaryArea(self.downstream_message_bus_def.id,
self.connection)
self.secondary_area.initialize_secondary_area(self.agent_area_dict)



class CoordinatingAgent:
Expand Down Expand Up @@ -337,12 +337,11 @@ def __init__(self,

# self.subscribe_to_feeder_bus()

def spawn_distributed_agent(self, distributed_agent: DistributedAgent):

''' def spawn_distributed_agent(self, distributed_agent: DistributedAgent):
distributed_agent.connect()
self.distributed_agents.append(distributed_agent)


'''
def on_control(self, control):
device_id = control.get('device')
command = control.get('command')
Expand Down
5 changes: 3 additions & 2 deletions gridappsd-field-bus-lib/gridappsd/field_interface/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gridappsd.field_interface.interfaces import FieldMessageBus
from gridappsd_field_interface.interfaces import FieldMessageBus
import dataclasses
import gridappsd.topics as t
import json
Expand All @@ -13,6 +13,7 @@ def get_context_by_feeder(cls, downstream_message_bus: FieldMessageBus, feeder_m
request = {'request_type' : 'get_context',
'modelId': feeder_mrid,
'areaId': area_id}
print(t.context_request_queue(downstream_message_bus.id))
response = downstream_message_bus.get_response(t.context_request_queue(downstream_message_bus.id), request, timeout=10)
return response

Expand All @@ -23,7 +24,7 @@ def get_context_by_message_bus(cls, downstream_message_bus: FieldMessageBus):

"""
request = {'request_type' : 'get_context',
'downstream_message_bus_id': downstream_message_bus.id
'areaId': downstream_message_bus.id
}
return downstream_message_bus.get_response(t.context_request_queue(downstream_message_bus.id), request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ def get_response(self, topic, message, timeout=5):

def disconnect(self):
"""
Disconnect the device from the concrete message bus.
Disconnect from the concrete message bus.
"""
pass
self.gridappsd_obj.disconnect()