async def execute(self, command: commands.Command) -> dict:
        encoded_request = command.encoded_request.upper()
        resp_future = self.add_request(encoded_request)

        try:
            await self._conn.write(encoded_request)
            message = await asyncio.wait_for(resp_future,
                                             timeout=self._timeout)

            # Create a new command of the same type to contain response
            response_cmd = type(command).from_encoded(encoded_request, message)
            decoded = response_cmd.decoded_response

            # If the call failed, its response will be an exception
            # We can raise it here
            if isinstance(decoded, BaseException):
                raise decoded

        except asyncio.CancelledError:  # pragma: no cover
            raise

        except asyncio.TimeoutError:
            raise exceptions.CommandTimeout(f'{type(command).__name__}')

        finally:
            self.remove_request(encoded_request)

        return decoded
Exemple #2
0
    async def execute(self, command: commands.Command) -> dict:
        encoded_request = command.encoded_request.upper()
        await self._conduit.write(encoded_request)

        while True:
            # Wait for a request resolution (matched by request)
            # Request will be resolved with a timestamped response
            queue = self._requests[encoded_request].queue

            try:
                response = await asyncio.wait_for(
                    queue.get(), timeout=REQUEST_TIMEOUT.seconds)
            except TimeoutError:
                raise exceptions.CommandTimeout(f'{type(command).__name__}')

            if not response.fresh:
                warnings.warn(f'Discarding stale response: {response}')
                continue

            # Create a new command of the same type to contain response
            response_cmd = type(command).from_encoded(encoded_request,
                                                      response.content)
            decoded = response_cmd.decoded_response

            # If the call failed, its response will be an exception
            # We can raise it here
            if isinstance(decoded, BaseException):
                raise decoded

            return decoded
Exemple #3
0
    async def _execute(
        self,
        opcode: Opcode,
        payload: Optional[EncodedPayload],
        /,
    ) -> list[EncodedPayload]:
        msg_id = self._next_id()

        request = EncodedRequest(msgId=msg_id, opcode=opcode, payload=payload)

        _, request_data = await self._codec.encode((codec.REQUEST_TYPE, None),
                                                   request.dict())
        fut: asyncio.Future[EncodedResponse] = asyncio.get_running_loop(
        ).create_future()
        self._active_messages[msg_id] = fut

        try:
            await self._conn.write(request_data)
            enc_response = await asyncio.wait_for(fut, timeout=self._timeout)

            if enc_response.error != ErrorCode.OK:
                raise exceptions.CommandException(
                    f'{opcode.name}, {enc_response.error.name}')

            return enc_response.payload

        except asyncio.TimeoutError:
            raise exceptions.CommandTimeout(opcode.name)

        finally:
            del self._active_messages[msg_id]
Exemple #4
0
 async def _reboot(self, request):
     self._start_time = datetime.now()
     raise exceptions.CommandTimeout()
Exemple #5
0
 async def _factory_reset(self, request):
     await self.startup(self.app)
     raise exceptions.CommandTimeout()