Skip to content

Commit

Permalink
refactor: common configuration for test clients
Browse files Browse the repository at this point in the history
  • Loading branch information
GoetzGoerisch committed Sep 26, 2024
1 parent 6fbdd9e commit 0f6d7af
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 86 deletions.
24 changes: 15 additions & 9 deletions TestEnvironment/readme.md → TestEnvironment/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@ In addition, we provide local Python MQTT clients to test publishing and subscri

## UA Cloud Publisher

- Frontend: <http://localhost>
- Connect to Simulated OPC Server: opc.tcp://plc-sim:50000/
- Frontend: `http://localhost`
- Connect to Simulated OPC Server: `opc.tcp://plc-sim:50000/`
- Configuration (in UI):
- Broker URL: hivemq4
- Broker Port: 1883
- Message Topic: data
- uncheck SAS
- uncheck TLS
- Broker URL: `hivemq4`
- Broker Port: `1883`
- Message Topic: `data`
- uncheck `SAS`
- uncheck `TLS`

## HiveMQ

- Control center: <http://localhost:8888>
- Credentials: admin / hivemq
- Control center: `http://localhost:8888`
- Credentials: `admin` / `hivemq`

## Testing MQTT Broker with Python Clients

# one publisher
python.exe .\pub_client_1.py

# one subscriber
python.exe .\sub_client_1.py

# two subscribers
python.exe .\sub_client_1_2.py

## Tips

- Error "Failed to connect to MQTT broker: Topic should not be empty." &rarr; Please Connect UA Cloud Publisher and OPC-UA Server.
10 changes: 10 additions & 0 deletions TestEnvironment/config.py
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
7 changes: 2 additions & 5 deletions TestEnvironment/pub_client_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import random
import time

import config # Import the configuration settings
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."""
Expand All @@ -21,7 +18,7 @@ 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)
client.connect(host=config.BROKER, port=config.PORT)

for i in range(20):
delay = random.randint(1, 5)
Expand Down
66 changes: 30 additions & 36 deletions TestEnvironment/sub_client_1.py
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()
51 changes: 51 additions & 0 deletions TestEnvironment/sub_client_1_2.py
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()
36 changes: 0 additions & 36 deletions TestEnvironment/sub_client_2.py

This file was deleted.

0 comments on commit 0f6d7af

Please sign in to comment.