2 from __future__
import absolute_import
4 from abc
import ABCMeta, abstractmethod
7 import paho.mqtt.client
as mqtt
10 from .util
import lookup_object, extract_values, populate_instance
14 u""" bridge generator function 16 :param (str|class) factory: Bridge class 17 :param (str|class) msg_type: ROS message type 18 :param str topic_from: incoming topic path 19 :param str topic_to: outgoing topic path 20 :param (float|None) frequency: publish frequency 21 :return Bridge: bridge object 23 if isinstance(factory, basestring):
25 if not issubclass(factory, Bridge):
26 raise ValueError(
"factory should be Bridge subclass")
27 if isinstance(msg_type, basestring):
29 if not issubclass(msg_type, rospy.Message):
31 "msg_type should be rospy.Message instance or its string" 34 topic_from=topic_from, topic_to=topic_to, msg_type=msg_type, **kwargs)
38 u""" Bridge base class 40 :param mqtt.Client _mqtt_client: MQTT client 41 :param _serialize: message serialize callable 42 :param _deserialize: message deserialize callable 44 __metaclass__ = ABCMeta
46 _mqtt_client = inject.attr(mqtt.Client)
47 _serialize = inject.attr(
'serializer')
48 _deserialize = inject.attr(
'deserializer')
49 _extract_private_path = inject.attr(
'mqtt_private_path_extractor')
53 u""" Bridge from ROS topic to MQTT 55 :param str topic_from: incoming ROS topic path 56 :param str topic_to: outgoing MQTT topic path 57 :param class msg_type: subclass of ROS Message 58 :param (float|None) frequency: publish frequency 61 def __init__(self, topic_from, topic_to, msg_type, frequency=None):
65 self.
_interval = 0
if frequency
is None else 1.0 / frequency
69 rospy.logdebug(
"ROS received from {}".format(self.
_topic_from))
70 now = rospy.get_time()
76 payload = bytearray(self.
_serialize(extract_values(msg)))
77 self._mqtt_client.publish(topic=self.
_topic_to, payload=payload)
81 u""" Bridge from MQTT to ROS topic 83 :param str topic_from: incoming MQTT topic path 84 :param str topic_to: outgoing ROS topic path 85 :param class msg_type: subclass of ROS Message 86 :param (float|None) frequency: publish frequency 87 :param int queue_size: ROS publisher's queue size 90 def __init__(self, topic_from, topic_to, msg_type, frequency=None,
97 self.
_interval =
None if frequency
is None else 1.0 / frequency
99 self._mqtt_client.subscribe(topic_from)
100 self._mqtt_client.message_callback_add(topic_from, self.
_callback_mqtt)
105 u""" callback from MQTT 107 :param mqtt.Client client: MQTT client used in connection 108 :param userdata: user defined data 109 :param mqtt.MQTTMessage mqtt_msg: MQTT message 111 rospy.logdebug(
"MQTT received from {}".format(mqtt_msg.topic))
112 now = rospy.get_time()
117 self._publisher.publish(ros_msg)
119 except Exception
as e:
123 u""" create ROS message from MQTT payload 125 :param mqtt.Message mqtt_msg: MQTT Message 126 :return rospy.Message: ROS Message 129 return populate_instance(msg_dict, self.
_msg_type())
132 __all__ = [
'register_bridge_factory',
'create_bridge',
'Bridge',
133 'RosToMqttBridge',
'MqttToRosBridge']
def _callback_mqtt(self, client, userdata, mqtt_msg)
def lookup_object(object_path, package='mqtt_bridge')
def __init__(self, topic_from, topic_to, msg_type, frequency=None, queue_size=10)
def create_bridge(factory, msg_type, topic_from, topic_to, kwargs)
def __init__(self, topic_from, topic_to, msg_type, frequency=None)
def _create_ros_message(self, mqtt_msg)
def _callback_ros(self, msg)