From a9d251755c17307a50242d2162f8ed92de08b3de Mon Sep 17 00:00:00 2001 From: Matthias Bodenbenner <m.bodenbenner@wzl-mq.rwth-aachen.de> Date: Fri, 18 Mar 2022 11:51:14 +0100 Subject: [PATCH] changed signature of connect method --- .gitlab-ci.yml | 4 ++-- README.md | 15 +++++++++------ setup.py | 6 +++--- src/mqtt/client.py | 18 ++++++++++++------ 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 15a378a..ff22068 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,7 +40,7 @@ Build Documentation: tags: - frodo script: - - docker build -t registry.git-ce.rwth-aachen.de/wzl-mq-ms/forschung-lehre/digital-mars/mqtt . + - docker build -t $CI_REGISTRY/$CI_PROJECT_PATH . Push Documentation: image: docker:20.10 @@ -51,7 +51,7 @@ Push Documentation: - frodo script: - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY - - docker push registry.git-ce.rwth-aachen.de/wzl-mq-ms/forschung-lehre/digital-mars/mqtt:latest + - docker push $CI_REGISTRY/$CI_PROJECT_PATH:latest Deploy Documentation: stage: deploy diff --git a/README.md b/README.md index 471cf0d..6a4ff74 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # WZL-MQTT [](https://git-ce.rwth-aachen.de/wzl-mq-ms/forschung-lehre/digital-mars/mqtt/badges/master) -Current stable version: 2.4.2 +Current stable version: 2.5.0 ## Installation Requires at least Python 3.6 @@ -27,7 +27,7 @@ MQTT_VHOST = "/" # initialize publisher and connect to the broker client = mqtt.MQTTPublisher() -client.connect(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST) +client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT) # create message and publish the message as UTF-8 encoded string message = json.dumps({"value": [random.uniform(0, 5) for i in range(3)], "timestamp": datetime.datetime.utcnow().isoformat() + "Z", @@ -55,7 +55,7 @@ def print_mqtt_message(topic, message): # initialize subscriber and connect to the broker client = mqtt.MQTTSubscriber() -client.connect(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST) +client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT) # register the callback and subscribe topic client.set_callback("PRINT", print_mqtt_message) @@ -71,16 +71,19 @@ while True: ``` Slightly more detailled examples can be found in the *sample* directory. -If there are any questions contact [Matthias Bodenbenner](mailto:m.bodenbenner@wzl.rwth-aachen.de). -To obtain credentials for MQTT-Broker of WZL-MQ-MS contact [Mark Sanders](mailto:m.sanders@wzl.rwth-aachen.de) +If there are any questions contact [Matthias Bodenbenner](mailto:m.bodenbenner@wzl-mq.rwth-aachen.de). ## Documentation For more detailed explanation and full API documentation, view [https://iot.wzl-mq.rwth-aachen.de/documentation/libs/mqtt/](https://iot.wzl-mq.rwth-aachen.de/documentation/libs/mqtt/) ## Changelog +2.5.0 + - changed signature of connect method + - specifying the port is optional now, if not specified the port automatically is derived from the "websocket" and "ssl" flags + 2.4.2 -- fixed a bug of websocket connection + - fixed a bug of websocket connection 2.4.1 - made the error message of code 1 more precise diff --git a/setup.py b/setup.py index 0a3325a..6e5916f 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ from setuptools import setup, find_packages setup(name='wzl-mqtt', - version='2.4.2', + version='2.5.0', url='', - author='Benjamin Montavon, Matthias Bodenbenner', - author_email='m.bodenbenner@wzl.rwth-aachen.de', + author='Matthias Bodenbenner, Benjamin Montavon', + author_email='m.bodenbenner@wzl-mq.rwth-aachen.de', description='Small library containing an MQTT publisher and receiver.', package_dir={'wzl': 'src'}, packages=['wzl.mqtt'], diff --git a/src/mqtt/client.py b/src/mqtt/client.py index 4ab774f..4dc84f8 100644 --- a/src/mqtt/client.py +++ b/src/mqtt/client.py @@ -78,25 +78,32 @@ class MQTTClient: """ return self._connected - def connect(self, broker: str, port: Union[str, int], username: str, - password: str, vhost: str = '', websocket: bool = False, - ssl: bool = False, keep_alive: int = 60): + def connect(self, broker: str, username: str, password: str, vhost: str = '', port: Union[str, int] = 0, + websocket: bool = False, ssl: bool = False, keep_alive: int = 60): """Opens a connection to an MQTT-broker under the given address and post. Args: broker: The address (URL) of the MQTT-broker to connect to. - port: The port behind which the broker is running and accessible. username: The username required for authentication at the broker. password: The password required for authentication at the broker. vhost: Virtual host to connect to at the MQTT-Broker. + port: The port behind which the broker is running and accessible. websocket: If true MQTT messages are published/received over WebSockets. If false, the default transportation over raw TCP is used. ssl: If true a secured TLS connection is established. keep_alive: maximum period in seconds allowed between communications with the broker. If no other messages are being exchanged, this controls the rate at which the client will send ping messages to the broker. + + If not given explicitly given, the port automatically resolved from the values of "websocket" and "ssl". """ try: address = broker + if port == 0: + if ssl: + port = 443 if websocket else 8883 + else: + port = 80 if websocket else 1883 + if ssl: self._client.tls_set() @@ -153,8 +160,7 @@ class MQTTClient: "\t\t 2. You may tried to use Publisher-Credentials for receiving messages or vise versa.\n" "\t\t 3. You tried to publish to or subscribe a topic which you are not allowed to do. \n" "\t\t 4. Something else, which has not been experienced by the developers of this library. Sorry!\n" - "If you don't find a solution, please contact Mark Sanders or Matthias Bodenbenner to check if your login credentials are correct!".format( - self.name, rc, mqtt.error_string(rc))) + .format(self.name, rc, mqtt.error_string(rc))) else: self._logger.info( "MQTT Client {} disconnected with code {} ({}).".format( -- GitLab