forked from platypush/platypush
Handle MQTT listeners by host with lists of topics, so we only need one listener thread per host
This commit is contained in:
parent
84099b2ab7
commit
022262eb78
1 changed files with 8 additions and 7 deletions
|
@ -116,9 +116,10 @@ class MqttBackend(Backend):
|
||||||
client.connect(host, port, 60)
|
client.connect(host, port, 60)
|
||||||
client.loop_forever()
|
client.loop_forever()
|
||||||
|
|
||||||
def on_connect(topic):
|
def on_connect(topics):
|
||||||
def handler(client, userdata, flags, rc):
|
def handler(client, userdata, flags, rc):
|
||||||
client.subscribe(topic)
|
for topic in topics:
|
||||||
|
client.subscribe(topic)
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
|
@ -135,18 +136,18 @@ class MqttBackend(Backend):
|
||||||
for i, listener in enumerate(listeners_conf):
|
for i, listener in enumerate(listeners_conf):
|
||||||
host = listener.get('host')
|
host = listener.get('host')
|
||||||
port = listener.get('port', self._default_mqtt_port)
|
port = listener.get('port', self._default_mqtt_port)
|
||||||
topic = listener.get('topic')
|
topics = listener.get('topics')
|
||||||
username = listener.get('username')
|
username = listener.get('username')
|
||||||
password = listener.get('password')
|
password = listener.get('password')
|
||||||
tls_cafile = listener.get('tls_cafile')
|
tls_cafile = listener.get('tls_cafile')
|
||||||
|
|
||||||
if not host or not topic:
|
if not host or not topics:
|
||||||
self.logger.warning('No host nor topic specified for listener ' +
|
self.logger.warning('No host nor list of topics specified for ' +
|
||||||
'n.{}'.format(i+1))
|
'listener n.{}'.format(i+1))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
client = mqtt.Client()
|
client = mqtt.Client()
|
||||||
client.on_connect = on_connect(topic)
|
client.on_connect = on_connect(topics)
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
|
|
||||||
if username and password:
|
if username and password:
|
||||||
|
|
Loading…
Reference in a new issue