-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(robot-server): status bar responds to runs in progress (#12794)
* add function to get status bar state * OT-2 API incorrectly renamed the `state` parameter for `set_status_bar_state` * Added dependency to ensure light task runs on the robot * Added light control driver & task; added tests for light control driver
- Loading branch information
Showing
6 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Background task to drive the status bar.""" | ||
from typing import Optional | ||
from logging import getLogger | ||
import asyncio | ||
|
||
from .engine_store import EngineStore | ||
from opentrons.hardware_control import HardwareControlAPI | ||
from opentrons.protocol_engine.types import EngineStatus | ||
from opentrons.hardware_control.types import StatusBarState | ||
|
||
log = getLogger(__name__) | ||
|
||
|
||
def _engine_status_to_status_bar(status: Optional[EngineStatus]) -> StatusBarState: | ||
"""Convert an engine status into a status bar status.""" | ||
if status is None: | ||
return StatusBarState.IDLE | ||
|
||
return { | ||
EngineStatus.IDLE: StatusBarState.IDLE, | ||
EngineStatus.RUNNING: StatusBarState.RUNNING, | ||
EngineStatus.PAUSED: StatusBarState.PAUSED, | ||
EngineStatus.BLOCKED_BY_OPEN_DOOR: StatusBarState.PAUSED, | ||
EngineStatus.STOP_REQUESTED: StatusBarState.RUNNING, | ||
EngineStatus.STOPPED: StatusBarState.RUNNING, | ||
EngineStatus.FINISHING: StatusBarState.RUNNING, | ||
EngineStatus.FAILED: StatusBarState.HARDWARE_ERROR, | ||
EngineStatus.SUCCEEDED: StatusBarState.RUN_COMPLETED, | ||
}[status] | ||
|
||
|
||
class LightController: | ||
"""LightController sets the status bar to match the protocol status.""" | ||
|
||
def __init__(self, api: HardwareControlAPI, engine_store: EngineStore) -> None: | ||
"""Create a new LightController.""" | ||
self._api = api | ||
self._engine_store = engine_store | ||
|
||
async def update( | ||
self, prev_status: Optional[EngineStatus], new_status: Optional[EngineStatus] | ||
) -> None: | ||
"""Update the status bar if the current run status has changed.""" | ||
if prev_status == new_status: | ||
# No change, don't try to set anything. | ||
return | ||
|
||
await self._api.set_status_bar_state( | ||
state=_engine_status_to_status_bar(status=new_status) | ||
) | ||
|
||
def get_current_status(self) -> Optional[EngineStatus]: | ||
"""Get the `status` value from the engine's active run engine.""" | ||
current_id = self._engine_store.current_run_id | ||
if current_id is not None: | ||
return self._engine_store.engine.state_view.commands.get_status() | ||
|
||
return None | ||
|
||
|
||
async def run_light_task(driver: LightController) -> None: | ||
"""Run the light control task. | ||
This is intended to be run as a background task once the EngineStore has been created. | ||
""" | ||
prev_status = driver.get_current_status() | ||
while True: | ||
await asyncio.sleep(0.1) | ||
new_status = driver.get_current_status() | ||
await driver.update(prev_status=prev_status, new_status=new_status) | ||
prev_status = new_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""Unit tests for `runs.light_control_task`.""" | ||
|
||
import pytest | ||
from typing import Optional | ||
from decoy import Decoy | ||
|
||
from opentrons.hardware_control import HardwareControlAPI | ||
from opentrons.hardware_control.types import StatusBarState | ||
from opentrons.protocol_engine.types import EngineStatus | ||
from robot_server.runs.engine_store import EngineStore | ||
from robot_server.runs.light_control_task import LightController | ||
|
||
|
||
@pytest.fixture | ||
def engine_store(decoy: Decoy) -> EngineStore: | ||
"""Mock out the EngineStore.""" | ||
return decoy.mock(cls=EngineStore) | ||
|
||
|
||
@pytest.fixture | ||
def subject( | ||
hardware_api: HardwareControlAPI, engine_store: EngineStore | ||
) -> LightController: | ||
"""Test subject - LightController.""" | ||
return LightController(api=hardware_api, engine_store=engine_store) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["active", "status"], | ||
[ | ||
[False, EngineStatus.IDLE], | ||
[True, EngineStatus.IDLE], | ||
[True, EngineStatus.RUNNING], | ||
[False, EngineStatus.FAILED], | ||
], | ||
) | ||
async def test_get_current_status( | ||
decoy: Decoy, | ||
engine_store: EngineStore, | ||
subject: LightController, | ||
active: bool, | ||
status: EngineStatus, | ||
) -> None: | ||
"""Test LightController.get_current_status.""" | ||
decoy.when(engine_store.current_run_id).then_return("fake_id" if active else None) | ||
decoy.when(engine_store.engine.state_view.commands.get_status()).then_return(status) | ||
|
||
expected = status if active else None | ||
|
||
assert subject.get_current_status() == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["prev_state", "new_state", "expected"], | ||
[ | ||
[None, None, StatusBarState.IDLE], | ||
[EngineStatus.IDLE, None, StatusBarState.IDLE], | ||
[EngineStatus.IDLE, EngineStatus.IDLE, None], | ||
[None, EngineStatus.IDLE, StatusBarState.IDLE], | ||
[None, EngineStatus.PAUSED, StatusBarState.PAUSED], | ||
[ | ||
EngineStatus.RUNNING, | ||
EngineStatus.BLOCKED_BY_OPEN_DOOR, | ||
StatusBarState.PAUSED, | ||
], | ||
[EngineStatus.RUNNING, EngineStatus.FAILED, StatusBarState.HARDWARE_ERROR], | ||
[EngineStatus.RUNNING, EngineStatus.SUCCEEDED, StatusBarState.RUN_COMPLETED], | ||
], | ||
) | ||
async def test_light_controller_update( | ||
decoy: Decoy, | ||
hardware_api: HardwareControlAPI, | ||
subject: LightController, | ||
prev_state: Optional[EngineStatus], | ||
new_state: Optional[EngineStatus], | ||
expected: StatusBarState, | ||
) -> None: | ||
"""Test LightController.update. | ||
Verifies that the status bar is NOT updated if the state is the same, and | ||
checks that state mapping is correct. | ||
""" | ||
await subject.update(prev_status=prev_state, new_status=new_state) | ||
|
||
call_count = 0 if prev_state == new_state else 1 | ||
|
||
decoy.verify( | ||
await hardware_api.set_status_bar_state(state=expected), times=call_count | ||
) |