async def execute(self, command: Command) -> CompletedCommand:
        """Command processing"""
        command_def = command.content.name
        if command_def not in self.STATE_COMMAND_MAP.get(
                self.current_state, {}
        ):
            raise UnsupportedCommandException(
                f"Can't execute '{command_def}' during "
                f"state '{self.current_state}'")

        handler = self._handlers.get(command_def)
        if not handler:
            raise UnsupportedCommandException(
                f"Command '{command_def}' is not supported."
            )

        with duration() as timed:
            await handler()

        self._events.append(
            models.ProtocolSessionEvent(
                source=models.EventSource.session_command,
                event=command.content.name,
                commandId=command.meta.identifier,
                timestamp=timed.end,
            )
        )

        return CompletedCommand(
            content=command.content,
            meta=command.meta,
            result=CommandResult(started_at=timed.start,
                                 completed_at=timed.end))
Exemple #2
0
def test_duration_raises(mock_utcnow, mock_start_time):
    try:
        with util.duration() as t:
            raise AssertionError()
    except AssertionError:
        pass

    assert t.start == mock_start_time
    assert t.end == mock_start_time + timedelta(days=1)
Exemple #3
0
 async def execute(self, command: Command) -> CompletedCommand:
     func = self.get_handler(command.content.name)
     if func:
         with duration() as timed:
             await func(self._hardware, command.content.data)
     else:
         raise UnsupportedCommandException(
             f"Command '{command.content.name}' is not supported.")
     return CompletedCommand(content=command.content,
                             meta=command.meta,
                             result=CommandResult(started_at=timed.start,
                                                  completed_at=timed.end))
Exemple #4
0
    async def execute(self, command: Command) -> CompletedCommand:
        """Execute command"""
        with duration() as time_it:
            name_arg = command.content.name.value
            data = command.content.data
            data_arg = data.dict() if data else {}

            await self._callable(name_arg, data_arg)

        return CompletedCommand(content=command.content,
                                meta=command.meta,
                                result=CommandResult(started_at=time_it.start,
                                                     completed_at=time_it.end))
Exemple #5
0
def test_duration(mock_utcnow, mock_start_time):
    with util.duration() as t:
        pass

    assert t.start == mock_start_time
    assert t.end == mock_start_time + timedelta(days=1)