diff --git a/docs/tut.restapi.rst b/docs/tut.restapi.rst index 08d1d99..a49f8bf 100644 --- a/docs/tut.restapi.rst +++ b/docs/tut.restapi.rst @@ -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. | diff --git a/epyt_flow/rest_api/scenario_handler.py b/epyt_flow/rest_api/scenario_handler.py index 7f5fc3c..1af80cc 100644 --- a/epyt_flow/rest_api/scenario_handler.py +++ b/epyt_flow/rest_api/scenario_handler.py @@ -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): @@ -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. diff --git a/epyt_flow/rest_api/server.py b/epyt_flow/rest_api/server.py index cd0cc59..252be33 100644 --- a/epyt_flow/rest_api/server.py +++ b/epyt_flow/rest_api/server.py @@ -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, \ @@ -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",