def gappsd() -> GridAPPSD: gridappsd = GridAPPSD() LOGGER.info('Gridappsd connected') yield gridappsd gridappsd.disconnect() LOGGER.info('Gridappsd disconnected')
def gridappsd_client(docker_dependencies): with run_gridappsd_container(True): gappsd = GridAPPSD() gappsd.connect() assert gappsd.connected yield gappsd gappsd.disconnect()
def gridappsd_client(request, docker_dependencies): with run_gridappsd_container(stop_after=STOP_CONTAINER_AFTER_TEST): gappsd = GridAPPSD() gappsd.connect() assert gappsd.connected models = gappsd.query_model_names() assert models is not None if request.cls is not None: request.cls.gridappsd_client = gappsd yield gappsd gappsd.disconnect()
def _main(): global appName, sim_id, feeder_mrid, gapps if len(sys.argv) < 2 or '-help' in sys.argv: usestr = '\nUsage: ' + sys.argv[0] + ' #nodes\n' usestr += ''' Optional command line arguments: -help: show this usage message ''' print(usestr, file=sys.stderr, flush=True) exit() appName = sys.argv[0] sim_config_file = './sim_starter/' + sys.argv[1] + '-config.json' gapps = GridAPPSD() with open(sim_config_file) as config_fp: sim_config = json.load(config_fp) # if there is a sys.argv[2] command line argument, that indicates not to start a simulation if len(sys.argv) < 3: print('SIM_STARTER initializing simulation from: ' + sim_config_file, file=sys.stderr, flush=True) sim = Simulation(gapps, sim_config) print('SIM_STARTER about to start simulation...', file=sys.stderr, flush=True) sim.start_simulation(timeout=90) sim_id = sim.simulation_id print('SIM_STARTER simulation started with id: ' + sim_id, file=sys.stderr, flush=True) print(sim_id, flush=True) # need to dump sim_config before passing it on to downstream modules # in order to convert single quotes to double quotes sim_req = json.dumps(sim_config) print(sim_req, flush=True) gapps.disconnect()
def _main(): global sim_id, gapps if len(sys.argv) < 3 or '-help' in sys.argv: usestr = '\nUsage: ' + sys.argv[ 0] + ' simulation_id tap|reg|cap|switch\n' usestr += ''' Optional command line arguments: -help: show this usage message ''' print(usestr, file=sys.stderr, flush=True) exit() gapps = GridAPPSD() sim_id = sys.argv[1] diff = DifferenceBuilder(sim_id) # hardwired for 13assets if sys.argv[2] == 'tap' or sys.argv[2] == 'reg': reg_id = '_A480E0A9-AD2B-4D8E-9478-71C29F738221' # node RG60.2 diff.add_difference(reg_id, 'TapChanger.step', 5, 8) elif sys.argv[2] == 'cap': cap_id = '_28456F60-7196-47E4-9BE6-54F7EAABC04A' # bus 611 diff.add_difference(cap_id, 'ShuntCompensator.sections', 0, 1) else: switch_id = '_4E1B3F09-CB88-4A5E-8198-24490EE7FC58' # between bus 671-692 diff.add_difference(switch_id, 'Switch.open', 1, 0) msg = diff.get_message() print(json.dumps(msg)) publish_to_topic = simulation_input_topic(sim_id) gapps.send(publish_to_topic, json.dumps(msg)) time.sleep(2) gapps.disconnect()
def gappsd(): gappsd = GridAPPSD() yield gappsd gappsd.disconnect() gappsd = None
def run_gridappsd_container(stop_after=True, rebuild_if_present=False) -> Container: """ A contextmanager that uses """ parent_container = get_docker_in_docker() client = docker.from_env() # if there is a parent_container then we need to make sure that it is connected # to the same network as our systems. If not then we need to modify the network # to include the parent container if parent_container: env = DEFAULT_GRIDAPPSD_DOCKER_CONFIG['gridappsd'].get('environment') if env is None: env = {} env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_ADDRESS.name] = 'gridappsd' env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_USER.name] = 'test_app_user' env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_PASSWORD.name] = '4Test' DEFAULT_GRIDAPPSD_DOCKER_CONFIG['gridappsd']['environment'] = env _log.debug(f"Running inside a container environment: {parent_container.name}") network = client.networks.get(NETWORK) has_it = False for x in network.containers: if x.name == parent_container.name: has_it = True _log.debug(f"parent_container {parent_container.name} is connected to the network.") break if not has_it: _log.debug(f"Connecting new container to the network: {parent_container.name}") network.connect(parent_container) else: _log.debug("Not running in a container") containers = Containers(DEFAULT_GRIDAPPSD_DOCKER_CONFIG) gridappsd_container = None try: gridappsd_container = client.containers.get("gridappsd") _log.info(f"{gridappsd_container.name} container found") if rebuild_if_present: gridappsd_container.kill() except docker.errors.NotFound: _log.debug("gridappsd container not found!") try: if gridappsd_container is None: containers.start() # the gridappsd container itself will take a bit to start up. time.sleep(30) tries = 30 while True: tries -= 1 if tries <=0: raise RuntimeError("Couldn't connect to gridappsd server in a timely manner!") try: g = GridAPPSD() if g.connected: _log.info("Connected to gridappsd!") g.disconnect() break except stomp.exception.ConnectFailedException or stomp.exception.NotConnectedException: _log.error("Retesting connection") yield gridappsd_container finally: if stop_after: containers.stop()
class GridAPPSDSimIntegration(BaseSimIntegration): """ The class is responsible for integration with GridAPPSD co-simulation platform. It provides integration support to register configuration, start, stop, publish, receive messages, pause and resume simulation """ def __init__(self, config, pubsub): super(GridAPPSDSimIntegration, self).__init__(config) self._work_callback = None self.config = config self.gridappsd = None self.sim = None self.event_callbacks = {} self.topic_callbacks = {} self.sim_id = None self.username = None self.password = None def register_inputs(self, config=None, callback=None, **kwargs): """ Register configuration parameters with GridAppsD. The config parameters may include but not limited to: - power_system_config - application_config - simulation_config - test_config - service_configs : Register agent callback method :return: """ self.config = config self.username = self.config.pop('username', 'system') self.password = self.config.pop('password', 'manager') self._work_callback = callback def register_event_callbacks(self, callbacks={}): """ Register for event callbacks for event notifications such as - on measurement change - on timestep change - on finish """ _log.debug("Registering for event callbacks") self.event_callbacks = callbacks def register_topic_callbacks(self, callbacks={}): """ Register for any simulation topic callbacks """ _log.debug("Registering for topic callbacks") self.topic_callbacks = callbacks def start_simulation(self, *args, **kwargs): """ Simulation start activities involve: - Creating GridAppsD connection gevent thread - Registering for event callbacks (if specified) - Registering for topic callbacks if specified - Starting simulation based on the input config :return: """ try: self.gridappsd = GridAPPSD(override_threading=self.receiver_thread, username=self.username, password=self.password) _log.debug('Gridappsd connected') _log.debug(f"connection config is: {self.config}") self.sim = Simulation(self.gridappsd, self.config) _log.debug('Gridappsd adding onstart callback') # Register for onstart callback to know if simulation has started self.sim.add_onstart_callback(self.sim_on_start) # Register event callbacks - on measurement, on timestep, on finish for name, cb in self.event_callbacks.items(): if name == 'MEASUREMENT': _log.debug('Gridappsd adding measurement callback') self.sim.add_onmesurement_callback(cb) elif name == 'TIMESTEP': _log.debug('Gridappsd adding timestep callback') self.sim.add_ontimestep_callback(cb) elif name == 'FINISH': _log.debug('Gridappsd adding finish callback') self.sim.add_oncomplete_callback(cb) # Register/Subscribe for simulation topics for topic, cb in self.topic_callbacks: _log.debug('Gridappsd subscribing to topics callback') self.gridappsd.subscribe(topic, cb) # Starting GridAppsD simulation self.sim.start_simulation() _log.debug(f"Gridappsd simulation id: {self.sim.simulation_id}") except stomp.exception.NotConnectedException as ex: _log.error("Unable to connect to GridAPPSD: {}".format(ex)) raise ex def sim_on_start(self, sim): """ Simulation on start callback to get notified when simulation starts """ _log.debug( f"GridAppsD simulation id inside sim_on_start(): {sim.simulation_id}" ) self.sim_id = sim.simulation_id def receiver_thread(self, arg): """ GridAPPSD connection thread """ self._receiver_thread = gevent.threading.Thread(group=None, target=arg) self._receiver_thread.daemon = True # Don't let thread prevent termination self._receiver_thread.start() _log.debug('Gridappsd receiver_thread started!') return self._receiver_thread def publish_to_simulation(self, topic, message, **kwargs): """ Publish message to GridAppsD :param topic: GridAppsD publication topic :param message: message :return: """ self.gridappsd.send(topic, message) def pause_simulation(self, timeout=None, **kwargs): """ Pause the GridAppsD simulation """ if timeout is None: self.sim.pause() else: self.sim.pause(timeout) def resume_simulation(self, *args, **kwargs): """ Resume the GridAppsD simulation """ self.sim.resume() def is_sim_installed(self, **kwargs): """ Flag to indicate if GridAppsD is installed """ return HAS_GAPPSD def stop_simulation(self, *args, **kwargs): """ Stop the simulation if running and disconnect from GridAppsD server :return: """ _log.debug('Stopping the simulation') try: if self.sim_id is not None: self.sim.stop() _log.debug('Disconnect GridAppsd') if self.gridappsd is not None: self.gridappsd.disconnect() except Exception: _log.error("Error stop GridAPPSD simulation")