From 28fdb2687f6e77225884a9862b6e3f17115c6688 Mon Sep 17 00:00:00 2001
From: Matthias Bodenbenner <m.bodenbenner@wzl-mq.rwth-aachen.de>
Date: Sat, 15 Apr 2023 09:22:15 +0200
Subject: [PATCH] V 2.5.3

---
 PyPI-README.md     |  7 ++++++-
 README.md          |  6 +++++-
 setup.py           |  2 +-
 src/mqtt/client.py | 20 ++++++++++++++++++--
 test/publish.py    |  8 ++++----
 5 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/PyPI-README.md b/PyPI-README.md
index ffe72ff..af02689 100644
--- a/PyPI-README.md
+++ b/PyPI-README.md
@@ -1,6 +1,6 @@
 # WZL-MQTT
 
-Current stable version: 2.5.2
+Current stable version: 2.5.3
 
 ## Documentation
 
@@ -79,6 +79,11 @@ while True:
 
 ## Changelog
 
+**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
+  - 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/README.md b/README.md
index 75544da..2c1ea95 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # WZL-MQTT
 
-Current stable version: 2.5.2
+Current stable version: 2.5.3
 
 ## Documentation
 
@@ -90,6 +90,10 @@ the European Union’s Horizon 2020 research and innovation programme and the EM
 
 ## Changelog
 
+**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
+
 **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 7ce09fe..3a799fc 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.2',
+      version='2.5.3',
       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 a9730d8..cdb05ad 100644
--- a/src/mqtt/client.py
+++ b/src/mqtt/client.py
@@ -97,19 +97,35 @@ class MQTTClient:
 
             If not given explicitly given, the port automatically resolved from the values of "websocket" and "ssl".
         """
+
+        address = broker
         try:
-            address = broker
+            if isinstance(port, str):
+                port = int(port)
+        except Exception as exception:
+            self._logger.error(f"Specified port \"{port}\" is invalid. Port must be of type string or integer. Exiting...")
+            exit()
 
+        try:
             if port == 0:
                 if ssl:
                     port = 443 if websocket else 8883
                 else:
                     port = 80 if websocket else 1883
 
+
             if ssl:
+                if port not in [8883, 443]:
+                    self._logger.error(
+                        f"Can not connect to the broker. If ssl is set, the port must be \"8883\" (or \"443\" in case websockets are used), but specified port is \"{port}\". Exiting...")
+                    exit()
                 self._client.tls_set()
 
             if websocket:
+                if port not in [80, 443]:
+                    self._logger.error(
+                        f"Can not connect to the broker. If websocket is set, the port must be \"80\" (or \"443\" in case ssl is used), but specified port is \"{port}\". Exiting...")
+                    exit()
                 self._client._transport = "websockets"
                 fields = address.split("/")
                 address = fields[0]
@@ -147,7 +163,7 @@ class MQTTClient:
     def _on_connect(self, client, userdata, flags, rc):
         if rc == 0:
             self._logger.info(
-                "MQTT Client {} connect terminated with code {} ({}).".format(
+                "MQTT Client {} connected successfully (code {}: {}).".format(
                     self.name, rc, mqtt.error_string(rc)))
             self._connected = True
         else:
diff --git a/test/publish.py b/test/publish.py
index 926458e..ffdabd0 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 = ""
-MQTT_PASSWORD = ""
+MQTT_USER = "bdn-aafdecf0-a14c-4cb5-bb08-3844399e0a25"
+MQTT_PASSWORD = "azesR4Q8~M7UBKh<7S~d\"NN-|)i9:Le["
 
 MQTT_BROKER = "mqtt.wzl-mq.rwth-aachen.de"
-MQTT_PORT = 8883
+MQTT_PORT = "0"
 MQTT_VHOST = "metrology"
 
 ### to connect to the central MQTT-Broker of MQ-MS use the following settings:
@@ -25,7 +25,7 @@ MQTT_VHOST = "metrology"
 
 if __name__ == "__main__":
     client = mqtt.MQTTPublisher(prefix="bdn-212d8419-c75c-471f-8ea5-f3a5c19ac42e")
-    client.connect(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=True)
+    client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT, ssl=True)
 
     while True:
         try:
-- 
GitLab