def __init__(self, simulation_id, gridappsd_obj, capacitors_dict, switches_dict, capacitors_meas_dict, switches_meas_dict): """ Create a ``SimulationSubscriber`` object This object is used as a subscription callback from a ``GridAPPSD`` object. This class receives simulation output and publishes voltage violation with the frequency as defined by message_period. Parameters ---------- simulation_id: str The simulation_id to use for publishing to a topic. gridappsd_obj: GridAPPSD An instatiated object that is connected to the gridappsd message bus usually this should be the same object which subscribes, but that isn't required. capacitor_dict: list(str) A list of capacitors mrids to turn on/off """ self._gapps = gridappsd_obj self.capacitors_dict = capacitors_dict self.switches_dict = switches_dict self.capacitors_meas_dict = capacitors_meas_dict self.switches_meas_dict = switches_meas_dict self.simulation_input = {} self.rcvd_input = False self._publish_to_topic = topics.service_output_topic('gridappsd-alarms',simulation_id)
def test_alarm_output(sim_config_file, sim_result_file): simulation_id = None sim_config_file = os.path.join( os.path.dirname(__file__), f"simulation_config_files/{sim_config_file}") sim_result_file = os.path.join( os.path.dirname(__file__), f"simulation_baseline_files/{sim_result_file}") assert os.path.exists( sim_config_file ), f"File {sim_config_file} must exist to run simulation test" with startup_containers(): with gappsd() as gapps: os.makedirs("/tmp/output", exist_ok=True) with open("/tmp/output/simulation.output", 'w') as outfile: sim_complete = False rcvd_measurement = False def onmeasurement(sim, timestep, measurements): nonlocal rcvd_measurement # if not rcvd_measurement: rcvd_measurement = True LOGGER.info('A measurement happened at %s', timestep) def onfinishsimulation(sim): nonlocal sim_complete sim_complete = True print("Completed simulator") with open(sim_config_file) as fp: run_config = json.load(fp) print(run_config["simulation_config"]["start_time"]) sim = Simulation(gapps, run_config) LOGGER.info('Starting the simulation') sim.add_onmesurement_callback(onmeasurement) sim.add_oncomplete_callback(onfinishsimulation) LOGGER.info('sim.add_onmesurement_callback') LOGGER.info("Querying Alarm topic for alarms") sim.start_simulation() print(sim.simulation_id) alarms_topic = t.service_output_topic('gridappsd-alarms', sim.simulation_id) log_topic = t.simulation_output_topic(sim.simulation_id) input_topic = t.simulation_input_topic(sim.simulation_id) gapps.subscribe(alarms_topic, on_message) gapps.subscribe(log_topic, on_message) # gapps.subscribe(input_topic, on_message) while not sim_complete: LOGGER.info('Sleeping') sleep(30)
def test_alarm_output(gridappsd_client, sim_config_file, sim_result_file): sim_config_file = os.path.join(os.path.dirname(__file__), f"simulation_config_files/{sim_config_file}") sim_result_file = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_result_file}") assert os.path.exists(sim_config_file), f"File {sim_config_file} must exist to run simulation test" gapps = gridappsd_client # Allow proven to come up sleep(30) sim_complete = False rcvd_measurement = False #def onmeasurement(sim, timestep, measurements): # nonlocal rcvd_measurement # if not rcvd_measurement: # rcvd_measurement = True # LOGGER.info('A measurement happened at %s', timestep) def onfinishsimulation(sim): nonlocal sim_complete sim_complete = True LOGGER.info('Simulation Complete') with open(sim_config_file) as fp: LOGGER.info('Loading config') run_config = json.load(fp) LOGGER.info(f'Simulation start time {run_config["simulation_config"]["start_time"]}') sim = Simulation(gapps, run_config) LOGGER.info('Starting the simulation') sim.start_simulation() #LOGGER.info('sim.add_onmesurement_callback') #sim.add_onmesurement_callback(onmeasurement) LOGGER.info('sim.add_oncomplete_callback') sim.add_oncomplete_callback(onfinishsimulation) LOGGER.info("Querying for alarm topic") alarms_topic = t.service_output_topic('gridappsd-alarms', sim.simulation_id) log_topic = t.simulation_output_topic(sim.simulation_id) gapps.subscribe(alarms_topic, on_message) gapps.subscribe(log_topic, on_message) while not sim_complete: LOGGER.info('Sleeping') sleep(30)
def __init__(self, simulation_id, gridappsd_obj, nominal_voltage_map, message_interval): """ Create a ``SimulationSubscriber`` object This object is used as a subscription callback from a ``GridAPPSD`` object. This class receives simulation output and publishes voltage violation with the frequency as defined by message_period. Parameters ---------- simulation_id: str The simulation_id to use for publishing to a topic. gridappsd_obj: GridAPPSD An instatiated object that is connected to the gridappsd message bus usually this should be the same object which subscribes, but that isn't required. capacitor_list: list(str) A list of capacitors mrids to turn on/off """ self._gapps = gridappsd_obj self.nominal_voltage_map = nominal_voltage_map self.message_interval = message_interval self._message_count = 0 self._publish_to_topic = topics.service_output_topic('voltage_violation',simulation_id)
sensors = dict() opts = get_opts() if opts.simulation_id == '-9999': raise SystemExit user_options = opts.request['service_configs'][0]['user_options'] service_id = "gridappsd-sensor-simulator" gapp = GridAPPSD(username=opts.username, password=opts.password, address=opts.address) read_topic = simulation_output_topic(opts.simulation_id) write_topic = service_output_topic(service_id, opts.simulation_id) log_file = "/tmp/gridappsd_tmp/{}/sensors.log".format(opts.simulation_id) if not os.path.exists(os.path.dirname(log_file)): os.makedirs(os.path.dirname(log_file)) with open(log_file, 'w') as fp: logging.basicConfig(stream=fp, level=logging.INFO) logging.getLogger().info( f"read topic: {read_topic}\nwrite topic: {write_topic}") logging.getLogger().info(f"user options: {user_options}") run_sensors = Sensors(gapp, read_topic=read_topic, write_topic=write_topic, user_options=user_options) run_sensors.main_loop()
ip.close() op.close() if __name__ == '__main__': sensors = dict() opts = get_opts() if opts.simulation_id == '-9999': print('entering test mode with', opts) run_test('Input.csv', 'Output.csv', opts) raise SystemExit read_topic = simulation_output_topic(opts.simulation_id) write_topic = service_output_topic("sensors", opts.simulation_id) gapp = GridAPPSD(username=opts.username, password=opts.password, stomp_address=opts.stomp_address, stomp_port=opts.stomp_port) sensor = Sensor( gapp, seed=opts.random_seed, nominal=opts. nominal, # TODO (Craig, Tom, Andy F): these 4 parameters will need to be different for each sensor instance perunit_confidence95=opts.perunit_confidence, perunit_dropping=opts.perunit_dropping, interval=opts.interval, output_topic=write_topic)
# print(gapps.query_object_types(model_id=None)) # print("Alka3") #print(gapps.query_data( query,database_type=POWERGRID_MODEL, timeout=30 )) # # with open("power2.json", 'r') as g: # # print(gapps.get_response(request_topic, json.load(g), timeout=30)) # print("Alka4") # print(gapps.query_object_dictionary('_0f6f3735-b297-46aa-8861-547d3cd0dee9', object_type='http://iec.ch/TC57/CIM100#ACLineSegment', object_id=None)) # with open("./weather_data.json", 'r') as g: # print(gapps.get_response(t.TIMESERIES, json.load(g), timeout=30)) # # with open("./query.json", 'r') as f: # query = json.load(f) # query["queryFilter"]["simulation_id"] = opts.simulation_id # print(query["queryFilter"]["simulation_id"]) # print("Alka") # print(gapps.get_response(t.TIMESERIES, query, timeout=30)) # For testing Alarms topic uncomment the below lines alarms_topic = t.service_output_topic('gridappsd-alarms', simulation_id) print(alarms_topic) print(simulation_id) print("Alka") print(gapps.subscribe(alarms_topic, Subscribe.on_message)) print(gapps.query_model_names(model_id=None)) #For testing the Logging API uncomment the below line #print(gapps.subscribe(simulation_log_topic(simulation_id), Subscribe.on_message)) while True: time.sleep(30)
# measurements = message['message']['measurements'] # timestamp = message['message']['timestamp'] # for k, v in measurements.items(): # # v['timestamp'] = timestamp # # v['class'] = sensor_test[k]['class'] # # v['type'] = sensor_test[k]['type'] # sim_output.append(v) sim.add_onstart_callback(onstart) sim.add_onmeasurement_callback(onmeasurment) sim.add_ontimestep_callback(ontimestep) sim.add_oncomplete_callback(onfinishsimulation) sim.start_simulation() read_topic = t.service_output_topic("gridappsd-sensor-simulator", sim.simulation_id) _log.debug("Reading topic for sensor output {read_topic}".format(read_topic=read_topic)) gapps.subscribe(read_topic, on_simulated_output) try: while True: if sim_complete: # print("sim output") # pprint(sim_output) break sleep(0.1) except KeyboardInterrupt: pass # Sleep to write to influx hopefully not necessary