async def test_session_command_execute_no_body(check_session_instance,
                                               mock_cal_session):
    await check_session_instance.command_executor.execute(
        create_command(CalibrationCommand.load_labware, EmptyModel()))

    mock_cal_session.trigger_transition.assert_called_once_with(
        trigger="calibration.loadLabware")
async def test_session_command_execute(check_session_instance,
                                       mock_cal_session):
    await check_session_instance.command_executor.execute(
        create_command(CalibrationCommand.jog, JogPosition(vector=(1, 2, 3))))

    mock_cal_session.trigger_transition.assert_called_once_with(
        trigger="calibration.jog", vector=(1.0, 2.0, 3.0))
예제 #3
0
async def session_command_execute_handler(
    sessionId: IdentifierType,
    command_request: CommandRequest,
    session_manager: SessionManager = Depends(get_session_manager),
) -> CommandResponse:
    """
    Execute a session command
    """
    session_obj = get_session(manager=session_manager,
                              session_id=sessionId,
                              api_router=router)
    if not session_manager.is_active(session_obj.meta.identifier):
        raise CommandExecutionException(
            reason=f"Session '{sessionId}' is not active. "
            "Only the active session can execute commands")

    command = create_command(command_request.data.attributes.command,
                             command_request.data.attributes.data)
    command_result = await session_obj.command_executor.execute(command)
    log.info(f"Command completed {command}")

    return CommandResponse(data=ResponseDataModel.create(
        attributes=SessionCommand(
            data=command_result.content.data,
            command=command_result.content.name,
            status=command_result.result.status,
            createdAt=command_result.meta.created_at,
            startedAt=command_result.result.started_at,
            completedAt=command_result.result.completed_at),
        resource_id=command_result.meta.identifier),
                           links=get_valid_session_links(sessionId, router))
 async def test_calls_handler(self, hardware, mocked_handlers):
     h = HardwareExecutor(hardware=hardware, command_filter=None)
     accepted_command = tuple(HardwareExecutor.COMMAND_TO_FUNC.keys())[0]
     c = create_command(name=accepted_command, data={'a': 3})
     await h.execute(c)
     h.COMMAND_TO_FUNC[accepted_command].assert_called_once_with(
         hardware, {'a': 3}
     )
예제 #5
0
async def session_command_execute_handler(
    session_id: route_models.IdentifierType,
    command_request: route_models.CommandRequest,
    session_manager: SessionManager = Depends(get_session_manager),
) -> route_models.CommandResponse:
    """
    Execute a session command
    """
    session_obj = get_session(manager=session_manager,
                              session_id=session_id,
                              api_router=router)
    if not session_manager.is_active(session_obj.meta.identifier):
        raise RobotServerError(
            status_code=http_status_codes.HTTP_403_FORBIDDEN,
            error=Error(
                title=f"Session '{session_id}' is not active",
                detail="Only the active session can execute commands",
            ))

    try:
        command = create_command(command_request.data.attributes.command,
                                 command_request.data.attributes.data)
        command_result = await session_obj.command_executor.execute(command)
        log.debug(f"Command completed {command}")
    # TODO: Amit 07/30/2020 - SessionCommandException should be a
    #  RobotServerError. Customized by raiser with status code and rest of
    #  Error attributes. The job here would be to log and re-raise.
    except CommandExecutionConflict as e:
        log.exception("Failed to execute command due to conflict")
        raise RobotServerError(status_code=http_status_codes.HTTP_409_CONFLICT,
                               error=Error(
                                   title="Command execution conflict",
                                   detail=str(e),
                               ))
    except SessionCommandException as e:
        log.exception("Failed to execute command")
        raise RobotServerError(
            status_code=http_status_codes.HTTP_400_BAD_REQUEST,
            error=Error(
                title="Command execution error",
                detail=str(e),
            ))

    return route_models.CommandResponse(data=ResponseDataModel.create(
        attributes=route_models.SessionCommand(
            data=command_result.content.data,
            command=command_result.content.name,
            status=command_result.result.status,
            createdAt=command_result.meta.created_at,
            startedAt=command_result.result.started_at,
            completedAt=command_result.result.completed_at),
        resource_id=command_result.meta.identifier),
                                        links=get_valid_session_links(
                                            session_id, router))
예제 #6
0
async def test_session_command_execute_raise(check_session_instance,
                                             mock_cal_session,
                                             command_exception):
    async def raiser(*args, **kwargs):
        raise command_exception("Cannot do it")

    mock_cal_session.trigger_transition.side_effect = raiser

    with pytest.raises(SessionCommandException):
        await check_session_instance.command_executor.execute(
            create_command(CommandName.jog, JogPosition(vector=(1, 2, 3))))
예제 #7
0
async def session_command_execute_handler(
    session_id: route_models.IdentifierType,
    command_request: route_models.CommandRequest,
    session_manager: SessionManager = Depends(get_session_manager),
) -> route_models.CommandResponse:
    """
    Execute a session command
    """
    session_obj = get_session(manager=session_manager,
                              session_id=session_id,
                              api_router=router)
    if not session_manager.is_active(session_obj.meta.identifier):
        raise RobotServerError(
            status_code=http_status_codes.HTTP_403_FORBIDDEN,
            error=Error(
                title=f"Session '{session_id}' is not active",
                detail="Only the active session can execute commands",
            ))

    try:
        command = create_command(command_request.data.attributes.command,
                                 command_request.data.attributes.data)
        command_result = await session_obj.command_executor.execute(command)
        log.debug(f"Command completed {command}")
    except SessionCommandException as e:
        log.exception("Failed to execute command")
        raise RobotServerError(
            status_code=http_status_codes.HTTP_400_BAD_REQUEST,
            error=Error(
                title="Command execution error",
                detail=str(e),
            ))

    return route_models.CommandResponse(data=ResponseDataModel.create(
        attributes=route_models.SessionCommand(
            data=command_result.content.data,
            command=command_result.content.name,
            status=command_result.result.status),
        resource_id=command_result.meta.identifier),
                                        links=get_valid_session_links(
                                            session_id, router))
 async def test_unsupported_command_raises(self, hardware, mocked_handlers):
     unknown_command = "-".join(HardwareExecutor.COMMAND_TO_FUNC.keys())
     h = HardwareExecutor(hardware=hardware, command_filter=None)
     c = create_command(name=unknown_command, data={'a': 3})
     with pytest.raises(UnsupportedCommandException):
         await h.execute(c)