app.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import
3 
4 import inject
5 import paho.mqtt.client as mqtt
6 import rospy
7 
8 from .bridge import create_bridge
9 from .mqtt_client import create_private_path_extractor
10 from .util import lookup_object
11 
12 
13 def create_config(mqtt_client, serializer, deserializer, mqtt_private_path):
14  if isinstance(serializer, basestring):
15  serializer = lookup_object(serializer)
16  if isinstance(deserializer, basestring):
17  deserializer = lookup_object(deserializer)
18  private_path_extractor = create_private_path_extractor(mqtt_private_path)
19  def config(binder):
20  binder.bind('serializer', serializer)
21  binder.bind('deserializer', deserializer)
22  binder.bind(mqtt.Client, mqtt_client)
23  binder.bind('mqtt_private_path_extractor', private_path_extractor)
24  return config
25 
26 
28  # init node
29  rospy.init_node('mqtt_bridge_node')
30 
31  # load parameters
32  params = rospy.get_param("~", {})
33  mqtt_params = params.pop("mqtt", {})
34  conn_params = mqtt_params.pop("connection")
35  mqtt_private_path = mqtt_params.pop("private_path", "")
36  bridge_params = params.get("bridge", [])
37 
38  # create mqtt client
39  mqtt_client_factory_name = rospy.get_param(
40  "~mqtt_client_factory", ".mqtt_client:default_mqtt_client_factory")
41  mqtt_client_factory = lookup_object(mqtt_client_factory_name)
42  mqtt_client = mqtt_client_factory(mqtt_params)
43 
44  # load serializer and deserializer
45  serializer = params.get('serializer', 'json:dumps')
46  deserializer = params.get('deserializer', 'json:loads')
47 
48  # dependency injection
49  config = create_config(
50  mqtt_client, serializer, deserializer, mqtt_private_path)
51  inject.configure(config)
52 
53  # configure and connect to MQTT broker
54  mqtt_client.on_connect = _on_connect
55  mqtt_client.on_disconnect = _on_disconnect
56  mqtt_client.connect(**conn_params)
57 
58  # configure bridges
59  bridges = []
60  for bridge_args in bridge_params:
61  bridges.append(create_bridge(**bridge_args))
62 
63  # start MQTT loop
64  mqtt_client.loop_start()
65 
66  # register shutdown callback and spin
67  rospy.on_shutdown(mqtt_client.disconnect)
68  rospy.on_shutdown(mqtt_client.loop_stop)
69  rospy.spin()
70 
71 
72 def _on_connect(client, userdata, flags, response_code):
73  rospy.loginfo('MQTT connected')
74 
75 
76 def _on_disconnect(client, userdata, response_code):
77  rospy.loginfo('MQTT disconnected')
78 
79 
80 __all__ = ['mqtt_bridge_node']
def create_private_path_extractor(mqtt_private_path)
Definition: mqtt_client.py:56
def lookup_object(object_path, package='mqtt_bridge')
Definition: util.py:8
def _on_disconnect(client, userdata, response_code)
Definition: app.py:76
def _on_connect(client, userdata, flags, response_code)
Definition: app.py:72
def create_config(mqtt_client, serializer, deserializer, mqtt_private_path)
Definition: app.py:13
def mqtt_bridge_node()
Definition: app.py:27
def create_bridge(factory, msg_type, topic_from, topic_to, kwargs)
Definition: bridge.py:13


mqtt_bridge
Author(s): Junya Hayashi
autogenerated on Thu Jun 6 2019 19:18:59