Skip to content

Commit

Permalink
refactor(robot-server): Robot server command responses (#7170)
Browse files Browse the repository at this point in the history
* rename models.
* Genericize command requests and responses

closes #6519
  • Loading branch information
amitlissack authored Jan 5, 2021
1 parent 85b3d6b commit 4cab53f
Show file tree
Hide file tree
Showing 18 changed files with 528 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ async def execute(self, command: Command) \
:raises: SessionCommandException
"""
raise UnsupportedCommandException(
f"'{command.content.name}' is not supported"
f"'{command.request.command}' is not supported"
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def __init__(self, command_handler: CommandHandler):
async def execute(self, command: Command) -> CompletedCommand:
"""Execute command"""
with duration() as time_it:
name_arg = command.content.name
data = command.content.data
name_arg = command.request.command
data = command.request.data
data_arg = data.dict() if data else {}

await self._callable(name_arg, data_arg)

return CompletedCommand(
content=command.content,
request=command.request,
meta=command.meta,
result=CommandResult(started_at=time_it.start,
completed_at=time_it.end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
from datetime import datetime
from dataclasses import dataclass, field
from typing import Optional
from typing import Optional, Generic, TypeVar

from robot_server.service.session.models.command import (
CommandDataType, CommandStatus, CommandResultType)
from robot_server.service.session.models.command_definitions import \
CommandDefinitionType
CommandStatus, RequestTypes)
from robot_server.service.session.models.common import (
IdentifierType, create_identifier)
from opentrons.util.helpers import utc_now


@dataclass(frozen=True)
class CommandContent:
name: CommandDefinitionType
data: CommandDataType


@dataclass(frozen=True)
class CommandMeta:
identifier: IdentifierType = field(default_factory=create_identifier)
created_at: datetime = field(default_factory=utc_now)


ResultTypeT = TypeVar("ResultTypeT")


@dataclass(frozen=True)
class CommandResult:
class CommandResult(Generic[ResultTypeT]):
started_at: datetime
completed_at: datetime
status: CommandStatus = CommandStatus.executed
data: Optional[CommandResultType] = None
data: Optional[ResultTypeT] = None


@dataclass(frozen=True)
class Command:
content: CommandContent
request: RequestTypes
meta: CommandMeta = field(default_factory=CommandMeta)


@dataclass(frozen=True)
class CompletedCommand:
content: CommandContent
request: RequestTypes
meta: CommandMeta
result: CommandResult


def create_command(name: CommandDefinitionType,
data: CommandDataType) -> Command:
def create_command(request: RequestTypes) -> Command:
"""Create a command object"""
return Command(
content=CommandContent(
name=name,
data=data
)
request=request
)

This file was deleted.

Loading

0 comments on commit 4cab53f

Please sign in to comment.