Skip to content
Snippets Groups Projects
Commit a9d25175 authored by Matthias Stefan Bodenbenner's avatar Matthias Stefan Bodenbenner
Browse files

changed signature of connect method

parent 65885302
Branches
No related tags found
No related merge requests found
......@@ -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
......
# WZL-MQTT
[![pipeline status](https://git-ce.rwth-aachen.de/wzl-mq-ms/forschung-lehre/digital-mars/mqtt/badges/master/pipeline.svg)](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,14 +71,17 @@ 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
......
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'],
......
......@@ -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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment