meb workflow in the api should now be completed

This commit is contained in:
Guille Gutierrez 2024-05-02 15:21:34 +02:00
parent 31ebe48f68
commit d98674a286
4 changed files with 61 additions and 38 deletions

View File

@ -29,7 +29,7 @@ from hub_api.config import Config
sh.begin_time = datetime.datetime.now()
app = flask.Flask('cerc_api')
app.json_provider_class = LazyJSONEncoder
app.config['MAX_CONTENT_LENGTH'] = Config.max_file_size
app.config['MAX_CONTENT_LENGTH'] = int(Config.max_file_size())
api = Api(app)
api.add_resource(Uptime, '/v1.4/uptime')

View File

@ -23,17 +23,16 @@ class Config:
environment = 'PROD'
database_name = 'montreal_retrofit'
self._max_file_size = 10 * 1024 * 1024 # 10 MB
self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._repository = Repository(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
self._energy_systems_catalog = EnergySystemsCatalogFactory('montreal_custom').catalog
self._dictionaries = {
'pluto': hub.helpers.dictionaries.Dictionaries.pluto_function_to_hub_function,
'htf': hub.helpers.dictionaries.Dictionaries.hft_function_to_hub_function,
'montreal': hub.helpers.dictionaries.Dictionaries.montreal_function_to_hub_function,
'alkis': hub.helpers.dictionaries.Dictionaries.alkis_function_to_hub_function,
'eilat': hub.helpers.dictionaries.Dictionaries.eilat_function_to_hub_function
'pluto': hub.helpers.dictionaries.Dictionaries().pluto_function_to_hub_function,
'htf': hub.helpers.dictionaries.Dictionaries().hft_function_to_hub_function,
'montreal': hub.helpers.dictionaries.Dictionaries().montreal_function_to_hub_function,
'alkis': hub.helpers.dictionaries.Dictionaries().alkis_function_to_hub_function,
'eilat': hub.helpers.dictionaries.Dictionaries().eilat_function_to_hub_function
}
@property
@ -48,10 +47,11 @@ class Config:
def energy_systems_catalog(self):
return self._energy_systems_catalog
@property
def max_file_size(self):
return self._max_file_size
@staticmethod
def max_file_size():
return 10 * 1024 * 1024 # 10 MB
@property
def function_dictionary(self, dictionary):
return self._dictionaries[dictionary]

View File

@ -23,7 +23,7 @@ from hub_api.helpers.session_helper import refresh_session
class InselMonthlyEnergyBalance(Resource, Config):
def __init__(self):
super().__init__()
self._extensions = ['geojson', 'citygml']
self._extensions = ['.geojson', '.citygml']
self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
self._city = None
@ -31,22 +31,35 @@ class InselMonthlyEnergyBalance(Resource, Config):
self._file_extension = Path(filename).suffix
return self._file_extension in self._extensions
@staticmethod
def _geojson(file_path):
height_field = request.args.get('height_field')
year_of_construction_field = request.args.get('year_of_construction_field')
function_field = request.args.get('function_field')
function_dictionary = Config.function_dictionary(request.args.get('function_dictionary_name'))
def _geojson(self, file_path):
try:
height_field = request.form.get('height_field')
year_of_construction_field = request.form.get('year_of_construction_field')
function_field = request.form.get('function_field')
function_dictionary = self._dictionaries[request.form.get('function_dictionary_name')]
return GeometryFactory('geojson',
path=file_path,
height_field=height_field,
year_of_construction_field=year_of_construction_field,
function_field=function_field,
function_to_hub=function_dictionary).city
except TypeError:
return None
@staticmethod
def _citygml(file_path):
raise NotImplementedError
def _citygml(self, file_path):
try:
year_of_construction_field = request.form.get('year_of_construction_field')
function_field = request.form.get('function_field')
function_dictionary = self._dictionaries[request.form.get('function_dictionary_name')]
hub_crs = request.form.get('hub_crs')
return GeometryFactory('citygml',
path=file_path,
year_of_construction_field=year_of_construction_field,
function_field=function_field,
function_to_hub=function_dictionary,
hub_crs=hub_crs).city
except TypeError:
return None
def post(self):
"""
@ -56,23 +69,32 @@ class InselMonthlyEnergyBalance(Resource, Config):
token = request.headers.get('token', None)
application_uuid = request.headers.get('application-uuid', None)
_session = refresh_session(session_id, token, application_uuid)
_session = {'token': token}
response_token = {'token': _session['token']}
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
else:
tmp_path = (self._tmp_path / token).resolve()
try:
os.mkdir(tmp_path)
except:
print('already exist')
geometry_file = request.files['geometry_file']
if not self._allowed_extensions(geometry_file):
return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=token)
if not self._allowed_extensions(geometry_file.filename):
shutil.rmtree(tmp_path)
return Response(json.dumps({'error': 'Unsupported media type'}), status=415, headers=response_token)
filename = secure_filename(geometry_file.filename)
file_path = os.path.join(tmp_path, filename)
geometry_file.save(file_path)
if self._file_extension == 'geojson':
self._city = InselMonthlyEnergyBalance._geojson(file_path)
if self._file_extension == '.geojson':
self._city = self._geojson(file_path)
else:
self._city = InselMonthlyEnergyBalance._citygml(file_path)
construction_handler = request.args.get('construction_handler')
usage_handler = request.args.get('usage_handler')
self._city = self._citygml(file_path)
if self._city is None:
shutil.rmtree(tmp_path)
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=response_token)
construction_handler = request.form.get('construction_handler')
usage_handler = request.form.get('usage_handler')
WeatherFactory('epw', self._city).enrich()
ConstructionFactory(construction_handler, self._city).enrich()
UsageFactory(usage_handler, self._city).enrich()
@ -87,7 +109,7 @@ class InselMonthlyEnergyBalance(Resource, Config):
ResultFactory('insel_monthly_energy_balance', self._city, tmp_path).enrich()
results = {}
for building in self._city.buildings:
results[building] = {
results[building.name] = {
'monthly_heating_demand': building.heating_demand[cte.MONTH],
'yearly_heating_demand': building.heating_demand[cte.YEAR],
'monthly_cooling_demand': building.cooling_demand[cte.MONTH],
@ -98,4 +120,4 @@ class InselMonthlyEnergyBalance(Resource, Config):
'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR]
}
shutil.rmtree(tmp_path)
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
return Response(json.dumps({'result': 'succeed', 'results': json.dumps(results)}), status=200, headers=response_token)

View File

@ -29,3 +29,4 @@ python-dotenv
mapbox_earcut
cerc-costs
cerc-co2-emission
werkzeug