""" Construction catalog SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca """ import json from flask import request, Response from flask_restful import Resource from hub_api.helpers.session_helper import refresh_session class ToJson: @staticmethod def usage_to_json(usage): dictionary = { 'usage': usage.usage, 'hours_day': usage.hours_day, 'days_year': usage.days_year, 'mechanical_air_change': usage.mechanical_air_change if usage.mechanical_air_change is not None else '', 'ventilation_rate': usage.ventilation_rate if usage.ventilation_rate is not None else '', 'occupancy': ToJson.occupancy_to_json(usage.occupancy), 'lighting': ToJson.lighting_to_json(usage.lighting), 'appliances': ToJson.appliances_to_json(usage.appliances), 'thermal_control': ToJson.thermal_control_to_json(usage.thermal_control) } return dictionary @staticmethod def occupancy_to_json(occupancy): dictionary = { 'occupancy_density': occupancy.occupancy_density, 'sensible_convective_internal_gain': occupancy.sensible_convective_internal_gain, 'sensible_radiative_internal_gain': occupancy.sensible_radiative_internal_gain, 'latent_internal_gain': occupancy.latent_internal_gain, 'schedules': [] } for schedule in occupancy.schedules: dictionary['schedules'].append(ToJson.schedule_to_json(schedule)) return dictionary @staticmethod def lighting_to_json(lighting): dictionary = { 'density': lighting.density, 'convective_fraction': lighting.convective_fraction, 'radiative_fraction': lighting.radiative_fraction, 'latent_fraction': lighting.latent_fraction, 'schedules': [] } for schedule in lighting.schedules: dictionary['schedules'].append(ToJson.schedule_to_json(schedule)) return dictionary @staticmethod def appliances_to_json(appliances): dictionary = { 'density': appliances.density, 'convective_fraction': appliances.convective_fraction, 'radiative_fraction': appliances.radiative_fraction, 'latent_fraction': appliances.latent_fraction, 'schedules': [] } for schedule in appliances.schedules: dictionary['schedules'].append(ToJson.schedule_to_json(schedule)) return dictionary @staticmethod def thermal_control_to_json(thermal_control): dictionary = { 'mean_heating_set_point': thermal_control.mean_heating_set_point, 'heating_set_back': thermal_control.heating_set_back, 'mean_cooling_set_point': thermal_control.mean_cooling_set_point, 'hvac_availability_schedules': [], 'heating_set_point_schedules': [], 'cooling_set_point_schedules': [], } for schedule in thermal_control.hvac_availability_schedules: dictionary['hvac_availability_schedules'].append(ToJson.schedule_to_json(schedule)) for schedule in thermal_control.heating_set_point_schedules: dictionary['heating_set_point_schedules'].append(ToJson.schedule_to_json(schedule)) for schedule in thermal_control.cooling_set_point_schedules: dictionary['cooling_set_point_schedules'].append(ToJson.schedule_to_json(schedule)) return dictionary @staticmethod def schedule_to_json(schedule): schedule_dictionary = { 'type': schedule.type, 'data_type': schedule.data_type, 'time_step': schedule.time_step, 'time_range': schedule.time_range, 'day_types': schedule.day_types, 'values': schedule.values, } return schedule_dictionary class UsageCatalogEntry(Resource): def __init__(self): pass @staticmethod def post(): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.usage_catalog name = None if request.data == b'' or request.json['name'] is None: response = {'error': 'Mandatory parameter "name" is missing'} return Response(json.dumps(response), headers=headers, status=400) try: name = request.json['name'] entry = catalog.get_entry(name) output = {'usages': [ToJson.usage_to_json(entry)]} return Response(json.dumps(output), headers=headers) except IndexError: response = {'error': f'Name "{name}" unknown'} return Response(json.dumps(response), headers=headers, status=400) class UsageCatalogEntries(Resource): def __init__(self): pass @staticmethod def post(): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.usage_catalog output = {'usages': []} content = catalog.entries() for usage in content.usages: output['usages'].append(ToJson.usage_to_json(usage)) return Response(json.dumps(output), headers=headers) class UsageCatalogNames(Resource): def __init__(self): pass @staticmethod def post(): session = refresh_session(request) if session is None: return Response(json.dumps({'error': 'invalid session'}), status=401) headers = session.headers catalog = session.usage_catalog return Response(json.dumps(catalog.names()), headers=headers)