epyt-flow 0.1.1__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- epyt_flow/EPANET/compile_linux.sh +4 -0
- epyt_flow/EPANET/compile_macos.sh +4 -0
- epyt_flow/VERSION +1 -1
- epyt_flow/__init__.py +29 -18
- epyt_flow/data/benchmarks/leakdb.py +7 -12
- epyt_flow/data/networks.py +404 -40
- epyt_flow/rest_api/base_handler.py +14 -0
- epyt_flow/rest_api/scada_data/__init__.py +0 -0
- epyt_flow/rest_api/{scada_data_handler.py → scada_data/data_handlers.py} +3 -162
- epyt_flow/rest_api/scada_data/export_handlers.py +140 -0
- epyt_flow/rest_api/scada_data/handlers.py +209 -0
- epyt_flow/rest_api/scenario/__init__.py +0 -0
- epyt_flow/rest_api/scenario/event_handlers.py +118 -0
- epyt_flow/rest_api/{scenario_handler.py → scenario/handlers.py} +86 -67
- epyt_flow/rest_api/scenario/simulation_handlers.py +174 -0
- epyt_flow/rest_api/scenario/uncertainty_handlers.py +118 -0
- epyt_flow/rest_api/server.py +61 -24
- epyt_flow/simulation/events/leakages.py +27 -17
- epyt_flow/simulation/scada/scada_data.py +545 -14
- epyt_flow/simulation/scada/scada_data_export.py +39 -12
- epyt_flow/simulation/scenario_config.py +14 -20
- epyt_flow/simulation/scenario_simulator.py +358 -114
- epyt_flow/simulation/sensor_config.py +693 -37
- epyt_flow/topology.py +149 -8
- epyt_flow/utils.py +75 -18
- {epyt_flow-0.1.1.dist-info → epyt_flow-0.3.0.dist-info}/METADATA +33 -5
- {epyt_flow-0.1.1.dist-info → epyt_flow-0.3.0.dist-info}/RECORD +30 -22
- epyt_flow/EPANET/compile.sh +0 -4
- {epyt_flow-0.1.1.dist-info → epyt_flow-0.3.0.dist-info}/LICENSE +0 -0
- {epyt_flow-0.1.1.dist-info → epyt_flow-0.3.0.dist-info}/WHEEL +0 -0
- {epyt_flow-0.1.1.dist-info → epyt_flow-0.3.0.dist-info}/top_level.txt +0 -0
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
"""
|
|
2
|
-
This module provides
|
|
2
|
+
This module provides REST API handlers for some requests concerning scenarios.
|
|
3
3
|
"""
|
|
4
4
|
import warnings
|
|
5
|
+
import os
|
|
5
6
|
import falcon
|
|
6
7
|
|
|
7
|
-
from
|
|
8
|
-
from
|
|
9
|
-
from
|
|
10
|
-
from
|
|
8
|
+
from ..base_handler import BaseHandler
|
|
9
|
+
from ..res_manager import ResourceManager
|
|
10
|
+
from ...utils import get_temp_folder, pack_zip_archive
|
|
11
|
+
from ...simulation import ScenarioSimulator, SensorConfig
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class ScenarioManager(ResourceManager):
|
|
@@ -36,7 +37,7 @@ class ScenarioBaseHandler(BaseHandler):
|
|
|
36
37
|
|
|
37
38
|
Parameters
|
|
38
39
|
----------
|
|
39
|
-
scenario_mgr : :class:`~epyt_flow.rest_api.
|
|
40
|
+
scenario_mgr : :class:`~epyt_flow.rest_api.scenario.handlers.ScenarioManager`
|
|
40
41
|
Instance for managing all scenarios.
|
|
41
42
|
"""
|
|
42
43
|
def __init__(self, scenario_mgr: ScenarioManager):
|
|
@@ -69,66 +70,85 @@ class ScenarioRemoveHandler(ScenarioBaseHandler):
|
|
|
69
70
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
70
71
|
|
|
71
72
|
|
|
72
|
-
class
|
|
73
|
+
class ScenarioExportHandler(ScenarioBaseHandler):
|
|
73
74
|
"""
|
|
74
|
-
Class for handling
|
|
75
|
+
Class for handling GET requests for exporting a given scenario to EPANET files
|
|
76
|
+
-- i.e. .inp and (otpionally) .msx files.
|
|
75
77
|
"""
|
|
76
|
-
def
|
|
78
|
+
def __create_temp_file_path(self, scenario_id: str, file_ext: str) -> None:
|
|
77
79
|
"""
|
|
78
|
-
|
|
80
|
+
Returns a path to a temporary file for storing the scenario.
|
|
79
81
|
|
|
80
82
|
Parameters
|
|
81
83
|
----------
|
|
82
|
-
resp : `falcon.Response`
|
|
83
|
-
Response instance.
|
|
84
84
|
scenario_id : `str`
|
|
85
85
|
UUID of the scenario.
|
|
86
|
+
file_ext : `str`
|
|
87
|
+
File extension.
|
|
86
88
|
"""
|
|
87
|
-
|
|
88
|
-
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
89
|
-
self.send_invalid_resource_id_error(resp)
|
|
90
|
-
return
|
|
89
|
+
return os.path.join(get_temp_folder(), f"{scenario_id}.{file_ext}")
|
|
91
90
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
91
|
+
def __send_temp_file(self, resp: falcon.Response, tmp_file: str,
|
|
92
|
+
content_type: str = "application/octet-stream") -> None:
|
|
93
|
+
"""
|
|
94
|
+
Sends a given file (`tmp_file`) to the the client.
|
|
97
95
|
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
resp : `falcon.Response`
|
|
99
|
+
Response instance.
|
|
100
|
+
tmp_file : `str`
|
|
101
|
+
Path to the temporary file to be send.
|
|
102
|
+
"""
|
|
103
|
+
resp.status = falcon.HTTP_200
|
|
104
|
+
resp.content_type = content_type
|
|
105
|
+
with open(tmp_file, 'rb') as f:
|
|
106
|
+
resp.text = f.read()
|
|
98
107
|
|
|
99
|
-
|
|
100
|
-
"""
|
|
101
|
-
Class for handling POST requests for creating a new scenario.
|
|
102
|
-
"""
|
|
103
|
-
def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
|
|
108
|
+
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
104
109
|
"""
|
|
105
|
-
|
|
110
|
+
Exports a given scenario to an .inp and (optionally) .msx file.
|
|
106
111
|
|
|
107
112
|
Parameters
|
|
108
113
|
----------
|
|
109
|
-
req : `falcon.Request`
|
|
110
|
-
Request instance.
|
|
111
114
|
resp : `falcon.Response`
|
|
112
115
|
Response instance.
|
|
113
116
|
scenario_id : `str`
|
|
114
117
|
UUID of the scenario.
|
|
115
118
|
"""
|
|
116
119
|
try:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
121
|
+
self.send_invalid_resource_id_error(resp)
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
125
|
+
|
|
126
|
+
f_inp_out = self.__create_temp_file_path(scenario_id, "inp")
|
|
127
|
+
f_msx_out = self.__create_temp_file_path(scenario_id, "msx")
|
|
128
|
+
my_scenario.save_to_epanet_file(f_inp_out, f_msx_out)
|
|
129
|
+
|
|
130
|
+
if os.path.isfile(f_msx_out):
|
|
131
|
+
f_out = self.__create_temp_file_path(scenario_id, "zip")
|
|
132
|
+
pack_zip_archive([f_inp_out, f_msx_out], f_out)
|
|
133
|
+
|
|
134
|
+
self.__send_temp_file(resp, f_out)
|
|
135
|
+
os.remove(f_out)
|
|
136
|
+
os.remove(f_msx_out)
|
|
137
|
+
else:
|
|
138
|
+
self.__send_temp_file(resp, f_inp_out)
|
|
139
|
+
os.remove(f_inp_out)
|
|
120
140
|
except Exception as ex:
|
|
121
141
|
warnings.warn(str(ex))
|
|
122
142
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
123
143
|
|
|
124
144
|
|
|
125
|
-
class
|
|
145
|
+
class ScenarioConfigHandler(ScenarioBaseHandler):
|
|
126
146
|
"""
|
|
127
|
-
Class for handling GET
|
|
147
|
+
Class for handling a GET request for getting the scenario configuration of a given scenario.
|
|
128
148
|
"""
|
|
129
149
|
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
130
150
|
"""
|
|
131
|
-
Gets
|
|
151
|
+
Gets the scenario configuration of a given scenario.
|
|
132
152
|
|
|
133
153
|
Parameters
|
|
134
154
|
----------
|
|
@@ -142,15 +162,20 @@ class ScenarioLeakageHandler(ScenarioBaseHandler):
|
|
|
142
162
|
self.send_invalid_resource_id_error(resp)
|
|
143
163
|
return
|
|
144
164
|
|
|
145
|
-
|
|
146
|
-
self.send_json_response(resp,
|
|
165
|
+
my_sceanrio_config = self.scenario_mgr.get(scenario_id).get_scenario_config()
|
|
166
|
+
self.send_json_response(resp, my_sceanrio_config)
|
|
147
167
|
except Exception as ex:
|
|
148
168
|
warnings.warn(str(ex))
|
|
149
169
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
150
170
|
|
|
151
|
-
|
|
171
|
+
|
|
172
|
+
class ScenarioNewHandler(ScenarioBaseHandler):
|
|
173
|
+
"""
|
|
174
|
+
Class for handling POST requests for creating a new scenario.
|
|
175
|
+
"""
|
|
176
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
|
|
152
177
|
"""
|
|
153
|
-
|
|
178
|
+
Creates/Loads a new scenario.
|
|
154
179
|
|
|
155
180
|
Parameters
|
|
156
181
|
----------
|
|
@@ -162,16 +187,9 @@ class ScenarioLeakageHandler(ScenarioBaseHandler):
|
|
|
162
187
|
UUID of the scenario.
|
|
163
188
|
"""
|
|
164
189
|
try:
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
leakage = self.load_json_data_from_request(req)
|
|
170
|
-
if not isinstance(leakage, Leakage):
|
|
171
|
-
self.send_json_parsing_error(resp)
|
|
172
|
-
return
|
|
173
|
-
|
|
174
|
-
self.scenario_mgr.get(scenario_id).add_leakage(leakage)
|
|
190
|
+
args = self.load_json_data_from_request(req)
|
|
191
|
+
scenario_id = self.scenario_mgr.create(**args)
|
|
192
|
+
self.send_json_response(resp, {"scenario_id": scenario_id})
|
|
175
193
|
except Exception as ex:
|
|
176
194
|
warnings.warn(str(ex))
|
|
177
195
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
@@ -237,6 +255,8 @@ class ScenarioGeneralParamsHandler(ScenarioBaseHandler):
|
|
|
237
255
|
|
|
238
256
|
Parameters
|
|
239
257
|
----------
|
|
258
|
+
req : `falcon.Request`
|
|
259
|
+
Request instance.
|
|
240
260
|
resp : `falcon.Request`
|
|
241
261
|
Request instance.
|
|
242
262
|
scenario_id : `str`
|
|
@@ -290,6 +310,8 @@ class ScenarioSensorConfigHandler(ScenarioBaseHandler):
|
|
|
290
310
|
|
|
291
311
|
Parameters
|
|
292
312
|
----------
|
|
313
|
+
req : `falcon.Request`
|
|
314
|
+
Request instance.
|
|
293
315
|
resp : `falcon.Request`
|
|
294
316
|
Request instance.
|
|
295
317
|
scenario_id : `str`
|
|
@@ -312,41 +334,38 @@ class ScenarioSensorConfigHandler(ScenarioBaseHandler):
|
|
|
312
334
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
313
335
|
|
|
314
336
|
|
|
315
|
-
class
|
|
337
|
+
class ScenarioNodeDemandPatternHandler(ScenarioBaseHandler):
|
|
316
338
|
"""
|
|
317
|
-
Class for handling
|
|
318
|
-
|
|
319
|
-
Parameters
|
|
320
|
-
----------
|
|
321
|
-
scada_data_mgr : :class:`~epyt_flow.rest_api.scenario_handler.ScadaDataBaseHandler`
|
|
322
|
-
SCADA data manager.
|
|
339
|
+
Class for handling POST requests for node demand patterns of a given scenario.
|
|
323
340
|
"""
|
|
324
|
-
def
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
super().__init__(**kwds)
|
|
328
|
-
|
|
329
|
-
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
341
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str,
|
|
342
|
+
node_id: str) -> None:
|
|
330
343
|
"""
|
|
331
|
-
|
|
344
|
+
Sets the demand pattern of a specific node in a given scenario.
|
|
332
345
|
|
|
333
346
|
Parameters
|
|
334
347
|
----------
|
|
348
|
+
req : `falcon.Request`
|
|
349
|
+
Request instance.
|
|
335
350
|
resp : `falcon.Response`
|
|
336
351
|
Response instance.
|
|
337
352
|
scenario_id : `str`
|
|
338
353
|
UUID of the scenario.
|
|
354
|
+
node_id : `str`
|
|
355
|
+
ID of the node.
|
|
339
356
|
"""
|
|
340
357
|
try:
|
|
341
358
|
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
342
359
|
self.send_invalid_resource_id_error(resp)
|
|
343
360
|
return
|
|
344
361
|
|
|
345
|
-
|
|
346
|
-
res = my_scenario.run_simulation()
|
|
362
|
+
params = self.load_json_data_from_request(req)
|
|
347
363
|
|
|
348
|
-
|
|
349
|
-
|
|
364
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
365
|
+
my_scenario.set_node_demand_pattern(node_id, params["base_demand"],
|
|
366
|
+
params["demand_pattern_id"],
|
|
367
|
+
params["demand_pattern"])
|
|
350
368
|
except Exception as ex:
|
|
351
369
|
warnings.warn(str(ex))
|
|
370
|
+
resp.data = str(ex)
|
|
352
371
|
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module provides REST API handlers concerning the simulation of scenarios.
|
|
3
|
+
"""
|
|
4
|
+
import warnings
|
|
5
|
+
import falcon
|
|
6
|
+
|
|
7
|
+
from .handlers import ScenarioBaseHandler
|
|
8
|
+
from ..scada_data.handlers import ScadaDataManager
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ScenarioSimulationHandler(ScenarioBaseHandler):
|
|
12
|
+
"""
|
|
13
|
+
Class for handling GET requests for simulating a given scenario.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
scada_data_mgr : :class:`~epyt_flow.rest_api.scada_data.handlers.ScadaDataManager`
|
|
18
|
+
SCADA data manager.
|
|
19
|
+
"""
|
|
20
|
+
def __init__(self, scada_data_mgr: ScadaDataManager, **kwds):
|
|
21
|
+
self.scada_data_mgr = scada_data_mgr
|
|
22
|
+
|
|
23
|
+
super().__init__(**kwds)
|
|
24
|
+
|
|
25
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Runs the simulation of a given scenario.
|
|
28
|
+
|
|
29
|
+
Note that in contrat to the GET request
|
|
30
|
+
(:func:`~epyt_flow.rest_api.scenario.simulation_handlers.ScenarioSimulationHandler.on_get`),
|
|
31
|
+
the POST request allows to specify additional arguments passed to
|
|
32
|
+
:func:`~epyt_flow.simulation.scenario_simulator.ScenarioSimulator.run_simulation`.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
req : `falcon.Request`
|
|
37
|
+
Request instance.
|
|
38
|
+
resp : `falcon.Response`
|
|
39
|
+
Response instance.
|
|
40
|
+
scenario_id : `str`
|
|
41
|
+
UUID of the scenario.
|
|
42
|
+
"""
|
|
43
|
+
try:
|
|
44
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
45
|
+
self.send_invalid_resource_id_error(resp)
|
|
46
|
+
return
|
|
47
|
+
|
|
48
|
+
params = self.load_json_data_from_request(req)
|
|
49
|
+
|
|
50
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
51
|
+
res = my_scenario.run_simulation(**params)
|
|
52
|
+
|
|
53
|
+
data_id = self.scada_data_mgr.create_new_item(res)
|
|
54
|
+
self.send_json_response(resp, {"data_id": data_id})
|
|
55
|
+
except Exception as ex:
|
|
56
|
+
warnings.warn(str(ex))
|
|
57
|
+
resp.data = str(ex)
|
|
58
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
59
|
+
|
|
60
|
+
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Runs the simulation of a given scenario.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
resp : `falcon.Response`
|
|
67
|
+
Response instance.
|
|
68
|
+
scenario_id : `str`
|
|
69
|
+
UUID of the scenario.
|
|
70
|
+
"""
|
|
71
|
+
try:
|
|
72
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
73
|
+
self.send_invalid_resource_id_error(resp)
|
|
74
|
+
return
|
|
75
|
+
|
|
76
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
77
|
+
res = my_scenario.run_simulation()
|
|
78
|
+
|
|
79
|
+
data_id = self.scada_data_mgr.create_new_item(res)
|
|
80
|
+
self.send_json_response(resp, {"data_id": data_id})
|
|
81
|
+
except Exception as ex:
|
|
82
|
+
warnings.warn(str(ex))
|
|
83
|
+
resp.data = str(ex)
|
|
84
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class ScenarioBasicQualitySimulationHandler(ScenarioBaseHandler):
|
|
88
|
+
"""
|
|
89
|
+
Class for handling POST requests for runing a basic quality simulation of a given scenario.
|
|
90
|
+
|
|
91
|
+
Parameters
|
|
92
|
+
----------
|
|
93
|
+
scada_data_mgr : :class:`~epyt_flow.rest_api.scada_data.handlers.ScadaDataManager`
|
|
94
|
+
SCADA data manager.
|
|
95
|
+
"""
|
|
96
|
+
def __init__(self, scada_data_mgr: ScadaDataManager, **kwds):
|
|
97
|
+
self.scada_data_mgr = scada_data_mgr
|
|
98
|
+
|
|
99
|
+
super().__init__(**kwds)
|
|
100
|
+
|
|
101
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
|
|
102
|
+
"""
|
|
103
|
+
Runs the basic quality simulation of a given scenario.
|
|
104
|
+
|
|
105
|
+
Parameters
|
|
106
|
+
----------
|
|
107
|
+
req : `falcon.Request`
|
|
108
|
+
Request instance.
|
|
109
|
+
resp : `falcon.Response`
|
|
110
|
+
Response instance.
|
|
111
|
+
scenario_id : `str`
|
|
112
|
+
UUID of the scenario.
|
|
113
|
+
"""
|
|
114
|
+
try:
|
|
115
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
116
|
+
self.send_invalid_resource_id_error(resp)
|
|
117
|
+
return
|
|
118
|
+
|
|
119
|
+
params = self.load_json_data_from_request(req)
|
|
120
|
+
|
|
121
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
122
|
+
res = my_scenario.run_basic_quality_simulation(**params)
|
|
123
|
+
|
|
124
|
+
data_id = self.scada_data_mgr.create_new_item(res)
|
|
125
|
+
self.send_json_response(resp, {"data_id": data_id})
|
|
126
|
+
except Exception as ex:
|
|
127
|
+
warnings.warn(str(ex))
|
|
128
|
+
resp.data = str(ex)
|
|
129
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class ScenarioAdvancedQualitySimulationHandler(ScenarioBaseHandler):
|
|
133
|
+
"""
|
|
134
|
+
Class for handling POST requests for runing an advanced quality simulation of a given scenario.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
scada_data_mgr : :class:`~epyt_flow.rest_api.scada_data.handlers.ScadaDataManager`
|
|
139
|
+
SCADA data manager.
|
|
140
|
+
"""
|
|
141
|
+
def __init__(self, scada_data_mgr: ScadaDataManager, **kwds):
|
|
142
|
+
self.scada_data_mgr = scada_data_mgr
|
|
143
|
+
|
|
144
|
+
super().__init__(**kwds)
|
|
145
|
+
|
|
146
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
|
|
147
|
+
"""
|
|
148
|
+
Runs the advanced quality simulation of a given scenario.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
req : `falcon.Request`
|
|
153
|
+
Request instance.
|
|
154
|
+
resp : `falcon.Response`
|
|
155
|
+
Response instance.
|
|
156
|
+
scenario_id : `str`
|
|
157
|
+
UUID of the scenario.
|
|
158
|
+
"""
|
|
159
|
+
try:
|
|
160
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
161
|
+
self.send_invalid_resource_id_error(resp)
|
|
162
|
+
return
|
|
163
|
+
|
|
164
|
+
params = self.load_json_data_from_request(req)
|
|
165
|
+
|
|
166
|
+
my_scenario = self.scenario_mgr.get(scenario_id)
|
|
167
|
+
res = my_scenario.run_advanced_quality_simulation(**params)
|
|
168
|
+
|
|
169
|
+
data_id = self.scada_data_mgr.create_new_item(res)
|
|
170
|
+
self.send_json_response(resp, {"data_id": data_id})
|
|
171
|
+
except Exception as ex:
|
|
172
|
+
warnings.warn(str(ex))
|
|
173
|
+
resp.data = str(ex)
|
|
174
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module provides REST API handlers for model and sensor uncertainties of scenarios.
|
|
3
|
+
"""
|
|
4
|
+
import warnings
|
|
5
|
+
import falcon
|
|
6
|
+
|
|
7
|
+
from .handlers import ScenarioBaseHandler
|
|
8
|
+
from ...simulation import SensorNoise, ModelUncertainty
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ScenarioModelUncertaintyHandler(ScenarioBaseHandler):
|
|
12
|
+
"""
|
|
13
|
+
Class for handling GET and POST requests concerning model uncertainty.
|
|
14
|
+
"""
|
|
15
|
+
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
16
|
+
"""
|
|
17
|
+
Gets the model uncertainties of a given scenario.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
resp : `falcon.Response`
|
|
22
|
+
Response instance.
|
|
23
|
+
scenario_id : `str`
|
|
24
|
+
UUID of the scenario.
|
|
25
|
+
"""
|
|
26
|
+
try:
|
|
27
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
28
|
+
self.send_invalid_resource_id_error(resp)
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
my_model_uncertainties = self.scenario_mgr.get(scenario_id).model_uncertainty
|
|
32
|
+
self.send_json_response(resp, my_model_uncertainties)
|
|
33
|
+
except Exception as ex:
|
|
34
|
+
warnings.warn(str(ex))
|
|
35
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
36
|
+
|
|
37
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
|
|
38
|
+
"""
|
|
39
|
+
Sets the model uncertainties of a given scenario.
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
req : `falcon.Request`
|
|
44
|
+
Request instance.
|
|
45
|
+
resp : `falcon.Response`
|
|
46
|
+
Response instance.
|
|
47
|
+
scenario_id : `str`
|
|
48
|
+
UUID of the scenario.
|
|
49
|
+
"""
|
|
50
|
+
try:
|
|
51
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
52
|
+
self.send_invalid_resource_id_error(resp)
|
|
53
|
+
return
|
|
54
|
+
|
|
55
|
+
model_uncertainty = self.load_json_data_from_request(req)
|
|
56
|
+
if not isinstance(model_uncertainty, ModelUncertainty):
|
|
57
|
+
self.send_json_parsing_error(resp)
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
self.scenario_mgr.get(scenario_id).model_uncertainty = model_uncertainty
|
|
61
|
+
except Exception as ex:
|
|
62
|
+
warnings.warn(str(ex))
|
|
63
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class ScenarioSensorUncertaintyHandler(ScenarioBaseHandler):
|
|
67
|
+
"""
|
|
68
|
+
Class for handling GET and POST requests concerning sensor uncertainty (i.e. noise).
|
|
69
|
+
"""
|
|
70
|
+
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
|
|
71
|
+
"""
|
|
72
|
+
Gets the sensor uncertainty (i.e. noise) of a given scenario.
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
resp : `falcon.Response`
|
|
77
|
+
Response instance.
|
|
78
|
+
scenario_id : `str`
|
|
79
|
+
UUID of the scenario.
|
|
80
|
+
"""
|
|
81
|
+
try:
|
|
82
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
83
|
+
self.send_invalid_resource_id_error(resp)
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
my_sensor_noise = self.scenario_mgr.get(scenario_id).sensor_noise
|
|
87
|
+
self.send_json_response(resp, my_sensor_noise)
|
|
88
|
+
except Exception as ex:
|
|
89
|
+
warnings.warn(str(ex))
|
|
90
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|
|
91
|
+
|
|
92
|
+
def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
|
|
93
|
+
"""
|
|
94
|
+
Sets the sensor uncertainty (i.e. noise) of a given scenario.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
req : `falcon.Request`
|
|
99
|
+
Request instance.
|
|
100
|
+
resp : `falcon.Response`
|
|
101
|
+
Response instance.
|
|
102
|
+
scenario_id : `str`
|
|
103
|
+
UUID of the scenario.
|
|
104
|
+
"""
|
|
105
|
+
try:
|
|
106
|
+
if self.scenario_mgr.validate_uuid(scenario_id) is False:
|
|
107
|
+
self.send_invalid_resource_id_error(resp)
|
|
108
|
+
return
|
|
109
|
+
|
|
110
|
+
sensor_noise = self.load_json_data_from_request(req)
|
|
111
|
+
if not isinstance(sensor_noise, SensorNoise):
|
|
112
|
+
self.send_json_parsing_error(resp)
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
self.scenario_mgr.get(scenario_id).sensor_noise = sensor_noise
|
|
116
|
+
except Exception as ex:
|
|
117
|
+
warnings.warn(str(ex))
|
|
118
|
+
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR
|