Command

Generic Command that can be used with `Protocol.execute`. The first type parameter of the `Command` determines the type that the `Command` accepts on init and serialization, and the second type parameter determines the type returned by deserialization, e.g. `Command[str, typing.List[bool]]` would ingest strings and returns a list of booleans.

Bases
typing.Generic[InType_co, OutType_co]

Methods

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

    Parameters

    • Name
      self
      Type
      Default
      Description

    • Name
      message
      Type
      InType_co
      Default
      Description

      The contents of the message to be sent to the device, pre-serialization.

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

      How long is seconds to wait for a response.

    • Name
      is_void
      Type
      typing.Optional[bool]
      Default
      = False
      Description

      If true, does not return a response. Void commands ignore all response validations.

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

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

    Response

    Type
    typing.Optional[OutType_co]
    Description

    The deserialized `Response.payload`

  • @abc.abstractmethod

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

    Serializes the message into bytes. Should use `self.message` if `message` is None.

    Parameters

    • Name
      self
      Type
      Default
      Description

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

      A command input, or None to use `self.message`.

    Response

    Type
    bytes
    Description

    The serialized message to be sent to the device.

  • @abc.abstractmethod

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

    Deserializes the `response` bytes. Should call `self.result()` if `response` is None.

    Parameters

    • Name
      self
      Type
      Default
      Description

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

      bytes to be deserialized, or None to use `self.result()`.

    Response

    Type
    typing.Optional[OutType_co]
    Description

    The deserialized `response`.

  • 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