def get_all_times_from_simulation(self): networks_list = get_session('timedimension-networks-list') simulation_id = get_session('timedimension-simulation-id') try: influxdb_client = self.connect_to_database() query = 'SELECT * FROM ' + ",".join( networks_list ) + ' WHERE "simulationRun" = \'' + simulation_id + '\'' sim_results = influxdb_client.query(query) time_list = list() if sim_results: feature_list = [] for key in list(sim_results.keys()): series = sim_results[key] fid = 1 for item in series: time_list.append(item['time']) # Keep only unique time values. time_list = list(dict.fromkeys(time_list)) return time_list except Exception as e: logger.error('error with query: ', str(e))
def get_asset_list(self): view_mode = get_session('mapeditor_view_mode') if not view_mode: user = get_session('user-email') view_mode = self.initialize_user(user) return asset_list[view_mode]
def get_profiles_settings(self): profiles_settings = dict() if self.settings_storage.has_system(PROFILES_SETTINGS): profiles_settings.update( self.settings_storage.get_system(PROFILES_SETTINGS)) user = get_session('user-email') user_group = get_session('user-group') role = get_session('user-role') mapeditor_role = get_session('user-mapeditor-role') if user is not None and self.settings_storage.has_user( user, PROFILES_SETTINGS): # add user profiles settings if available profiles_settings.update( self.settings_storage.get_user(user, PROFILES_SETTINGS)) if user_group is not None: for group in user_group: identifier = self._get_identifier(SettingType.PROJECT, group) if self.settings_storage.has_project(identifier, PROFILES_SETTINGS): # add project profiles server settings if available # Note: this is a a specific implementation for a dict element with a list of servers. When # additional settings must be added, this implementation must be extended. project_profiles_settings = self.settings_storage.get_project( identifier, PROFILES_SETTINGS) if 'profiles_servers' in project_profiles_settings: profiles_settings['profiles_servers'].extend( project_profiles_settings['profiles_servers']) return profiles_settings
def request_ielgas_ldc(info): asset_id = info['id'] # set_session('ielgas_monitor_id', asset_id) active_es_id = get_session('active_es_id') esh = get_handler() asset = esh.get_by_id(active_es_id, asset_id) asset_name = asset.name power = None if hasattr(asset, 'power'): power = asset.power elif hasattr(asset, 'capacity'): power = asset.capacity power_pos = None power_neg = None self.database_client = InfluxDBClient(host=self.plugin_settings['database_host'], port=self.plugin_settings['database_port'], database=self.plugin_settings['database_name']) user = get_session('user-email') user_config = self.get_user_settings(user) results = dict() measurements = user_config['measurements'] for m in measurements: try: query = 'SELECT "'+FIELD_NAME+'" FROM "' + m + '" WHERE assetId=\'' + asset_id + '\'' logger.debug(query) res = self.database_client.query(query) if res: results = res break except Exception as e: logger.error('error with query: ', str(e)) first_key = list(results.keys())[0] series = results[first_key] ldc_series = [] for item in series: ldc_series.append(item['allocationEnergy']) ldc_series.sort(reverse=True) ldc_series_decimate = [] for idx, item in enumerate(ldc_series): if idx % 40 == 0: ldc_series_decimate.append(item * 1e6) if item > 0: power_pos = power if item < 0: power_neg = -power emit('ldc-data', {'asset_name': asset_name, 'ldc_series': ldc_series_decimate, 'power_pos': power_pos, 'power_neg': power_neg})
def make_simulation_favorite(sim_id): with self.flask_app.app_context(): user_email = get_session('user-email') last_list = [] fav_list = [] if user_email is not None: if self.settings_storage.has_user(user_email, ESSIM_SIMULATION_LIST): last_list = self.settings_storage.get_user( user_email, ESSIM_SIMULATION_LIST) if self.settings_storage.has_user(user_email, ESSIM_FAVORITES_LIST): fav_list = self.settings_storage.get_user( user_email, ESSIM_FAVORITES_LIST) for sim in list(last_list): if sim['simulation_id'] == sim_id: fav_list.insert(0, sim) last_list.remove(sim) print(fav_list) self.settings_storage.set_user(user_email, ESSIM_SIMULATION_LIST, last_list) self.settings_storage.set_user(user_email, ESSIM_FAVORITES_LIST, fav_list) return json.dumps({ 'simulations': last_list, 'favorites': fav_list })
def receive_energyasset_info(shapefile_energyasset_list): with self.flask_app.app_context(): print("Received shpcvrt_receive_energyasset_info") esh = get_handler() active_es_id = get_session('active_es_id') es = esh.get_energy_system(active_es_id) area = es.instance[0].area for shapefile_energyasset in shapefile_energyasset_list: energy_asset = shapefile_energyasset['energy_asset'] shapefile_name = shapefile_energyasset['shapefile_name'] zipfile_row = shapefile_energyasset['zipfile_row'] # zipfile_name = self.zipfiles[int(zipfile_row)] # directory = get_session('shapefile_dir' + zipfile_row) shapefile_name_base = os.path.splitext(shapefile_name)[0] print(shapefile_name_base + ' --> ' + energy_asset) if energy_asset != 'ignore': basename = os.path.basename(shapefile_name_base) subarea = esdl.Area(id=str(uuid4()), name=basename) area.area.append(subarea) self.process_shapefile(subarea, shapefile_name_base, energy_asset) # update uuid_dict recursively for the main area (could take long?) esh.add_object_to_dict(es_id=active_es_id, esdl_object=area, recursive=True) self.executor.submit(process_energy_system, esh=esh, filename='test', force_update_es_id=active_es_id)
def view_modes_get_mode_info(self): user = get_session('user-email') settings = self.get_user_settings(user) return { 'current_mode': settings['mode'], 'possible_modes': list(view_modes_config.keys()) }
def mapeditor_user_ui_setting_get(info): category = info['category'] name = info['name'] user_email = get_session('user-email') res = self.get_user_ui_setting(user_email, category, name) return res
def set_param(self, object, attr_name, attr_value): with self.flask_app.app_context(): esh = get_handler() active_es_id = get_session('active_es_id') try: attribute = object.eClass.findEStructuralFeature(attr_name) if attribute is not None: if attr_value == "": parsed_value = attribute.eType.default_value else: parsed_value = attribute.eType.from_string(attr_value) if attribute.many: eOrderedSet = object.eGet(attr_name) eOrderedSet.clear() # TODO no support for multi-select of enums eOrderedSet.append(parsed_value) object.eSet(attr_name, eOrderedSet) else: if attribute.name == 'id': esh.remove_object_from_dict(active_es_id, object) object.eSet(attr_name, parsed_value) esh.add_object_to_dict(active_es_id, object) else: object.eSet(attr_name, parsed_value) else: print('Error setting attribute {} of {} to {}, unknown attribute'.format(attr_name, object.name, attr_value)) except Exception as e: print('Error setting attribute {} of {} to {}, caused by {}'.format(attr_name, object.name, attr_value, str(e)))
def add_authorization_header(): token = get_session('jwt-token') headers = { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/xml' } return headers
def store_esdl_services_list(settings): user = get_session('user-email') # TODO: check settings format before storing self.set_user_settings(user, settings) # Emit the new list to the browser such that the UI is updated emit('esdl_services', settings)
def calculate_ldc(asset_id): with self.flask_app.app_context(): esh = get_handler() active_es_id = get_session('active_es_id') asset = esh.get_by_id(active_es_id, asset_id) self.calculate_load_duration_curve(asset_id, asset.name)
def get_colors(self): active_es_id = get_session('active_es_id') esh = get_handler() colors = dict() num_default_colors = 0 es = esh.get_energy_system(active_es_id) esi = es.energySystemInformation if esi: carriers = esi.carriers if carriers: for c in carriers.carrier: if active_es_id + c.id in self.mapeditor_colors: colors[active_es_id + c.id] = self.mapeditor_colors[active_es_id + c.id] else: colors[active_es_id + c.id] = { "es_id": active_es_id, "carrier_id": c.id, "color": default_colors[num_default_colors] } num_default_colors += 1 return colors
def get_carrier(carr_id): active_es_id = get_session('active_es_id') esh = get_handler() response = CarrierMessage() carr = esh.get_by_id(active_es_id, carr_id) if carr: response.id = carr_id response.name = carr.name response.type = carr.eClass.name if isinstance(carr, esdl.EnergyCarrier): response.emission = carr.emission response.energy_content = carr.energyContent response.energy_content_unit = unit_to_string( carr.energyContentUnit) response.state_of_matter = carr.stateOfMatter.__str__() response.renewable_type = carr.energyCarrierType.__str__() if isinstance(carr, esdl.ElectricityCommodity): response.voltage = carr.voltage if isinstance(carr, esdl.GasCommodity): response.pressure = carr.pressure if isinstance(carr, esdl.HeatCommodity): response.supply_temperature = carr.supplyTemperature response.return_temperature = carr.returnTemperature return response
def delete_ref(self, message: DeleteRefMessage): parent_object = self.get_object_from_identifier(message.parent) esh = get_handler() active_es_id = get_session('active_es_id') reference: EReference = parent_object.eClass.findEStructuralFeature( message.ref_name) ref_object = parent_object.eGet(reference) print('deleting', message, ref_object) if reference.containment: try: esh.remove_object_from_dict(active_es_id, ref_object, True) resource = esh.get_resource(active_es_id) fragment = ref_object.eURIFragment() del resource._resolve_mem[ fragment] # update fragment cache for pyecore<0.12.0 except KeyError as e: print('ESDL Browser: can\'t delete id from uuid_dict', e) ref_object.delete( recursive=True ) # will automatically remove the reference from the list else: if reference.many: eOrderedSet = parent_object.eGet(reference) eOrderedSet.remove(ref_object) else: parent_object.eSet(reference, reference.get_default_value()) # send updated reference description to frontend response = ESDLEcore.describe_reference_value( parent_object.eGet(reference), ESDLBrowser.generate_repr) print('delete ref response', response) return response
def socketio_duplicate(message): with self.flask_app.app_context(): esh = get_handler() active_es_id = get_session('active_es_id') print('Duplicate EnergyAsset: %s' % message) duplicate = duplicate_energy_asset(esh, active_es_id, message['asset_id']) self.add_asset_and_emit(esh, active_es_id, duplicate, message['area_bld_id'])
def socketio_esdldrive_open(message): with self.flask_app.app_context(): path = message['path'] token = get_session('jwt-token') uri = ESDLDriveHttpURI( url + resource_endpoint + path, headers_function=add_authorization_header) logger.debug('ESDLDrive open: {} ({})'.format( message, uri.plain)) esh = get_handler() try: es = esh.load_file(uri) except Exception as e: logger.error("Error in loading file from ESDLDrive: " + str(e)) #send_alert('Error loading ESDL file with id {} from store'.format(store_id)) return if es.name: title = drive_name + ': ' + es.name else: title = drive_name + ' ES id: ' + es.id set_session('active_es_id', es.id) set_session('es_filename', title) # TODO: separate filename and title es_info_list = {} set_session("es_info_list", es_info_list) emit('clear_ui') emit('clear_esdl_layer_list') self.executor.submit(process_energy_system, esh, None, title) # run in seperate thread
def get_ibis_contours(info): with self.flask_app.app_context(): logger.info("getting ibis request") esh = get_handler() active_es_id = get_session('active_es_id') add_boundary_to_ESDL = info["add_boundary_to_ESDL"] rin_list = info["rin_list"] es_edit = esh.get_energy_system(es_id=active_es_id) instance = es_edit.instance area = instance[0].area try: url = 'http://' + settings.ibis_config["host"] + ':' + settings.ibis_config["port"] + \ settings.ibis_config["path_contour"] + rin_list logger.info(url) r = requests.get(url) if len(r.text) > 0: area_list = json.loads(r.text) except Exception as e: logger.info( 'ERROR in accessing IBIS boundary service for {}'. format(rin_list)) return None if add_boundary_to_ESDL: self.add_geometries_to_esdl(esh, active_es_id, area_list) self.emit_geometries_to_client(esh, active_es_id, area_list)
def socketio_create_object(message): # {'parent': {'id': parent_object.id, 'fragment': parent_object.fragment}, 'name': reference_data.name, 'type': types[0]} esh = get_handler() active_es_id = get_session('active_es_id') parent_object = self.get_object_from_identifier(message['parent']) reference_name = message['name'] attribute = parent_object.eClass.findEStructuralFeature(reference_name) if attribute is not None: object_type = message['type'] new_object = ESDLEcore.instantiate_type(object_type) if attribute.many: eOrderedSet = parent_object.eGet(reference_name) eOrderedSet.append(new_object) else: parent_object.eSet(reference_name, new_object) else: print("Error: Can't find reference {} of {}", reference_name, parent_object.name) return if hasattr(new_object, 'id'): new_object.id = esh.generate_uuid() esh.add_object_to_dict(active_es_id, new_object) print('adding to uuid dict ' + new_object.id) if hasattr(new_object, 'name'): new_object.name = 'New' + new_object.eClass.name browse_data = self.get_browse_to_data(new_object) emit('esdl_browse_to', browse_data, namespace='/esdl')
def mapeditor_user_ui_setting_set(info): category = info['category'] name = info['name'] value = info['value'] user_email = get_session('user-email') return self.set_user_ui_setting(user_email, category, name, value)
def socket_io_delete_ref(message): # esdl_browse_delete_ref active_es_id = get_session('active_es_id') esh = get_handler() reference_name = message['name'] ref_object = self.get_object_from_identifier(message['ref_id']) parent_object = self.get_object_from_identifier(message['parent']) reference: EReference = parent_object.eClass.findEStructuralFeature(reference_name) if reference.containment: try: esh.remove_object_from_dict(active_es_id, ref_object, True) resource = esh.get_resource(active_es_id) fragment = ref_object.eURIFragment() del resource._resolve_mem[fragment] except KeyError as e: print('ESDL Browser: can\'t delete id from uuid_dict') if isinstance(ref_object, esdl.EnergyAsset): emit('delete_esdl_object', {'asset_id': ref_object.id}) for port in ref_object.port: from_id = port.id to_ports = port.connectedTo for to_port in to_ports: to_id = to_port.id emit('remove_single_connection', {'from-port-id':from_id, 'to-port-id':to_id, 'es_id': active_es_id}) ref_object.delete(recursive=True) # will automatically remove the reference from the list else: if reference.many: eOrderedSet = parent_object.eGet(reference) eOrderedSet.remove(ref_object) else: parent_object.eSet(reference, reference.get_default_value()) browse_data = self.get_browse_to_data(parent_object) emit('esdl_browse_to', browse_data, namespace='/esdl')
def spatop_preprocess_subarea(): with self.flask_app.app_context(): esh = get_handler() active_es_id = get_session('active_es_id') es = esh.get_energy_system(active_es_id) area = es.instance[0].area return self.preprocess_area(area)
def get_profile_groups(self): user_group = get_session('user-group') dpg = copy.deepcopy(default_profile_groups) possible_groups = dpg["groups"] possible_groups.extend( self._create_group_profiles_for_projects(user_group)) return possible_groups
def categorize_object_attributes_and_references(self, object, attributes, references): attr_dict = {attr['name']: attr for attr in attributes} ref_dict = {ref['name']: ref for ref in references} view_mode = get_session('mapeditor_view_mode') this_view_mode_config = view_modes_config[view_mode] categorized_list = dict() for key in this_view_mode_config: categorized_list[key] = list() for objtype in this_view_mode_config[key]: if isinstance(object, esdl.getEClassifier(objtype)): for feature in this_view_mode_config[key][objtype]: if feature in attr_dict: attr_info = deepcopy(attr_dict[feature]) del attr_dict[feature] categorized_list[key].append(attr_info) elif feature in ref_dict: ref_info = deepcopy(ref_dict[feature]) del ref_dict[feature] categorized_list[key].append(ref_info) categorized_list['Advanced'] = list() for feature in attr_dict: attr_info = deepcopy(attr_dict[feature]) categorized_list['Advanced'].append(attr_info) for feature in ref_dict: attr_info = deepcopy(ref_dict[feature]) categorized_list['Advanced'].append(attr_info) return categorized_list
def delete_simulation(sim_fav, sim_id): if sim_fav == 'simulation' or sim_fav == 'favorite': if sim_fav == 'simulation': essim_list = ESSIM_SIMULATION_LIST if sim_fav == 'favorite': essim_list = ESSIM_FAVORITES_LIST with self.flask_app.app_context(): user_email = get_session('user-email') sim_list = [] if user_email is not None: if self.settings_storage.has_user( user_email, essim_list): sim_list = self.settings_storage.get_user( user_email, essim_list) for sim in list(sim_list): if sim['simulation_id'] == sim_id: sim_list.remove(sim) self.settings_storage.set_user( user_email, essim_list, sim_list) print(sim_list) return json.dumps(sim_list) else: return "Unknown operation", 404
def download_file(): """ Download a file by POSTing to a remote service, and providing some parameters, if they're provided. Returns a base64 encoded version of the file, which the frontend should be able to parse and offer as download to the user. """ url = request.json["remote_url"] with_jwt_token = request.args.get("with_jwt_token", True) jwt_token = get_session("jwt-token") headers = ({ "Authorization": f"Bearer {jwt_token}" } if with_jwt_token else None) request_params = request.json.get("request_params") r = requests.post(url, json=request_params, headers=headers) if r.status_code == 200: # Get the filename from the header. parsed_header = cgi.parse_header( r.headers["Content-Disposition"])[-1] filename = os.path.basename(parsed_header["filename"]) # Encode the file to base64. In this way, we can send it to the frontend. result = base64.b64encode(r.content).decode() return jsonify({ "base64file": result, "filename": filename }), 200 else: return jsonify([]), r.status_code
def post_data(): """Post provided data to external services. Request JSON is expected to have the following format: { 'remote_url': <The remote URL to send the data to>, 'request_params: {}, } Returns: [type]: [description] """ logger.info(request.json) url = request.json["remote_url"] with_jwt_token = request.args.get("with_jwt_token", True) jwt_token = get_session("jwt-token") headers = ({ "Authorization": f"Bearer {jwt_token}" } if with_jwt_token else None) request_params = request.json.get("request_params") r = requests.post(url, json=request_params, headers=headers, timeout=DEFAULT_TIMEOUT) try: resp_json = r.json() except Exception: resp_json = [] return jsonify(resp_json), r.status_code
def set_area_restrictions(area_id): with self.flask_app.app_context(): esh = get_handler() print(area_id) area_id = get_area_id_from_mapeditor_id(area_id) active_es_id = get_session('active_es_id') current_es = esh.get_energy_system(active_es_id) top_area = current_es.instance[0].area area = ESDLEnergySystem.find_area(top_area, area_id) measures = area.measures current_area_measures_list = [] if measures: for m in measures.measure: if isinstance(m, esdl.MeasureReference): this_measure = m.reference else: this_measure = m current_area_measures_list.append({ "id": this_measure.id, "name": this_measure.name }) data = { "current_measures": current_area_measures_list, "area_id": area_id } emit('vesta_restrictions_data', data)
def get_tooltip_asset_attrs(asset, shape): user_settings = get_session('user_settings') attrs_dict = dict() if user_settings: if 'ui_settings' in user_settings: if 'tooltips' in user_settings['ui_settings']: tooltip_settings = user_settings['ui_settings']['tooltips'] if (shape == 'marker' or shape == 'polygon' ) and 'marker_tooltip_format' in tooltip_settings: tooltip_format = tooltip_settings['marker_tooltip_format'] elif shape == 'line' and 'line_tooltip_format' in tooltip_settings: tooltip_format = tooltip_settings['line_tooltip_format'] attr_names_list = re.findall('\{(.*?)\}', tooltip_format) for attr_names in attr_names_list: for attr_name in attr_names.split('/'): if attr_name not in ['name', 'id']: attr = asset.eClass.findEStructuralFeature( attr_name) if attr and asset.eIsSet(attr_name): value = asset.eGet(attr_name) if isinstance(attr.eType, EEnum): attrs_dict[attr_name] = value.name else: attrs_dict[attr_name] = value return attrs_dict
def spatop_connect_unconnected_assets(params): with self.flask_app.app_context(): print(params) connect_asset_type = params["connect_asset_type"] connect_to_asset_type = params["connect_to_asset_type"] esh = get_handler() active_es_id = get_session('active_es_id') es = esh.get_energy_system(active_es_id) # Collect all assets of the required types connect_asset_list = list() connect_to_asset_list = list() for c in es.eAllContents(): if type(c).__name__ == connect_asset_type: connect_asset_list.append(c) if type(c).__name__ == connect_to_asset_type: connect_to_asset_list.append(c) connections_list = list() # Iterate over connect_asset_list for c in connect_asset_list: # TODO: fix assume one port port_c = c.port[0] if not port_c.connectedTo: shape_c = Shape.create(c.geometry) min_distance = 1e99 closest_ct = None # Find closest asset to connect to for ct in connect_to_asset_list: shape_ct = Shape.create(ct.geometry) if shape_ct.shape.distance(shape_c.shape) < min_distance: min_distance = shape_ct.shape.distance(shape_c.shape) closest_ct = ct # Determine the type of port to connect to if type(port_c) == esdl.InPort: find_port_type = esdl.OutPort else: find_port_type = esdl.InPort # Find first port that matches criteria for p in closest_ct.port: if isinstance(p, find_port_type): p.connectedTo.append(port_c) pct_coord = get_asset_and_coord_from_port_id(esh, active_es_id, p.id)['coord'] pc_coord = get_asset_and_coord_from_port_id(esh, active_es_id, port_c.id)['coord'] connections_list.append \ ({'from-port-id': port_c.id, 'from-asset-id': c.id, 'from-asset-coord': pc_coord, 'to-port-id': p.id, 'to-asset-id': closest_ct.id, 'to-asset-coord': pct_coord}) break emit('add_connections', {'es_id': active_es_id, 'conn_list': connections_list})