diff --git a/.gitignore b/.gitignore index fac4925fe73c3e5b7c043cdfd2cd4cc7b6312feb..5f9fc3bca0802f43d0683ed15e7f4eccb758c964 100644 --- a/.gitignore +++ b/.gitignore @@ -115,4 +115,4 @@ venv.bak/ logs/ -private_testing/ +private_test/ diff --git a/README.md b/README.md index f9f011946ec3de10b40a78012dd6c282e9c90cb3..fa7446f08df15820ed813652586ffd9c0b4d6315 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WZL-MQTT -Current stable version: 2.5.4 +Current stable version: 2.6.0 ## Documentation @@ -90,6 +90,10 @@ the European Union’s Horizon 2020 research and innovation programme and the EM ## Changelog +**2.6.0** - 2024-01-10 + - added the classmethod "client_names()" to client to retrieve the name of all instances + - the classmethod "get(username)" of the client raises the "ChildNotFoundError" if there is no instance having the specified username + **2.5.4** - 2023-04-18 - fixed usage of a randomized client_id, so that the same credentials can be used for multiple clients in parallel diff --git a/setup.py b/setup.py index f27bff26d882e8831a3cac11a56e0db28e1865fc..5e3a0a2043d43c43668ed2dd013a1e696b793784 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ with open("PyPI-README.md", "r", encoding="utf-8") as fh: long_description = fh.read() setup(name='wzl-mqtt', - version='2.5.4', + version='2.6.0', url='https://git-ce.rwth-aachen.de/wzl-mq-public/iot/mqtt/', project_urls={ "Bug Tracker": "https://git-ce.rwth-aachen.de/wzl-mq-public/iot/mqtt/-/issues", diff --git a/src/mqtt/client.py b/src/mqtt/client.py index 8dfb4c3a55924f1324e3df1e4ff34d5d92287781..03434b6e16b95338b1d0079e803bb2b9af4364c7 100644 --- a/src/mqtt/client.py +++ b/src/mqtt/client.py @@ -3,11 +3,11 @@ import sys import traceback import uuid import warnings -from typing import Callable, Union +from typing import Callable, Union, List import paho.mqtt.client as mqtt -from .exceptions import ConnectionError +from .exceptions import ConnectionError, ClientNotFoundError from .exceptions import PublishError from .exceptions import SubscriptionError from .logger import Logger @@ -69,8 +69,23 @@ class MQTTClient: Returns: The client identified by the given key. + Raises: + ChildNotFoundError: If no client with the given username could be found. + + """ + try: + return cls.instances[username] + except KeyError: + raise ClientNotFoundError(f'There is no client with name "{username}".') + + @classmethod + def client_names(cls) -> List[str]: + """Returns al list of the usernames of all clients. + + Returns: List of the usernames of all clients. + """ - return cls.instances[username] + return [key for key in cls.instances] @property def connected(self) -> bool: diff --git a/src/mqtt/exceptions.py b/src/mqtt/exceptions.py index a60c22937f6cba9e02713b81d3016cd69661137c..b378e20963bf904675b92143e5d7617817908f46 100644 --- a/src/mqtt/exceptions.py +++ b/src/mqtt/exceptions.py @@ -21,3 +21,8 @@ class CallbackError(MQTTException): class PublishError(MQTTException): def __init__(self, *args, **kwargs): MQTTException.__init__(self, *args, **kwargs) + + +class ClientNotFoundError(MQTTException): + def __init__(self, *args, **kwargs): + MQTTException.__init__(self, *args, **kwargs)