Compare commits
35 Commits
anh/api-re
...
main
Author | SHA1 | Date | |
---|---|---|---|
34779af69a | |||
55638fe147 | |||
a9af75bc3b | |||
7b45317d4b | |||
bb0bd4d31f | |||
ce18c72fe3 | |||
73641f985e | |||
8fbfd4be89 | |||
3be3121922 | |||
d7634a0d45 | |||
5097c69761 | |||
7a5fa1f09b | |||
b49e70c0d2 | |||
f5b48fee4e | |||
921034a884 | |||
d66ad362c7 | |||
1371f3f2e5 | |||
20c7425637 | |||
7b1cc06454 | |||
a322c59787 | |||
6d4c1437a7 | |||
d98674a286 | |||
31ebe48f68 | |||
c414de2635 | |||
3f35ce1c2c | |||
7753d69a54 | |||
8cfd6ebe82 | |||
0623faaafc | |||
8223d6ff2a | |||
d059a75e8d | |||
087845f83f | |||
6f344c8f2c | |||
3d3a20f560 | |||
0661ab21ec | |||
ab4745bca9 |
54
bootstrap.py
54
bootstrap.py
|
@ -18,16 +18,20 @@ import threading
|
||||||
import hub_api.helpers.session_helper as sh
|
import hub_api.helpers.session_helper as sh
|
||||||
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
|
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
|
||||||
from hub_api.control.uptime import Uptime
|
from hub_api.control.uptime import Uptime
|
||||||
|
from hub_api.persistence.full_retrofit_results import FullRetrofitResults
|
||||||
from hub_api.persistence.retrofit_results import RetrofitResults
|
from hub_api.persistence.retrofit_results import RetrofitResults
|
||||||
from hub_api.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance
|
from hub_api.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance
|
||||||
from hub_api.workflow.costs import Costs
|
from hub_api.workflow.costs import Costs
|
||||||
from hub_api.workflow.energy_plus import EnergyPlus
|
from hub_api.workflow.energy_plus import EnergyPlus
|
||||||
|
from hub_api.workflow.glb import Glb
|
||||||
from hub_api.energy_plus.idf_generator import IdfGenerator
|
from hub_api.energy_plus.idf_generator import IdfGenerator
|
||||||
|
from hub_api.config import Config
|
||||||
|
|
||||||
|
|
||||||
sh.begin_time = datetime.datetime.now()
|
sh.begin_time = datetime.datetime.now()
|
||||||
app = flask.Flask('cerc_api')
|
app = flask.Flask('cerc_api')
|
||||||
app.json_provider_class = LazyJSONEncoder
|
app.json_provider_class = LazyJSONEncoder
|
||||||
|
app.config['MAX_CONTENT_LENGTH'] = int(Config.max_file_size())
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
|
|
||||||
api.add_resource(Uptime, '/v1.4/uptime')
|
api.add_resource(Uptime, '/v1.4/uptime')
|
||||||
|
@ -39,6 +43,7 @@ api.add_resource(KeepSessionAlive, '/v1.4/session/keep-alive')
|
||||||
|
|
||||||
# persistence
|
# persistence
|
||||||
api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results')
|
api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results')
|
||||||
|
api.add_resource(FullRetrofitResults, '/v1.4/persistence/full-retrofit-results')
|
||||||
|
|
||||||
# energy plus
|
# energy plus
|
||||||
api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')
|
api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')
|
||||||
|
@ -46,39 +51,38 @@ api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')
|
||||||
# workflows
|
# workflows
|
||||||
api.add_resource(Costs, '/v1.4/workflow/costs')
|
api.add_resource(Costs, '/v1.4/workflow/costs')
|
||||||
api.add_resource(EnergyPlus, '/v1.4/workflow/energy-plus')
|
api.add_resource(EnergyPlus, '/v1.4/workflow/energy-plus')
|
||||||
api.add_resource(InselMonthlyEnergyBalance,
|
api.add_resource(Glb, '/v1.4/workflow/glb')
|
||||||
'/v1.4/workflow/insel-monthly-energy-balance')
|
api.add_resource(InselMonthlyEnergyBalance, '/v1.4/workflow/insel-monthly-energy-balance')
|
||||||
|
|
||||||
yml_path = Path('./hub/docs/openapi-specs.yml').resolve()
|
yml_path = Path('./docs/openapi-specs.yml').resolve()
|
||||||
|
|
||||||
with open(yml_path, "r") as stream:
|
with open(yml_path, "r") as stream:
|
||||||
swagger_config = {
|
swagger_config = {
|
||||||
"headers": [],
|
"headers": [],
|
||||||
"specs": [
|
"specs": [
|
||||||
{
|
{
|
||||||
"endpoint": '/api/apispec',
|
"endpoint": '/api/apispec',
|
||||||
"route": '/api/apispec/apispec.json',
|
"route": '/api/apispec/apispec.json',
|
||||||
"rule_filter": lambda rule: True, # all in
|
"rule_filter": lambda rule: True, # all in
|
||||||
"model_filter": lambda tag: True, # all in
|
"model_filter": lambda tag: True, # all in
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"static_url_path": "/api/static",
|
"static_url_path": "/api/static",
|
||||||
"specs_route": "/api/api-docs/",
|
"specs_route": "/api/api-docs/",
|
||||||
"openapi": "3.0.0"
|
"openapi": "3.0.0"
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
Swagger(app, template=yaml.safe_load(stream), config=swagger_config)
|
Swagger(app, template=yaml.safe_load(stream), config=swagger_config)
|
||||||
except yaml.YAMLError as exc:
|
except yaml.YAMLError as exc:
|
||||||
print(f'error: {exc}')
|
print(f'error: {exc}')
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def home():
|
def home():
|
||||||
return Response(headers={'Access-Control-Allow-Origin': '*'})
|
return Response(headers={'Access-Control-Allow-Origin': '*'})
|
||||||
|
|
||||||
|
|
||||||
sh.debug_mode = True
|
sh.debug_mode = False
|
||||||
|
|
||||||
threading.Thread(target=sh.expired_sessions_collector,
|
threading.Thread(target=sh.expired_sessions_collector, daemon=True, args="5").start()
|
||||||
daemon=True, args="5").start()
|
|
||||||
app.run(port=15789, host="0.0.0.0", debug=sh.debug_mode)
|
app.run(port=15789, host="0.0.0.0", debug=sh.debug_mode)
|
||||||
|
|
|
@ -3,13 +3,16 @@ Config
|
||||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||||
Copyright © 2023 Project Peter Yefi peteryefi@gmail.com
|
Copyright © 2023 Project Peter Yefi peteryefi@gmail.com
|
||||||
"""
|
"""
|
||||||
|
import distutils
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import hub.helpers.dictionaries
|
||||||
from hub.persistence.db_control import DBControl
|
from hub.persistence.db_control import DBControl
|
||||||
from hub.persistence.repository import Repository
|
from hub.persistence.repository import Repository
|
||||||
from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory
|
from hub.catalog_factories.energy_systems_catalog_factory import EnergySystemsCatalogFactory
|
||||||
|
from pymongo import MongoClient
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
@ -18,13 +21,26 @@ class Config:
|
||||||
dotenv_path = "{}/.local/etc/hub_api/.env".format(os.path.expanduser('~'))
|
dotenv_path = "{}/.local/etc/hub_api/.env".format(os.path.expanduser('~'))
|
||||||
if platform.system() == 'Linux':
|
if platform.system() == 'Linux':
|
||||||
dotenv_path = Path(dotenv_path).resolve()
|
dotenv_path = Path(dotenv_path).resolve()
|
||||||
|
environment = 'PROD'
|
||||||
environment = 'TEST'
|
load_dotenv(dotenv_path=dotenv_path)
|
||||||
database_name = 'montreal_retrofit_test'
|
self._database_name = os.getenv(f'{environment}_DB_NAME')
|
||||||
|
self._database = DBControl(db_name=self._database_name, app_env=environment, dotenv_path=dotenv_path)
|
||||||
self._database = DBControl(db_name=database_name, app_env=environment, dotenv_path=dotenv_path)
|
self._repository = Repository(db_name=self._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._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
|
||||||
|
}
|
||||||
|
# mongodb
|
||||||
|
_mongodb = os.getenv(f'{environment}_MONGO_DB')
|
||||||
|
_mongodb_database = os.getenv(f'{environment}_MONGO_DB_DATABASE')
|
||||||
|
self._mongodb_collection_prefix = os.getenv(f'{environment}_MONGO_DB_COLLECTION_PREFIX')
|
||||||
|
_client = MongoClient(_mongodb)
|
||||||
|
self._montreal_retrofit_db = _client[_mongodb_database]
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def database(self):
|
def database(self):
|
||||||
|
@ -37,3 +53,23 @@ class Config:
|
||||||
@property
|
@property
|
||||||
def energy_systems_catalog(self):
|
def energy_systems_catalog(self):
|
||||||
return self._energy_systems_catalog
|
return self._energy_systems_catalog
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def max_file_size():
|
||||||
|
return 10 * 1024 * 1024 # 10 MB
|
||||||
|
|
||||||
|
@property
|
||||||
|
def insel(self):
|
||||||
|
return distutils.spawn.find_executable('insel')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def sra(self):
|
||||||
|
return distutils.spawn.find_executable('sra')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def montreal_retrofit_db(self):
|
||||||
|
return self._montreal_retrofit_db
|
||||||
|
|
||||||
|
@property
|
||||||
|
def mongodb_collection_prefix(self):
|
||||||
|
return self._mongodb_collection_prefix
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -7,7 +7,7 @@ Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from flask import request, Response
|
from flask import request, Response
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
|
|
||||||
|
@ -25,9 +25,10 @@ class SessionStart(Resource, Config):
|
||||||
try:
|
try:
|
||||||
application_uuid = uuid.UUID(request.headers.get('application-uuid', None))
|
application_uuid = uuid.UUID(request.headers.get('application-uuid', None))
|
||||||
user_info = self.database.user_login(name=username, password=password, application_uuid=application_uuid)
|
user_info = self.database.user_login(name=username, password=password, application_uuid=application_uuid)
|
||||||
except ValueError:
|
except (ValueError, SQLAlchemyError, TypeError):
|
||||||
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
ip = request.remote_addr
|
ip = request.remote_addr
|
||||||
|
print(f'received valid login from {ip} {user_info}')
|
||||||
if user_info:
|
if user_info:
|
||||||
session_id = str(uuid.uuid4())
|
session_id = str(uuid.uuid4())
|
||||||
token = str(uuid.uuid4())
|
token = str(uuid.uuid4())
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,14 +1,54 @@
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from flask import request, Response, make_response, send_file
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
|
from hub.city_model_structure.city import City
|
||||||
|
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
|
||||||
|
from hub.version import __version__ as version
|
||||||
|
|
||||||
from hub_api.config import Config
|
from hub_api.config import Config
|
||||||
|
from hub_api.helpers.session_helper import refresh_session, session
|
||||||
|
|
||||||
|
|
||||||
class IdfGenerator(Resource, Config):
|
class IdfGenerator(Resource, Config):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""
|
"""
|
||||||
API call generate the IDF file for the input data
|
API call generate the IDF file for the input data
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
session_id = request.headers.get('session-id', None)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
token = _session['token']
|
||||||
|
application_id = session(session_id)['application_id']
|
||||||
|
user_id = session(session_id)['user_id']
|
||||||
|
|
||||||
|
tmp_path = (self._tmp_path / token).resolve()
|
||||||
|
try:
|
||||||
|
os.mkdir(tmp_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
payload = request.get_json()
|
||||||
|
for key, value in payload.items():
|
||||||
|
db_city = self.database.get_city(self.database.building(value, user_id, application_id, key).city_id)
|
||||||
|
|
||||||
|
if version != db_city.hub_release:
|
||||||
|
return Response(json.dumps({
|
||||||
|
'error': 'The selected building belongs to an old hub release and cannot be loaded.'
|
||||||
|
}), status=422)
|
||||||
|
idf_file = tmp_path/db_city.name
|
||||||
|
city = City.load_compressed(db_city.pickle_path, idf_file)
|
||||||
|
EnergyBuildingsExportsFactory('idf', city, tmp_path, target_buildings=[value]).export()
|
||||||
|
response = make_response(send_file(idf_file))
|
||||||
|
response.headers['token'] = token
|
||||||
|
return response
|
||||||
|
|
2
hub_api/energy_plus/tmp/.gitignore
vendored
Normal file
2
hub_api/energy_plus/tmp/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
Binary file not shown.
|
@ -3,9 +3,13 @@ Session helper
|
||||||
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||||
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
||||||
"""
|
"""
|
||||||
import uuid
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import shutil
|
||||||
import time
|
import time
|
||||||
|
import uuid
|
||||||
|
from copy import deepcopy
|
||||||
|
from glob import glob
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
sessions = {}
|
sessions = {}
|
||||||
begin_time = None
|
begin_time = None
|
||||||
|
@ -23,14 +27,19 @@ def expired_sessions_collector(session_timeout_duration):
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
if bool(sessions):
|
if bool(sessions):
|
||||||
for session_uuid in sessions:
|
_sessions = deepcopy(sessions)
|
||||||
print(sessions)
|
for session_uuid in _sessions:
|
||||||
print(sessions[session_uuid]['expire'])
|
_expire = datetime.datetime.strptime(_sessions[session_uuid]['expire'], '%Y-%m-%d %H:%M:%S.%f')
|
||||||
_expire = datetime.datetime.strptime(sessions[session_uuid]['expire'], '%Y-%m-%d %H:%M:%S.%f')
|
|
||||||
if _expire < datetime.datetime.now():
|
if _expire < datetime.datetime.now():
|
||||||
print("session for user: ", sessions[session_uuid]['user'], "expired.")
|
print("session for user: ", _sessions[session_uuid]['user'], "expired.")
|
||||||
|
response_path = (Path(__file__).parent.parent / f'response_files/{session_uuid}').resolve()
|
||||||
|
if response_path.exists():
|
||||||
|
shutil.rmtree(response_path)
|
||||||
del sessions[session_uuid]
|
del sessions[session_uuid]
|
||||||
|
existing_directories = glob(f'{Path(__file__).parent.parent.resolve()}/response_files/*')
|
||||||
|
for directory in existing_directories:
|
||||||
|
if directory not in _sessions.keys():
|
||||||
|
shutil.rmtree(directory)
|
||||||
time.sleep(60 * int(session_timeout_duration))
|
time.sleep(60 * int(session_timeout_duration))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ from hub.helpers.dictionaries import Dictionaries
|
||||||
|
|
||||||
|
|
||||||
from hub_api.mockup.properties import *
|
from hub_api.mockup.properties import *
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
|
|
||||||
class Building:
|
class Building:
|
||||||
|
@ -28,45 +27,44 @@ class Building:
|
||||||
self._total_pv_area = building_info.total_pv_area
|
self._total_pv_area = building_info.total_pv_area
|
||||||
self._energy_systems_archetype_name = building_info.system_name
|
self._energy_systems_archetype_name = building_info.system_name
|
||||||
self._heating_consumption = {
|
self._heating_consumption = {
|
||||||
cte.YEAR: results['yearly_heating_consumption'],
|
cte.YEAR: results['insel meb']['yearly_heating_consumption'],
|
||||||
cte.MONTH: results['monthly_heating_consumption']
|
cte.MONTH: results['insel meb']['monthly_heating_consumption']
|
||||||
}
|
}
|
||||||
print(self._heating_consumption)
|
|
||||||
self._cooling_consumption = {
|
self._cooling_consumption = {
|
||||||
cte.YEAR: results['yearly_cooling_consumption'],
|
cte.YEAR: results['insel meb']['yearly_cooling_consumption'],
|
||||||
cte.MONTH: results['monthly_cooling_consumption']
|
cte.MONTH: results['insel meb']['monthly_cooling_consumption']
|
||||||
}
|
}
|
||||||
self._domestic_hot_water_consumption = {
|
self._domestic_hot_water_consumption = {
|
||||||
cte.YEAR: results['yearly_domestic_hot_water_consumption'],
|
cte.YEAR: results['insel meb']['yearly_domestic_hot_water_consumption'],
|
||||||
cte.MONTH: results['monthly_domestic_hot_water_consumption']
|
cte.MONTH: results['insel meb']['monthly_domestic_hot_water_consumption']
|
||||||
}
|
}
|
||||||
self._lighting_electrical_demand = {
|
self._lighting_electrical_demand = {
|
||||||
cte.YEAR: results['yearly_lighting_electrical_demand'],
|
cte.YEAR: results['insel meb']['yearly_lighting_electrical_demand'],
|
||||||
cte.MONTH: results['monthly_lighting_electrical_demand']
|
cte.MONTH: results['insel meb']['monthly_lighting_electrical_demand']
|
||||||
}
|
}
|
||||||
self._appliances_electrical_demand = {
|
self._appliances_electrical_demand = {
|
||||||
cte.YEAR: results['yearly_appliances_electrical_demand'],
|
cte.YEAR: results['insel meb']['yearly_appliances_electrical_demand'],
|
||||||
cte.MONTH: results['monthly_appliances_electrical_demand']
|
cte.MONTH: results['insel meb']['monthly_appliances_electrical_demand']
|
||||||
}
|
}
|
||||||
self._heating_peak_load = {
|
self._heating_peak_load = {
|
||||||
cte.YEAR: results['yearly_heating_peak_load'],
|
cte.YEAR: results['insel meb']['yearly_heating_peak_load'],
|
||||||
cte.MONTH: results['monthly_heating_peak_load']
|
cte.MONTH: results['insel meb']['monthly_heating_peak_load']
|
||||||
}
|
}
|
||||||
self._cooling_peak_load = {
|
self._cooling_peak_load = {
|
||||||
cte.YEAR: results['yearly_cooling_peak_load'],
|
cte.YEAR: results['insel meb']['yearly_cooling_peak_load'],
|
||||||
cte.MONTH: results['monthly_cooling_peak_load']
|
cte.MONTH: results['insel meb']['monthly_cooling_peak_load']
|
||||||
}
|
}
|
||||||
self._lighting_peak_load = {
|
self._lighting_peak_load = {
|
||||||
cte.YEAR: results['yearly_lighting_peak_load'],
|
cte.YEAR: results['insel meb']['yearly_lighting_peak_load'],
|
||||||
cte.MONTH: results['monthly_lighting_peak_load']
|
cte.MONTH: results['insel meb']['monthly_lighting_peak_load']
|
||||||
}
|
}
|
||||||
self._appliances_peak_load = {
|
self._appliances_peak_load = {
|
||||||
cte.YEAR: results['yearly_appliances_peak_load'],
|
cte.YEAR: results['insel meb']['yearly_appliances_peak_load'],
|
||||||
cte.MONTH: results['monthly_appliances_peak_load']
|
cte.MONTH: results['insel meb']['monthly_appliances_peak_load']
|
||||||
}
|
}
|
||||||
self._onsite_electrical_production = {
|
self._onsite_electrical_production = {
|
||||||
cte.YEAR: results['yearly_on_site_electrical_production'],
|
cte.YEAR: results['insel meb']['yearly_on_site_electrical_production'],
|
||||||
cte.MONTH: results['monthly_on_site_electrical_production']
|
cte.MONTH: results['insel meb']['monthly_on_site_electrical_production']
|
||||||
}
|
}
|
||||||
self._catalog_archetype = catalog_archetype
|
self._catalog_archetype = catalog_archetype
|
||||||
|
|
||||||
|
@ -215,14 +213,13 @@ class Building:
|
||||||
:return: [EnergySystem]
|
:return: [EnergySystem]
|
||||||
"""
|
"""
|
||||||
_energy_systems = []
|
_energy_systems = []
|
||||||
|
|
||||||
for system in self._catalog_archetype.systems:
|
for system in self._catalog_archetype.systems:
|
||||||
_hub_demand_types = []
|
_hub_demand_types = []
|
||||||
for demand_type in system.demand_types:
|
for demand_type in system.demand_types:
|
||||||
# todo: generalize this when we have more catalogs
|
# todo: generalize this when we have more catalogs
|
||||||
_hub_demand_types.append(Dictionaries().montreal_demand_type_to_hub_energy_demand_type[demand_type])
|
_hub_demand_types.append(Dictionaries().montreal_demand_type_to_hub_energy_demand_type[demand_type])
|
||||||
demands = _hub_demand_types
|
demands = _hub_demand_types
|
||||||
fuel_type = Dictionaries().montreal_custom_fuel_to_hub_fuel[system.generation_system.fuel_type]
|
fuel_type = Dictionaries().montreal_custom_fuel_to_hub_fuel[system.generation_systems[0].fuel_type]
|
||||||
generic_generation_system = GenericGenerationSystem()
|
generic_generation_system = GenericGenerationSystem()
|
||||||
generic_generation_system.fuel_type = fuel_type
|
generic_generation_system.fuel_type = fuel_type
|
||||||
generation_system = GenerationSystem()
|
generation_system = GenerationSystem()
|
||||||
|
|
Binary file not shown.
45
hub_api/persistence/full_retrofit_results.py
Normal file
45
hub_api/persistence/full_retrofit_results.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
from flask import Response, request
|
||||||
|
from flask_restful import Resource
|
||||||
|
|
||||||
|
from hub_api.config import Config
|
||||||
|
from hub_api.helpers.session_helper import refresh_session
|
||||||
|
from hub_api.persistence.mock import dic
|
||||||
|
|
||||||
|
|
||||||
|
class FullRetrofitResults(Resource, Config):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
session_id = request.headers.get('session-id', None)
|
||||||
|
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
|
||||||
|
return Response(json.dumps(dic), status=200)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
results = {'current status': [],
|
||||||
|
'skin retrofit': [],
|
||||||
|
'system retrofit and pv': [],
|
||||||
|
'skin and system retrofit with pv': []
|
||||||
|
}
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
response_token = {'token': _session['token']}
|
||||||
|
json_request = request.get_json()
|
||||||
|
for scenario in json_request['scenarios']:
|
||||||
|
for key, buildings in scenario.items():
|
||||||
|
mongodb_collection = f'{self.mongodb_collection_prefix}{key.replace(" ", "_")}'
|
||||||
|
building_query = ''
|
||||||
|
for building in buildings:
|
||||||
|
building_query = f'{building_query} {{"alias": "{building}"}},'
|
||||||
|
query = f'{{"$or": [{building_query[:-1]}]}}'
|
||||||
|
cursor = self.montreal_retrofit_db[mongodb_collection].find(json.loads(query))
|
||||||
|
for result in cursor:
|
||||||
|
del result['_id']
|
||||||
|
result['building'] = result['alias']
|
||||||
|
results[key].append(result)
|
||||||
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)
|
1
hub_api/persistence/mock.py
Normal file
1
hub_api/persistence/mock.py
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,101 +1,142 @@
|
||||||
import json
|
import json
|
||||||
|
import threading
|
||||||
|
|
||||||
|
from co2_emission.co2_emission import Co2Emission
|
||||||
|
from costs.cost import Cost
|
||||||
from flask import Response, request
|
from flask import Response, request
|
||||||
from flask_restful import Resource
|
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.config import Config
|
||||||
from hub_api.helpers.session_helper import session, refresh_session
|
from hub_api.helpers.session_helper import session, refresh_session
|
||||||
from hub_api.mockup.building import Building
|
from hub_api.mockup.building import Building
|
||||||
|
from hub_api.persistence.mock import dic
|
||||||
|
|
||||||
|
|
||||||
class RetrofitResults(Resource, Config):
|
class RetrofitResults(Resource, Config):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._scenario_ids = {'current status': 0,
|
||||||
|
'skin retrofit': 1,
|
||||||
|
'system retrofit and pv': 2,
|
||||||
|
'skin and system retrofit with pv': 3
|
||||||
|
}
|
||||||
|
|
||||||
|
def _calculate_building(self, building_results, user_id, application_id, scenario, scenario_id):
|
||||||
|
building_info = self.database.building(building_results['building'], user_id, application_id, scenario)
|
||||||
|
archetype = self.energy_systems_catalog.get_entry(building_info.system_name)
|
||||||
|
mockup_building = Building(building_info, building_results, archetype)
|
||||||
|
life_cycle = Cost(mockup_building, retrofit_scenario=scenario_id).life_cycle
|
||||||
|
operational_co2 = Co2Emission(mockup_building).operational_co2
|
||||||
|
global_capital_costs = life_cycle[f'Scenario {scenario_id}']['global_capital_costs']
|
||||||
|
global_operational_costs = life_cycle[f'Scenario {scenario_id}']['global_operational_costs']
|
||||||
|
global_capital_incomes = life_cycle[f'Scenario {scenario_id}']['global_capital_incomes']
|
||||||
|
global_maintenance_costs = life_cycle[f'Scenario {scenario_id}']['global_maintenance_costs']
|
||||||
|
building_results['total_heating_area'] = building_info.total_heating_area
|
||||||
|
building_results['year_of_construction'] = building_info.year_of_construction
|
||||||
|
building_results['function'] = building_info.function
|
||||||
|
building_results['costs'] = {
|
||||||
|
'total_capital_costs_skin': life_cycle[f'Scenario {scenario_id}']['total_capital_costs_skin'],
|
||||||
|
'total_capital_costs_systems': life_cycle[f'Scenario {scenario_id}']['total_capital_costs_systems'],
|
||||||
|
'end_of_life_costs': life_cycle[f'Scenario {scenario_id}']['end_of_life_costs'],
|
||||||
|
'total_operational_costs': life_cycle[f'Scenario {scenario_id}']['total_operational_costs'],
|
||||||
|
'total_maintenance_costs': life_cycle[f'Scenario {scenario_id}']['total_maintenance_costs'],
|
||||||
|
'operational_incomes': life_cycle[f'Scenario {scenario_id}']['operational_incomes'],
|
||||||
|
'capital_incomes': life_cycle[f'Scenario {scenario_id}']['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_id}']['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_id}']['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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
building_results['operational_co2'] = operational_co2
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""
|
"""
|
||||||
API call for requesting a specified list of enriched persistence
|
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
|
# todo: cost and co2 libs are using default canadians values, in the future need to be optionally API configurable
|
||||||
session_id = request.headers.get('session-id', None)
|
session_id = request.headers.get('session-id', None)
|
||||||
|
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
|
||||||
|
return Response(json.dumps(dic), status=200)
|
||||||
token = request.headers.get('token', None)
|
token = request.headers.get('token', None)
|
||||||
application_uuid = request.headers.get('application-uuid', None)
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
_session = refresh_session(session_id, token, application_uuid)
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
if _session is None:
|
if _session is None:
|
||||||
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
token = {'token': _session['token']}
|
else:
|
||||||
application_id = session(session_id)['application_id']
|
token = {'token': _session['token']}
|
||||||
user_id = session(session_id)['user_id']
|
application_id = session(session_id)['application_id']
|
||||||
|
user_id = session(session_id)['user_id']
|
||||||
payload = request.get_json()
|
payload = request.get_json()
|
||||||
if 'scenarios' not in payload:
|
if 'scenarios' not in payload:
|
||||||
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
|
return Response(json.dumps({'error': 'Bad request'}), status=400, headers=token)
|
||||||
|
|
||||||
results = self.database.results(user_id, application_id, payload)
|
results = self.database.results(user_id, application_id, payload)
|
||||||
if results == {}:
|
if results == {}:
|
||||||
# no data found for the given parameters
|
# no data found for the given parameters
|
||||||
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
||||||
|
|
||||||
# deserialize the response to return pure json
|
# deserialize the response to return pure json
|
||||||
|
t = []
|
||||||
for scenario in results:
|
for scenario in results:
|
||||||
|
scenario_id = self._scenario_ids[scenario]
|
||||||
for building_results in results[scenario]:
|
for building_results in results[scenario]:
|
||||||
values = []
|
f = threading.Thread(
|
||||||
building_info = self.database.building(building_results['building'], user_id, application_id, scenario)
|
target=self._calculate_building,
|
||||||
results_dictionary = {}
|
args=(building_results, user_id, application_id, scenario, scenario_id)
|
||||||
archetype = self.energy_systems_catalog.get_entry(building_info.system_name)
|
)
|
||||||
for value in building_results['insel meb']:
|
t.append(f)
|
||||||
key = next(iter(value))
|
f.start()
|
||||||
values.append({key: json.loads(str(value[key]))})
|
for f in t:
|
||||||
results_dictionary[key] = json.loads(str(value[key]))
|
f.join()
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
building_results['operational_co2'] = operational_co2
|
|
||||||
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=token)
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
session_id = request.headers.get('session-id', None)
|
||||||
|
if session_id == "deece4fa-6809-42b1-a4e6-36e9f3c6edc2":
|
||||||
|
return Response(json.dumps(dic), status=200)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
results = {'meb': []}
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
response_token = {'token': _session['token']}
|
||||||
|
buildings = request.get_json()['buildings']
|
||||||
|
building_query = ''
|
||||||
|
for building in buildings:
|
||||||
|
building_query = f'{building_query} {{"alias": "{building}"}},'
|
||||||
|
query = f'{{"$or": [{building_query[:-1]}]}}'
|
||||||
|
cursor = self.mongodb_meb.find(json.loads(query))
|
||||||
|
for result in cursor:
|
||||||
|
del result['_id']
|
||||||
|
results['meb'].append(result)
|
||||||
|
|
||||||
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)
|
||||||
|
|
2
hub_api/response_files/.gitignore
vendored
Normal file
2
hub_api/response_files/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
*
|
||||||
|
!.gitignore
|
|
@ -2,22 +2,13 @@ from flask_restful import Resource
|
||||||
|
|
||||||
from hub_api.config import Config
|
from hub_api.config import Config
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
config_json_path = "../configs/config.json"
|
|
||||||
|
|
||||||
with open(config_json_path, "r") as stream:
|
|
||||||
config = json.load(stream)
|
|
||||||
if config:
|
|
||||||
print(f'{json.dumps(config, indent=2)}')
|
|
||||||
|
|
||||||
|
|
||||||
class Costs(Resource, Config):
|
class Costs(Resource, Config):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""
|
"""
|
||||||
API call for performing the cost workflow
|
API call for performing the cost workflow
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
|
@ -1,14 +1,117 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import zipfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from flask import Response, request, send_file, make_response
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
|
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
|
||||||
|
from hub.imports.construction_factory import ConstructionFactory
|
||||||
|
from hub.imports.geometry_factory import GeometryFactory
|
||||||
|
from hub.imports.usage_factory import UsageFactory
|
||||||
|
from hub.imports.weather_factory import WeatherFactory
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
from hub_api.config import Config
|
from hub_api.config import Config
|
||||||
|
from hub_api.helpers.session_helper import refresh_session
|
||||||
|
|
||||||
|
|
||||||
class EnergyPlus(Resource, Config):
|
class EnergyPlus(Resource, Config):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._extensions = ['.geojson', '.gml']
|
||||||
|
self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
|
||||||
|
self._response_path = (Path(__file__).parent.parent / 'response_files').resolve()
|
||||||
|
self._city = None
|
||||||
|
|
||||||
|
def _allowed_extensions(self, filename):
|
||||||
|
self._file_extension = Path(filename).suffix
|
||||||
|
return self._file_extension in self._extensions
|
||||||
|
|
||||||
|
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_to_hub')]
|
||||||
|
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 KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
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 KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""
|
"""
|
||||||
API call for performing the energy plus workflow
|
API call for performing the monthly energy balance workflow
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
session_id = request.headers.get('session-id', None)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
response_token = {'token': _session['token']}
|
||||||
|
tmp_path = (self._tmp_path / token).resolve()
|
||||||
|
response_path = (self._response_path / session_id).resolve()
|
||||||
|
try:
|
||||||
|
os.mkdir(tmp_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
os.mkdir(response_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
geometry_file = request.files['geometry_file']
|
||||||
|
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 = self._geojson(file_path)
|
||||||
|
else:
|
||||||
|
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()
|
||||||
|
_idf = EnergyBuildingsExportsFactory('idf', self._city, tmp_path).export()
|
||||||
|
_idf.run()
|
||||||
|
result_files = [
|
||||||
|
str((tmp_path / f'{self._city.name}_out.csv').resolve()),
|
||||||
|
str((tmp_path / f'{self._city.name}_out.eso').resolve()),
|
||||||
|
str((tmp_path / f'{self._city.name}.idf').resolve()),
|
||||||
|
]
|
||||||
|
result_zip = (response_path / f'{token}.zip').resolve()
|
||||||
|
with zipfile.ZipFile(result_zip, 'w') as zf:
|
||||||
|
for result_file in result_files:
|
||||||
|
zf.write(result_file, Path(result_file).name)
|
||||||
|
shutil.rmtree(tmp_path)
|
||||||
|
response = make_response(send_file(result_zip))
|
||||||
|
response.headers['token'] = token
|
||||||
|
return response
|
||||||
|
|
105
hub_api/workflow/glb.py
Normal file
105
hub_api/workflow/glb.py
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
import glob
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import zipfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from flask import Response, request, send_file, make_response
|
||||||
|
from flask_restful import Resource
|
||||||
|
from hub.exports.exports_factory import ExportsFactory
|
||||||
|
from hub.imports.geometry_factory import GeometryFactory
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
|
from hub_api.config import Config
|
||||||
|
from hub_api.helpers.session_helper import refresh_session
|
||||||
|
|
||||||
|
|
||||||
|
class Glb(Resource, Config):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._extensions = ['.geojson', '.gml']
|
||||||
|
self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
|
||||||
|
self._response_path = (Path(__file__).parent.parent / 'response_files').resolve()
|
||||||
|
self._city = None
|
||||||
|
|
||||||
|
def _allowed_extensions(self, filename):
|
||||||
|
self._file_extension = Path(filename).suffix
|
||||||
|
return self._file_extension in self._extensions
|
||||||
|
|
||||||
|
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_to_hub')]
|
||||||
|
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 KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
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 KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
"""
|
||||||
|
API call for performing the monthly energy balance workflow
|
||||||
|
"""
|
||||||
|
session_id = request.headers.get('session-id', None)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
response_token = {'token': _session['token']}
|
||||||
|
tmp_path = (self._tmp_path / token).resolve()
|
||||||
|
response_path = (self._response_path / session_id).resolve()
|
||||||
|
try:
|
||||||
|
os.mkdir(tmp_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
os.mkdir(response_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
geometry_file = request.files['geometry_file']
|
||||||
|
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 = self._geojson(file_path)
|
||||||
|
else:
|
||||||
|
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)
|
||||||
|
ExportsFactory('glb', self._city, tmp_path).export()
|
||||||
|
result_files = glob.glob(f'{tmp_path}/*.glb')
|
||||||
|
result_zip = (response_path / f'{token}.zip').resolve()
|
||||||
|
with zipfile.ZipFile(result_zip, 'w') as zf:
|
||||||
|
for result_file in result_files:
|
||||||
|
zf.write(result_file, Path(result_file).name)
|
||||||
|
shutil.rmtree(tmp_path)
|
||||||
|
response = make_response(send_file(result_zip))
|
||||||
|
response.headers['token'] = token
|
||||||
|
return response
|
|
@ -1,14 +1,137 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import hub.helpers.constants as cte
|
||||||
|
from flask import Response, request
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
|
from hub.exports.energy_building_exports_factory import EnergyBuildingsExportsFactory
|
||||||
|
from hub.exports.exports_factory import ExportsFactory
|
||||||
|
from hub.imports.construction_factory import ConstructionFactory
|
||||||
|
from hub.imports.geometry_factory import GeometryFactory
|
||||||
|
from hub.imports.results_factory import ResultFactory
|
||||||
|
from hub.imports.usage_factory import UsageFactory
|
||||||
|
from hub.imports.weather_factory import WeatherFactory
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
from hub_api.config import Config
|
from hub_api.config import Config
|
||||||
|
from hub_api.helpers.session_helper import refresh_session
|
||||||
|
|
||||||
|
|
||||||
class InselMonthlyEnergyBalance(Resource, Config):
|
class InselMonthlyEnergyBalance(Resource, Config):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._extensions = ['.geojson', '.gml']
|
||||||
|
self._tmp_path = (Path(__file__).parent / 'tmp').resolve()
|
||||||
|
self._city = None
|
||||||
|
|
||||||
|
def _allowed_extensions(self, filename):
|
||||||
|
self._file_extension = Path(filename).suffix
|
||||||
|
return self._file_extension in self._extensions
|
||||||
|
|
||||||
|
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_to_hub')]
|
||||||
|
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 KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _citygml(self, file_path):
|
||||||
|
# try:
|
||||||
|
year_of_construction_field = request.form.get('year_of_construction_field')
|
||||||
|
if year_of_construction_field == '':
|
||||||
|
year_of_construction_field = None
|
||||||
|
function_field = request.form.get('function_field')
|
||||||
|
if function_field == '':
|
||||||
|
function_field = None
|
||||||
|
function_dictionary = self._dictionaries[request.form.get('function_to_hub')]
|
||||||
|
return GeometryFactory('citygml',
|
||||||
|
path=file_path,
|
||||||
|
year_of_construction_field=year_of_construction_field,
|
||||||
|
function_field=function_field,
|
||||||
|
function_to_hub=function_dictionary).city
|
||||||
|
# except KeyError:
|
||||||
|
# return None
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
"""
|
"""
|
||||||
API call for performing the monthly energy balance workflow
|
API call for performing the monthly energy balance workflow
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
session_id = request.headers.get('session-id', None)
|
||||||
|
token = request.headers.get('token', None)
|
||||||
|
application_uuid = request.headers.get('application-uuid', None)
|
||||||
|
_session = refresh_session(session_id, token, application_uuid)
|
||||||
|
if _session is None:
|
||||||
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
||||||
|
else:
|
||||||
|
response_token = {'token': _session['token']}
|
||||||
|
tmp_path = (self._tmp_path / token).resolve()
|
||||||
|
try:
|
||||||
|
os.mkdir(tmp_path)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
geometry_file = request.files['geometry_file']
|
||||||
|
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 = self._geojson(file_path)
|
||||||
|
else:
|
||||||
|
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()
|
||||||
|
ExportsFactory('sra', self._city, tmp_path).export()
|
||||||
|
sra_file = str((tmp_path / f'{self._city.name}_sra.xml').resolve())
|
||||||
|
subprocess.run([self.sra, sra_file], stdout=subprocess.DEVNULL)
|
||||||
|
ResultFactory('sra', self._city, tmp_path).enrich()
|
||||||
|
EnergyBuildingsExportsFactory('insel_monthly_energy_balance', self._city, tmp_path).export()
|
||||||
|
for building in self._city.buildings:
|
||||||
|
insel_path = (tmp_path / f'{building.name}.insel')
|
||||||
|
subprocess.run([self.insel, str(insel_path)], stdout=subprocess.DEVNULL)
|
||||||
|
ResultFactory('insel_monthly_energy_balance', self._city, tmp_path).enrich()
|
||||||
|
results = {}
|
||||||
|
for building in self._city.buildings:
|
||||||
|
results[building.name] = {
|
||||||
|
'total_heating_area': building.floor_area * building.storeys_above_ground,
|
||||||
|
'year_of_construction': building.year_of_construction,
|
||||||
|
'function': building.function,
|
||||||
|
'monthly_heating_demand': building.heating_demand[cte.MONTH],
|
||||||
|
'yearly_heating_demand': building.heating_demand[cte.YEAR],
|
||||||
|
'monthly_cooling_demand': building.cooling_demand[cte.MONTH],
|
||||||
|
'yearly_cooling_demand': building.cooling_demand[cte.YEAR],
|
||||||
|
'monthly_lighting_peak_load': building.lighting_peak_load[cte.MONTH],
|
||||||
|
'yearly_lighting_peak_load': building.lighting_peak_load[cte.YEAR],
|
||||||
|
'monthly_appliances_peak_load': building.appliances_peak_load[cte.MONTH],
|
||||||
|
'yearly_appliances_peak_load': building.appliances_peak_load[cte.YEAR],
|
||||||
|
'monthly_cooling_peak_load': building.cooling_peak_load[cte.MONTH],
|
||||||
|
'yearly_cooling_peak_load': building.cooling_peak_load[cte.YEAR],
|
||||||
|
'monthly_heating_peak_load': building.heating_peak_load[cte.MONTH],
|
||||||
|
'yearly_heating_peak_load': building.heating_peak_load[cte.YEAR],
|
||||||
|
'monthly_lighting_electrical_demand': building.lighting_electrical_demand[cte.MONTH],
|
||||||
|
'yearly_lighting_electrical_demand': building.lighting_electrical_demand[cte.YEAR],
|
||||||
|
'monthly_appliances_electrical_demand': building.appliances_electrical_demand[cte.MONTH],
|
||||||
|
'yearly_appliances_electrical_demand': building.appliances_electrical_demand[cte.YEAR],
|
||||||
|
'monthly_domestic_hot_water_heat_demand': building.domestic_hot_water_heat_demand[cte.MONTH],
|
||||||
|
'yearly_domestic_hot_water_heat_demand': building.domestic_hot_water_heat_demand[cte.YEAR],
|
||||||
|
}
|
||||||
|
shutil.rmtree(tmp_path)
|
||||||
|
return Response(json.dumps({'result': 'succeed', 'results': results}), status=200, headers=response_token)
|
||||||
|
|
2
hub_api/workflow/tmp/.gitignore
vendored
Normal file
2
hub_api/workflow/tmp/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.gitignore
|
||||||
|
!.gitignore
|
|
@ -1,23 +0,0 @@
|
||||||
import datetime
|
|
||||||
|
|
||||||
import flask
|
|
||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
from flasgger import LazyJSONEncoder, Swagger
|
|
||||||
from flask import Response
|
|
||||||
from flask_restful import Api
|
|
||||||
import threading
|
|
||||||
|
|
||||||
app = flask.Flask('cerc_api')
|
|
||||||
app.json_provider_class = LazyJSONEncoder
|
|
||||||
api = Api(app)
|
|
||||||
|
|
||||||
# Parse and register services.json
|
|
||||||
|
|
||||||
services_json_path = "./configs/services.json"
|
|
||||||
|
|
||||||
for service in json.load(open(services_json_path)):
|
|
||||||
print(f"service: {service}")
|
|
||||||
api.add_resource(service["name"], service["endpoint"])
|
|
||||||
|
|
||||||
app.run(port=5000, debug=True, host="0.0.0.0")
|
|
|
@ -1,9 +0,0 @@
|
||||||
{
|
|
||||||
"apiVersion": "v1.4.0",
|
|
||||||
"services": [
|
|
||||||
{
|
|
||||||
"name": "Costs",
|
|
||||||
"endpoint": "workflow/costs"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
from flask_restful import Resource
|
|
||||||
|
|
||||||
# from hub_api.config import Config
|
|
||||||
|
|
||||||
import json
|
|
||||||
|
|
||||||
|
|
||||||
class Costs(Resource):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
def post(self):
|
|
||||||
"""
|
|
||||||
API call for performing the cost workflow
|
|
||||||
"""
|
|
||||||
raise NotImplementedError()
|
|
|
@ -28,4 +28,8 @@ cerc-hub
|
||||||
python-dotenv
|
python-dotenv
|
||||||
mapbox_earcut
|
mapbox_earcut
|
||||||
cerc-costs
|
cerc-costs
|
||||||
cerc-co2-emission
|
cerc-co2-emission
|
||||||
|
werkzeug
|
||||||
|
sqlalchemy
|
||||||
|
pathlib
|
||||||
|
pymongo
|
Loading…
Reference in New Issue
Block a user