2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
HeatPump Service
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Project Author Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
|
2023-01-17 19:00:22 -05:00
|
|
|
from flask import request, Response
|
|
|
|
from hub_api.config import Config
|
2023-01-10 12:12:01 -05:00
|
|
|
from flask_restful import Resource
|
2023-01-16 15:31:02 -05:00
|
|
|
from hub_api.helpers.auth import role_required
|
2023-01-10 12:12:01 -05:00
|
|
|
from utils import HeatPumpSimulator
|
|
|
|
from utils import validate_hp_model
|
2023-01-16 15:31:02 -05:00
|
|
|
from persistence.models import UserRoles
|
2023-01-17 19:00:22 -05:00
|
|
|
from utils import expand_energy_demand
|
|
|
|
from hub_logger import logger
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
|
2023-01-17 19:00:22 -05:00
|
|
|
class HeatPump(Config, Resource):
|
2023-01-10 12:12:01 -05:00
|
|
|
def __init__(self):
|
2023-01-17 19:00:22 -05:00
|
|
|
super().__init__()
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-01-16 15:31:02 -05:00
|
|
|
@role_required([UserRoles.Admin.value, UserRoles.Hub_Reader.value])
|
2023-01-17 19:00:22 -05:00
|
|
|
def post(self, city_id):
|
|
|
|
payload = request.get_json()
|
|
|
|
city = self.get_city(city_id)
|
|
|
|
if validate_hp_model(payload['HeatPumpType'], payload['HeatPumpModel']):
|
|
|
|
# expand energy demand values
|
|
|
|
expand_energy_demand(payload['EnergyDemand'])
|
2023-01-10 12:12:01 -05:00
|
|
|
try:
|
|
|
|
# Run simulation and return output file here
|
2023-01-17 19:00:22 -05:00
|
|
|
hp_simulator = HeatPumpSimulator(city, payload)
|
|
|
|
results = hp_simulator.run_hp_simulation()
|
|
|
|
return Response(json.dumps(results), status=200)
|
2023-01-10 12:12:01 -05:00
|
|
|
except Exception as err:
|
2023-01-17 19:00:22 -05:00
|
|
|
logger.error(err)
|
2023-01-10 12:12:01 -05:00
|
|
|
return Response(json.dumps({'error_message': 'Sorry an error occurred while running HP Simulation'}))
|
|
|
|
else:
|
|
|
|
return Response(json.dumps({'error_message': 'Wrong heat pump type/model combination'}), status=400)
|