ByteCommand

The most basic form of a `Command`, which ingests and returns bytes.

Bases
Command[bytes, bytes]

Methods

  • __init__(self, command : bytes, timeout : typing.Optional[float]) -> None

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      command
      Type
      bytes
      Default
      Description

    • Name
      timeout
      Type
      typing.Optional[float]
      Default
      = None
      Description

  • serialize(self, message : typing.Optional[bytes]) -> bytes

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      message
      Type
      typing.Optional[bytes]
      Default
      = None
      Description

    Response

    Type
    bytes
    Description

  • deserialize(self, response : typing.Optional[bytes]) -> bytes

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      response
      Type
      typing.Optional[bytes]
      Default
      = None
      Description

    Response

    Type
    bytes
    Description

  • result(self) -> typing.Optional[OutType_co]

    Deserializes the `Response.payload.result()` bytes.

    Response

    Type
    typing.Optional[OutType_co]
    Description

    The deserialized `Response.payload`

  • validate_request(self, message : bytes) -> bool

    Validate a serialized message. Called within `Command.request` before generating a `Request` object.

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      message
      Type
      bytes
      Default
      Description

      The message to set as the `Request.payload`, if valid.

    Response

    Type
    bool
    Description

    Whether or not the `message` is valid.

  • _set_response(self) -> None

    Set the result of `self.response.payload` to `self._response_buffer` and clears `self._response_buffer`.

  • validate_response(self, data : bytes) -> None

    This method is called by `Protocol.data_received` and is responsible for setting the `Response.payload`. It manages the `_response_buffer` that accumulates the response bytes and calls `_validate_response` to determine whether the accumulated message in the `_response_buffer` is finished or 'valid'. If the response is valid, it sets the `Response.payload.result` to the accumulated bytes from the `_response_buffer`.

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      data
      Type
      bytes
      Default
      Description

      The bytes from the `Transport` to add to the response_buffer and evaluate for completeness.

  • _validate_response(self, data : bytes) -> bool

    Validate the data received from the `Transport` and determine if the data is a complete response. Subclasses override this method to specify behavior when validating a response.

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      data
      Type
      bytes
      Default
      Description

      The bytes to evaluate for completeness.

    Response

    Type
    bool
    Description

  • match_response(self, data : bytes) -> bool

    For devices that allow parallel command processing, first check if `data` belongs to this command and then validate the response.

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      data
      Type
      bytes
      Default
      Description

      The bytes to check for match during parallel processing, usually an identifier shared by request and response.

    Response

    Type
    bool
    Description

    True if the `data` matches to this command, otherwise False.

Attributes

  • Name
    receiver
    Type
    typing.Optional[Protocol]
    Value

    = None

    Description

  • Name
    _response
    Type
    typing.Optional[Response]
    Value

    = None

    Description

  • Name
    _request
    Type
    typing.Optional[Request]
    Value

    = None

    Description

  • Name
    message
    Type
    Value

    = message

    Description

  • Name
    timeout
    Type
    Value

    = timeout

    Description

  • Name
    _response_buffer
    Type
    Value

    = b''

    Description

  • Name
    is_void
    Type
    Value

    = is_void

    Description

  • Name
    request
    Type
    Request
    Value

    = None

    Description

    The `Request` which will be used by the `Protocol` to send bytes to the device. Calls `validate_request` on the `command` before serializing it and creating the `Request` object.

  • Name
    response
    Type
    Response
    Value

    = None

    Description

    The `Response` used by `Protocol.data_received` to set the `payload` of the command. When using the `Protocol.execute` method, the `Protocol` will call `validate_response` from within it's `Protocol.data_received` method and only set the result if valid.

Copyright © 2024