groove-x / mqtt_bridge

mqtt_bridge provides a functionality to bridge between ROS and MQTT in bidirectional
MIT License
159 stars 146 forks source link

private_path extension #24

Closed martinwimpff closed 5 years ago

martinwimpff commented 5 years ago

I want to make a second private_path for the ROS topic path equivalent to the normal one. I made the following changes:

In app.py :

def mqtt_bridge_node():
mqtt_private_path2 = mqtt_params.pop("private_path2", "") 
...
config = create_config(mqtt_client, serializer, deserializer, mqtt_private_path, mqtt_private_path2)
def create_config(mqtt_client, serializer, deserializer, mqtt_private_path, mqtt_private_path2):
    private_path_extractor = create_private_path_extractor(mqtt_private_path, mqtt_private_path2)

In bridge.py:

class RosToMqttBridge(Bridge):
        def __init__(self, topic_from, topic_to, msg_type, frequency=None):
        self._topic_from = self._extract_private_path(topic_from)

In mqtt_client.py:

def create_private_path_extractor(mqtt_private_path, mqtt_private_path2):
    def extractor(topic_path):
        if topic_path.startswith('~/'):
            return '{}/{}'.format(mqtt_private_path, topic_path[2:])
        elif topic_path.startswith('v/'):
            return '{}/{}'.format(mqtt_private_path2, topic_path[2:])
        return topic_path

Unfortunately it doesn't work if I write something like that in my demo_params.yaml file: topic_from: v/something whereas this works: topic_to: v/something

Any ideas what's missing?

martinwimpff commented 5 years ago

Found the problem myself: In the class RosToMqttBridge: rospy.Subscriber(topic_from, msg_type, self._callback_ros) should change to: rospy.Subscriber(self._topic_from, msg_type, self._callback_ros) In the class MqttToRosBridge: self._mqtt_client.subscribe(topic_from) self._mqtt_client.message_callback_add(topic_from, self._callback_mqtt) should change to: self._mqtt_client.subscribe(self._topic_from) self._mqtt_client.message_callback_add(self._topic_from, self._callback_mqtt)