""" Construction SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca """ import json import uuid from flask import Response, request from flask_restful import Resource from city_model_structure.building_demand.layer import Layer from city_model_structure.building_demand.material import Material from hub_api.helpers.session_helper import refresh_session class Construction(Resource): def __init__(self): pass @staticmethod def put(): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers city = session.city 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), headers=headers, status=400) return Response(json.dumps(response), headers=headers)