diff --git a/sample/publish.py b/sample/publish.py index 60bed0fe43e0834cec3c6fa73d2c3413c4073203..8e5571530cf25510dc941d8cc0900699e385f6ba 100644 --- a/sample/publish.py +++ b/sample/publish.py @@ -10,24 +10,21 @@ MQTT_USER = "" MQTT_PASSWORD = "" MQTT_BROKER = "127.0.0.1" -MQTT_PORT = 1883 MQTT_VHOST = "/" +WEBSOCKET = False +SSL = True -## To connect to the central MQTT-Broker of MQ-MS use the settings below. -## Ask Mark Sanders (sdr) for personal credentials. -# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de" -# MQTT_PORT = 1883 -# MQTT_VHOST = "metrology" +topic = "test" # set topic to publish according to MQTT syntax! if __name__ == "__main__": client = mqtt.MQTTPublisher() - client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_USER, MQTT_PASSWORD) + client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=SSL, websocket=WEBSOCKET) while True: try: message = json.dumps({"value": [random.uniform(0, 5) for i in range(3)], "timestamp": datetime.datetime.utcnow().isoformat() + "Z", "covariance": [[2, 0, 0], [0, 2, 0], [0, 0, 0]], "nonce": str(uuid.uuid4()), "hash": None, "unit": "MTR"}) - client.publish("channel-001", message.encode("utf-8"), 0) + client.publish(topic, message.encode("utf-8"), 0) time.sleep(1) except KeyboardInterrupt: break diff --git a/sample/receive.py b/sample/receive.py index 1315312004be614751601ad3ffc9de5c08f4080f..58ab8137587eac271f5a5299441a2ea44d4341a5 100644 --- a/sample/receive.py +++ b/sample/receive.py @@ -2,32 +2,25 @@ import time from wzl import mqtt -logger = mqtt.root_logger.get('Receiver') - MQTT_USER = "" MQTT_PASSWORD = "" MQTT_BROKER = "127.0.0.1" -MQTT_PORT = 1883 MQTT_VHOST = "/" +WEBSOCKET = False +SSL = True -## To connect to the central MQTT-Broker of MQ-MS use the settings below. -## Ask Mark Sanders (sdr) for personal credentials. -# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de" -# MQTT_PORT = 1883 -# MQTT_VHOST = "metrology" - -topic = "#" # set topic to subscribe according to MQTT syntax! +topic = "test" # 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() - client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_USER, MQTT_PASSWORD) + client.connect(MQTT_BROKER, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, ssl=SSL, websocket=WEBSOCKET) client.set_callback("PRINT", print_mqtt_message) client.subscribe(topic, qos) diff --git a/src/mqtt/__init__.py b/src/mqtt/__init__.py index 5919f59a911e0c66ab90c828762186d0dac38366..440b517c74b604707231027ab96b298bd5b3a178 100644 --- a/src/mqtt/__init__.py +++ b/src/mqtt/__init__.py @@ -1,9 +1,8 @@ -from .exceptions import MQTTException -from .exceptions import ConnectionError -from .exceptions import SubscriptionError -from .exceptions import CallbackError -from .exceptions import PublishError - from .client import MQTTPublisher from .client import MQTTSubscriber +from .exceptions import CallbackError +from .exceptions import ConnectionError +from .exceptions import MQTTException +from .exceptions import PublishError +from .exceptions import SubscriptionError from .logger import Logger diff --git a/src/mqtt/logger.py b/src/mqtt/logger.py index f924e93aa18be48f7f5baa981b8cf14e1edbe1bd..b9c2874cc57cc728097a62825949909768f21c8a 100644 --- a/src/mqtt/logger.py +++ b/src/mqtt/logger.py @@ -11,8 +11,8 @@ class Logger(object): """Creates a basic logger for console/terminal and optional file output. Args: - filename: Name of the file the output should be logged to, file format can be ommited. - Current timestamp and file format is automatically appened to the given filename. + filename: Name of the file the output should be logged to, file format can be omitted. + Current timestamp and file format is automatically append to the given filename. If no, filename is given, the output is written to console/terminal only. path: Optional path to the logging file. If omitted, file is created at the current working directory. level: Specifies which information should be logged as specified by the python logging module. @@ -63,7 +63,7 @@ class Logger(object): Returns: A basic python logger with the given name. """ - return logging.getLogger(name) + return self.root_logger.getChild(name) def set_logging_level(self, level: int, target: str = '') -> None: """Sets the logging level. @@ -84,3 +84,6 @@ class Logger(object): self.console_handler.setLevel(level) if self._filename is not None: self.file_handler.setLevel(level) + self.root_logger.setLevel(level) + self.root_logger.addHandler(self.file_handler) + self.root_logger.addHandler(self.console_handler)