def get_or_create_esi_qau(esh: EnergySystemHandler, active_es_id: str) -> esdl.QuantityAndUnits: es: esdl.EnergySystem = esh.get_energy_system(active_es_id) if not es.energySystemInformation: esi: esdl.EnergySystemInformation = esdl.EnergySystemInformation( id=str(uuid4())) es.energySystemInformation = esi esh.add_object_to_dict(active_es_id, esi) if not es.energySystemInformation.quantityAndUnits: qaus = esdl.QuantityAndUnits(id=str(uuid4())) es.energySystemInformation.quantityAndUnits = qaus esh.add_object_to_dict(active_es_id, qaus) return es.energySystemInformation.quantityAndUnits
def add_sector(es, sector_name, sector_code, sector_descr): esi = es.energySystemInformation if not esi: esi = esdl.EnergySystemInformation(id=str(uuid.uuid4())) es.energySystemInformation = esi sectors = esi.sectors if not sectors: sectors = esdl.Sectors(id=str(uuid.uuid4())) esi.sectors = sectors sector = sectors.sector sector_info = esdl.Sector() sector_info.id = str(uuid.uuid4()) sector_info.name = sector_name sector_info.code = sector_code sector_info.description = sector_descr sector.append(sector_info)
def create_external_ref(): rset = ResourceSet() rset.resource_factory['esdl'] = XMLResource rset.resource_factory['*'] = XMLResource car_rs = rset.get_resource( HttpURI( 'https://edr.hesi.energy/store/esdl/80e3ac6a-94b1-4a85-a0d1-a68de4251243?format=xml' )) units_rs = rset.get_resource( HttpURI( 'http://localhost:9080/store/resource/public/QuantityAndUnits.esdl' )) es_rs = rset.get_resource( 'C:\\Users\\werkmane\\OneDrive - TNO\\Documents\\ESDL\\EnergySystems\\Untitled EnergySystem.esdl' ) units = units_rs.contents[0] ext_carriers = car_rs.contents[0] #es = esdl.EnergySystem(name='example', id=str(uuid4())) #es_rs = rset.create_resource(uri=StringURI('example.esdl')) #es_rs.append(es) es = es_rs.contents[0] esi = esdl.EnergySystemInformation() es.energySystemInformation = esi carriers = esdl.Carriers() esi.carriers = carriers """ Containment relations are copied, not referenced. """ #esi.carriers = ext_carriers #carriers.carrier.append(first) first: esdl.Carrier = ext_carriers.carrier[0] print(first.eURIFragment()) print(first.eResource.uri.plain) ec = esdl.EnergyCarrier(id=str(uuid4()), name="test") powerUnit = units_rs.uuid_dict['powerUnit'] ec.energyContentUnit = esdl.QuantityAndUnitReference(reference=powerUnit) carriers.carrier.append(ec) print(ec.energyContentUnit) uri = StringURI('to_string_' + 'left' + '.esdl') uri = URI('ES with external ref.esdl') es_rs.save(uri)
def collect_info(self, area, include_all_areas): esh = get_handler() active_es_id = get_session('active_es_id') es = esh.get_energy_system(active_es_id) heat_qau = esdl.QuantityAndUnitType( physicalQuantity=esdl.PhysicalQuantityEnum.ENERGY, multiplier=esdl.MultiplierEnum.MEGA, unit=esdl.UnitEnum.JOULE) esi = es.energySystemInformation if not esi: esi = esdl.EnergySystemInformation(id=str(uuid.uuid4())) es.energySystemInformation = esi qaus = esi.quantityAndUnits if not qaus: qaus = esdl.QuantityAndUnits(id=str(uuid.uuid4())) esi.quantityAndUnits = qaus qaus.quantityAndUnit.append(heat_qau) for ar in area.area: asset_list, qau_list = self.call_residentail_EG_service(ar.id, ar.scope) for qau in qau_list: try: q = esh.get_by_id(active_es_id, qau.id) except: # Only add qau if not already exists qaus.quantityAndUnit.append(qau) esh.add_object_to_dict(active_es_id, qau) for asset in list(asset_list): if isinstance(asset, esdl.GasDemand): gv = asset.port[0].profile[0].value # in m3 hv = gv * 35.17 # in MJ hd = esdl.HeatingDemand(id=str(uuid.uuid4()), name="HD_"+ar.id) hd.port.append(esdl.InPort(id=str(uuid.uuid4()),name="InPort")) hd.port[0].profile.append(esdl.SingleValue(id=str(uuid.uuid4()), value=hv)) hd.port[0].profile[0].profileQuantityAndUnit = esdl.QuantityAndUnitReference(reference=heat_qau) ar.asset.append(hd) esh.add_object_to_dict(active_es_id, hd, True)
def call_esdl_service(self, service_params): """Actually call an ESDL service.""" esh = get_handler() active_es_id = get_session("active_es_id") # {'service_id': '18d106cf-2af1-407d-8697-0dae23a0ac3e', 'area_scope': 'provincies', 'area_id': '12', # 'query_parameters': {'bebouwingsafstand': '32432', 'restrictie': 'vliegveld', 'preferentie': 'infrastructuur', 'geometrie': 'true'}} # Find the currently active service. service = None # services_list = get_session('services_list') user_email = get_session('user-email') role = get_session('user-role') services_list = self.get_user_services_list(user_email, role) for config_service in services_list: if config_service["id"] == service_params["service_id"]: service = config_service break # If it's a workflow, look in its steps. if config_service["type"] in ("workflow", "vueworkflow"): for step in config_service["workflow"]: if (step["type"] in ("service", "custom")): if "service" in step and step["service"][ "id"] == service_params["service_id"]: service = step["service"] break if service is None: return False, None url = service["url"] headers = service["headers"] if service.get("with_jwt_token", False): jwt_token = get_session("jwt-token") headers["Authorization"] = f"Bearer {jwt_token}" body = {} if "send_email_in_post_body_parameter" in service: body[service["send_email_in_post_body_parameter"]] = get_session( "user-email") if service["type"] == "geo_query": area_scope_tag = service["geographical_scope"]["url_area_scope"] area_id_tag = service["geographical_scope"]["url_area_id"] area_scope = service_params["area_scope"] url = url.replace(area_scope_tag, area_scope) ares_id = service_params["area_id"] url = url.replace(area_id_tag, ares_id) if "url_area_subscope" in service["geographical_scope"]: area_subscope_tag = service["geographical_scope"][ "url_area_subscope"] area_subscope = service_params["area_subscope"] url = url.replace(area_subscope_tag, area_subscope) elif service["type"].startswith("send_esdl"): esdlstr = esh.to_string(active_es_id) if service["body"] == "url_encoded": body["energysystem"] = urllib.parse.quote(esdlstr) # print(body) elif service["body"] == "base64_encoded": esdlstr_bytes = esdlstr.encode('utf-8') esdlstr_base64_bytes = base64.b64encode(esdlstr_bytes) body["energysystem"] = esdlstr_base64_bytes.decode('utf-8') else: body = esdlstr elif service["type"] == "simulation": esdlstr = esh.to_string(active_es_id) if service["body"] == "url_encoded": body["energysystem"] = urllib.parse.quote(esdlstr) elif service["body"] == "base64_encoded": esdlstr_bytes = esdlstr.encode('utf-8') esdlstr_base64_bytes = base64.b64encode(esdlstr_bytes) body["energysystem"] = esdlstr_base64_bytes.decode('utf-8') else: body = esdlstr if "body_config" in service: if service["body_config"]["type"] == "text": esdlstr = esh.to_string(active_es_id) if service["body_config"]["encoding"] == "none": body = esdlstr if service["body_config"]["encoding"] == "url_encoded": body = urllib.parse.quote(esdlstr) if service["body_config"]["encoding"] == "base64_encoded": esdlstr_bytes = esdlstr.encode('utf-8') esdlstr_base64_bytes = base64.b64encode(esdlstr_bytes) body = esdlstr_base64_bytes.decode('utf-8') if service["body_config"]["type"] == "json": body = {} for param in service["body_config"]['parameters']: if param["type"] == "esdl": esdlstr = esh.to_string(active_es_id) if param["encoding"] == "none": body[param["parameter"]] = esdlstr if param["encoding"] == "url_encoded": body[param["parameter"]] = urllib.parse.quote( esdlstr) if param["encoding"] == "base64_encoded": esdlstr_bytes = esdlstr.encode('utf-8') esdlstr_base64_bytes = base64.b64encode( esdlstr_bytes) body[param[ "parameter"]] = esdlstr_base64_bytes.decode( 'utf-8') if param["type"] == "value": body[param["parameter"]] = param["value"] if param["type"] == "json_string": body_params = service_params["body_config"] for bp in body_params: if param["parameter"] == bp: body[param["parameter"]] = body_params[bp] query_params = service_params["query_parameters"] config_service_params = service.get("query_parameters", {}) if query_params: first_qp = True for key in query_params: if query_params[ key]: # to filter empty lists for multi-selection parameters for cfg_service_param in config_service_params: if cfg_service_param["parameter_name"] == key: if "location" in cfg_service_param: if cfg_service_param["location"] == "url": url = url.replace( "<" + cfg_service_param["parameter_name"] + ">", str(query_params[key]), ) elif cfg_service_param[ "location"] == "body" and isinstance( body, dict): body[cfg_service_param[ "parameter_name"]] = query_params[key] else: if first_qp: url = url + "?" else: url = url + "&" url = (url + key + "=" + self.array2list(query_params[key])) first_qp = False try: if service["http_method"] == "get": r = requests.get(url, headers=headers) elif service["http_method"] == "post": if service["type"].endswith("json") or ( "body_config" in service and service["body_config"]["type"] == "json"): kwargs = {"json": body} else: kwargs = {"data": body} r = requests.post(url, headers=headers, **kwargs) else: # Should not happen, there should always be a method. return False, None if r.status_code == 200 or r.status_code == 201: # print(r.text) if service["result"][0]["action"] == "esdl": if "encoding" in service["result"][0]: if service["result"][0]["encoding"] == "url_encoded": esdl_response = urllib.parse.quote(r.text) elif service["result"][0][ "encoding"] == "base64_encoded": esdlstr_bytes = r.text.encode('utf-8') esdlstr_base64_bytes = base64.b64decode( esdlstr_bytes) esdl_response = esdlstr_base64_bytes.decode( 'utf-8') else: esdl_response = r.text else: esdl_response = r.text es, parse_info = esh.add_from_string( service["name"], esdl_response) # TODO deal with parse_info? return True, None elif service["result"][0]["action"] == "print": return True, json.loads(r.text) elif service["result"][0]["action"] == "add_assets": es_edit = esh.get_energy_system(es_id=active_es_id) instance = es_edit.instance area = instance[0].area asset_str_list = json.loads(r.text) # Fix for services that return an ESDL string that represents one asset if isinstance(asset_str_list, str): asset_str_list = {"add_assets": [asset_str_list]} try: for asset_str in asset_str_list["add_assets"]: asset = ESDLAsset.load_asset_from_string(asset_str) esh.add_object_to_dict(active_es_id, asset) ESDLAsset.add_object_to_area( es_edit, asset, area.id) asset_ui, conn_list = energy_asset_to_ui( esh, active_es_id, asset) emit( "add_esdl_objects", { "es_id": active_es_id, "asset_pot_list": [asset_ui], "zoom": True, }, ) emit( "add_connections", { "es_id": active_es_id, "conn_list": conn_list }, ) except Exception as e: logger.warning("Exception occurred: " + str(e)) return False, None return True, {"send_message_to_UI_but_do_nothing": {}} elif service["result"][0]["action"] == "add_notes": es_edit = esh.get_energy_system(es_id=active_es_id) esi = es_edit.energySystemInformation if not esi: esi = es_edit.energySystemInformation = esdl.EnergySystemInformation( id=str(uuid.uuid4())) esh.add_object_to_dict(active_es_id, esi) notes = esi.notes if not notes: notes = esi.notes = esdl.Notes(id=str(uuid.uuid4())) esh.add_object_to_dict(active_es_id, notes) notes_from_service = ESDLAsset.load_asset_from_string( esdl_string=r.text) if isinstance(notes_from_service, esdl.Notes): notes_list = [] for note in list(notes_from_service.note): notes.note.append(note) esh.add_object_to_dict(active_es_id, note) map_location = note.mapLocation if map_location: coords = { 'lng': map_location.lon, 'lat': map_location.lat } notes_list.append({ 'id': note.id, 'location': coords, 'title': note.title, 'text': note.text, 'author': note.author }) # , 'date': n.date}) emit('add_notes', { 'es_id': active_es_id, 'notes_list': notes_list }) else: logger.error("Service with id " + service_params["service_id"] + " did not return a esdl.Notes object") return False, None return True, {"send_message_to_UI_but_do_nothing": {}} elif service["result"][0]["action"] == "asset_feedback": service_results = json.loads(r.text) asset_results_dict = dict() for sr in service_results: asset_results_dict[sr['assetID']] = sr['messages'] return True, {"asset_feedback": asset_results_dict} elif service["result"][0]["action"] == "show_message": return True, {"message": service["result"][0]["message"]} else: logger.warning("Error running ESDL service - response " + str(r.status_code) + " with reason: " + str(r.reason)) logger.warning(r) logger.warning(r.content) return False, str(r.content) except Exception as e: logger.exception("Error accessing external ESDL service: " + str(e)) return False, None return False, None
def update_carrier(carr_id, carr_info): active_es_id = get_session('active_es_id') esh = get_handler() es = esh.get_energy_system(active_es_id) carrier_info = CarrierMessage(**carr_info) add_carrier = False try: carrier = esh.get_by_id(active_es_id, carr_id) except KeyError: module = importlib.import_module('esdl.esdl') carr_class_ = getattr(module, carrier_info.type) carrier = carr_class_(id=carrier_info.id) add_carrier = True carrier.name = carrier_info.name if carrier_info.type == 'EnergyCarrier': if carrier_info.emission is not None: carrier.emission = float(carrier_info.emission) if carrier_info.energy_content is not None: carrier.energyContent = float(carrier_info.energy_content) carrier.stateOfMatter = esdl.StateOfMatterEnum.from_string( carrier_info.state_of_matter) carrier.energyCarrierType = esdl.RenewableTypeEnum.from_string( carrier_info.renewable_type) carrier.energyContentUnit = build_qau_from_unit_string( carrier_info.energy_content_unit) carrier.energyContentUnit.physicalQuantity = esdl.PhysicalQuantityEnum.ENERGY carrier.emissionUnit = build_qau_from_unit_string('kg/GJ') carrier.emissionUnit.physicalQuantity = esdl.PhysicalQuantityEnum.EMISSION if carrier_info.type == 'ElectricityCommodity': if carrier_info.voltage is not None: carrier.voltage = float(carrier_info.voltage) if carrier_info.type == 'GasCommodity': if carrier_info.pressure is not None: carrier.pressure = float(carrier_info.pressure) if carrier_info.type == 'HeatCommodity': if carrier_info.supply_temperature is not None: carrier.supplyTemperature = float( carrier_info.supply_temperature) if carrier_info.return_temperature is not None: carrier.returnTemperature = float( carrier_info.return_temperature) if add_carrier: esi = es.energySystemInformation if not esi: esi = esdl.EnergySystemInformation(id=str(uuid4())) es.energySystemInformation = esi esh.add_object_to_dict(active_es_id, esi) ecs = esi.carriers if not ecs: ecs = esdl.Carriers(id=str(uuid4())) esi.carriers = ecs esh.add_object_to_dict(active_es_id, ecs) ecs.carrier.append(carrier) esh.add_object_to_dict(active_es_id, carrier) # send list as a result carrier_list = ESDLEnergySystem.get_carrier_list(es) return carrier_list
def update_environmental_profiles(self, action, profile_info): active_es_id = get_session('active_es_id') esh = get_handler() es = esh.get_energy_system(active_es_id) esi = es.energySystemInformation print(profile_info) if action == 'save': for x in esdl.EnvironmentalProfiles.eClass.eAllStructuralFeatures( ): if isinstance(x, EReference): if profile_info['name'] == x.name: if not esi: esi = es.energySystemInformation = esdl.EnergySystemInformation( id=str(uuid4())) esh.add_object_to_dict(active_es_id, esi) if not esi.environmentalProfiles: esi.environmentalProfiles = esdl.EnvironmentalProfiles( id=str(uuid4())) esh.add_object_to_dict(active_es_id, esi.environmentalProfiles) env_profiles = esi.environmentalProfiles profile = env_profiles.eGet(x) if not profile: profile = instantiate_type(profile_info['type']) profile.id = str(uuid4()) esh.add_object_to_dict(active_es_id, profile) else: if type(profile) != profile_info['type']: profile = instantiate_type( profile_info['type']) if isinstance(profile, esdl.SingleValue): profile.value = str2float(profile_info['value']) if isinstance(profile, esdl.InfluxDBProfile): std_profiles = Profiles.get_instance( ).get_profiles()['profiles'] if profile_info['profile_id'] in std_profiles: std_profile = std_profiles[ profile_info['profile_id']] print(std_profile) profile.id = str(uuid4()) profile.name = std_profile.name profile.host = std_profile.host profile.port = std_profile.port profile.database = std_profile.database profile.measurement = std_profile.measurement profile.field = std_profile.field profile.filters = std_profile.filters profile.startDate = std_profile.startDate profile.endDate = std_profile.endDate else: logger.error( 'Profile is referenced that was not communicated before' ) # Create a QuantityAndUnit instance and set the unit based on the information received if 'unit' in profile_info: profile.profileQuantityAndUnit = build_qau_from_unit_string( profile_info['unit']) esh.add_object_to_dict( active_es_id, profile.profileQuantityAndUnit) elif 'default_unit' in profile_info: profile.profileQuantityAndUnit = build_qau_from_unit_string( profile_info['default_unit']) esh.add_object_to_dict( active_es_id, profile.profileQuantityAndUnit) # Set the physical quantity based on the name of the profile reference self.update_quantity_in_qau( profile.profileQuantityAndUnit, x.name) env_profiles.eSet(x, profile) elif action == 'delete': env_profile_refname = profile_info['name'] env_profiles = esi.environmentalProfiles env_profiles.eSet(env_profile_refname, None) else: logger.error('Unknown action on environmental profiles')
def add_energy_system_information(self): '''Add Energy System Information''' esi = esdl.EnergySystemInformation(id='energy_system_information') self.energy_system.energySystemInformation = esi