-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: common configuration for test clients
- Loading branch information
1 parent
6fbdd9e
commit 0f6d7af
Showing
6 changed files
with
108 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
Configuration settings for MQTT clients. | ||
""" | ||
|
||
# Broker settings | ||
BROKER = "localhost" # MQTT broker URL | ||
PORT = 1883 | ||
|
||
# Time for Subscriber to live | ||
TIMELIVE = 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,30 @@ | ||
""" | ||
MQTT Subscriber 1 for receiving messages. | ||
""" | ||
|
||
import paho.mqtt.client as mqtt | ||
|
||
# Broker settings | ||
BROKER = "localhost" # MQTT broker URL | ||
PORT = 1883 | ||
|
||
# Time for Subscriber to live | ||
TIMELIVE = 60 | ||
|
||
|
||
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): # pylint: disable=unused-argument | ||
"""Callback function for when a message is received.""" | ||
print(msg.payload.decode()) | ||
|
||
|
||
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() | ||
""" | ||
MQTT Subscriber 1 for receiving messages. | ||
""" | ||
|
||
import config # Import the configuration settings | ||
import paho.mqtt.client as mqtt | ||
|
||
|
||
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): # pylint: disable=unused-argument | ||
"""Callback function for when a message is received.""" | ||
print(msg.payload.decode()) | ||
|
||
|
||
def main(): | ||
"""Main function to set up the MQTT client and start the loop.""" | ||
sub_client = mqtt.Client() | ||
sub_client.connect(host=config.BROKER, port=config.PORT, keepalive=config.TIMELIVE) | ||
sub_client.on_connect = on_connect | ||
sub_client.on_message = on_message | ||
sub_client.loop_forever() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
MQTT Subscribers for receiving messages. | ||
""" | ||
|
||
import config | ||
import paho.mqtt.client as mqtt | ||
|
||
|
||
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_subscriber1(client, userdata, msg): # pylint: disable=unused-argument | ||
"""Callback function for when a message is received by subscriber 1.""" | ||
print(f"Subscriber 1 received: {msg.payload.decode()}") | ||
|
||
|
||
def on_message_subscriber2(client, userdata, msg): # pylint: disable=unused-argument | ||
"""Callback function for when a message is received by subscriber 2.""" | ||
print(f"Subscriber 2 received: {msg.payload.decode()}") | ||
|
||
|
||
def setup_subscriber(on_message_callback): | ||
"""Sets up the MQTT client and starts the loop with the given message callback.""" | ||
sub_client = mqtt.Client() | ||
sub_client.connect(host=config.BROKER, port=config.PORT, keepalive=config.TIMELIVE) | ||
sub_client.on_connect = on_connect | ||
sub_client.on_message = on_message_callback | ||
sub_client.loop_start() | ||
return sub_client | ||
|
||
|
||
def main(): | ||
"""Main function to set up both subscribers.""" | ||
subscriber1 = setup_subscriber(on_message_subscriber1) | ||
subscriber2 = setup_subscriber(on_message_subscriber2) | ||
|
||
# Keep the script running | ||
try: | ||
while True: | ||
pass | ||
except KeyboardInterrupt: | ||
subscriber1.loop_stop() | ||
subscriber2.loop_stop() | ||
print("Stopped...") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.