pycsamt.iot.protocols#
Telemetry transports for IoT-enabled field acquisition.
This package supersedes the former single-module protocols.py. It
keeps the original public names (IoTProtocol,
TelemetryAck, TelemetryClient,
build_telemetry_client()) and adds concrete clients:
FileTelemetryClient— newline-delimited JSON (stdlib).HTTPTelemetryClient— HTTP(S) POST (stdliburllib).MQTTTelemetryClient— MQTT (needspaho-mqtt).SerialTelemetryClient— UART (needspyserial).WebSocketTelemetryClient— WebSocket (needswebsocket-client).
Every client supports dry_run=True for offline recording.
Module Attributes
Concrete client for each protocol with a real transport implementation. |
Functions
|
Construct a telemetry client for protocol. |
- class pycsamt.iot.protocols.IoTProtocol(*values)#
-
Supported telemetry protocol identifiers.
- MQTT = 'mqtt'#
- HTTP = 'http'#
- LORA = 'lora'#
- SERIAL = 'serial'#
- FILE = 'file'#
- WEBSOCKET = 'websocket'#
- class pycsamt.iot.protocols.TelemetryAck(ok, protocol, packet_id, detail='')#
Bases:
objectAcknowledgement returned by a telemetry client.
- exception pycsamt.iot.protocols.TelemetryError#
Bases:
RuntimeErrorRaised when a telemetry transport operation fails.
- class pycsamt.iot.protocols.BaseTelemetryClient(endpoint=None, *, protocol=None, dry_run=False, **options)#
Bases:
objectCommon behaviour for telemetry transports.
- Parameters:
endpoint (str, optional) – Transport-specific destination (broker URL, file path, URL, serial port, …).
protocol (IoTProtocol) – The protocol this client implements.
dry_run (bool) – When
True, packets are recorded tosentand no real transport is opened.**options – Transport-specific options (credentials, timeouts, TLS, …).
- protocol: IoTProtocol = 'file'#
- sent: list[TelemetryPacket]#
- connect()#
Open the transport (no-op in dry-run mode).
- Return type:
None
- disconnect()#
Close the transport (no-op in dry-run mode).
- Return type:
None
- receive(*, timeout=None)#
Receive one payload, or
Nonein dry-run/unsupported mode.
- subscribe(topic)#
Register interest in topic (transport permitting).
- Parameters:
topic (str)
- Return type:
None
- class pycsamt.iot.protocols.TelemetryClient(endpoint=None, *, protocol=IoTProtocol.FILE, dry_run=True, **options)#
Bases:
BaseTelemetryClientGeneric recorder used for dry-run simulation and unmapped protocols.
Retains the historical dry-run behaviour: with
dry_run=True(default) packets are recorded tosent. Withdry_run=Falseit has no real transport and raisesNotImplementedErroron send, since a concrete client should be used instead.- Parameters:
endpoint (str | None)
protocol (IoTProtocol)
dry_run (bool)
options (Any)
- class pycsamt.iot.protocols.FileTelemetryClient(endpoint=None, *, dry_run=False, append=True, **options)#
Bases:
BaseTelemetryClientAppend/replay telemetry packets to a JSON-lines file.
- protocol: IoTProtocol = 'file'#
- class pycsamt.iot.protocols.HTTPTelemetryClient(endpoint=None, *, dry_run=False, timeout=10.0, token=None, headers=None, method='POST', **options)#
Bases:
BaseTelemetryClientPOST telemetry packets to an HTTP(S) endpoint.
- Parameters:
- protocol: IoTProtocol = 'http'#
- class pycsamt.iot.protocols.MQTTTelemetryClient(endpoint=None, *, dry_run=False, host=None, port=1883, username=None, password=None, tls=False, keepalive=60, client_id=None, **options)#
Bases:
BaseTelemetryClientPublish/subscribe telemetry over MQTT.
- Parameters:
- protocol: IoTProtocol = 'mqtt'#
- class pycsamt.iot.protocols.SerialTelemetryClient(endpoint=None, *, dry_run=False, baudrate=115200, timeout=1.0, **options)#
Bases:
BaseTelemetryClientSend/receive telemetry over a serial port.
- protocol: IoTProtocol = 'serial'#
- class pycsamt.iot.protocols.StoreAndForwardClient(client, *, spool_path=None, max_queue=None, base_backoff_s=1.0, max_backoff_s=300.0)#
Bases:
objectWrap a telemetry client with a persistent offline send buffer.
- Parameters:
client (BaseTelemetryClient) – The underlying transport used for real sends.
spool_path (str, optional) – Path to a JSON-lines spool file. When given, the queue is persisted on every change and reloaded on construction, so a node keeps its backlog across restarts.
max_queue (int, optional) – Maximum number of buffered packets. When the buffer is full the oldest packet is dropped (the newest data is the most valuable for a live survey).
Nonemeans unbounded.base_backoff_s (float) – Base delay for the exponential backoff hint (see
next_retry_delay_s).max_backoff_s (float) – Ceiling for the backoff hint.
- property next_retry_delay_s: float#
Exponential-backoff hint for when to next call
flush().Returns
0.0when the queue is empty. After consecutive flush failures the delay grows asbase * 2**(failures-1), capped atmax_backoff_s– a caller’s scheduling loop can honour this without this class ever sleeping itself.
- send(packet)#
Send packet, or queue it when the transport is unavailable.
A direct send is attempted only when the buffer is empty, so a backlog is never overtaken by fresh packets. On transport failure the packet is queued and a
queuedacknowledgement is returned rather than raising.- Parameters:
packet (Any)
- Return type:
- class pycsamt.iot.protocols.WebSocketTelemetryClient(endpoint=None, *, dry_run=False, timeout=10.0, **options)#
Bases:
BaseTelemetryClientSend/receive telemetry over a WebSocket connection.
- protocol: IoTProtocol = 'websocket'#
- pycsamt.iot.protocols.CLIENT_REGISTRY: dict[IoTProtocol, type[BaseTelemetryClient]] = {IoTProtocol.FILE: <class 'pycsamt.iot.protocols.file.FileTelemetryClient'>, IoTProtocol.HTTP: <class 'pycsamt.iot.protocols.http.HTTPTelemetryClient'>, IoTProtocol.MQTT: <class 'pycsamt.iot.protocols.mqtt.MQTTTelemetryClient'>, IoTProtocol.SERIAL: <class 'pycsamt.iot.protocols.serial.SerialTelemetryClient'>, IoTProtocol.WEBSOCKET: <class 'pycsamt.iot.protocols.websocket.WebSocketTelemetryClient'>}#
Concrete client for each protocol with a real transport implementation.
- pycsamt.iot.protocols.build_telemetry_client(protocol, *, endpoint=None, dry_run=True, **options)[source]#
Construct a telemetry client for protocol.
- Parameters:
protocol (str or IoTProtocol) – Transport identifier (
"mqtt","http","file","serial","websocket", or"lora").endpoint (str, optional) – Transport destination (broker URL, file path, HTTP URL, port).
dry_run (bool) – When
True(default), the returned client records packets without opening a real transport — safe for tests and demos.**options – Transport-specific options forwarded to the client.
- Returns:
A concrete client when the protocol has a real transport, else a generic
TelemetryClientrecorder (e.g. forlora).- Return type: