-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (86 loc) · 3.56 KB
/
Copy pathapp.py
File metadata and controls
110 lines (86 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import secrets
import threading
from fastapi import FastAPI
from api.models import (
CreateOrderRequest,
CreateOrderResponse,
GetOrderResponse,
ServiceUnavailableResponse,
BadRequestResponse,
InternalServerErrorResponse,
)
from api.core import ewh_pipeline
from api.data_helpers import get_user_metadata
app = FastAPI(
title="Electric Water Heater Flexibility API",
description="""
OpenAPI specifications for **INESC TEC Enershare Electric Water Heater Flexibility API**.
Welcome to our OpenAPI documentation for the INESC TEC Enershare Electric Water Heater Flexibility API.
This REST API provides endpoints for initiating a request for Electric Water Heater (EWH) load optimization, and for
retrieving the result, respectively.
**This documentation will guide you through:**
1. **Request EWH Simulations**: Initiates a request for load optimization for a specific Electric Water Heater.
Users can provide the EWH specifications (power, maximum allowed water temperature, capacity), desired hot water
comfort temperature, and a time schedule during which the optimization should occur.
2. **Retrieve EWH Simulations Result**: Retrieves the result of a previously initiated Electric Water Heater load
optimization request based on the provided request ID.
**Developers // Contacts:**
- José Paulos (jose.paulos@inesctec.pt)
""",
)
error_responses = {
400: {"description": "Bad request", "model": BadRequestResponse},
500: {"description": "Service Unavailable", "model": InternalServerErrorResponse},
503: {"description": "Service Unavailable", "model": ServiceUnavailableResponse}
}
@app.post("/api/ewh/request",
response_model=CreateOrderResponse,
status_code=201,
responses=error_responses,
tags=["api"],
description="Endpoint to Get EWH Simulation Results")
def create_order(ewh_request: CreateOrderRequest):
# Access the data using EWHRequest model
user_id = ewh_request.user
start_datetime = ewh_request.datetime_start
end_datetime = ewh_request.datetime_end
ewh_specs = ewh_request.ewh_specs
# Check if minimum conditions to run model are met
# -- right now we are just checking if user is registered in DS
# Check if user exists in dataspace:
metadata = get_user_metadata(identifier=user_id)
if not metadata["data_available"]:
response = {
"order_status": "failed",
"message": "Failed to fetch user metadata/data (unavailable)."
}
return response
else:
# Create new order id
order_id = secrets.token_urlsafe(45)
# Run ewh pipeline in separate thread and continue with response
t = threading.Thread(target=ewh_pipeline,
args=(
order_id,
user_id,
start_datetime,
end_datetime,
ewh_specs
))
t.start() # Detatch
response = {
"order_id": order_id,
"order_status": "placed"
}
return response
@app.get("/api/ewh/result", response_model=GetOrderResponse,
status_code=200,
responses=error_responses,
tags=["api"],
description="Endpoint to Get EWH Simulation Results")
def get_order(order_id: str):
# Query database for Order ID status. If "complete", return result
return {}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8080)