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

updated sample scripts to newest library version

parent 5b713523
No related branches found
No related tags found
No related merge requests found
...@@ -10,24 +10,21 @@ MQTT_USER = "" ...@@ -10,24 +10,21 @@ MQTT_USER = ""
MQTT_PASSWORD = "" MQTT_PASSWORD = ""
MQTT_BROKER = "127.0.0.1" MQTT_BROKER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_VHOST = "/" MQTT_VHOST = "/"
WEBSOCKET = False
SSL = True
## To connect to the central MQTT-Broker of MQ-MS use the settings below. topic = "test" # set topic to publish according to MQTT syntax!
## Ask Mark Sanders (sdr) for personal credentials.
# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de"
# MQTT_PORT = 1883
# MQTT_VHOST = "metrology"
if __name__ == "__main__": if __name__ == "__main__":
client = mqtt.MQTTPublisher() 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: while True:
try: try:
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",
"covariance": [[2, 0, 0], [0, 2, 0], [0, 0, 0]], "nonce": str(uuid.uuid4()), "hash": None, "unit": "MTR"}) "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) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
break break
...@@ -2,32 +2,25 @@ import time ...@@ -2,32 +2,25 @@ import time
from wzl import mqtt from wzl import mqtt
logger = mqtt.root_logger.get('Receiver')
MQTT_USER = "" MQTT_USER = ""
MQTT_PASSWORD = "" MQTT_PASSWORD = ""
MQTT_BROKER = "127.0.0.1" MQTT_BROKER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_VHOST = "/" MQTT_VHOST = "/"
WEBSOCKET = False
SSL = True
## To connect to the central MQTT-Broker of MQ-MS use the settings below. topic = "test" # set topic to subscribe according to MQTT syntax!
## 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!
qos = 0 # set QoS according to MQTT specifications! qos = 0 # set QoS according to MQTT specifications!
def print_mqtt_message(topic, message): 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__": if __name__ == "__main__":
client = mqtt.MQTTSubscriber() 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.set_callback("PRINT", print_mqtt_message)
client.subscribe(topic, qos) client.subscribe(topic, qos)
......
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 MQTTPublisher
from .client import MQTTSubscriber 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 from .logger import Logger
...@@ -11,8 +11,8 @@ class Logger(object): ...@@ -11,8 +11,8 @@ class Logger(object):
"""Creates a basic logger for console/terminal and optional file output. """Creates a basic logger for console/terminal and optional file output.
Args: Args:
filename: Name of the file the output should be logged to, file format can be ommited. filename: Name of the file the output should be logged to, file format can be omitted.
Current timestamp and file format is automatically appened to the given filename. 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. 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. 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. level: Specifies which information should be logged as specified by the python logging module.
...@@ -63,7 +63,7 @@ class Logger(object): ...@@ -63,7 +63,7 @@ class Logger(object):
Returns: A basic python logger with the given name. 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: def set_logging_level(self, level: int, target: str = '') -> None:
"""Sets the logging level. """Sets the logging level.
...@@ -84,3 +84,6 @@ class Logger(object): ...@@ -84,3 +84,6 @@ class Logger(object):
self.console_handler.setLevel(level) self.console_handler.setLevel(level)
if self._filename is not None: if self._filename is not None:
self.file_handler.setLevel(level) self.file_handler.setLevel(level)
self.root_logger.setLevel(level)
self.root_logger.addHandler(self.file_handler)
self.root_logger.addHandler(self.console_handler)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment