embutils.serial.stream module
Stream implementation.
- date
2021
- author
Christian Wiche
- contact
- license
The MIT License (MIT)
- class embutils.serial.stream.AbstractSerializedStreamCodec[source]
Bases:
embutils.utils.serialized.AbstractSerializedCodecAbstractSerializedCodec 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.AbstractServiceThis 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:
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)
on_connect: This event is emitted when the system is able to connect to the device. Subscribe using callbacks with syntax:
def <callback>()
on_reconnect: This event is emitted when the system is able to reconnect to the device. Subscribe using callbacks with syntax:
def <callback>()
on_disconnect: This event is emitted when the system gets disconnected from the device. Subscribe using callbacks with syntax:
def <callback>()
Class initialization.
- Parameters
device (Device) – Serial device handler.
codec (AbstractSerializedStreamCodec) – Serialized objects codec handler.
- 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.
- _task() None[source]
Stream process:
Tries to decode items.
If an item is available, emits an event.
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.