Beispiel #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)
Beispiel #2
0
    async def read_parameter(self, id_, *args):
        try:
            if isinstance(id_, str):
                param = NetworkParameter[id_]
            else:
                param = NetworkParameter(id_)
        except (KeyError, ValueError):
            raise KeyError("Unknown parameter id: %s" % (id_,))

        data = t.serialize(args, NETWORK_PARAMETER_SCHEMA[param])
        r = await self._command(Command.read_parameter, 1 + len(data), param, data)
        data = t.deserialize(r[2], NETWORK_PARAMETER_SCHEMA[param])[0]
        LOGGER.debug("Read parameter %s response: %s", param.name, data)
        return data
Beispiel #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)