diff --git a/datahub/data.py b/datahub/data.py index ea0b2b2..232a18c 100644 --- a/datahub/data.py +++ b/datahub/data.py @@ -7,3 +7,15 @@ opal_df: pd.DataFrame = create_opal_frame() dsr_data: list[dict[str, NDArray | str]] = [] # type: ignore[type-arg] wesim_data: dict[str, dict] = {} # type: ignore[type-arg] + +model_running: bool = False +model_resetting: bool = False + + +def reset_data() -> None: + """Reset the OPAL and DSR data to their initial (empty) values.""" + global opal_df + global dsr_data + + opal_df = create_opal_frame() + dsr_data = [] diff --git a/datahub/main.py b/datahub/main.py index 49078d3..2364fa6 100644 --- a/datahub/main.py +++ b/datahub/main.py @@ -245,3 +245,80 @@ def get_wesim_data() -> dict[str, dict[str, dict]]: # type: ignore[type-arg] dt.wesim_data = get_wesim() return {"data": dt.wesim_data} + + +@app.post("/set_model_signals") +def set_model_signals(start: bool) -> str: + """POST method function for setting start and stop model signals. + + It has the query parameter: + - `start`: A boolean to indicate the model should start running or stop running. + + \f + + Args: + start: A bool flag for if the model should start running or stop running. + True to start the model, False to stop the model. + + Returns: + A confirmation message + """ # noqa: D301 + message = "Start signal received" if start else "Stop signal received" + log.info(message) + dt.model_running = start + + return message + + +@app.post("/model_ready") +def signal_model_ready(ready: bool) -> str: + """POST method function for indicating when the model has reset and is ready to run. + + It has the query parameter: + - `ready`: A boolean to indicate the model has completed setup and is ready. + + \f + + Args: + ready: A bool flag for if the model has completed setup and is ready. + + Returns: + A confirmation message + """ # noqa: D301 + message = "Ready signal received" if ready else "Not-Ready signal received" + log.info(message) + dt.model_resetting = not ready + + return message + + +@app.get("/start") +def get_start_signal() -> bool: + """GET method function for getting start model signal. + + It returns a boolean: True for if the model should start running. + + \f + + Returns: + A bool flag for if the model should start + """ # noqa: D301 + log.info("Start signal requested") + + return dt.model_running and not dt.model_resetting + + +@app.get("/stop") +def get_stop_signal() -> bool: + """GET method function for getting stop model signal. + + It returns a boolean: True for if the model should stop running. + + \f + + Returns: + A bool flag for if the model should stop + """ # noqa: D301 + log.info("Start signal requested") + + return not dt.model_running diff --git a/datahub/opal.py b/datahub/opal.py index 4464c85..878b7db 100644 --- a/datahub/opal.py +++ b/datahub/opal.py @@ -166,6 +166,6 @@ def get_opal_row(data: dict[str, int | float] | list[int | float]) -> pd.DataFra row = pd.DataFrame( [data_array], index=[data_index], columns=list(opal_headers.keys()) ) - row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="S") + row["Time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(row["Time"], unit="m") return row diff --git a/tests/test_dsr_api.py b/tests/test_dsr_api.py index 057a9f8..768f1d4 100644 --- a/tests/test_dsr_api.py +++ b/tests/test_dsr_api.py @@ -12,7 +12,7 @@ @pytest.fixture(autouse=True) def reset_dsr_data(): """Pytest Fixture for resetting DSR data global variable.""" - dt.dsr_data = [] + dt.reset_data() def test_post_dsr_api(dsr_data_path): diff --git a/tests/test_opal.py b/tests/test_opal.py index f0deb9c..e483cf9 100644 --- a/tests/test_opal.py +++ b/tests/test_opal.py @@ -44,10 +44,10 @@ def test_append_opal_data(opal_data): # Checks that data appended to Dataframe matches data input. data_1["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_1["time"], unit="S" + data_1["time"], unit="m" ) data_2["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_2["time"], unit="S" + data_2["time"], unit="m" ) assert (df.loc[1] == list(data_1.values())[1:]).all() @@ -58,7 +58,7 @@ def test_append_opal_data(opal_data): assert len(df.index) == 2 data_3["time"] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta( - data_3["time"], unit="S" + data_3["time"], unit="m" ) assert (df.loc[2] == list(data_3.values())[1:]).all() @@ -100,6 +100,6 @@ def test_append_opal_data_array(opal_data_array): assert df.shape == (1, len(opal_headers)) - data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="S") + data_1[1] = pd.Timestamp(OPAL_START_DATE) + pd.to_timedelta(data_1[1], unit="m") assert (df.loc[1] == data_1[1:]).all() diff --git a/tests/test_opal_api.py b/tests/test_opal_api.py index 3367e56..e15a26c 100644 --- a/tests/test_opal_api.py +++ b/tests/test_opal_api.py @@ -9,9 +9,7 @@ @pytest.fixture(autouse=True) def reset_opal_data(): """Pytest Fixture for resetting Opal data global variable.""" - from datahub.opal import create_opal_frame - - dt.opal_df = create_opal_frame() + dt.reset_data() def test_post_opal_api(client, opal_data):