如何使用 linux shell 命令订阅多个主题(至少 2 个)并使用 Mosquitto MQTT 将收到的消息重新发布到单个主题

How to subscribe to multiple topics (at least 2) and republish the message recieved to a single topic with Mosquitto MQTT using linux shell comands

提问人:Samuel Mikhael 提问时间:9/27/2023 最后编辑:Samuel Mikhael 更新时间:9/28/2023 访问量:119

问:

我试图找出一种订阅多个主题的方法,然后在收到消息后,该函数应根据主题将其发布到不同的代理。

我想实现一个这样的函数: 当发布者发布消息时,发布的主题的值将存储在名为“topic”的变量中,消息将存储在名为“message”的变量中。这是我的代码:

#!/bin/bash
SOURCE_BROKER="192.168.1.37"
DEST_BROKER="192.168.1.25"
while true
do
#Subscribe to both topics ("0_0" and "0_1")
  mosquitto_sub -h $SOURCE_BROKER -t "0_0" -t "0_1" -u pi -P Pass123 | while read -r topic message
  do
    # This is a callback to be executed every time a client subscribes to a topic
    echo "Client subscribed to ${topic}"
    # Check the topic and redirect the subscription
    if [ "$topic" == "0_0" ]; then
      # Publish the message to the broker (SOURCE_BROKER)
      mosquitto_pub -h $SOURCE_BROKER -t "$topic" -m "$message" -u pi -P Pass123
    elif [ "$topic" == "0_1" ]; then
      # Publish the message to the new broker (DEST_BROKER)
      mosquitto_pub -h $DEST_BROKER -t "$topic" -m "$message" -u pi -P Pass123
    fi
  done
  sleep 10 # Wait 10 seconds until reconnection
done

然后我发现它不起作用,因为mosquitto_sub同时订阅多个主题时不会返回主题名称。有没有其他方法可以实现这个功能?

bash shell mqtt mosquitto messagebroker

评论

0赞 hardillb 9/28/2023
添加到行中,它将返回 (也永远不会返回没有-vmosquitto_subtopic messagemosquitto_sub-v)

答:

4赞 Freeman 9/27/2023 #1

如您所见,我使用 separate 分别订阅每个主题,然后将收到的消息传递给函数,该函数根据主题处理逻辑,我还使用运算符在后台运行第一个,以便两个订阅可以同时处于活动状态!mosquitto_subhandle_message&mosquitto_sub

#!/bin/bash

SOURCE_BROKER="192.168.1.37"
DEST_BROKER="192.168.1.25"

# Function to handle the received message
handle_message() {
    topic="$1"
    message="$2"

    # This is a callback to be executed every time a client subscribes to a topic
    echo "Client subscribed to ${topic}"

    # Check the topic and redirect the subscription
    if [ "$topic" == "0_0" ]; then
        # Publish the message to the broker (SOURCE_BROKER)
        mosquitto_pub -h "$SOURCE_BROKER" -t "$topic" -m "$message" -u pi -P Pass123
    elif [ "$topic" == "0_1" ]; then
        # Publish the message to the new broker (DEST_BROKER)
        mosquitto_pub -h "$DEST_BROKER" -t "$topic" -m "$message" -u pi -P Pass123
    fi

    #exit the loop after forwarding the message
    exit
}

# Subscribe to both topics ("0_0" and "0_1")
mosquitto_sub -h "$SOURCE_BROKER" -t "0_0" -u pi -P Pass123 | while read -r message
do
    handle_message "0_0" "$message"
done &

mosquitto_sub -h "$SOURCE_BROKER" -t "0_1" -u pi -P Pass123 | while read -r message
do
    handle_message "0_1" "$message"
done

评论

0赞 shellter 9/28/2023
有趣。消息订阅在 shell 环境中有哪些用例?您是否看到终端中显示消息?此外,我半信半疑地希望看到一个允许 n 个潜艇的 or 循环解决方案。这不可能吗?感谢您的任何反馈,祝您好运whilefor
0赞 Freeman 9/28/2023
@shellter 感谢您的评论!在 shell 环境中,订阅消息可用于各种任务,例如监视、日志记录或基于特定事件触发操作,消息在接收时确实会出现在终端中。至于你关于允许多个订阅的问题,你是对的,可以使用循环结构来实现这一点。在这个特定的脚本中,我展示了一个包含两个订阅的示例,但您可以轻松地扩展它以使用循环处理“n”个订阅。
0赞 Samuel Mikhael 9/28/2023
@shellter 您的代码正在将消息转发到两条消息的正确代理。但问题是,一旦它收到 2 个主题中任何一个的公开消息,它将继续以无限循环的方式转发它
0赞 Freeman 9/28/2023
@SamuelMikhael您可以添加一个条件,以便在消息转发后退出循环,我将更新我的答案。
1赞 Samuel Mikhael 9/28/2023
@Freeman您的解决方案运行良好,谢谢。