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
98 changes: 60 additions & 38 deletions gridappsd-field-bus-lib/gridappsd/field_interface/agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@
from datetime import datetime
from typing import Dict

from cimgraph.loaders import ConnectionParameters, gridappsd
from cimgraph.loaders.gridappsd import GridappsdConnection
from cimgraph.models import DistributedModel, SecondaryArea, SwitchArea
from cimgraph.databases import ConnectionParameters
from cimgraph.databases.gridappsd import GridappsdConnection
from cimgraph.models import FeederModel
from cimgraph.models.distributed_area import DistributedArea

from gridappsd import DifferenceBuilder
import gridappsd.topics as t
from gridappsd.field_interface.context import LocalContext
from gridappsd.field_interface.gridappsd_field_bus import GridAPPSDMessageBus
from gridappsd.field_interface.interfaces import (FieldMessageBus,
MessageBusDefinition)


CIM_PROFILE = None
IEC61970_301 = None
cim = None
sparql = None

_log = logging.getLogger(__name__)


def set_cim_profile(cim_profile):
def set_cim_profile(cim_profile: str, iec61970_301: int):
global CIM_PROFILE
global IEC61970_301
global cim
CIM_PROFILE = cim_profile
IEC61970_301 = iec61970_301
cim = importlib.import_module('cimgraph.data_profile.' + cim_profile)
gridappsd.set_cim_profile(cim_profile)


@dataclass
Expand Down Expand Up @@ -57,9 +63,12 @@ def __init__(self,
self.simulation_id = simulation_id
self.context = None

#TODO: Change params and connection to local connection
self.params = ConnectionParameters()
# TODO: Change params and connection to local connection
self.params = ConnectionParameters(cim_profile=CIM_PROFILE,
iec61970_301=IEC61970_301)

self.connection = GridappsdConnection(self.params)
self.connection.cim_profile = cim_profile

self.app_id = agent_config['app_id']
self.description = agent_config['description']
Expand All @@ -68,7 +77,7 @@ def __init__(self,
if ('context_manager' not in self.app_id):
self.agent_id = "da_" + self.app_id
else:
self.agent_id = downstream_message_bus_def.id+'.context_manager'
self.agent_id = downstream_message_bus_def.id + '.context_manager'

self.agent_area_dict = agent_area_dict

Expand All @@ -88,7 +97,7 @@ def __init__(self,

# self.context = ContextManager.get(self.feeder_id, self.area_id)

#if agent_dict is not None:
# if agent_dict is not None:
# self.addressable_equipments = agent_dict['addressable_equipment']
# self.unaddressable_equipments = agent_dict['unaddressable_equipment']

Expand All @@ -104,7 +113,7 @@ def _connect(self):

if ('context_manager' not in self.app_id):
self.agent_id = "da_" + self.app_id + "_" + self.downstream_message_bus.id

if self.agent_area_dict is None:
context = LocalContext.get_context_by_message_bus(
self.downstream_message_bus)
Expand All @@ -117,7 +126,7 @@ 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):

if self.upstream_message_bus is not None:
Expand Down Expand Up @@ -159,7 +168,6 @@ def subscribe_to_messages(self):
self.app_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)} \
Expand Down Expand Up @@ -220,22 +228,26 @@ def get_registration_details(self):
self.upstream_message_bus.id,
self.downstream_message_bus.id)
return dataclasses.asdict(details)

def publish_downstream(self, message):
self.downstream_message_bus.send(t.field_message_bus_topic(self.downstream_message_bus.id), message)

def publish_upstream(self, message):
self.upstream_message_bus.send(t.field_message_bus_topic(self.upstream_message_bus.id), message)
self.downstream_message_bus.send(
t.field_message_bus_topic(self.downstream_message_bus.id), message)

def publish_upstream(self, message):
self.upstream_message_bus.send(
t.field_message_bus_topic(self.upstream_message_bus.id), message)

def send_control_command(self, differenceBuilder : DifferenceBuilder):
def send_control_command(self, differenceBuilder: DifferenceBuilder):
if self.simulation_id is not None:
LocalContext.send_control_command(self.downstream_message_bus, differenceBuilder)
LocalContext.send_control_command(self.downstream_message_bus,
differenceBuilder)

'''
TODO This block needs to be tested with device interface
else:
self.downstream_message_bus.send(devie_interface_topic, differenceBuilder)
'''
'''


''' TODO this has not been implemented yet, so we are commented them out for now.
# not all agent would use this
Expand All @@ -254,19 +266,21 @@ def __init__(self,
agent_config: Dict,
feeder_dict=None,
simulation_id=None):
super().__init__(upstream_message_bus_def,
downstream_message_bus_def, agent_config,
feeder_dict, simulation_id)
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

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,
topology=self.agent_area_dict)
feeder = cim.EquipmentContainer(
mRID=self.downstream_message_bus_def.id)
self.feeder_area = DistributedArea(connection=self.connection,
container=feeder,
distributed=True)
self.feeder_area.build_from_topo_message(
topology_dict=self.agent_area_dict, centralized_graph=None)


class SwitchAreaAgent(DistributedAgent):
Expand All @@ -285,9 +299,13 @@ def __init__(self,
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)
container = cim.EquipmentContainer(
mRID=self.downstream_message_bus_def.id)
self.switch_area = DistributedArea(container=container,
connection=self.connection,
distributed=True)
self.switch_area.build_from_topo_message(
topology_dict=self.agent_area_dict, centralized_graph=None)


class SecondaryAreaAgent(DistributedAgent):
Expand All @@ -307,12 +325,16 @@ def __init__(self,

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)

_log.warning(
f"No addressable equipment in the secondary area with down stream message bus id: {self.downstream_message_bus.id}."
)
container = cim.EquipmentContainer(
mRID=self.downstream_message_bus_def.id)
self.secondary_area = DistributedArea(container=container,
connection=self.connection,
distributed=True)
self.secondary_area.build_from_topo_message(
topology_dict=self.agent_area_dict, centralized_graph=None)


class CoordinatingAgent:
Expand All @@ -336,7 +358,7 @@ def __init__(self,
self.system_message_bus = GridAPPSDMessageBus(system_message_bus_def)
self.system_message_bus.connect()

#This will change when we have multiple feeders per system
# This will change when we have multiple feeders per system
self.downstream_message_bus = self.system_message_bus

# self.context = ContextManager.getContextByFeeder(self.feeder_id)
Expand Down
Loading