Skip to content

Commit

Permalink
Add gridappsd query
Browse files Browse the repository at this point in the history
  • Loading branch information
craig8 committed Jun 6, 2024
1 parent 7b4e04c commit 9ff85e8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 30 deletions.
17 changes: 10 additions & 7 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ cleanse_storage: true
# This directory will be created if it does not exist.
storage_path: data_store

# gridappsd:
# # GridAPPS-D API endpoint
# api_endpoint: tcp://127.0.0.1:61613
# # GridAPPS-D username
# username: system
# # GridAPPS-D password
# password: manager
gridappsd:
# GridAPPS-D API endpoint
address: 'localhost'
port: 61613
model_name: 'ieee13nodeckt'

# GridAPPS-D username
username: system
# GridAPPS-D password
password: manager

# End Device
devices:
Expand Down
Binary file added deps/cim_graph-1.0.0.dev1-py3-none-any.whl
Binary file not shown.
14 changes: 14 additions & 0 deletions ieee_2030_5/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from ieee_2030_5.config import InvalidConfigFile, ServerConfiguration
from ieee_2030_5.data.indexer import add_href
from ieee_2030_5.server.server_constructs import initialize_2030_5
from ieee_2030_5.adapters.gridappsd_adapter import GridAPPSDAdapter

_log = logging.getLogger()

Expand Down Expand Up @@ -203,6 +204,19 @@ def _main():
create_certs = not opts.no_create_certs
tls_repo = get_tls_repository(config, create_certs)

if config.gridappsd is not None and create_certs:

from gridappsd import GridAPPSD

gapps = GridAPPSD(stomp_address=config.gridappsd.address,
stomp_port=config.gridappsd.port,
username=config.gridappsd.username,
password=config.gridappsd.password)
assert gapps.connected

gridappsd_adpt = GridAPPSDAdapter(gapps, config.gridappsd.model_name)
gridappsd_adpt.create_der_client_certificates(tls_repo)

if opts.show_lfdi:
for cn in config.devices:
sys.stdout.write(f"{cn.id} {tls_repo.lfdi(cn.id)}\n")
Expand Down
40 changes: 40 additions & 0 deletions ieee_2030_5/adapters/gridappsd_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from attrs import define, field
from gridappsd import GridAPPSD
from ieee_2030_5.certs import TLSRepository


@define
class GridAPPSDAdapter:
gapps: GridAPPSD
model_name: str
model_id: str = field(default=None)

def create_der_client_certificates(self, tls: TLSRepository):
models = self.gapps.query_model_info()
for m in models['data']['models']:
if m['modelName'] == self.model_name:
self.model_id = m['modelId']
break
if not self.model_id:
raise ValueError(f"Model {self.model_name} not found")

from cimgraph.data_profile import CIM_PROFILE
import cimgraph.data_profile.rc4_2021 as cim
from cimgraph.models import FeederModel
from cimgraph.databases.gridappsd import GridappsdConnection
from cimgraph.databases import ConnectionParameters

cim_profile = CIM_PROFILE.RC4_2021.value
iec = 7
params = ConnectionParameters(cim_profile=cim_profile, iec61970_301=iec)

conn = GridappsdConnection(params)
conn.cim_profile = cim_profile
feeder = cim.Feeder(mRID=self.model_id)

network = FeederModel(connection=conn, container=feeder, distributed=False)

network.get_all_edges(cim.PowerElectronicsConnection)

for inv in network.graph[cim.PowerElectronicsConnection].values():
tls.create_cert(inv.mRID)
53 changes: 30 additions & 23 deletions ieee_2030_5/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def __hash__(self):
@dataclass_json
@dataclass
class GridappsdConfiguration:
model_name: str
address: str = 'localhost'
port: int = 61613
username: str = 'system'
password: str = 'manager'
field_bus_config: Optional[str] = None
field_bus_def: Optional[MessageBusDefinition] = None
feeder_id_file: Optional[str] = None
Expand Down Expand Up @@ -303,29 +308,31 @@ def __post_init__(self):

if self.gridappsd:
self.gridappsd = GridappsdConfiguration.from_dict(self.gridappsd)
if Path(self.gridappsd.feeder_id_file).exists():
self.gridappsd.feeder_id = Path(self.gridappsd.feeder_id_file).read_text().strip()
if Path(self.gridappsd.simulation_id_file).exists():
self.gridappsd.simulation_id = Path(
self.gridappsd.simulation_id_file).read_text().strip()

if not self.gridappsd.feeder_id:
raise ValueError(
"Feeder id from gridappsd not found in feeder_id_file nor was specified "
"in gridappsd config section.")

# TODO: This might not be the best place for this manipulation
self.gridappsd.field_bus_def = MessageBusDefinition.load(
self.gridappsd.field_bus_config)
self.gridappsd.field_bus_def.id = self.gridappsd.feeder_id

_log.info("Gridappsd Configuration For Simulation")
_log.info(f"feeder id: {self.gridappsd.feeder_id}")
if self.gridappsd.simulation_id:
_log.info(f"simulation id: {self.gridappsd.simulation_id}")
else:
_log.info("no simulation id")
_log.info("x" * 80)

# TODO Configuration for field bus here
# if Path(self.gridappsd.feeder_id_file).exists():
# self.gridappsd.feeder_id = Path(self.gridappsd.feeder_id_file).read_text().strip()
# if Path(self.gridappsd.simulation_id_file).exists():
# self.gridappsd.simulation_id = Path(
# self.gridappsd.simulation_id_file).read_text().strip()

# if not self.gridappsd.feeder_id:
# raise ValueError(
# "Feeder id from gridappsd not found in feeder_id_file nor was specified "
# "in gridappsd config section.")

# # TODO: This might not be the best place for this manipulation
# self.gridappsd.field_bus_def = MessageBusDefinition.load(
# self.gridappsd.field_bus_config)
# self.gridappsd.field_bus_def.id = self.gridappsd.feeder_id

# _log.info("Gridappsd Configuration For Simulation")
# _log.info(f"feeder id: {self.gridappsd.feeder_id}")
# if self.gridappsd.simulation_id:
# _log.info(f"simulation id: {self.gridappsd.simulation_id}")
# else:
# _log.info("no simulation id")
# _log.info("x" * 80)

# if self.field_bus_config:
# self.field_bus_def = MessageBusDefinition.load(self.field_bus_config)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ gridappsd-field-bus = {version = "^2024.4.1a0", allow-prereleases = true}
flet = "^0.22.1"

xsdata = {extras = ["lxml"], version = "^22.5"}
cim-graph = {path = "deps/cim_graph-1.0.0.dev1-py3-none-any.whl"}
[tool.poetry.group.dev.dependencies]
m2r2 = "^0.3.2"
pytest = "^7.1.3"
Expand Down

0 comments on commit 9ff85e8

Please sign in to comment.