api_v1.4/hub_api/construction.py

77 lines
4.0 KiB
Python

"""
Construction
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
Code contributors: Peter Yefi peteryefi@gmail.com
"""
import json
import uuid
from flask import Response, request
from flask_restful import Resource
from hub_api.config import Config
from city_model_structure.building_demand.layer import Layer
from city_model_structure.building_demand.material import Material
from persistence.models import UserRoles
from hub_api.helpers.auth import role_required
class Construction(Resource, Config):
def __init__(self):
super().__init__()
@role_required([UserRoles.Admin.value, UserRoles.Hub_Reader.value])
def put(self, city_id):
city = self.get_city(city_id)
try:
building_names = request.json['building_names']
constructions = request.json['constructions']
for construction in constructions:
construction_type = construction['type']
layers = construction['layers']
for building_name in building_names:
for building in city.buildings:
if building.human_readable_name != building_name:
continue
for internal_zone in building.internal_zones:
for thermal_zone in internal_zone.thermal_zones:
thermal_zone.additional_thermal_bridge_u_value = request.json['extra_loses_due_to_thermal_bridges']
thermal_zone.indirect_heated_area_ratio = request.json['indirect_heated_ratio']
thermal_zone.infiltration_rate_system_on = request.json['infiltration_rate_for_ventilation_system_on']
thermal_zone.infiltration_rate_system_off = request.json['infiltration_rate_for_ventilation_system_off']
for thermal_boundary in thermal_zone.thermal_boundaries:
if thermal_boundary.parent_surface.type == construction_type:
_layers = []
for layer in layers:
_layer = Layer()
thermal_resistance = layer['material']['thermal_resistance']
conductivity = layer['material']['conductivity']
density = layer['material']['density']
specific_heat = layer['material']['specific_heat']
_layer.thickness = layer['thickness']
_material = Material()
_material.id = layer['material']['id']
_material.name = layer['material']['name']
_material.solar_absorptance = layer['material']['solar_absorptance']
_material.thermal_absorptance = layer['material']['thermal_absorptance']
_material.visible_absorptance = layer['material']['visible_absorptance']
_material.no_mass = layer['material']['no_mass']
_material.thermal_resistance = (thermal_resistance if thermal_resistance != '' else None)
_material.conductivity = (conductivity if conductivity != '' else None)
_material.density = (density if density != '' else None)
_material.specific_heat = (specific_heat if specific_heat != '' else None)
_layer.material = _material
_layers.append(_layer)
thermal_boundary.layers = _layers
if 'window' in construction.keys():
for thermal_opening in thermal_boundary.thermal_openings:
thermal_opening.frame_ratio = construction['window']['frame_ratio']
thermal_opening.g_value = construction['window']['g_value']
thermal_opening.overall_u_value = construction['window']['overall_u_value']
response = {'result': 'succeed'}
except KeyError as ex:
response = {'error': f'Mandatory parameter {ex} is missing'}
return Response(json.dumps(response), status=400)
return Response(json.dumps(response), status=200)