api_v1.4/hub_api/heat_pump.py

40 lines
1.4 KiB
Python

"""
HeatPump Service
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Peter Yefi peteryefi@gmail.com
"""
import json
from flask import request, Response
from hub_api.config import Config
from flask_restful import Resource
from hub_api.helpers.auth import role_required
from utils import HeatPumpSimulator
from utils import validate_hp_model
from persistence.models import UserRoles
from utils import expand_energy_demand
from hub_logger import logger
class HeatPump(Config, Resource):
def __init__(self):
super().__init__()
@role_required([UserRoles.Admin.value, UserRoles.Hub_Reader.value])
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'])
try:
# Run simulation and return output file here
hp_simulator = HeatPumpSimulator(city, payload)
results = hp_simulator.run_hp_simulation()
return Response(json.dumps(results), status=200)
except Exception as err:
logger.error(err)
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)