Skip to content

Commit 1777310

Browse files
committed
refactor: linting to follow Pylint recommendations
1 parent df8a7da commit 1777310

File tree

3 files changed

+80
-50
lines changed

3 files changed

+80
-50
lines changed

TestEnvironment/pub_client_1.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
# simulator device 1 for mqtt message publishing
2-
import random
3-
import time
4-
5-
import paho.mqtt.client as paho
6-
7-
# brokersettings
8-
BROKER = "localhost" # mqtt broker url + port exposed to local
9-
PORT = 1883
10-
11-
12-
def on_publish(client, userdata, result):
13-
print("Device 1 : Data published.")
14-
pass
15-
16-
17-
client = paho.Client(client_id="admin")
18-
client.on_publish = on_publish
19-
client.connect(host=BROKER, port=PORT)
20-
for i in range(20):
21-
d = random.randint(1, 5)
22-
23-
# telemetry to send
24-
MESSAGE = "Device 1 : Data " + str(i)
25-
time.sleep(d)
26-
27-
# publish message
28-
ret = client.publish(topic="data", payload=MESSAGE)
29-
30-
print("Stopped...")
1+
"""
2+
Simulator device 1 for MQTT message publishing.
3+
"""
4+
5+
import random
6+
import time
7+
8+
import paho.mqtt.client as paho
9+
10+
# Broker settings
11+
BROKER = "localhost" # MQTT broker URL
12+
PORT = 1883
13+
14+
15+
def on_publish(client, userdata, result): # pylint: disable=unused-argument
16+
"""Callback function for when a message is published."""
17+
print("Device 1: Data published.")
18+
19+
20+
def main():
21+
"""Main function to publish MQTT messages."""
22+
client = paho.Client(client_id="admin")
23+
client.on_publish = on_publish
24+
client.connect(host=BROKER, port=PORT)
25+
26+
for i in range(20):
27+
delay = random.randint(1, 5)
28+
message = f"Device 1: Data {i}"
29+
time.sleep(delay)
30+
client.publish(topic="data", payload=message)
31+
32+
print("Stopped...")
33+
34+
35+
if __name__ == "__main__":
36+
main()

TestEnvironment/sub_client_1.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1+
"""
2+
MQTT Subscriber 1 for receiving messages.
3+
"""
4+
15
import paho.mqtt.client as mqtt
26

3-
# broker settings
4-
BROKER = "localhost" # mqtt broker url + port exposed to local
7+
# Broker settings
8+
BROKER = "localhost" # MQTT broker URL
59
PORT = 1883
610

7-
# time for Subscriber to live
11+
# Time for Subscriber to live
812
TIMELIVE = 60
913

1014

11-
def on_connect(client, userdata, flags, rc):
15+
def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument
16+
"""Callback function for when the client connects to the broker."""
1217
print("Connected with result code " + str(rc))
1318
client.subscribe(topic="data")
1419

1520

16-
def on_message(client, userdata, msg):
21+
def on_message(client, userdata, msg): # pylint: disable=unused-argument
22+
"""Callback function for when a message is received."""
1723
print(msg.payload.decode())
1824

1925

20-
sub_client = mqtt.Client()
21-
sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE)
22-
sub_client.on_connect = on_connect
23-
sub_client.on_message = on_message
24-
sub_client.loop_forever()
26+
def main():
27+
"""Main function to set up the MQTT client and start the loop."""
28+
sub_client = mqtt.Client()
29+
sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE)
30+
sub_client.on_connect = on_connect
31+
sub_client.on_message = on_message
32+
sub_client.loop_forever()
33+
34+
35+
if __name__ == "__main__":
36+
main()

TestEnvironment/sub_client_2.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1+
"""
2+
MQTT Subscriber 2 for receiving messages.
3+
"""
4+
15
import paho.mqtt.client as mqtt
26

3-
# broker settings
4-
BROKER = "localhost" # mqtt broker url + port exposed to local
7+
# Broker settings
8+
BROKER = "localhost" # MQTT broker URL
59
PORT = 1883
610

7-
# time for Subscriber to live
11+
# Time for Subscriber to live
812
TIMELIVE = 60
913

1014

11-
def on_connect(client, userdata, flags, rc):
15+
def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument
16+
"""Callback function for when the client connects to the broker."""
1217
print("Connected with result code " + str(rc))
1318
client.subscribe(topic="data")
1419

1520

16-
def on_message(client, userdata, msg):
21+
def on_message(client, userdata, msg): # pylint: disable=unused-argument
22+
"""Callback function for when a message is received."""
1723
print(msg.payload.decode())
1824

1925

20-
sub_client = mqtt.Client()
21-
sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE)
22-
sub_client.on_connect = on_connect
23-
sub_client.on_message = on_message
24-
sub_client.loop_forever()
26+
def main():
27+
"""Main function to set up the MQTT client and start the loop."""
28+
sub_client = mqtt.Client()
29+
sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE)
30+
sub_client.on_connect = on_connect
31+
sub_client.on_message = on_message
32+
sub_client.loop_forever()
33+
34+
35+
if __name__ == "__main__":
36+
main()

0 commit comments

Comments
 (0)