embutils.serial.stream module

Stream implementation.

date

2021

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

class embutils.serial.stream.AbstractSerializedStreamCodec[source]

Bases: embutils.utils.serialized.AbstractSerializedCodec

AbstractSerializedCodec variant for stream usage. This class includes the logic required to decode a serialized object from a byte stream.

_abc_impl = <_abc_data object>
abstract decode_stream(device: embutils.serial.device.Device) Optional[embutils.utils.serialized.AbstractSerialized][source]

Decodes a serialized object from a byte stream.

Parameters

device (Device) – Stream source.

Returns

Deserialized object if able, None otherwise.

Return type

Optional[AbstractSerialized]

Raises

ConnectionError – Device raised exception while reading.

class embutils.serial.stream.Stream(device: embutils.serial.device.Device, codec: embutils.serial.stream.AbstractSerializedStreamCodec, **kwargs)[source]

Bases: embutils.utils.service.AbstractService

This class is used to send and receive serialized objects through a serial device in asynchronous way. When a new item is received this class will notify the system using events.

Available events:

  1. on_receive: This event is emitted when an object is received and deserialized from the serial device. Subscribe using callbacks with syntax:

    def <callback>(item: AbstractSerialized)
    
  2. on_connect: This event is emitted when the system is able to connect to the device. Subscribe using callbacks with syntax:

    def <callback>()
    
  3. on_reconnect: This event is emitted when the system is able to reconnect to the device. Subscribe using callbacks with syntax:

    def <callback>()
    
  4. on_disconnect: This event is emitted when the system gets disconnected from the device. Subscribe using callbacks with syntax:

    def <callback>()
    

Class initialization.

Parameters
RECONNECT_PERIOD_S = 0.5

Stream reconnect period

_abc_impl = <_abc_data object>
_device_connect() bool[source]

Perform connection attempts on the serial device while the service is running.

Returns

True if connection succeeded, false otherwise.

Return type

bool

_device_init(start: bool = True) None[source]

Initializes the serial device.

Parameters

start (bool) – True if used on start handler. False otherwise.

_on_end() None[source]

Closes device when service gets terminated.

_on_pause() None[source]

Closes device when paused.

_on_resume() None[source]

Ensures device connected on service resume.

_on_start() None[source]

Ensures device connected on service start.

_task() None[source]

Stream process:

  1. Tries to decode items.

  2. If an item is available, emits an event.

  3. Handle disconnection status.

static _transfer_debug(item: embutils.utils.serialized.AbstractSerialized, received: bool) None[source]

Print the sent/received items on the logger:

Parameters
  • item (AbstractSerialized) – Item that is being sent/received.

  • received (bool) – Flag to indicate if we are sending/receiving the item.

property codec: embutils.serial.stream.AbstractSerializedStreamCodec

Serialized objects codec handler.

property device: embutils.serial.device.Device

Serial device handler.

send(item: embutils.utils.serialized.AbstractSerialized) None[source]

Send a serializable item through the serial device.

Parameters

item (AbstractSerialized) – Item to send.