From f0438e6776f5668c08baa8dabf92653aef3abfa9 Mon Sep 17 00:00:00 2001 From: Guille Date: Mon, 31 Jul 2023 16:55:39 -0400 Subject: [PATCH 1/7] partial completion of results --- hub_api/mockup/building.py | 8 ++++---- hub_api/persistence/retrofit_results.py | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hub_api/mockup/building.py b/hub_api/mockup/building.py index 466742d..7e98a71 100644 --- a/hub_api/mockup/building.py +++ b/hub_api/mockup/building.py @@ -5,14 +5,14 @@ Copyright © 2023 Concordia CERC group Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ -from properties import * +from hub_api.mockup.properties import * class Building: """ Building class """ - def __init__(self, building_info, catalog_archetype): + def __init__(self, building_info, result, catalog_archetype): self._function = building_info.function self._area = building_info.area self._volume = building_info.volume @@ -21,8 +21,8 @@ class Building: self._windows_area = building_info.windows_area self._roof_area = building_info.roof_area self._total_pv_area = building_info.total_pv_area - self._heating_consumption = building_info.heating_consumption - self._cooling_consumption = building_info.cooling_consumption + self._heating_consumption = heating_consumption + self._cooling_consumption = cooling_consumption self._domestic_hot_water_consumption = building_info.domestic_hot_water_consumption self._lighting_electrical_demand = building_info.lighting_electrical_demand self._appliances_electrical_demand = building_info.appliances_electrical_demand diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index 938b032..89b9852 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -6,6 +6,7 @@ from flask_restful import Resource from hub_api.config import Config from hub_api.helpers.session_helper import session, refresh_session from hub_api.mockup.building import Building +from costs.cost import Cost class RetrofitResults(Resource, Config): @@ -32,6 +33,7 @@ class RetrofitResults(Resource, Config): # retrieve the buildings info buildings = [] buildings_info = [] + mockups = {} scenario_name = None for scenario in payload['scenarios']: @@ -43,7 +45,7 @@ class RetrofitResults(Resource, Config): building_info = self.database.building(building, user_id, application_id, scenario_name) archetype = self.energy_systems_catalog.get_entry(building_info.system_name) buildings_info.append(building_info) - buildings.append(Building(building_info, archetype)) + mockups[building_info.name] = Building(building_info, archetype) results = self.database.results(user_id, application_id, payload) if results == {}: @@ -57,4 +59,7 @@ class RetrofitResults(Resource, Config): key = next(iter(value)) values.append({key: json.loads(str(value[key]))}) building_results['insel meb'] = values + building_results['costs'] = Cost(mockups[building_results['name']], scenario_name=scenario) + + return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) From fdf3491dc1478d8c46e2d54ce47545de73170471 Mon Sep 17 00:00:00 2001 From: Guille Date: Mon, 31 Jul 2023 16:56:37 -0400 Subject: [PATCH 2/7] partial completion of results --- hub_api/mockup/building.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hub_api/mockup/building.py b/hub_api/mockup/building.py index 7e98a71..8925a69 100644 --- a/hub_api/mockup/building.py +++ b/hub_api/mockup/building.py @@ -12,7 +12,7 @@ class Building: """ Building class """ - def __init__(self, building_info, result, catalog_archetype): + def __init__(self, building_info, results, catalog_archetype): self._function = building_info.function self._area = building_info.area self._volume = building_info.volume @@ -23,12 +23,12 @@ class Building: self._total_pv_area = building_info.total_pv_area self._heating_consumption = heating_consumption self._cooling_consumption = cooling_consumption - self._domestic_hot_water_consumption = building_info.domestic_hot_water_consumption - self._lighting_electrical_demand = building_info.lighting_electrical_demand - self._appliances_electrical_demand = building_info.appliances_electrical_demand - self._heating_peak_load = building_info.heating_peak_load - self._cooling_peak_load = building_info.cooling_peak_load - self._onsite_electrical_production = building_info.onsite_electrical_production + self._domestic_hot_water_consumption = domestic_hot_water_consumption + self._lighting_electrical_demand = lighting_electrical_demand + self._appliances_electrical_demand = appliances_electrical_demand + self._heating_peak_load = heating_peak_load + self._cooling_peak_load =cooling_peak_load + self._onsite_electrical_production = onsite_electrical_production self._catalog_archetype = catalog_archetype @property From d9a464e6d90a052d3456f20a642b8fc335d2e411 Mon Sep 17 00:00:00 2001 From: Guille Date: Tue, 1 Aug 2023 16:40:49 -0400 Subject: [PATCH 3/7] cost completed --- hub_api/mockup/building.py | 60 ++++++++++++++++++--- hub_api/persistence/retrofit_results.py | 72 ++++++++++++++++++------- 2 files changed, 104 insertions(+), 28 deletions(-) diff --git a/hub_api/mockup/building.py b/hub_api/mockup/building.py index 8925a69..014b460 100644 --- a/hub_api/mockup/building.py +++ b/hub_api/mockup/building.py @@ -5,6 +5,7 @@ Copyright © 2023 Concordia CERC group Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ +import hub.helpers.constants as cte from hub_api.mockup.properties import * @@ -21,14 +22,45 @@ class Building: self._windows_area = building_info.windows_area self._roof_area = building_info.roof_area self._total_pv_area = building_info.total_pv_area - self._heating_consumption = heating_consumption - self._cooling_consumption = cooling_consumption - self._domestic_hot_water_consumption = domestic_hot_water_consumption - self._lighting_electrical_demand = lighting_electrical_demand - self._appliances_electrical_demand = appliances_electrical_demand - self._heating_peak_load = heating_peak_load - self._cooling_peak_load =cooling_peak_load - self._onsite_electrical_production = onsite_electrical_production + self._energy_systems_archetype_name = building_info.system_name + self._heating_consumption = { + cte.YEAR: results['yearly_heating_consumption'], + cte.MONTH: results['monthly_heating_consumption'] + } + self._cooling_consumption = { + cte.YEAR: results['yearly_cooling_consumption'], + cte.MONTH: results['monthly_cooling_consumption'] + } + self._domestic_hot_water_consumption = { + cte.YEAR: results['yearly_domestic_hot_water_consumption'], + cte.MONTH: results['monthly_domestic_hot_water_consumption'] + } + self._lighting_electrical_demand = { + cte.YEAR: {'insel meb': results['yearly_lighting_electrical_demand'][0]} + } + self._appliances_electrical_demand = { + cte.YEAR: {'insel meb': results['yearly_appliances_electrical_demand'][0]} + } + self._heating_peak_load = { + cte.YEAR: results['yearly_heating_peak_load'], + cte.MONTH: results['monthly_heating_peak_load'] + } + self._cooling_peak_load = { + cte.YEAR: results['yearly_cooling_peak_load'], + cte.MONTH: results['monthly_cooling_peak_load'] + } + self._lighting_peak_load = { + cte.YEAR: results['yearly_lighting_peak_load'], + cte.MONTH: results['monthly_lighting_peak_load'] + } + self._appliances_peak_load = { + cte.YEAR: results['yearly_appliances_peak_load'], + cte.MONTH: results['monthly_appliances_peak_load'] + } + self._onsite_electrical_production = { + cte.YEAR: results['yearly_on_site_electrical_production'], + cte.MONTH: results['monthly_on_site_electrical_production'] + } self._catalog_archetype = catalog_archetype @property @@ -93,10 +125,22 @@ class Building: def cooling_peak_load(self): return self._cooling_peak_load + @property + def lighting_peak_load(self): + return self._lighting_peak_load + + @property + def appliances_peak_load(self): + return self._appliances_peak_load + @property def onsite_electrical_production(self): return self._onsite_electrical_production + @property + def energy_systems_archetype_name(self): + return self._energy_systems_archetype_name + @property def energy_systems(self): _energy_systems = [] diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index 89b9852..2aeb6ca 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -30,36 +30,68 @@ class RetrofitResults(Resource, Config): if 'scenarios' not in payload: return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token) - # retrieve the buildings info - buildings = [] - buildings_info = [] - mockups = {} - scenario_name = None - - for scenario in payload['scenarios']: - scenario_name = next(iter(scenario)) - for name in scenario[scenario_name]: - if name not in buildings: - buildings.append(name) - for building in buildings: - building_info = self.database.building(building, user_id, application_id, scenario_name) - archetype = self.energy_systems_catalog.get_entry(building_info.system_name) - buildings_info.append(building_info) - mockups[building_info.name] = Building(building_info, archetype) - results = self.database.results(user_id, application_id, payload) if results == {}: # no data found for the given parameters return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) + # deserialize the response to return pure json scenario = next(iter(results)) + for building_results in results[scenario]: values = [] + building_info = self.database.building(building_results['building'], user_id, application_id, scenario) + results_dictionary = {} + archetype = self.energy_systems_catalog.get_entry(building_info.system_name) for value in building_results['insel meb']: key = next(iter(value)) values.append({key: json.loads(str(value[key]))}) + results_dictionary[key] = json.loads(str(value[key])) building_results['insel meb'] = values - building_results['costs'] = Cost(mockups[building_results['name']], scenario_name=scenario) - - + mockup_building = Building(building_info, results_dictionary, archetype) + life_cycle = Cost(mockup_building, retrofit_scenario=scenario).life_cycle + global_capital_costs = life_cycle[f'Scenario {scenario}']['global_capital_costs'] + global_operational_costs = life_cycle[f'Scenario {scenario}']['global_operational_costs'] + global_capital_incomes = life_cycle[f'Scenario {scenario}']['global_capital_incomes'] + global_maintenance_costs = life_cycle[f'Scenario {scenario}']['global_maintenance_costs'] + building_results['costs'] = { + 'total_capital_costs_skin': life_cycle[f'Scenario {scenario}']['total_capital_costs_skin'], + 'total_capital_costs_systems': life_cycle[f'Scenario {scenario}']['total_capital_costs_systems'], + 'end_of_life_costs': life_cycle[f'Scenario {scenario}']['end_of_life_costs'], + 'total_operational_costs': life_cycle[f'Scenario {scenario}']['total_operational_costs'], + 'total_maintenance_costs': life_cycle[f'Scenario {scenario}']['total_maintenance_costs'], + 'operational_incomes': life_cycle[f'Scenario {scenario}']['operational_incomes'], + 'capital_incomes': life_cycle[f'Scenario {scenario}']['capital_incomes'], + 'global_capital_costs': { + 'B2010_opaque_walls': global_capital_costs['B2010_opaque_walls'].tolist(), + 'B2020_transparent': global_capital_costs['B2020_transparent'].tolist(), + 'B3010_opaque_roof': global_capital_costs['B3010_opaque_roof'].tolist(), + 'B10_superstructure': global_capital_costs['B10_superstructure'].tolist(), + 'D3020_heat_generating_systems': global_capital_costs['D3020_heat_generating_systems'].tolist(), + 'D3030_cooling_generation_systems': global_capital_costs['D3030_cooling_generation_systems'].tolist(), + 'D3080_other_hvac_ahu': global_capital_costs['D3080_other_hvac_ahu'].tolist(), + 'D5020_lighting_and_branch_wiring': global_capital_costs['D5020_lighting_and_branch_wiring'].tolist(), + 'D301010_photovoltaic_system': global_capital_costs['D301010_photovoltaic_system'].tolist(), + }, + 'global_end_of_life_costs': life_cycle[f'Scenario {scenario}']['global_end_of_life_costs']['End_of_life_costs'].tolist(), + 'global_operational_costs': { + 'fixed_costs_electricity_peak': global_operational_costs['Fixed_costs_electricity_peak'].tolist(), + 'fixed_costs_electricity_monthly': global_operational_costs['Fixed_costs_electricity_monthly'].tolist(), + 'variable_costs_electricity': global_operational_costs['Variable_costs_electricity'].tolist(), + 'fixed_costs_gas': global_operational_costs['Fixed_costs_gas'].tolist(), + 'variable_costs_gas': global_operational_costs['Variable_costs_gas'].tolist() + }, + 'global_maintenance_costs': { + 'heating_maintenance': global_maintenance_costs['Heating_maintenance'].tolist(), + 'cooling_maintenance': global_maintenance_costs['Cooling_maintenance'].tolist(), + 'pv_maintenance': global_maintenance_costs['PV_maintenance'].tolist(), + }, + 'global_operational_incomes': life_cycle[f'Scenario {scenario}']['global_operational_incomes']['Incomes electricity'].tolist(), + 'global_capital_incomes': { + 'subsidies_construction': global_capital_incomes['Subsidies construction'].tolist(), + 'subsidies_hvac': global_capital_incomes['Subsidies HVAC'].tolist(), + 'subsidies_pv': global_capital_incomes['Subsidies PV'].tolist() + } + } + print(results) return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) From beed9f4e22f07f0e607ade5ecd5a11c07b5bf567 Mon Sep 17 00:00:00 2001 From: Guille Date: Wed, 2 Aug 2023 15:47:09 -0400 Subject: [PATCH 4/7] complete results --- hub_api/mockup/building.py | 20 +++++++++++++++----- hub_api/persistence/retrofit_results.py | 9 +++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/hub_api/mockup/building.py b/hub_api/mockup/building.py index 014b460..f9d9166 100644 --- a/hub_api/mockup/building.py +++ b/hub_api/mockup/building.py @@ -6,7 +6,11 @@ Project Coder Pilar Monsalvete Alvarez de Uribarri pilar.monsalvete@concordia.ca """ import hub.helpers.constants as cte +from hub.helpers.dictionaries import Dictionaries + + from hub_api.mockup.properties import * +import pandas as pd class Building: @@ -27,6 +31,7 @@ class Building: cte.YEAR: results['yearly_heating_consumption'], cte.MONTH: results['monthly_heating_consumption'] } + print(self._heating_consumption) self._cooling_consumption = { cte.YEAR: results['yearly_cooling_consumption'], cte.MONTH: results['monthly_cooling_consumption'] @@ -36,10 +41,12 @@ class Building: cte.MONTH: results['monthly_domestic_hot_water_consumption'] } self._lighting_electrical_demand = { - cte.YEAR: {'insel meb': results['yearly_lighting_electrical_demand'][0]} + cte.YEAR: pd.DataFrame(results['yearly_lighting_electrical_demand'], columns=['insel meb']), + cte.MONTH: pd.DataFrame(results['monthly_lighting_electrical_demand'], columns=['insel meb']) } self._appliances_electrical_demand = { - cte.YEAR: {'insel meb': results['yearly_appliances_electrical_demand'][0]} + cte.YEAR: pd.DataFrame(results['yearly_appliances_electrical_demand'], columns=['insel meb']), + cte.MONTH: pd.DataFrame(results['monthly_appliances_electrical_demand'], columns=['insel meb']) } self._heating_peak_load = { cte.YEAR: results['yearly_heating_peak_load'], @@ -144,10 +151,13 @@ class Building: @property def energy_systems(self): _energy_systems = [] - for system in self._catalog_archetype.systems: - demands = system.demand_types - fuel_type = system.generation_system.fuel_type + _hub_demand_types = [] + for demand_type in system.demand_types: + # todo: generalize this when we have more catalogs + _hub_demand_types.append(Dictionaries().montreal_demand_type_to_hub_energy_demand_type[demand_type]) + demands = _hub_demand_types + fuel_type = Dictionaries().montreal_custom_fuel_to_hub_fuel[system.generation_system.fuel_type] generic_generation_system = GenericGenerationSystem() generic_generation_system.fuel_type = fuel_type generation_system = GenerationSystem() diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index 2aeb6ca..f357e8e 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -3,10 +3,11 @@ import json from flask import Response, request from flask_restful import Resource +from costs.cost import Cost +from co2_emission.co2_emission import Co2Emission from hub_api.config import Config from hub_api.helpers.session_helper import session, refresh_session from hub_api.mockup.building import Building -from costs.cost import Cost class RetrofitResults(Resource, Config): @@ -17,6 +18,7 @@ class RetrofitResults(Resource, Config): """ API call for requesting a specified list of enriched persistence """ + # todo: cost and co2 libraries are using default canadians values, in the future need to be optionally API configurable session_id = request.headers.get('session-id', None) token = request.headers.get('token', None) application_uuid = request.headers.get('application-uuid', None) @@ -50,6 +52,8 @@ class RetrofitResults(Resource, Config): building_results['insel meb'] = values mockup_building = Building(building_info, results_dictionary, archetype) life_cycle = Cost(mockup_building, retrofit_scenario=scenario).life_cycle + + operational_co2 = Co2Emission(mockup_building).operational_co2 global_capital_costs = life_cycle[f'Scenario {scenario}']['global_capital_costs'] global_operational_costs = life_cycle[f'Scenario {scenario}']['global_operational_costs'] global_capital_incomes = life_cycle[f'Scenario {scenario}']['global_capital_incomes'] @@ -93,5 +97,6 @@ class RetrofitResults(Resource, Config): 'subsidies_pv': global_capital_incomes['Subsidies PV'].tolist() } } - print(results) + print(operational_co2) + building_results['operational_co2'] = operational_co2 return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) From cb9b9bb2512bacf1072d8f522c0b194cd3fa925c Mon Sep 17 00:00:00 2001 From: cerc Date: Thu, 3 Aug 2023 09:53:26 -0400 Subject: [PATCH 5/7] server modifications --- bootstrap.py | 12 +++++++----- hub_api/config.py | 4 ++-- hub_api/docs/openapi-specs.yml | 18 +++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index 547f0a4..ba099a0 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -9,6 +9,7 @@ import datetime import flask import yaml +from pathlib import Path from flasgger import LazyJSONEncoder, Swagger from flask import Response from flask_restful import Api @@ -47,20 +48,21 @@ api.add_resource(Costs, '/v1.4/workflow/costs') api.add_resource(EnergyPlus, '/v1.4/workflow/energy-plus') api.add_resource(InselMonthlyEnergyBalance, '/v1.4/workflow/insel-monthly-energy-balance') +yml_path = Path('./docs/openapi-specs.yml').resolve() -with open("hub_api/docs/openapi-specs.yml", "r") as stream: +with open(yml_path, "r") as stream: swagger_config = { "headers": [], "specs": [ { - "endpoint": 'apispec', - "route": '/v1.4/apispec.json', + "endpoint": '/api/apispec', + "route": '/api/apispec/apispec.json', "rule_filter": lambda rule: True, # all in "model_filter": lambda tag: True, # all in } ], - "static_url_path": "/api/v1.4/static", - "specs_route": "/v1.4/api-docs/", + "static_url_path": "/api/static", + "specs_route": "/api/api-docs/", "openapi": "3.0.0" } try: diff --git a/hub_api/config.py b/hub_api/config.py index 6543edf..2cc2e83 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -15,9 +15,9 @@ from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCa class Config: def __init__(self): - dotenv_path = "{}/.env".format(os.path.expanduser('~')) + dotenv_path = "{}/.local/etc/hub_api/.env".format(os.path.expanduser('~')) if platform.system() == 'Linux': - dotenv_path = Path('/home/guille/.local/etc/hub/.env').resolve() + dotenv_path = Path(dotenv_path).resolve() environment = 'TEST' database_name = 'montreal_retrofit_test' diff --git a/hub_api/docs/openapi-specs.yml b/hub_api/docs/openapi-specs.yml index 40e1929..faa0804 100644 --- a/hub_api/docs/openapi-specs.yml +++ b/hub_api/docs/openapi-specs.yml @@ -9,7 +9,7 @@ externalDocs: description: Find out more about Swagger url: http://swagger.io paths: - /v1.4/uptime: + /api/v1.4/uptime: get: parameters: [] @@ -26,7 +26,7 @@ paths: schema: $ref: '#/components/schemas/uptime' - /v1.4/session/start: + /api/v1.4/session/start: put: parameters: - in: header @@ -78,7 +78,7 @@ paths: schema: $ref: '#/components/schemas/unauthorized' - /v1.4/session/keep-alive: + /api/v1.4/session/keep-alive: put: security: - session-id: [] @@ -111,7 +111,7 @@ paths: schema: $ref: '#/components/schemas/unauthorized' - /v1.4/session/end: + /api/v1.4/session/end: put: security: - session-id: [] @@ -144,7 +144,7 @@ paths: schema: $ref: '#/components/schemas/unauthorized' - /v1.4/persistence/retrofit-results: + /api/v1.4/persistence/retrofit-results: post: security: - session-id: [ ] @@ -192,7 +192,7 @@ paths: schema: $ref: '#/components/schemas/unauthorized' - /v1.4/workflow/costs: + /api/v1.4/workflow/costs: post: security: - session-id: [ ] @@ -213,7 +213,7 @@ paths: schema: $ref: '#/components/schemas/not-implemented-error' - /v1.4/workflow/energy-plus: + /api/v1.4/workflow/energy-plus: post: security: - session-id: [ ] @@ -234,7 +234,7 @@ paths: schema: $ref: '#/components/schemas/not-implemented-error' - /v1.4/workflow/insel-monthly-energy-balance: + /api/v1.4/workflow/insel-monthly-energy-balance: post: security: - session-id: [ ] @@ -579,4 +579,4 @@ components: properties: error: type: string - example: 'NotImplementedError' \ No newline at end of file + example: 'NotImplementedError' From b06a1d3d53109cafdc96a8055362cec7ee5291ab Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 3 Aug 2023 10:42:41 -0400 Subject: [PATCH 6/7] correct expired sessions collector --- hub_api/helpers/session_helper.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hub_api/helpers/session_helper.py b/hub_api/helpers/session_helper.py index 8f73ce4..5fff078 100644 --- a/hub_api/helpers/session_helper.py +++ b/hub_api/helpers/session_helper.py @@ -23,13 +23,14 @@ def expired_sessions_collector(session_timeout_duration): """ while True: if bool(sessions): - for _session in list(sessions): - print(_session) - _expire = datetime.datetime.strptime(_session['expire'], '%Y-%m-%d %H:%M:%S.%f') + for session_uuid in sessions: + print(sessions) + print(sessions[session_uuid]['expire']) + _expire = datetime.datetime.strptime(sessions[session_uuid]['expire'], '%Y-%m-%d %H:%M:%S.%f') if _expire < datetime.datetime.now(): - print("session for user: ", _session['username'], "expired.") + print("session for user: ", sessions[session_uuid]['user'], "expired.") - del sessions[session] + del sessions[session_uuid] time.sleep(60 * int(session_timeout_duration)) From b96617bb216434138a9cc71870e0025db5a57844 Mon Sep 17 00:00:00 2001 From: Guille Date: Thu, 3 Aug 2023 12:38:19 -0400 Subject: [PATCH 7/7] correct retrofit results to apply costs and co2 to all scenariosa --- hub_api/persistence/retrofit_results.py | 115 ++++++++++++------------ 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/hub_api/persistence/retrofit_results.py b/hub_api/persistence/retrofit_results.py index f357e8e..44d7439 100644 --- a/hub_api/persistence/retrofit_results.py +++ b/hub_api/persistence/retrofit_results.py @@ -38,65 +38,64 @@ class RetrofitResults(Resource, Config): return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token) # deserialize the response to return pure json - scenario = next(iter(results)) - for building_results in results[scenario]: - values = [] - building_info = self.database.building(building_results['building'], user_id, application_id, scenario) - results_dictionary = {} - archetype = self.energy_systems_catalog.get_entry(building_info.system_name) - for value in building_results['insel meb']: - key = next(iter(value)) - values.append({key: json.loads(str(value[key]))}) - results_dictionary[key] = json.loads(str(value[key])) - building_results['insel meb'] = values - mockup_building = Building(building_info, results_dictionary, archetype) - life_cycle = Cost(mockup_building, retrofit_scenario=scenario).life_cycle + for scenario in results: + for building_results in results[scenario]: + values = [] + building_info = self.database.building(building_results['building'], user_id, application_id, scenario) + results_dictionary = {} + archetype = self.energy_systems_catalog.get_entry(building_info.system_name) + for value in building_results['insel meb']: + key = next(iter(value)) + values.append({key: json.loads(str(value[key]))}) + results_dictionary[key] = json.loads(str(value[key])) + building_results['insel meb'] = values + mockup_building = Building(building_info, results_dictionary, archetype) + life_cycle = Cost(mockup_building, retrofit_scenario=scenario).life_cycle - operational_co2 = Co2Emission(mockup_building).operational_co2 - global_capital_costs = life_cycle[f'Scenario {scenario}']['global_capital_costs'] - global_operational_costs = life_cycle[f'Scenario {scenario}']['global_operational_costs'] - global_capital_incomes = life_cycle[f'Scenario {scenario}']['global_capital_incomes'] - global_maintenance_costs = life_cycle[f'Scenario {scenario}']['global_maintenance_costs'] - building_results['costs'] = { - 'total_capital_costs_skin': life_cycle[f'Scenario {scenario}']['total_capital_costs_skin'], - 'total_capital_costs_systems': life_cycle[f'Scenario {scenario}']['total_capital_costs_systems'], - 'end_of_life_costs': life_cycle[f'Scenario {scenario}']['end_of_life_costs'], - 'total_operational_costs': life_cycle[f'Scenario {scenario}']['total_operational_costs'], - 'total_maintenance_costs': life_cycle[f'Scenario {scenario}']['total_maintenance_costs'], - 'operational_incomes': life_cycle[f'Scenario {scenario}']['operational_incomes'], - 'capital_incomes': life_cycle[f'Scenario {scenario}']['capital_incomes'], - 'global_capital_costs': { - 'B2010_opaque_walls': global_capital_costs['B2010_opaque_walls'].tolist(), - 'B2020_transparent': global_capital_costs['B2020_transparent'].tolist(), - 'B3010_opaque_roof': global_capital_costs['B3010_opaque_roof'].tolist(), - 'B10_superstructure': global_capital_costs['B10_superstructure'].tolist(), - 'D3020_heat_generating_systems': global_capital_costs['D3020_heat_generating_systems'].tolist(), - 'D3030_cooling_generation_systems': global_capital_costs['D3030_cooling_generation_systems'].tolist(), - 'D3080_other_hvac_ahu': global_capital_costs['D3080_other_hvac_ahu'].tolist(), - 'D5020_lighting_and_branch_wiring': global_capital_costs['D5020_lighting_and_branch_wiring'].tolist(), - 'D301010_photovoltaic_system': global_capital_costs['D301010_photovoltaic_system'].tolist(), - }, - 'global_end_of_life_costs': life_cycle[f'Scenario {scenario}']['global_end_of_life_costs']['End_of_life_costs'].tolist(), - 'global_operational_costs': { - 'fixed_costs_electricity_peak': global_operational_costs['Fixed_costs_electricity_peak'].tolist(), - 'fixed_costs_electricity_monthly': global_operational_costs['Fixed_costs_electricity_monthly'].tolist(), - 'variable_costs_electricity': global_operational_costs['Variable_costs_electricity'].tolist(), - 'fixed_costs_gas': global_operational_costs['Fixed_costs_gas'].tolist(), - 'variable_costs_gas': global_operational_costs['Variable_costs_gas'].tolist() - }, - 'global_maintenance_costs': { - 'heating_maintenance': global_maintenance_costs['Heating_maintenance'].tolist(), - 'cooling_maintenance': global_maintenance_costs['Cooling_maintenance'].tolist(), - 'pv_maintenance': global_maintenance_costs['PV_maintenance'].tolist(), - }, - 'global_operational_incomes': life_cycle[f'Scenario {scenario}']['global_operational_incomes']['Incomes electricity'].tolist(), - 'global_capital_incomes': { - 'subsidies_construction': global_capital_incomes['Subsidies construction'].tolist(), - 'subsidies_hvac': global_capital_incomes['Subsidies HVAC'].tolist(), - 'subsidies_pv': global_capital_incomes['Subsidies PV'].tolist() + operational_co2 = Co2Emission(mockup_building).operational_co2 + global_capital_costs = life_cycle[f'Scenario {scenario}']['global_capital_costs'] + global_operational_costs = life_cycle[f'Scenario {scenario}']['global_operational_costs'] + global_capital_incomes = life_cycle[f'Scenario {scenario}']['global_capital_incomes'] + global_maintenance_costs = life_cycle[f'Scenario {scenario}']['global_maintenance_costs'] + building_results['costs'] = { + 'total_capital_costs_skin': life_cycle[f'Scenario {scenario}']['total_capital_costs_skin'], + 'total_capital_costs_systems': life_cycle[f'Scenario {scenario}']['total_capital_costs_systems'], + 'end_of_life_costs': life_cycle[f'Scenario {scenario}']['end_of_life_costs'], + 'total_operational_costs': life_cycle[f'Scenario {scenario}']['total_operational_costs'], + 'total_maintenance_costs': life_cycle[f'Scenario {scenario}']['total_maintenance_costs'], + 'operational_incomes': life_cycle[f'Scenario {scenario}']['operational_incomes'], + 'capital_incomes': life_cycle[f'Scenario {scenario}']['capital_incomes'], + 'global_capital_costs': { + 'B2010_opaque_walls': global_capital_costs['B2010_opaque_walls'].tolist(), + 'B2020_transparent': global_capital_costs['B2020_transparent'].tolist(), + 'B3010_opaque_roof': global_capital_costs['B3010_opaque_roof'].tolist(), + 'B10_superstructure': global_capital_costs['B10_superstructure'].tolist(), + 'D3020_heat_generating_systems': global_capital_costs['D3020_heat_generating_systems'].tolist(), + 'D3030_cooling_generation_systems': global_capital_costs['D3030_cooling_generation_systems'].tolist(), + 'D3080_other_hvac_ahu': global_capital_costs['D3080_other_hvac_ahu'].tolist(), + 'D5020_lighting_and_branch_wiring': global_capital_costs['D5020_lighting_and_branch_wiring'].tolist(), + 'D301010_photovoltaic_system': global_capital_costs['D301010_photovoltaic_system'].tolist(), + }, + 'global_end_of_life_costs': life_cycle[f'Scenario {scenario}']['global_end_of_life_costs']['End_of_life_costs'].tolist(), + 'global_operational_costs': { + 'fixed_costs_electricity_peak': global_operational_costs['Fixed_costs_electricity_peak'].tolist(), + 'fixed_costs_electricity_monthly': global_operational_costs['Fixed_costs_electricity_monthly'].tolist(), + 'variable_costs_electricity': global_operational_costs['Variable_costs_electricity'].tolist(), + 'fixed_costs_gas': global_operational_costs['Fixed_costs_gas'].tolist(), + 'variable_costs_gas': global_operational_costs['Variable_costs_gas'].tolist() + }, + 'global_maintenance_costs': { + 'heating_maintenance': global_maintenance_costs['Heating_maintenance'].tolist(), + 'cooling_maintenance': global_maintenance_costs['Cooling_maintenance'].tolist(), + 'pv_maintenance': global_maintenance_costs['PV_maintenance'].tolist(), + }, + 'global_operational_incomes': life_cycle[f'Scenario {scenario}']['global_operational_incomes']['Incomes electricity'].tolist(), + 'global_capital_incomes': { + 'subsidies_construction': global_capital_incomes['Subsidies construction'].tolist(), + 'subsidies_hvac': global_capital_incomes['Subsidies HVAC'].tolist(), + 'subsidies_pv': global_capital_incomes['Subsidies PV'].tolist() + } } - } - print(operational_co2) - building_results['operational_co2'] = operational_co2 + building_results['operational_co2'] = operational_co2 return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)