forked from ibpsa/project1-boptest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restapi.py
211 lines (175 loc) · 6.14 KB
/
restapi.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- coding: utf-8 -*-
"""
This module implements the REST API used to interact with the test case.
The API is implemented using the ``flask`` package.
"""
# GENERAL PACKAGE IMPORT
# ----------------------
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
# ----------------------
# TEST CASE IMPORT
# ----------------
from testcase import TestCase
# ----------------
# FLASK REQUIREMENTS
# ------------------
app = Flask(__name__)
cors = CORS(app, resources={r"*": {"origins": "*"}})
api = Api(app)
# ------------------
# INSTANTIATE TEST CASE
# ---------------------
case = TestCase()
# ---------------------
# DEFINE ARGUMENT PARSERS
# -----------------------
# ``step`` interface
parser_step = reqparse.RequestParser()
parser_step.add_argument('step')
# ``initialize`` interface
parser_initialize = reqparse.RequestParser()
parser_initialize.add_argument('start_time')
parser_initialize.add_argument('warmup_period')
# ``advance`` interface
parser_advance = reqparse.RequestParser()
for key in case.u.keys():
parser_advance.add_argument(key)
#``forecast_parameters`` interface
parser_forecast_parameters = reqparse.RequestParser()
forecast_parameters = ['horizon','interval']
for arg in forecast_parameters:
parser_forecast_parameters.add_argument(arg)
# ``price_scenario`` interface
parser_scenario = reqparse.RequestParser()
parser_scenario.add_argument('electricity_price')
parser_scenario.add_argument('time_period')
# ``results`` interface
results_var = reqparse.RequestParser()
results_var.add_argument('point_name')
results_var.add_argument('start_time')
results_var.add_argument('final_time')
# -----------------------
# DEFINE REST REQUESTS
# --------------------
class Advance(Resource):
'''Interface to advance the test case simulation.'''
def post(self):
'''POST request with input data to advance the simulation one step
and receive current measurements.'''
u = parser_advance.parse_args(strict=True)
y = case.advance(u)
return y
class Initialize(Resource):
'''Interface to initialize the test case simulation.'''
def put(self):
'''PUT request to initialize the test.'''
args = parser_initialize.parse_args(strict=True)
start_time = float(args['start_time'])
warmup_period = float(args['warmup_period'])
y = case.initialize(start_time,warmup_period)
return y
class Step(Resource):
'''Interface to test case simulation step size.'''
def get(self):
'''GET request to receive current simulation step in seconds.'''
step = case.get_step()
return step
def put(self):
'''PUT request to set simulation step in seconds.'''
args = parser_step.parse_args(strict=True)
step = args['step']
case.set_step(step)
return step, 201
class Inputs(Resource):
'''Interface to test case inputs.'''
def get(self):
'''GET request to receive list of available inputs.'''
u_list = case.get_inputs()
return u_list
class Measurements(Resource):
'''Interface to test case measurements.'''
def get(self):
'''GET request to receive list of available measurements.'''
y_list = case.get_measurements()
return y_list
class Results(Resource):
'''Interface to test case result data.'''
def put(self):
'''PUT request to receive measurement data.'''
args = results_var.parse_args(strict=True)
var = args['point_name']
start_time = float(args['start_time'])
final_time = float(args['final_time'])
Y = case.get_results(var, start_time, final_time)
for key in Y:
Y[key] = Y[key].tolist()
return Y
class KPI(Resource):
'''Interface to test case KPIs.'''
def get(self):
'''GET request to receive KPI data.'''
kpi = case.get_kpis()
return kpi
class Forecast_Parameters(Resource):
'''Interface to test case forecast parameters.'''
def get(self):
'''GET request to receive forecast parameters.'''
forecast_parameters = case.get_forecast_parameters()
return forecast_parameters
def put(self):
'''PUT request to set forecast horizon and interval inseconds.'''
args = parser_forecast_parameters.parse_args(strict=True)
horizon = args['horizon']
interval = args['interval']
case.set_forecast_parameters(horizon, interval)
forecast_parameters = case.get_forecast_parameters()
return forecast_parameters
class Forecast(Resource):
'''Interface to test case forecast data.'''
def get(self):
'''GET request to receive forecast data.'''
forecast = case.get_forecast()
return forecast
class Scenario(Resource):
'''Interface to test case scenario.'''
def get(self):
'''GET request to receive current scenario.'''
scenario = case.get_scenario()
return scenario
def put(self):
'''PUT request to set scenario.'''
scenario = parser_scenario.parse_args(strict=True)
result = case.set_scenario(scenario)
return result
class Name(Resource):
'''Interface to test case name.'''
def get(self):
'''GET request to receive test case name.'''
name = case.get_name()
return name
class Version(Resource):
'''Interface to BOPTEST version.'''
def get(self):
'''GET request to receive BOPTEST version.'''
version = case.get_version()
return version
# --------------------
# ADD REQUESTS TO API WITH URL EXTENSION
# --------------------------------------
api.add_resource(Advance, '/advance')
api.add_resource(Initialize, '/initialize')
api.add_resource(Step, '/step')
api.add_resource(Inputs, '/inputs')
api.add_resource(Measurements, '/measurements')
api.add_resource(Results, '/results')
api.add_resource(KPI, '/kpi')
api.add_resource(Forecast_Parameters, '/forecast_parameters')
api.add_resource(Forecast, '/forecast')
api.add_resource(Scenario, '/scenario')
api.add_resource(Name, '/name')
api.add_resource(Version, '/version')
# --------------------------------------
if __name__ == '__main__':
app.run(host='0.0.0.0')