Skip to content

Commit

Permalink
Add API calls and button functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbland committed Dec 13, 2023
1 parent 0dadec9 commit 1f5595c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 49 deletions.
53 changes: 53 additions & 0 deletions app/datahub_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,56 @@ def get_wesim_data() -> dict[str, dict]: # type: ignore[type-arg]
req = request_datahub("wesim")

return req.json()["data"]


def start_model() -> str:
"""Function for starting the model.
Returns:
str: Message to display on the control app
"""
try:
# Get signal to see if model is already running
start_signal = request_datahub("start").json()
if start_signal:
return "Model is already running"

# If not running, send signal to start
requests.post(f"{DH_URL}/set_model_signals", json={"start": True})

# Get signal to see if model has started properly
start_signal = request_datahub("start").json()
return (
"Model started successfully"
if start_signal
else "Failed to start the model"
)

except (DataHubConnectionError, DataHubRequestError):
return "Failed to connect to the DataHub"


def stop_model() -> str:
"""Function for stopping the model.
Returns:
str: Message to display on the control app
"""
try:
# Get signal to see if model is already stopped
stop_signal = request_datahub("stop").json()
if stop_signal:
return "Model is not running"

# If running, send signal to stop
requests.post(f"{DH_URL}/set_model_signals", json={"start": False})

# Get signal to see if model has stopped properly
stop_signal = request_datahub("stop").json()
return (
"Model stopped successfully"
if not stop_signal
else "Failed to stop the model"
)
except (DataHubConnectionError, DataHubRequestError):
return "Failed to connect to the DataHub"
114 changes: 65 additions & 49 deletions app/pages/control.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Controller Page for Dash app."""

import dash # type: ignore
from dash import Input, Output, State, callback, ctx, dcc, html # type: ignore
from dash import Input, Output, State, callback, dcc, html # type: ignore
from dash_iconify import DashIconify # type: ignore

from .. import LIVE_MODEL, log
from .. import core_api as core
from .. import log
from ..data import data_interval
from ..datahub_api import start_model, stop_model

dash.register_page(__name__)

Expand Down Expand Up @@ -224,9 +225,6 @@ def get_button(func: str, icon: str) -> html.Button:
Output("message", "children", allow_duplicate=True),
[
Input("button_update", "n_clicks"),
Input("button_start", "n_clicks"),
Input("button_stop", "n_clicks"),
Input("button_restart", "n_clicks"),
],
[
State("Hub01_dropdown", "value"),
Expand All @@ -241,10 +239,7 @@ def get_button(func: str, icon: str) -> html.Button:
prevent_initial_call=True,
)
def update_button_click(
button_update: int | None,
button_start: int | None,
button_stop: int | None,
button_restart: int | None,
n_clicks: int | None,
Hub01_dropdown: str,
Hub02_dropdown: str,
PC01_Top_dropdown: str,
Expand All @@ -254,47 +249,24 @@ def update_button_click(
PC02_Left_dropdown: str,
PC02_Right_dropdown: str,
) -> list[str]:
"""Placeholder function for buttons."""
button_id = ctx.triggered_id[7:]

if button_id == "update":
"""Will make an API call to set up OVE sections accoding to dropdowns.
Args: Value inputs for the 8 dropdown menus
"""
log.debug("Clicked Update Button!")
message = core.assign_sections(
{
"Hub01": Hub01_dropdown,
"Hub02": Hub02_dropdown,
"PC01-Top": PC01_Top_dropdown,
"PC01-Left": PC01_Left_dropdown,
"PC01-Right": PC01_Right_dropdown,
"PC02-Top": PC02_Top_dropdown,
"PC02-Left": PC02_Left_dropdown,
"PC02-Right": PC02_Right_dropdown,
}
)
return [message]

elif button_id == "start":
"""Will make an API call to start the Gridlington simulation and Datahub."""
log.debug("Clicked Start Button!")
return ["Clicked Start Button!"]

elif button_id == "stop":
"""Will make an API call to stop the Gridlington simulation and Datahub."""
log.debug("Clicked Stop Button!")
return ["Clicked Stop Button!"]

elif button_id == "restart":
"""Will make an API call to restart the Gridlington simulation and Datahub."""
log.debug("Clicked Restart Button!")
core.refresh_sections()
return ["Clicked Restart Button!"]
"""Will make an API call to set up OVE sections accoding to dropdowns.
else:
return [""]
Args: Value inputs for the 8 dropdown menus
"""
log.debug("Clicked Update Button!")
message = core.assign_sections(
{
"Hub01": Hub01_dropdown,
"Hub02": Hub02_dropdown,
"PC01-Top": PC01_Top_dropdown,
"PC01-Left": PC01_Left_dropdown,
"PC01-Right": PC01_Right_dropdown,
"PC02-Top": PC02_Top_dropdown,
"PC02-Left": PC02_Left_dropdown,
"PC02-Right": PC02_Right_dropdown,
}
)
return [message]


@callback(
Expand Down Expand Up @@ -328,6 +300,50 @@ def default_button_click(n_clicks: int | None) -> list[str]:
]


@callback(
Output("message", "children", allow_duplicate=True),
Output("data_interval", "disabled", allow_duplicate=True),
[Input("button_start", "n_clicks")],
prevent_initial_call=True,
)
def start_button_click(n_clicks: int | None) -> tuple[str, bool]:
"""Function for start button."""
log.debug("Clicked Start Button!")
if LIVE_MODEL:
message = start_model()
else:
message = "Clicked Start Button!"
return message, False


@callback(
Output("message", "children", allow_duplicate=True),
Output("data_interval", "disabled", allow_duplicate=True),
[Input("button_stop", "n_clicks")],
prevent_initial_call=True,
)
def stop_button_click(n_clicks: int | None) -> tuple[str, bool]:
"""Function for stop button."""
log.debug("Clicked Stop Button!")
if LIVE_MODEL:
message = stop_model()
else:
message = "Clicked Stop Button!"
return message, True


@callback(
Output("message", "children", allow_duplicate=True),
[Input("button_restart", "n_clicks")],
prevent_initial_call=True,
)
def restart_button_click(n_clicks: int | None) -> list[str]:
"""Function for restart button. TODO."""
log.debug("Clicked Restart Button!")
core.refresh_sections()
return ["Clicked Restart Button!"]


@callback(
[Output("data_interval", "interval")], [Input("update-interval-slider", "value")]
)
Expand Down

0 comments on commit 1f5595c

Please sign in to comment.