Skip to content

Client

Module Name: client.py

Description: This module contains the relevant classes for the persistmq functionality. It provides a convinient interface for moving from pure paho-mqtt. The Client spawns a process which runs in parallel and caches the queued data, if there are communication problems

Usage: Import the PersistClient class and create an instance: from persistmq.client import PersistClient my_client = PersistClient(client_id="testclient", cache_path=Path("/tmp/mymqttcache")) my_client.connect_async(mqtt_host="localhost") my_persist_client.publish("dt/blah", "Test Message") my_persist_client.stop()

Classes: - PersistClient: The class for convinient usage of the PersistMqWorker - PersistMqWorker: Worker class (do not use directly)

Depends on: - paho.mqtt

Author: Michael Oberhofer

Version: 0.0.1

Date: August 31, 2024

PersistClient

Bases: object

__init__(client_id, cache_type='sq3', cache_path=Path('./cache'), **kwargs)

Create an instance of PersistClient

Parameters:

Name Type Description Default
client_id str

id for underlying mqtt-client

required
cache_type str

method of caching ["sq3", "pickle"]. Defaults to "sq3".

'sq3'
cache_path Path

path for cache file(s). Defaults to Path("./cache").

Path('./cache')
**kwargs

additional arguments for paho-client

{}

connect_async(mqtt_host, mqtt_port=1883, **kwargs)

Start the worker process with connection to broker (uses paho mqtt connect_async method)

Parameters:

Name Type Description Default
mqtt_host str

hostname, ip of broker

required
mqtt_port int

port of broker. Defaults to 1883.

1883
kwargs optional

additional parameter for paho mqtt connect_async

{}

publish(topic, payload, qos=2)

Add a message to be published

Parameters:

Name Type Description Default
topic str

topic to be published

required
payload Union[str, bytes, bytearray, int, float, None]

payload to be published

required
qos int

Quality of Service. Defaults to 2.

2

stop()

Gently stop the worker and cache remaining data from queue

PersistMqWorker

__init__(mqtt_client, mqtt_host, mqtt_port, message_q, command_q, cache_path, **kwargs)

Worker class for actual processing the messages

Parameters:

Name Type Description Default
mqtt_client Client

paho-mqtt client instance to be used for actual publishing

required
mqtt_host str

hostname/ip of the mqtt broker to connect

required
mqtt_port int

port of the mqtt broker to connect

required
message_q Queue

queue for messages

required
command_q Queue

queue for worker commands

required
cache_path Path

cache path for either files or database

required