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
No related branches found
No related tags found
No related merge requests found
...@@ -40,7 +40,7 @@ Build Documentation: ...@@ -40,7 +40,7 @@ Build Documentation:
tags: tags:
- frodo - frodo
script: 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: Push Documentation:
image: docker:20.10 image: docker:20.10
...@@ -51,7 +51,7 @@ Push Documentation: ...@@ -51,7 +51,7 @@ Push Documentation:
- frodo - frodo
script: script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY - 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: Deploy Documentation:
stage: deploy stage: deploy
......
# WZL-MQTT # 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) [![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 ## Installation
Requires at least Python 3.6 Requires at least Python 3.6
...@@ -27,7 +27,7 @@ MQTT_VHOST = "/" ...@@ -27,7 +27,7 @@ MQTT_VHOST = "/"
# initialize publisher and connect to the broker # initialize publisher and connect to the broker
client = mqtt.MQTTPublisher() 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 # 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", 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): ...@@ -55,7 +55,7 @@ def print_mqtt_message(topic, message):
# initialize subscriber and connect to the broker # initialize subscriber and connect to the broker
client = mqtt.MQTTSubscriber() 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 # register the callback and subscribe topic
client.set_callback("PRINT", print_mqtt_message) client.set_callback("PRINT", print_mqtt_message)
...@@ -71,14 +71,17 @@ while True: ...@@ -71,14 +71,17 @@ while True:
``` ```
Slightly more detailled examples can be found in the *sample* directory. 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). If there are any questions contact [Matthias Bodenbenner](mailto:m.bodenbenner@wzl-mq.rwth-aachen.de).
To obtain credentials for MQTT-Broker of WZL-MQ-MS contact [Mark Sanders](mailto:m.sanders@wzl.rwth-aachen.de)
## Documentation ## 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/) 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 ## 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 2.4.2
- fixed a bug of websocket connection - fixed a bug of websocket connection
......
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup(name='wzl-mqtt', setup(name='wzl-mqtt',
version='2.4.2', version='2.5.0',
url='', url='',
author='Benjamin Montavon, Matthias Bodenbenner', author='Matthias Bodenbenner, Benjamin Montavon',
author_email='m.bodenbenner@wzl.rwth-aachen.de', author_email='m.bodenbenner@wzl-mq.rwth-aachen.de',
description='Small library containing an MQTT publisher and receiver.', description='Small library containing an MQTT publisher and receiver.',
package_dir={'wzl': 'src'}, package_dir={'wzl': 'src'},
packages=['wzl.mqtt'], packages=['wzl.mqtt'],
......
...@@ -78,25 +78,32 @@ class MQTTClient: ...@@ -78,25 +78,32 @@ class MQTTClient:
""" """
return self._connected return self._connected
def connect(self, broker: str, port: Union[str, int], username: str, def connect(self, broker: str, username: str, password: str, vhost: str = '', port: Union[str, int] = 0,
password: str, vhost: str = '', websocket: bool = False, websocket: bool = False, ssl: bool = False, keep_alive: int = 60):
ssl: bool = False, keep_alive: int = 60):
"""Opens a connection to an MQTT-broker under the given address and post. """Opens a connection to an MQTT-broker under the given address and post.
Args: Args:
broker: The address (URL) of the MQTT-broker to connect to. 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. username: The username required for authentication at the broker.
password: The password 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. 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. 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. ssl: If true a secured TLS connection is established.
keep_alive: maximum period in seconds allowed between communications with the broker. 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 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: try:
address = broker address = broker
if port == 0:
if ssl:
port = 443 if websocket else 8883
else:
port = 80 if websocket else 1883
if ssl: if ssl:
self._client.tls_set() self._client.tls_set()
...@@ -153,8 +160,7 @@ class MQTTClient: ...@@ -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 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 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" "\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( .format(self.name, rc, mqtt.error_string(rc)))
self.name, rc, mqtt.error_string(rc)))
else: else:
self._logger.info( self._logger.info(
"MQTT Client {} disconnected with code {} ({}).".format( "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