Ejemplo n.º 1
0
 def data_received(self, data):
     try:
         command = Command(data[0])
         schema, solicited = RX_COMMANDS[command]
     except ValueError:
         LOGGER.debug("Unknown command received: 0x%02x", data[0])
         return
     seq = data[1]
     try:
         status = Status(data[2])
     except ValueError:
         status = data[2]
     try:
         data, _ = t.deserialize(data[5:], schema)
     except Exception as exc:
         LOGGER.warning("Failed to deserialize frame: %s",
                        binascii.hexlify(data))
         if solicited and seq in self._awaiting:
             fut = self._awaiting.pop(seq)
             fut.set_exception(exc)
         return
     if solicited and seq in self._awaiting:
         fut = self._awaiting.pop(seq)
         if status != Status.SUCCESS:
             fut.set_exception(
                 CommandError(status, "%s, status: %s" % (command, status)))
             return
         fut.set_result(data)
     getattr(self, "_handle_%s" % (command.name, ))(data)
Ejemplo n.º 2
0
 async def _command(self, cmd, *args):
     LOGGER.debug("Command %s %s", cmd, args)
     if self._uart is None:
         # connection was lost
         raise CommandError(Status.ERROR, "API is not running")
     data, seq = self._api_frame(cmd, *args)
     self._uart.send(data)
     fut = asyncio.Future()
     self._awaiting[seq] = fut
     try:
         return await asyncio.wait_for(fut, timeout=COMMAND_TIMEOUT)
     except asyncio.TimeoutError:
         LOGGER.warning("No response to '%s' command", cmd)
         self._awaiting.pop(seq)
         raise
Ejemplo n.º 3
0
    def data_received(self, data):
        try:
            command = Command(data[0])
            schema, solicited = RX_COMMANDS[command]
        except ValueError:
            LOGGER.debug("Unknown command received: 0x%02x", data[0])
            return
        seq = data[1]
        try:
            status = Status(data[2])
        except ValueError:
            status = data[2]

        fut = None
        if solicited and seq in self._awaiting:
            fut = self._awaiting.pop(seq)
            if status != Status.SUCCESS:
                try:
                    fut.set_exception(
                        CommandError(status,
                                     "%s, status: %s" % (command, status)))
                except asyncio.InvalidStateError:
                    LOGGER.warning(
                        "Duplicate or delayed response for 0x:%02x sequence",
                        seq)
                return

        try:
            data, _ = t.deserialize(data[5:], schema)
        except Exception:
            LOGGER.warning("Failed to deserialize frame: %s",
                           binascii.hexlify(data))
            if fut is not None and not fut.done():
                fut.set_exception(
                    APIException(
                        f"Failed to deserialize frame: {binascii.hexlify(data)}"
                    ))
            return

        if fut is not None:
            try:
                fut.set_result(data)
            except asyncio.InvalidStateError:
                LOGGER.warning(
                    "Duplicate or delayed response for 0x:%02x sequence", seq)

        getattr(self, "_handle_%s" % (command.name, ))(data)