Skip to content

Commit

Permalink
Add uncertainty handler to REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
andreArtelt committed May 24, 2024
1 parent 1c66a77 commit f4eb25a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/tut.restapi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ and is implemented in the class :class:`~epyt_flow.rest_api.server.RestApiServic
+-----------+-------------------------------------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| GET, POST | /scenario/{scenario_id}/sensor_config | :class:`~epyt_flow.rest_api.scenario_handler.ScenarioSensorConfigHandler` | Gets or sets the sensor configuration of a given scenario. |
+-----------+-------------------------------------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| GET, POST | /scenario/{scenario_id}/uncertainty/model | :class:`~epyt_flow.rest_api.scenario_handler.ScenarioModelUnceraintyHandler` | Gets or sets the model uncertainties of a given scenario. |
+-----------+-------------------------------------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| GET, POST | /scenario/{scenario_id}/uncertainty/sensors | :class:`~epyt_flow.rest_api.scenario_handler.ScenarioSensorUnceraintyHandler` | Gets or sets the sensor uncertainties (i.e. noise) of a given scenario. |
+-----------+-------------------------------------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| GET, POST | /scenario/{scenario_id}/leakages | :class:`~epyt_flow.rest_api.scenario_handler.ScenarioLeakageHandler` | Gets or adds a leakage to a given scenario. |
+-----------+-------------------------------------------------------+------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| GET, POST | /scenario/{scenario_id}/sensor_faults | :class:`~epyt_flow.rest_api.scenario_handler.ScenarioSensorFaultHandler` | Gets or adds a sensor fault to a given scenario. |
Expand Down
113 changes: 112 additions & 1 deletion epyt_flow/rest_api/scenario_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from .res_manager import ResourceManager
from ..utils import get_temp_folder, pack_zip_archive
from .scada_data_handler import ScadaDataManager
from ..simulation import ScenarioSimulator, Leakage, SensorConfig, SensorFault
from ..simulation import ScenarioSimulator, Leakage, SensorConfig, SensorFault, \
SensorNoise, ModelUncertainty


class ScenarioManager(ResourceManager):
Expand Down Expand Up @@ -196,6 +197,116 @@ def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR


class ScenarioModelUncertaintyHandler(ScenarioBaseHandler):
"""
Class for handling GET and POST requests concerning model uncertainty.
"""
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
"""
Gets the model uncertainties of a given scenario.
Parameters
----------
resp : `falcon.Response`
Response instance.
scenario_id : `str`
UUID of the scenario.
"""
try:
if self.scenario_mgr.validate_uuid(scenario_id) is False:
self.send_invalid_resource_id_error(resp)
return

my_model_uncertainties = self.scenario_mgr.get(scenario_id).model_uncertainty
self.send_json_response(resp, my_model_uncertainties)
except Exception as ex:
warnings.warn(str(ex))
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR

def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
"""
Sets the model uncertainties of a given scenario.
Parameters
----------
req : `falcon.Request`
Request instance.
resp : `falcon.Response`
Response instance.
scenario_id : `str`
UUID of the scenario.
"""
try:
if self.scenario_mgr.validate_uuid(scenario_id) is False:
self.send_invalid_resource_id_error(resp)
return

model_uncertainty = self.load_json_data_from_request(req)
if not isinstance(model_uncertainty, ModelUncertainty):
self.send_json_parsing_error(resp)
return

self.scenario_mgr.get(scenario_id).model_uncertainty = model_uncertainty
except Exception as ex:
warnings.warn(str(ex))
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR


class ScenarioSensorUncertaintyHandler(ScenarioBaseHandler):
"""
Class for handling GET and POST requests concerning sensor uncertainty (i.e. noise).
"""
def on_get(self, _, resp: falcon.Response, scenario_id: str) -> None:
"""
Gets the sensor uncertainty (i.e. noise) of a given scenario.
Parameters
----------
resp : `falcon.Response`
Response instance.
scenario_id : `str`
UUID of the scenario.
"""
try:
if self.scenario_mgr.validate_uuid(scenario_id) is False:
self.send_invalid_resource_id_error(resp)
return

my_sensor_noise = self.scenario_mgr.get(scenario_id).sensor_noise
self.send_json_response(resp, my_sensor_noise)
except Exception as ex:
warnings.warn(str(ex))
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR

def on_post(self, req: falcon.Request, resp: falcon.Response, scenario_id: str) -> None:
"""
Sets the sensor uncertainty (i.e. noise) of a given scenario.
Parameters
----------
req : `falcon.Request`
Request instance.
resp : `falcon.Response`
Response instance.
scenario_id : `str`
UUID of the scenario.
"""
try:
if self.scenario_mgr.validate_uuid(scenario_id) is False:
self.send_invalid_resource_id_error(resp)
return

sensor_noise = self.load_json_data_from_request(req)
if not isinstance(sensor_noise, SensorNoise):
self.send_json_parsing_error(resp)
return

self.scenario_mgr.get(scenario_id).sensor_noise = sensor_noise
except Exception as ex:
warnings.warn(str(ex))
resp.status = falcon.HTTP_INTERNAL_SERVER_ERROR


class ScenarioLeakageHandler(ScenarioBaseHandler):
"""
Class for handling GET and POST requests concerning leakages.
Expand Down
7 changes: 6 additions & 1 deletion epyt_flow/rest_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
ScenarioGeneralParamsHandler, ScenarioSensorConfigHandler, ScenarioSimulationHandler, \
ScenarioTopologyHandler, ScenarioConfigHandler, ScenarioLeakageHandler, \
ScenarioBasicQualitySimulationHandler, ScenarioAdvancedQualitySimulationHandler, \
ScenarioNodeDemandPatternHandler, ScenarioSensorFaultHandler, ScenarioExportHandler
ScenarioNodeDemandPatternHandler, ScenarioSensorFaultHandler, ScenarioExportHandler, \
ScenarioModelUncertaintyHandler, ScenarioSensorUncertaintyHandler
from .scada_data_handler import ScadaDataManager, ScadaDataSensorConfigHandler, \
ScadaDataPressuresHandler, ScadaDataDemandsHandler, ScadaDataFlowsHandler, \
ScadaDataLinksQualityHandler, ScadaDataNodesQualityHandler, ScadaDataRemoveHandler, \
Expand Down Expand Up @@ -50,6 +51,10 @@ def __init__(self, port: int = 8080):
ScenarioGeneralParamsHandler(self.scenario_mgr))
self.app.add_route("/scenario/{scenario_id}/sensor_config",
ScenarioSensorConfigHandler(self.scenario_mgr))
self.app.add_route("/scenario/{scenario_id}/uncertainty/model",
ScenarioModelUncertaintyHandler(self.scenario_mgr))
self.app.add_route("/scenario/{scenario_id}/uncertainty/sensors",
ScenarioSensorUncertaintyHandler(self.scenario_mgr))
self.app.add_route("/scenario/{scenario_id}/leakages",
ScenarioLeakageHandler(self.scenario_mgr))
self.app.add_route("/scenario/{scenario_id}/sensor_faults",
Expand Down

0 comments on commit f4eb25a

Please sign in to comment.