From 17773102b1151cf5a25cb74bb0b3d6fdfde2ea2a Mon Sep 17 00:00:00 2001 From: Goetz Goerisch Date: Thu, 26 Sep 2024 10:11:35 +0200 Subject: [PATCH] refactor: linting to follow Pylint recommendations --- TestEnvironment/pub_client_1.py | 66 ++++++++++++++++++--------------- TestEnvironment/sub_client_1.py | 32 +++++++++++----- TestEnvironment/sub_client_2.py | 32 +++++++++++----- 3 files changed, 80 insertions(+), 50 deletions(-) diff --git a/TestEnvironment/pub_client_1.py b/TestEnvironment/pub_client_1.py index cca8dd2..51e70a2 100644 --- a/TestEnvironment/pub_client_1.py +++ b/TestEnvironment/pub_client_1.py @@ -1,30 +1,36 @@ -# simulator device 1 for mqtt message publishing -import random -import time - -import paho.mqtt.client as paho - -# brokersettings -BROKER = "localhost" # mqtt broker url + port exposed to local -PORT = 1883 - - -def on_publish(client, userdata, result): - print("Device 1 : Data published.") - pass - - -client = paho.Client(client_id="admin") -client.on_publish = on_publish -client.connect(host=BROKER, port=PORT) -for i in range(20): - d = random.randint(1, 5) - - # telemetry to send - MESSAGE = "Device 1 : Data " + str(i) - time.sleep(d) - - # publish message - ret = client.publish(topic="data", payload=MESSAGE) - -print("Stopped...") +""" +Simulator device 1 for MQTT message publishing. +""" + +import random +import time + +import paho.mqtt.client as paho + +# Broker settings +BROKER = "localhost" # MQTT broker URL +PORT = 1883 + + +def on_publish(client, userdata, result): # pylint: disable=unused-argument + """Callback function for when a message is published.""" + print("Device 1: Data published.") + + +def main(): + """Main function to publish MQTT messages.""" + client = paho.Client(client_id="admin") + client.on_publish = on_publish + client.connect(host=BROKER, port=PORT) + + for i in range(20): + delay = random.randint(1, 5) + message = f"Device 1: Data {i}" + time.sleep(delay) + client.publish(topic="data", payload=message) + + print("Stopped...") + + +if __name__ == "__main__": + main() diff --git a/TestEnvironment/sub_client_1.py b/TestEnvironment/sub_client_1.py index 5f4ab5d..4b31eda 100644 --- a/TestEnvironment/sub_client_1.py +++ b/TestEnvironment/sub_client_1.py @@ -1,24 +1,36 @@ +""" +MQTT Subscriber 1 for receiving messages. +""" + import paho.mqtt.client as mqtt -# broker settings -BROKER = "localhost" # mqtt broker url + port exposed to local +# Broker settings +BROKER = "localhost" # MQTT broker URL PORT = 1883 -# time for Subscriber to live +# Time for Subscriber to live TIMELIVE = 60 -def on_connect(client, userdata, flags, rc): +def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument + """Callback function for when the client connects to the broker.""" print("Connected with result code " + str(rc)) client.subscribe(topic="data") -def on_message(client, userdata, msg): +def on_message(client, userdata, msg): # pylint: disable=unused-argument + """Callback function for when a message is received.""" print(msg.payload.decode()) -sub_client = mqtt.Client() -sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) -sub_client.on_connect = on_connect -sub_client.on_message = on_message -sub_client.loop_forever() +def main(): + """Main function to set up the MQTT client and start the loop.""" + sub_client = mqtt.Client() + sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) + sub_client.on_connect = on_connect + sub_client.on_message = on_message + sub_client.loop_forever() + + +if __name__ == "__main__": + main() diff --git a/TestEnvironment/sub_client_2.py b/TestEnvironment/sub_client_2.py index 5f4ab5d..b1c0247 100644 --- a/TestEnvironment/sub_client_2.py +++ b/TestEnvironment/sub_client_2.py @@ -1,24 +1,36 @@ +""" +MQTT Subscriber 2 for receiving messages. +""" + import paho.mqtt.client as mqtt -# broker settings -BROKER = "localhost" # mqtt broker url + port exposed to local +# Broker settings +BROKER = "localhost" # MQTT broker URL PORT = 1883 -# time for Subscriber to live +# Time for Subscriber to live TIMELIVE = 60 -def on_connect(client, userdata, flags, rc): +def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument + """Callback function for when the client connects to the broker.""" print("Connected with result code " + str(rc)) client.subscribe(topic="data") -def on_message(client, userdata, msg): +def on_message(client, userdata, msg): # pylint: disable=unused-argument + """Callback function for when a message is received.""" print(msg.payload.decode()) -sub_client = mqtt.Client() -sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) -sub_client.on_connect = on_connect -sub_client.on_message = on_message -sub_client.loop_forever() +def main(): + """Main function to set up the MQTT client and start the loop.""" + sub_client = mqtt.Client() + sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) + sub_client.on_connect = on_connect + sub_client.on_message = on_message + sub_client.loop_forever() + + +if __name__ == "__main__": + main()