Esempio n. 1
0
def _main():
    _log.debug("Starting application")
    print(
        "Application starting!!!-------------------------------------------------------"
    )
    global message_period

    # arguments to be passed
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request", help="Simulation Request")
    parser.add_argument(
        "--message_period",
        help="How often the sample app will send open/close capacitor message.",
        default=DEFAULT_MESSAGE_PERIOD)
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    message_period = int(opts.message_period)
    sim_request = json.loads(opts.request.replace("\'", ""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    _log.debug("Model mrid is: {}".format(model_mrid))

    # Interaction with the web-based GridAPPSD interface
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())

    # the three lines (uncommented) below are from Shiva
    '''
    The Powergrid Model Data Manager API allows you to query the powergrid model data store.
    
    Query request should be sent on following queue: 
    goss.gridappsd.process.request.data.powergridmodel
    
    i.e. if I want any powergrid component data then the query above has to be addressed
    '''
    topic = "goss.gridappsd.process.request.data.powergridmodel"

    # returns the MRID for AC lines and switch
    ACline, obj_msr_loadsw, obj_msr_reg, switches, regulators = get_meas_mrid(
        gapps, model_mrid, topic)

    # print("\n ************ ACLine ********* \n")
    # print(ACline)

    # print("\n ************ obj_msr_loadsw ********* \n")
    # print(obj_msr_loadsw)
    # print(sh)

    # toggling the switch ON and OFF
    toggler = NodalVoltage(opts.simulation_id, gapps, ACline, obj_msr_loadsw,
                           obj_msr_reg, switches, regulators)

    # gapps.subscribe calls the on_message function
    gapps.subscribe(listening_to_topic, toggler)
    while True:
        time.sleep(0.1)
Esempio n. 2
0
def _main():
    parser = argparse.ArgumentParser()
    parser.add_argument("simulation_id",
                        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request",
                        help="Simulation Request")
    # These are now set through the docker container interface via env variables or defaulted to
    # proper values.
    #
    # parser.add_argument("-u", "--user", default="system",
    #                     help="The username to authenticate with the message bus.")
    # parser.add_argument("-p", "--password", default="manager",
    #                     help="The password to authenticate with the message bus.")
    # parser.add_argument("-a", "--address", default="127.0.0.1",
    #                     help="tcp address of the mesage bus.")
    # parser.add_argument("--port", default=61613, type=int,
    #                     help="the stomp port on the message bus.")
    #
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    #TODO: read this from simulation request as below 
    # message_interval = sim_request["service_config"]["voltage-violation"]["message_interval"]
    message_interval = 5
    sim_request = json.loads(opts.request.replace("\'",""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    gapps = GridAPPSD(opts.simulation_id, address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(), password=utils.get_gridappsd_pass())
    nominal_voltage_dict = get_nominal_voltage(gapps, model_mrid)
    subscriber = SimulationSubscriber(opts.simulation_id, gapps, nominal_voltage_dict, message_interval)
    gapps.subscribe(listening_to_topic, subscriber)
    while True:
        time.sleep(0.1)
Esempio n. 3
0
def test_can_start_gridappsd_within_dependency_context_manager_all_cleanup():

    Containers.reset_all_containers()

    with run_dependency_containers(True) as cont:
        # True in this method will remove the containsers
        with run_gridappsd_container(True) as dep_con:
            # Default cleanup is true within run_gridappsd_container method
            timeout = 0
            gapps = None
            time.sleep(10)
            while timeout < 30:
                try:
                    gapps = GridAPPSD()
                    gapps.connect()
                    break
                except:
                    time.sleep(1)
                    timeout += 1

            assert gapps
            assert gapps.connected

    # Filter out the two config containers that we start up for volume data.
    containers = [
        x.name for x in Containers.container_list() if "config" not in x.name
    ]
    assert not len(containers)
Esempio n. 4
0
def handle_request(headers, message):
    global openSW

    if lock_flag:
        print("MICROSERVICES waiting for lock to be opened", flush=True)
        print("MICROSERVICES waiting for lock to be opened",
              file=logfile,
              flush=True)

    while lock_flag:
        time.sleep(0.1)

    print("MICROSERVICES I got the request", flush=True)
    print("MICROSERVICES I got the request", file=logfile, flush=True)
    gapps = GridAPPSD()

    if message['requestType'] == 'LOOPS':
        out_topic = "/topic/goss.gridappsd.model-validator.topology.out"
        if message['modelType'] == 'STATIC':
            openSW = []
            response = find_all_cycles()
        else:
            response = find_all_cycles()
    elif message['requestType'] == 'GRAPH':
        out_topic = "/topic/goss.gridappsd.model-validator.graph.out"
        response = graph()
    elif message['requestType'] == 'ISOLATED_SECTIONS':
        out_topic = "/topic/goss.gridappsd.model-validator.topology.out"
        response = {'Is the graph connected': connected()}
    else:
        response = 'Invalid request type'

    print("MICROSERVICES I am sending the response", flush=True)
    print("MICROSERVICES I am sending the response", file=logfile, flush=True)
    gapps.send(out_topic, response)
Esempio n. 5
0
def _main():
    _log.debug("Starting application")
    print(
        "Application starting!!!-------------------------------------------------------"
    )
    global message_period
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request", help="Simulation Request")
    parser.add_argument(
        "--message_period",
        help="How often the sample app will send open/close capacitor message.",
        default=DEFAULT_MESSAGE_PERIOD)
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    message_period = int(opts.message_period)
    sim_request = json.loads(opts.request.replace("\'", ""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    _log.debug("Model mrid is: {}".format(model_mrid))
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())
    topic = "goss.gridappsd.process.request.data.powergridmodel"

    # Run several query to extract the model information
    ACline, obj_msr_loadsw = get_meas_mrid(gapps, model_mrid, topic)

    # Subscribe to simulation output and interface to platform
    toggler = LearnPlatform(opts.simulation_id, gapps, ACline, obj_msr_loadsw)
    gapps.subscribe(listening_to_topic, toggler)
    while True:
        time.sleep(0.1)
Esempio n. 6
0
    def __init__(self, gad: GridAPPSD, sim_id: str, sim_start_ts: int,
                 log_interval=60):
        """Initialize attributes, subscribe to the simulation log.

        :param gad: Initialized gridappsd.GridAPPSD object.
        :param sim_id: Simulation ID of the simulation to track.
        :param sim_start_ts: Simulation start timestamp in seconds since
            the epoch.
        :param log_interval: How many simulation seconds in between
            logging the current simulation time.
        """
        # Setup logging.
        self.log = logging.getLogger(self.__class__.__name__)

        # Simply set the simulation starting time as an attribute.
        self.sim_start_ts = sim_start_ts
        self.log_interval = log_interval

        # Use attributes for tracking the simulation time and current
        # time (as indicated by the most recent message).
        self.sim_time = None
        self.time_step = None
        self.msg_time = None
        self.last_log_time = None

        # Compile a regular expression for extracting the time from the
        # message.
        self.regexp = re.compile('(?:incrementing to )([0-9]+)')

        # Subscribe to the simulation log.
        gad.subscribe(topic=topics.simulation_log_topic(sim_id),
                      callback=self._on_message)

        self.log.info('SimulationClock configured and initialized.')
Esempio n. 7
0
def _main():
    _log.debug("Starting application")
    print(
        "Application starting-------------------------------------------------------"
    )
    global message_period
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request", help="Simulation Request")
    parser.add_argument(
        "--message_period",
        help="How often the sample app will send open/close capacitor message.",
        default=DEFAULT_MESSAGE_PERIOD)
    # These are now set through the docker container interface via env variables or defaulted to
    # proper values.
    #
    # parser.add_argument("-u", "--user", default="system",
    #                     help="The username to authenticate with the message bus.")
    # parser.add_argument("-p", "--password", default="manager",
    #                     help="The password to authenticate with the message bus.")
    # parser.add_argument("-a", "--address", default="127.0.0.1",
    #                     help="tcp address of the mesage bus.")
    # parser.add_argument("--port", default=61613, type=int,
    #                     help="the stomp port on the message bus.")
    #
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    print(listening_to_topic)
    message_period = int(opts.message_period)
    sim_request = json.loads(opts.request.replace("\'", ""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    print("\n \n The model running is IEEE 123 node with MRID:")
    print(model_mrid)

    _log.debug("Model mrid is: {}".format(model_mrid))
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())

    # Get measurement MRIDS for primary nodes in the feeder
    topic = "goss.gridappsd.process.request.data.powergridmodel"
    message = {
        "modelId": model_mrid,
        "requestType": "QUERY_OBJECT_MEASUREMENTS",
        "resultFormat": "JSON",
        "objectType": "ACLineSegment"
    }
    obj_msr_loads = gapps.get_response(topic, message, timeout=90)
    # print(obj_msr_loads)
    with open('measid.json', 'w') as json_file:
        json.dump(obj_msr_loads, json_file)

    toggler = SwitchingActions(opts.simulation_id, gapps, obj_msr_loads)
    print("Now subscribing")
    gapps.subscribe(listening_to_topic, toggler)
    while True:
        time.sleep(0.1)
Esempio n. 8
0
def _main():
    from gridappsd import utils
    _log.info("Starting application")
    _log.info(str(sys.argv))
    _log.info("Args ")
    for arg in sys.argv[1:]:
        _log.info(type(arg))
        _log.info(arg)

    # _log.info("Run local only -JEFF")
    # exit(0)
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request", help="Simulation Request")
    parser.add_argument("opt", help="opt")
    opts = parser.parse_args()
    _log.info(opts)
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    print(opts.request)
    sim_request = json.loads(opts.request.replace("\'", ""))
    model_mrid = sim_request['power_system_config']['Line_name']
    start_time = sim_request['simulation_config']['start_time']
    app_config = sim_request["application_config"]["applications"]
    print(app_config)

    # app_config = [json.loads(app['config_string']) for app in app_config if app['name'] == 'solar_forecasting_app'][0]
    app = [
        app for app in app_config if app['name'] == 'solar_forecasting_app'
    ][0]
    print(app)
    if app['config_string']:
        # app_config = json.loads(app['config_string'])
        app_config = json.loads(app['config_string'].replace(u'\u0027', '"'))
    else:
        app_config = {'run_freq': 60, 'run_on_host': False}

    ## Run the docker container. WOOT!
    if 'run_on_host' in app_config and app_config['run_on_host']:
        exit(0)

    _log.info("Model mrid is: {}".format(model_mrid))
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())

    print(listening_to_topic)
    # gapps = GridAPPSD(opts.simulation_id)
    solar_forecast = Solar_Forecast(opts.simulation_id, gapps, model_mrid,
                                    start_time, app_config)
    gapps.subscribe(listening_to_topic, solar_forecast)
    # gapps.subscribe(ghi_weather_topic, solar_forecast)

    # ls_output = subprocess.Popen(["python solar_forecasting/util/post_goss_ghi.py","1357048800","720" , "10"], stdout=subprocess.PIPE)

    while True:
        time.sleep(0.1)
Esempio n. 9
0
class GridAPPSDVolttron(Agent):
    """
    Document agent constructor here.
    """
    def __init__(self, setting1=1, setting2="some/random/topic", **kwargs):
        super(GridAPPSDVolttron, self).__init__(**kwargs)
        self._gapps = None
        self._publish_event = None
        self._test_topic = "/topic/data"
        self._receiver_thread = None

    def receiver_thread(self, arg):
        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()
        return self._receiver_thread

    def publish_next(self):
        _log.debug("Publishing next")
        if self._publish_event is not None:
            self._publish_event.cancel()
        self._gapps.send(self._test_topic, "foo bar")
        if self._gapps.connected:
            self._gapps.send("/queue/test", "foo bar")
        else:
            print("Not connected")
        now = get_aware_utc_now()
        print(utils.get_utc_seconds_from_epoch(now))
        next_update_time = now + datetime.timedelta(seconds=5)
        _log.debug(f'Scheduling nex time {next_update_time}')
        self._publish_event = self.core.schedule(next_update_time,
                                                 self.publish_next)
        _log.debug(f'After scheduling next time {next_update_time}')
        gevent.sleep(0.1)

    @Core.receiver("onstart")
    def onstart(self, sender, **kwargs):
        self._gapps = GridAPPSD(override_threading=self.receiver_thread)

        # goss_log_level=logging.DEBUG)
        # stomp_log_level=logging.DEBUG)

        def message_published(headers, body):
            _log.debug(f"Received from gridappsd {headers}, body: {body}")
            self.vip.pubsub.publish('pubsub',
                                    "data/foo",
                                    headers=headers,
                                    message=body)

        self._gapps.subscribe(self._test_topic, message_published)
        gevent.sleep(0.1)
        self.publish_next()

    @Core.receiver("onstop")
    def onstop(self, sender, **kwargs):
        print("Stopping data!")

        # self._gapps.disconnect()
        print("After disconnect")
Esempio n. 10
0
def gappsd() -> GridAPPSD:
    gridappsd = GridAPPSD()
    LOGGER.info('Gridappsd connected')

    yield gridappsd

    gridappsd.disconnect()
    LOGGER.info('Gridappsd disconnected')
Esempio n. 11
0
def _main_local():
    simulation_id =1543123248
    listening_to_topic = simulation_output_topic(simulation_id)

    model_mrid = '_E407CBB6-8C8D-9BC9-589C-AB83FBF0826D'

    gapps = GridAPPSD(simulation_id)
    # __GRIDAPPSD_URI__ = os.environ.get("GRIDAPPSD_URI", "localhost:61613")
    # gapps = GridAPPSD(simulation_id, address=__GRIDAPPSD_URI__)
    solar_forecast = Solar_Forecast(simulation_id, gapps, model_mrid, 1357048800)
    gapps.subscribe(ghi_weather_topic, solar_forecast)

    while True:
        time.sleep(0.1)
Esempio n. 12
0
    def onstart(self, sender, **kwargs):
        self._gapps = GridAPPSD(override_threading=self.receiver_thread)

        # goss_log_level=logging.DEBUG)
        # stomp_log_level=logging.DEBUG)

        def message_published(headers, body):
            _log.debug(f"Received from gridappsd {headers}, body: {body}")
            self.vip.pubsub.publish('pubsub',
                                    "data/foo",
                                    headers=headers,
                                    message=body)

        self._gapps.subscribe(self._test_topic, message_published)
        gevent.sleep(0.1)
        self.publish_next()
Esempio n. 13
0
    def __init__(self, simID, measDict, appName):
        """Initialize a GOSSListener."""
        # Set properties from inputs
        self.measDict = measDict
        self.simID = simID
        self.appName = appName

        # Create a GridAPPSD object and subscribe to simulation output.
        self.gossObj = GridAPPSD(simulation_id=self.simID,
                                 source=self.appName,
                                 base_simulation_status_topic=SIM_LOG)
        self.gossObj.subscribe(SIM_OUT, self)

        # Initialize dictionary for storing simulation data
        # TODO: we probably shouldn't stack this up in memory... recipe for
        # disaster.
        self.data = {}
Esempio n. 14
0
def _main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    opts = parser.parse_args()
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())
    x = input("Press Enter to continue...")
    message = {'command': 'StartSimulation'}
    print(fncs_input_topic(opts.simulation_id))
    print(json.dumps(message))
    gapps.send(fncs_input_topic(opts.simulation_id), json.dumps(message))
    while True:
        sleep(0.1)
Esempio n. 15
0
class GOSSListener(object):
    def __init__(self, simID, measDict, appName):
        """Initialize a GOSSListener."""
        # Set properties from inputs
        self.measDict = measDict
        self.simID = simID
        self.appName = appName

        # Create a GridAPPSD object and subscribe to simulation output.
        self.gossObj = GridAPPSD(simulation_id=self.simID,
                                 source=self.appName,
                                 base_simulation_status_topic=SIM_LOG)
        self.gossObj.subscribe(SIM_OUT, self)

        # Initialize dictionary for storing simulation data
        # TODO: we probably shouldn't stack this up in memory... recipe for
        # disaster.
        self.data = {}

    def on_message(self, headers, msg):
        """Forward relevant information along."""
        # Get the message as a dictionary.
        msg = json.loads(msg)

        # Grabe the timestamp
        t = int(msg['timestamp'])

        # Ensure we don't have this timestamp already.
        try:
            self.data[t]
        except KeyError:
            # Good, we don't have the key. Create it.
            self.data[t] = {}
        else:
            # This key already exists? We're in trouble...
            raise UserWarning("We already have data for time {}".format)

        # Get the simulation output as a dictionary.
        # May need to wrap this in a try-catch, TBD
        simOut = json.loads(msg['output'])

        # Loop over our measurements, extract info from the sim output
        for k in self.measDict:
            self.data[t][k] = simOut[self.simID][k]
            print(simOut[self.simID][k])
Esempio n. 16
0
def _main():
    _log.debug("Starting application"
               )  # display of log messages while the simulation is running
    # th elogs are displayed on the web based simulation (you can check this while running the simulation)
    print(
        "Application starting!!!-------------------------------------------------------"
    )
    global message_period
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "simulation_id",
        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request", help="Simulation Request")
    parser.add_argument(
        "--message_period",
        help="How often the sample app will send open/close capacitor message.",
        default=DEFAULT_MESSAGE_PERIOD)
    # These are now set through the docker container interface via env variables or defaulted to
    # proper values.
    #
    # parser.add_argument("-u", "--user", default="system",
    #                     help="The username to authenticate with the message bus.")
    # parser.add_argument("-p", "--password", default="manager",
    #                     help="The password to authenticate with the message bus.")
    # parser.add_argument("-a", "--address", default="127.0.0.1",
    #                     help="tcp address of the mesage bus.")
    # parser.add_argument("--port", default=61613, type=int,
    #                     help="the stomp port on the message bus.")
    #
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    message_period = int(opts.message_period)
    sim_request = json.loads(opts.request.replace("\'", ""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    _log.debug("Model mrid is: {}".format(model_mrid))  # object MRID
    gapps = GridAPPSD(opts.simulation_id,
                      address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(),
                      password=utils.get_gridappsd_pass())

    capacitors = get_capacitor_mrids(gapps, model_mrid)
    toggler = CapacitorToggler(opts.simulation_id, gapps, capacitors)
    gapps.subscribe(listening_to_topic, toggler)
    while True:
        time.sleep(0.1)
Esempio n. 17
0
    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 logger_and_gridapspd():

    gapps = GridAPPSD()
    logger = Logger(gapps)

    yield logger, gapps

    logger = None
    gapps = None
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()
Esempio n. 20
0
class GridAPPSDVolttron(Agent):
    """
    Document agent constructor here.
    """
    def __init__(self, setting1=1, setting2="some/random/topic", **kwargs):
        super(GridAPPSDVolttron, self).__init__(**kwargs)
        self._gapps = GridAPPSD(override_threading=gevent.spawn)
        self._publish_event = None

    def publish_next(self):
        if self._publish_event is not None:
            self._publish_event.cancel()
        if self._gapps.connected:
            self._gapps.send("/queue/test", "foo bar")
        else:
            print("Not connected")
        now = get_aware_utc_now()
        print(utils.get_utc_seconds_from_epoch(now))
        next_update_time = now + datetime.timedelta(seconds=5)
        self._publish_event = self.core.schedule(next_update_time,
                                                 self.publish_next)
        gevent.sleep(0.1)

    @Core.receiver("onstart")
    def onstart(self, sender, **kwargs):
        def message_published(headers, body):
            self.vip.pubsub.publish('pubsub',
                                    "data/foo",
                                    headers=headers,
                                    message=body)

        self._gapps.subscribe('/queue/test', message_published)
        gevent.sleep(0.1)
        self.publish_next()

    @Core.receiver("onstop")
    def onstop(self, sender, **kwargs):
        print("Stopping data!")

        # self._gapps.disconnect()
        print("After disconnect")
def start(log_file, feeder_mrid, model_api_topic):
    global logfile
    logfile = log_file

    global G, measid_lbs, loadbreaksw, undirected_graph  

    print("\nTOPOLOGY_VALIDATOR starting!!!------------------------------------------------------------")
    print("\nTOPOLOGY_VALIDATOR starting!!!------------------------------------------------------------", file=logfile)

    gapps = GridAPPSD()
    # NOTE: Use of modelType as STATIC or OPERATIONAL will change the response
    message = {"modelId": feeder_mrid,
                   "requestType": "LOOPS",
                   "modelType": "OPERATIONAL",
                   "resultFormat": "JSON"}
    out_topic = "/topic/goss.gridappsd.model-validator.topology.out"
    gapps.subscribe(out_topic, on_message)

    in_topic = "/topic/goss.gridappsd.model-validator.topology.in"
    gapps.send(in_topic, message)
    print("TOPOLOGY_VALIDATOR sent request to microservice; waiting for response\n", flush=True)
    print("TOPOLOGY_VALIDATOR sent request to microservice; waiting for response\n", file=logfile)
    
    global exit_flag
    exit_flag = False

    while not exit_flag:
        time.sleep(0.1)
Esempio n. 22
0
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()
Esempio n. 23
0
def gridappsd_client(docker_dependencies):
    with run_gridappsd_container(True):
        gappsd = GridAPPSD()
        gappsd.connect()
        assert gappsd.connected

        yield gappsd

        gappsd.disconnect()
Esempio n. 24
0
def get_gad_object(**kwargs):
    """Helper to get a GridAPPSD object.

    :param kwargs: Passed directly to the GridAPPSD constructor.
    """
    # Get platform address
    address = get_gad_address()

    # TODO: handle connection failures?
    # TODO: flexibility for different username/password?
    gad = GridAPPSD(address=address, username=gad_utils.get_gridappsd_user(),
                    password=gad_utils.get_gridappsd_pass(), **kwargs)

    LOG.debug('GridAPPSD object created and connected to the platform.')
    return gad
Esempio n. 25
0
def check_topology(feeder_mrid, model_api_topic, simulation_id):
    global measid_lbs, loadbreaksw, undirected_graph, openSW, openSW_CIM, sourcebus
    global lock_flag, feeder_id

    feeder_id = feeder_mrid
    openSW = []

    SPARQLManager = getattr(importlib.import_module('shared.sparql'),
                            'SPARQLManager')

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    # Get graph connectivity
    undirected_graph = sparql_mgr.graph_query()
    sourcebus, sourcevang = sparql_mgr.sourcebus_query()
    sourcebus = sourcebus.upper()
    openSW_CIM = sparql_mgr.opensw()
    print('MICROSERVICES conectivity information obtained', flush=True)
    print('MICROSERVICES conectivity information obtained',
          file=logfile,
          flush=True)

    loadbreaksw = sparql_mgr.switch_query()
    measid_lbs = sparql_mgr.switch_meas_query()
    find_all_cycles()

    lock_flag = True
    print("MICROSERVICES setting initial lock", flush=True)
    print("MICROSERVICES setting initial lock", file=logfile, flush=True)
    sim_output_topic = simulation_output_topic(simulation_id)
    gapps.subscribe(sim_output_topic, on_message)

    in_topic = "/topic/goss.gridappsd.model-validator.graph.in"
    gapps.subscribe(in_topic, handle_request)

    in_topic = "/topic/goss.gridappsd.model-validator.topology.in"
    gapps.subscribe(in_topic, handle_request)

    global exit_flag
    exit_flag = False

    while not exit_flag:
        time.sleep(0.1)
Esempio n. 26
0
def test_multiple_runs_in_a_row_with_dependency_context_manager():

    Containers.reset_all_containers()

    with run_dependency_containers():
        pass

    containers = [
        x for x in Containers.container_list() if "config" not in x.name
    ]
    assert len(containers) == 5

    with run_gridappsd_container():
        timeout = 0
        gapps = None

        while timeout < 30:
            try:
                gapps = GridAPPSD()
                gapps.connect()
                break
            except:
                time.sleep(1)
                timeout += 1

        assert gapps
        assert gapps.connected

    with run_gridappsd_container():
        timeout = 0
        gapps = None
        time.sleep(10)
        while timeout < 30:
            try:
                gapps = GridAPPSD()
                gapps.connect()
                break
            except:
                time.sleep(1)
                timeout += 1

        assert gapps
        assert gapps.connected
def start(log_file, feeder_mrid, model_api_topic):
    global logfp
    logfp = log_file

    global xfm_df, load_df, der_df
    
    print("\nTRANSFORMER_CAPACITY starting!!!------------------------------------------------")
    print("\nTRANSFORMER_CAPACITY starting!!!------------------------------------------------", file=logfp)

    SPARQLManager = getattr(importlib.import_module('shared.sparql'), 'SPARQLManager')

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    # Get transformer data
    xfm_df = sparql_mgr.query_transformers()
    print('TRANSFORMER_CAPACITY transformer data obtained', flush=True)
    print('TRANSFORMER_CAPACITY transformer data obtained', file=logfp)
    
    load_df = sparql_mgr.query_energyconsumer()
    der_df = sparql_mgr.query_der()
    print('TRANSFORMER_CAPACITY load and DER data obtained', flush=True)
    print('TRANSFORMER_CAPACITY load and DER data obtained', file=logfp)

    # Subscribe to microservice for getting the graph information
    message = {"modelId": feeder_mrid,
                   "requestType": "GRAPH",
                   "modelType": "STATIC",
                   "resultFormat": "JSON"}
    out_topic = "/topic/goss.gridappsd.model-validator.graph.out"
    gapps.subscribe(out_topic, callback)

    in_topic = "/topic/goss.gridappsd.model-validator.graph.in"
    gapps.send(in_topic, message)
    print("TRANSFORMER_CAPACITY sent request to microservice; waiting for response\n", flush=True)
    print("TRANSFORMER_CAPACITY sent request to microservice; waiting for response\n", file=logfp)

    global exit_flag
    exit_flag = False

    while not exit_flag:
        time.sleep(0.1)
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()
        "id": "gridappsd-sensor-simulator",
        "user_options": {
            "sensors-config": {
                "_99db0dc7-ccda-4ed5-a772-a7db362e9818": {
                    "nominal-value": 100,
                    "perunit-confidence-band": 0.02,
                    "aggregation-interval": 5,
                    "perunit-drop-rate": 0.01
                },
                "_ee65ee31-a900-4f98-bf57-e752be924c4d": {},
                "_f2673c22-654b-452a-8297-45dae11b1e14": {}
            },
            "random-seed": 0,
            "default-aggregation-interval": 30,
            "passthrough-if-not-specified": False,
            "default-perunit-confidence-band": 0.01,
            "default-perunit-drop-rate": 0.05
        }
    }]
}

gapps = GridAPPSD()
request = {
    "configurationType": "CIM Dictionary",
    "parameters": {
        "model_id": "_C1C3E687-6FFD-C753-582B-632A27E28507"
    }
}
simulation = Simulation(gapps, run_config_123)
simulation.start_simulation()
    def start_bridge(self):
        """Register with the fncs_broker and return.

            Function arguments:
                broker_location -- Type: string. Description: The ip location and port
                    for the fncs_broker. It must not be an empty string.
                    Default: 'tcp://localhost:5570'.
            Function returns:
                None.
            Function exceptions:
                RuntimeError()
                ValueError()
            """
        global is_initialized

        # First connect with goos via the GridAPPSD interface
        self._gridappsd = GridAPPSD(self.simulation_id, id=2,
                                    base_simulation_status_topic=BASE_SIMULATION_STATUS_TOPIC)
        self._gridappsd_listener = GridAPPSDListener(self, self._gridappsd)

        self._gridappsd.subscribe(self.simulation_input_topic,
                                  callback=self._gridappsd_listener)
        self._gridappsd.send_simulation_status("STARTING",
                                               "Starting Bridge for simulation id: {}".format(self.simulation_id))

        configuration_zpl = ''
        try:

            message_str = 'Registering with FNCS broker ' + str(self.simulation_id) + ' and broker ' + self.fncs_broker_location
            self._gridappsd.send_simulation_status("STARTED", message_str)

            message_str = 'connected to goss {}'.format(self._gridappsd.connected)
            self._gridappsd.send_simulation_status("STARTED", message_str)
            if not self.simulation_id or type(self.simulation_id) != str:
                raise ValueError(
                    'simulation_id must be a nonempty string.\n'
                    + 'simulation_id = {0}'.format(self.simulation_id))

            if not self.fncs_broker_location or type(self.fncs_broker_location) != str:
                raise ValueError(
                    'broker_location must be a nonempty string.\n'
                    + 'broker_location = {0}'.format(self.fncs_broker_location))
            fncs_configuration = {
                'name': 'FNCS_GOSS_Bridge_' + self.simulation_id,
                'time_delta': '1s',
                'broker': self.fncs_broker_location,
                'values': {
                    self.simulation_id: {
                        'topic': self.simulation_id + '/fncs_output',
                        'default': '{}',
                        'type': 'JSON',
                        'list': 'false'
                    }
                }
            }

            configuration_zpl = ('name = {0}\n'.format(fncs_configuration['name'])
                                 + 'time_delta = {0}\n'.format(fncs_configuration['time_delta'])
                                 + 'broker = {0}\nvalues'.format(fncs_configuration['broker']))
            for x in fncs_configuration['values'].keys():
                configuration_zpl += '\n    {0}'.format(x)
                configuration_zpl += '\n        topic = {0}'.format(
                    fncs_configuration['values'][x]['topic'])
                configuration_zpl += '\n        default = {0}'.format(
                    fncs_configuration['values'][x]['default'])
                configuration_zpl += '\n        type = {0}'.format(
                    fncs_configuration['values'][x]['type'])
                configuration_zpl += '\n        list = {0}'.format(
                    fncs_configuration['values'][x]['list'])
            fncs.initialize(configuration_zpl)

            is_initialized = fncs.is_initialized()
            if is_initialized:
                message_str = 'Registered with fncs ' + str(is_initialized)
                self._gridappsd.send_simulation_status("RUNNING", message_str)

        except Exception as e:
            message_str = 'Error while registering with fncs broker ' + str(e)
            self._gridappsd.send_simulation_status('ERROR', message_str, 'ERROR')
            if fncs.is_initialized():
                fncs.die()

        if not fncs.is_initialized():
            message_str = 'fncs.initialize(configuration_zpl) failed!\n' + 'configuration_zpl = {0}'.format(
                configuration_zpl)
            self._gridappsd.send_simulation_status('ERROR', message_str, 'ERROR')
            if fncs.is_initialized():
                fncs.die()
            raise RuntimeError(
                'fncs.initialize(configuration_zpl) failed!\n'
                + 'configuration_zpl = {0}'.format(configuration_zpl))