""" Usage 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 from flask import Response, request from flask_restful import Resource from city_model_structure.attributes.schedule import Schedule from city_model_structure.building_demand.appliances import Appliances from city_model_structure.building_demand.lighting import Lighting from city_model_structure.building_demand.occupancy import Occupancy from city_model_structure.building_demand.thermal_control import ThermalControl from city_model_structure.building_demand.usage_zone import UsageZone import helpers.constants as cte from hub_api.helpers.auth import role_required from hub_api.config import Config from persistence.models import UserRoles class Usage(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) catalog = request.json['usage_catalog'] usage_name = None try: building_names = request.json['building_names'] usages = request.json['usages'] seconds_in_hour = cte.MINUTES_TO_SECONDS * cte.HOUR_TO_MINUTES 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: internal_zone.usage_zones = [] for usage in usages: usage_entry = catalog.get_entry(usage['name']) occupancy_schedules = [] for schedule in usage_entry.occupancy.schedules: occupancy_schedule = Schedule() occupancy_schedule.type = schedule.type occupancy_schedule.values = schedule.values occupancy_schedule.data_type = schedule.data_type occupancy_schedule.time_step = schedule.time_step occupancy_schedule.time_range = schedule.time_range occupancy_schedule.day_types = schedule.day_types occupancy_schedules.append(occupancy_schedule) occupancy = Occupancy() occupancy.occupancy_density = usage_entry.occupancy.occupancy_density occupancy.sensible_convective_internal_gain = usage_entry.occupancy.sensible_convective_internal_gain occupancy.sensible_radiative_internal_gain = usage_entry.occupancy.sensible_radiative_internal_gain occupancy.latent_internal_gain = usage_entry.occupancy.latent_internal_gain occupancy.occupancy_schedules = occupancy_schedules lighting_schedules = [] for schedule in usage_entry.lighting.schedules: lighting_schedule = Schedule() lighting_schedule.type = schedule.type lighting_schedule.values = schedule.values lighting_schedule.data_type = schedule.data_type lighting_schedule.time_step = schedule.time_step lighting_schedule.time_range = schedule.time_range lighting_schedule.day_types = schedule.day_types lighting_schedules.append(lighting_schedule) lighting = Lighting() lighting.density = usage_entry.lighting.density lighting.convective_fraction = usage_entry.lighting.convective_fraction lighting.radiative_fraction = usage_entry.lighting.radiative_fraction lighting.latent_fraction = usage_entry.lighting.latent_fraction lighting.schedules = lighting_schedules appliances_schedules = [] for schedule in usage_entry.appliances.schedules: appliances_schedule = Schedule() appliances_schedule.type = schedule.type appliances_schedule.values = schedule.values appliances_schedule.data_type = schedule.data_type appliances_schedule.time_step = schedule.time_step appliances_schedule.time_range = schedule.time_range appliances_schedule.day_types = schedule.day_types appliances_schedules.append(appliances_schedule) appliances = Appliances() appliances.density = usage_entry.appliances.density appliances.convective_fraction = usage_entry.appliances.convective_fraction appliances.radiative_fraction = usage_entry.appliances.radiative_fraction appliances.latent_fraction = usage_entry.appliances.latent_fraction appliances.schedules = appliances_schedules hvac_schedules = [] for schedule in usage_entry.thermal_control.hvac_availability_schedules: hvac_schedule = Schedule() hvac_schedule.type = schedule.type hvac_schedule.values = schedule.values hvac_schedule.data_type = schedule.data_type hvac_schedule.time_step = schedule.time_step hvac_schedule.time_range = schedule.time_range hvac_schedule.day_types = schedule.day_types hvac_schedules.append(hvac_schedule) heating_schedules = [] for schedule in usage_entry.thermal_control.heating_set_point_schedules: heating_schedule = Schedule() heating_schedule.type = schedule.type heating_schedule.values = schedule.values heating_schedule.data_type = schedule.data_type heating_schedule.time_step = schedule.time_step heating_schedule.time_range = schedule.time_range heating_schedule.day_types = schedule.day_types heating_schedules.append(heating_schedule) cooling_schedules = [] for schedule in usage_entry.thermal_control.cooling_set_point_schedules: cooling_schedule = Schedule() cooling_schedule.type = schedule.type cooling_schedule.values = schedule.values cooling_schedule.data_type = schedule.data_type cooling_schedule.time_step = schedule.time_step cooling_schedule.time_range = schedule.time_range cooling_schedule.day_types = schedule.day_types cooling_schedules.append(cooling_schedule) thermal_control = ThermalControl() thermal_control.mean_heating_set_point = usage_entry.thermal_control.mean_heating_set_point thermal_control.heating_set_back = usage_entry.thermal_control.heating_set_back thermal_control.mean_cooling_set_point = usage_entry.thermal_control.mean_cooling_set_point thermal_control.hvac_availability_schedules = hvac_schedules thermal_control.heating_set_point_schedules = heating_schedules thermal_control.cooling_set_point_schedules = cooling_schedules usage_zone = UsageZone() usage_zone.usage = usage_entry.usage usage_zone.percentage = usage['percentage'] usage_zone.hours_day = usage_entry.hours_day usage_zone.days_year = usage_entry.days_year usage_zone.mechanical_air_change = usage_entry.mechanical_air_change if usage_entry.mechanical_air_change is None: usage_zone.mechanical_air_change = ((usage_entry.ventilation_rate * internal_zone.area) / internal_zone.volume) * seconds_in_hour usage_zone.occupancy = occupancy usage_zone.lighting = lighting usage_zone.appliances = appliances usage_zone.thermal_control = thermal_control internal_zone.usage_zones.append(usage_zone) response = {'result': 'succeed'} except KeyError as ex: response = {'error': f'Mandatory parameter {ex} is missing'} return Response(json.dumps(response), status=400) except IndexError: response = {'error': f'Name "{usage_name}" unknown'} return Response(json.dumps(response), status=400) return Response(json.dumps(response))