2 import paho.mqtt.client
as mqtt
5 from .bridge
import create_bridge
6 from .mqtt_client
import create_private_path_extractor
7 from .util
import lookup_object
10 def create_config(mqtt_client, serializer, deserializer, mqtt_private_path):
11 if isinstance(serializer, str):
13 if isinstance(deserializer, str):
17 binder.bind(
'serializer', serializer)
18 binder.bind(
'deserializer', deserializer)
19 binder.bind(mqtt.Client, mqtt_client)
20 binder.bind(
'mqtt_private_path_extractor', private_path_extractor)
26 rospy.init_node(
'mqtt_bridge_node')
29 params = rospy.get_param(
"~", {})
30 mqtt_params = params.pop(
"mqtt", {})
31 conn_params = mqtt_params.pop(
"connection")
32 mqtt_private_path = mqtt_params.pop(
"private_path",
"")
33 bridge_params = params.get(
"bridge", [])
36 mqtt_client_factory_name = rospy.get_param(
37 "~mqtt_client_factory",
".mqtt_client:default_mqtt_client_factory")
39 mqtt_client = mqtt_client_factory(mqtt_params)
42 serializer = params.get(
'serializer',
'msgpack:dumps')
43 deserializer = params.get(
'deserializer',
'msgpack:loads')
47 mqtt_client, serializer, deserializer, mqtt_private_path)
48 inject.configure(config)
51 mqtt_client.on_connect = _on_connect
52 mqtt_client.on_disconnect = _on_disconnect
53 mqtt_client.connect(**conn_params)
57 for bridge_args
in bridge_params:
61 mqtt_client.loop_start()
64 rospy.on_shutdown(mqtt_client.disconnect)
65 rospy.on_shutdown(mqtt_client.loop_stop)
70 rospy.loginfo(
'MQTT connected')
74 rospy.loginfo(
'MQTT disconnected')
77 __all__ = [
'mqtt_bridge_node']
def create_config(mqtt_client, serializer, deserializer, mqtt_private_path)
def _on_disconnect(client, userdata, response_code)
def _on_connect(client, userdata, flags, response_code)
def create_private_path_extractor