diff --git a/PyPI-README.md b/PyPI-README.md index af026898da27f78e9833e57e6713b8c1364ca9e4..d84d4c22b6b8baecf0c42a4b7517fe4c54301882 100644 --- a/PyPI-README.md +++ b/PyPI-README.md @@ -1,6 +1,6 @@ # WZL-MQTT -Current stable version: 2.5.3 +Current stable version: 2.5.4 ## Documentation @@ -79,6 +79,9 @@ while True: ## Changelog +**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 + **2.5.3** - 2023-04-15 - if the port is specified as string it is also correctly processed now - improved user feedback in case of successful connection to the broker diff --git a/README.md b/README.md index 2c1ea955d171320bc96553a11ca9a92d32b02a55..f9f011946ec3de10b40a78012dd6c282e9c90cb3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WZL-MQTT -Current stable version: 2.5.3 +Current stable version: 2.5.4 ## Documentation @@ -90,9 +90,13 @@ the European Union’s Horizon 2020 research and innovation programme and the EM ## Changelog +**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 + **2.5.3** - 2023-04-15 - if the port is specified as string it is also correctly processed now - - improved logging output in case of successful connection to the broker + - improved user feedback in case of successful connection to the broker + - improved user feedback if the combination of port, ssl and websocket is invalid **2.5.2** - 2023-03-29 - client handles slash in at the end of prefix and the beginning of topic to avoid multiple consecutive slashes correctly now diff --git a/setup.py b/setup.py index 3a799fc6e3bc9bc9d35fed0789532b29c382c3a7..f27bff26d882e8831a3cac11a56e0db28e1865fc 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.3', + version='2.5.4', 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", @@ -21,6 +21,6 @@ setup(name='wzl-mqtt', "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], - install_requires=['paho-mqtt==1.5.1'], + install_requires=['paho-mqtt~=1.5.1'], python_requires='>=3.6', zip_safe=False) diff --git a/src/mqtt/client.py b/src/mqtt/client.py index cdb05ad966aa99d10f3af9975f4a1f3ff73f6c9a..8dfb4c3a55924f1324e3df1e4ff34d5d92287781 100644 --- a/src/mqtt/client.py +++ b/src/mqtt/client.py @@ -43,15 +43,16 @@ class MQTTClient: self._logger = logger.get( 'MQTTClient') if logger is not None else Logger().get('MQTTClient') - if username in MQTTClient.instances: + self.name = username if username is not None else str(uuid.uuid4()) + + if self.name in MQTTClient.instances: self._logger.error( - "MQTT Client {} already exists!".format(username)) - raise IndexError("MQTT Client {} already exists!".format(username)) + "MQTT Client {} already exists!".format(self.name)) + raise IndexError("MQTT Client {} already exists!".format(self.name)) - MQTTClient.instances[username] = self - self.name = username if username is not None else uuid.uuid4() + MQTTClient.instances[self.name] = self self.prefix = prefix - self._client = mqtt.Client(username, *args, **kwargs) + self._client = mqtt.Client(self.name, *args, **kwargs) self._client.on_connect = self._on_connect self._client.on_disconnect = self._on_disconnect diff --git a/test/3d_test.py b/test/3d_test.py deleted file mode 100644 index ed09203e12f896a4b1d574944ff5de79dc2fc1f2..0000000000000000000000000000000000000000 --- a/test/3d_test.py +++ /dev/null @@ -1,37 +0,0 @@ -import MQTT -import json -from datetime import datetime -import time -import random - -MQTT_USER = "test" -MQTT_PASSWORD = "test" - -MQTT_BROKER = "localhost"#"134.130.225.37"# -MQTT_PORT = 1883 -MQTT_VHOST = "/" - -client = MQTT.Client(MQTT_USER) -client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_USER, MQTT_PASSWORD) - -pos = [0,0,0] - -i=0 -while True: - i+=1 - client.publish( - topic="quatsch"+"/VAR-position", - message=json.dumps({ - "value": pos, - "timestamp": datetime.utcnow().isoformat()+"Z", - "covariance": None, - "nonce": {}, - "hash": None, - "unit": "P1" - }), - qos=0, - ) - print(i) - pos = [max(0,min(10,value + random.uniform(-0.1,0.1))) for value in pos] - print(pos) - time.sleep(1) \ No newline at end of file diff --git a/test/__init__.py b/test/__init__.py deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/test/measure_latency.pyw b/test/measure_latency.pyw deleted file mode 100644 index 5ae236ffc2193f7398b38bda14bb8a71dd8b2a60..0000000000000000000000000000000000000000 --- a/test/measure_latency.pyw +++ /dev/null @@ -1,85 +0,0 @@ -import asyncio -import datetime -# For sample data -import json -import logging -import time -import uuid - -# import matplotlib.pyplot as plt -import numpy - -from src import mqtt - -### Ask for settings and individual credentials ### - -MQTT_PUBLISHER_USER = "" -MQTT_PUBLISHER_PASSWORD = "" - -MQTT_RECEIVER_USER = "" -MQTT_RECEIVER_PASSWORD = "" - -### to connect to the central MQTT-Broker of MQ-MS use the following settings: -MQTT_BROKER = "localhost" # wzl-mbroker01.wzl.rwth-aachen.de" -MQTT_PORT = 1883 -MQTT_VHOST = "" # "metrology" - -### Ask for settings and individual credentials ### - -topic = "#" # set topic to subscribe according to MQTT syntax! -qos = 0 # set QoS according to MQTT specifications! - -counter = 0 -starts = [] -delay = [] - -def print_mqtt_message(topic, message): - global starts, delay - stop = time.perf_counter() - counter = json.loads(message.decode("utf-8"))['value'] - for count, start in starts: - if count == counter: - delay += [stop - start] - break - - # print("{}\r\n### {} ###\r\n{}\r\n".format(datetime.datetime.utcnow().isoformat() + "Z", topic, message.decode("utf-8"))) - -if __name__=="__main__": - console_log = logging.StreamHandler() - console_log.setFormatter(mqtt.formatter) - - mqtt.logger.addHandler(console_log) - mqtt.logger.setLevel(logging.INFO) - - pclient = mqtt.MQTTPublisher(MQTT_PUBLISHER_USER) - pclient.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_PUBLISHER_USER, MQTT_PUBLISHER_PASSWORD) - - client = mqtt.MQTTSubscriber(MQTT_RECEIVER_USER) - client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_RECEIVER_USER, MQTT_RECEIVER_PASSWORD) - - client.set_callback("PRINT", print_mqtt_message) - client.subscribe(topic, qos) - - loop = asyncio.get_event_loop() - - while counter < 1e2: - try: - message = json.dumps({"value" : counter, "timestamp" : datetime.datetime.utcnow().isoformat() + "Z", "covariance": [[2,0,0], [0,2,0], [0,0,0]], "nonce" : str(uuid.uuid4()), "hash" : None, "unit" : "MTR"}) - starts += [(counter, time.perf_counter())] - pclient.publish(MQTT_PUBLISHER_USER + "/channel-001",message.encode("utf-8"), 0) - time.sleep(0.1) - counter += 1 - except KeyboardInterrupt: - break - - delay = numpy.array(delay) - print("Nr. messages: {}".format(delay.size)) - print("Average delay: {} ms".format(delay.mean() * 1e3)) - print("Std. Dev. delay: {} ms".format(delay.std() * 1e3)) - - # n, bins, patches = plt.hist(delay, 50, density=True, facecolor='g', alpha=0.75) - # plt.xlabel('Delay') - # plt.ylabel('Count') - # plt.text(0.01, 50, r'$\mu={},\ \sigma={}$'.format(delay.mean() * 1e3, delay.std() * 1e3)) - # plt.show() - diff --git a/test/performance_test.py b/test/performance_test.py deleted file mode 100644 index 0e85820d892a039e24dabb6f776f15eb13b9af46..0000000000000000000000000000000000000000 --- a/test/performance_test.py +++ /dev/null @@ -1,34 +0,0 @@ -import src.mqtt as mqtt -import json -from datetime import datetime -import random -import time - -MQTT_USER = "test" -MQTT_PASSWORD = "test" - -MQTT_BROKER = "localhost"#"134.130.225.37"# -MQTT_PORT = 1883 -MQTT_VHOST = "/" - -client = mqtt.client.MQTTPublisher(MQTT_USER) -client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+ MQTT_USER, MQTT_PASSWORD) -test=0 -i=0 -while True: - i+=1 - test += random.random()-0.5 - client.publish( - topic="quatsch"+"/OBJ-Environment/OBJ-0/VAR-TEST", - message=json.dumps({ - "value": [float(test)], - "timestamp": datetime.utcnow().isoformat()+"Z", - "covariance": None, - "nonce": {}, - "hash": None, - "unit": "P1" - }), - qos=0, - ) - print(i) - time.sleep(1) \ No newline at end of file diff --git a/test/publish.py b/test/publish.py index ffdabd0a8eed6cb705b9caaf27d4e621dbb9985f..be96c6f350b673cbc2b94b9115faa70e5aa9ad97 100644 --- a/test/publish.py +++ b/test/publish.py @@ -8,11 +8,11 @@ from src import mqtt ### Ask for settings and individual credentials ### -MQTT_USER = "bdn-aafdecf0-a14c-4cb5-bb08-3844399e0a25" -MQTT_PASSWORD = "azesR4Q8~M7UBKh<7S~d\"NN-|)i9:Le[" +MQTT_USER = "" +MQTT_PASSWORD = "" MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de" -MQTT_PORT = "0" +MQTT_PORT = 8883 MQTT_VHOST = "metrology" ### to connect to the central MQTT-Broker of MQ-MS use the following settings: @@ -24,7 +24,7 @@ MQTT_VHOST = "metrology" if __name__ == "__main__": - client = mqtt.MQTTPublisher(prefix="bdn-212d8419-c75c-471f-8ea5-f3a5c19ac42e") + client = mqtt.MQTTPublisher() client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT, ssl=True) while True: diff --git a/test/receive.py b/test/receive.py index ed7a604ec69f67ca89bd7a44800499c42cc37054..f1948e3d57255542cc0f9da3697eeb66a5719fe9 100644 --- a/test/receive.py +++ b/test/receive.py @@ -2,7 +2,7 @@ import time from src import mqtt -logger = mqtt.root_logger.get('Receiver') +# logger = mqtt.root_logger.get('Receiver') ### Ask for settings and individual credentials ### @@ -13,22 +13,17 @@ MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de" MQTT_PORT = 8883 MQTT_VHOST = "metrology" -## to connect to the central MQTT-Broker of MQ-MS use the following settings: -# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de" -# MQTT_PORT = 1883 -# MQTT_VHOST = "metrology" - ### Ask for settings and individual credentials ### topic = "#" # set topic to subscribe according to MQTT syntax! qos = 0 # set QoS according to MQTT specifications! def print_mqtt_message(topic, message): - logger.info("### {} ###\r\n{}\r\n".format(topic, message.decode("utf-8"))) + print("### {} ###\r\n{}\r\n".format(topic, message.decode("utf-8"))) if __name__=="__main__": - client = mqtt.MQTTSubscriber(prefix="bdn-212d8419-c75c-471f-8ea5-f3a5c19ac42e") - client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_USER, vhost=MQTT_VHOST, MQTT_PASSWORD) + client = mqtt.MQTTSubscriber() + client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True, port=MQTT_PORT) client.set_callback("PRINT", print_mqtt_message) # client.subscribe(topic, qos)