forked from platypush/platypush
Handle backend stop/disconnection on MQTT
This commit is contained in:
parent
1f4efbb427
commit
e1920700ec
1 changed files with 16 additions and 11 deletions
|
@ -2,9 +2,6 @@ import json
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
|
||||||
import paho.mqtt.publish as publisher
|
|
||||||
|
|
||||||
from platypush.backend import Backend
|
from platypush.backend import Backend
|
||||||
from platypush.context import get_plugin
|
from platypush.context import get_plugin
|
||||||
from platypush.message import Message
|
from platypush.message import Message
|
||||||
|
@ -65,6 +62,7 @@ class MqttBackend(Backend):
|
||||||
self.topic = '{}/{}'.format(topic, self.device_id)
|
self.topic = '{}/{}'.format(topic, self.device_id)
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
|
self._client = None
|
||||||
|
|
||||||
self.tls_cafile = os.path.abspath(os.path.expanduser(tls_cafile)) \
|
self.tls_cafile = os.path.abspath(os.path.expanduser(tls_cafile)) \
|
||||||
if tls_cafile else None
|
if tls_cafile else None
|
||||||
|
@ -127,25 +125,32 @@ class MqttBackend(Backend):
|
||||||
name='MQTTProcessor',
|
name='MQTTProcessor',
|
||||||
args=(msg,)).start()
|
args=(msg,)).start()
|
||||||
|
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
super().run()
|
super().run()
|
||||||
client = mqtt.Client()
|
self._client = mqtt.Client()
|
||||||
client.on_connect = on_connect
|
self._client.on_connect = on_connect
|
||||||
client.on_message = on_message
|
self._client.on_message = on_message
|
||||||
|
|
||||||
if self.username and self.password:
|
if self.username and self.password:
|
||||||
client.username_pw_set(self.username, self.password)
|
self._client.username_pw_set(self.username, self.password)
|
||||||
|
|
||||||
if self.tls_cafile:
|
if self.tls_cafile:
|
||||||
client.tls_set(ca_certs=self.tls_cafile, certfile=self.tls_certfile,
|
self._client.tls_set(ca_certs=self.tls_cafile, certfile=self.tls_certfile,
|
||||||
keyfile=self.tls_keyfile, tls_version=self.tls_version,
|
keyfile=self.tls_keyfile, tls_version=self.tls_version,
|
||||||
ciphers=self.tls_ciphers)
|
ciphers=self.tls_ciphers)
|
||||||
|
|
||||||
client.connect(self.host, self.port, 60)
|
self._client.connect(self.host, self.port, 60)
|
||||||
self.logger.info('Initialized MQTT backend on host {}:{}, topic {}'.
|
self.logger.info('Initialized MQTT backend on host {}:{}, topic {}'.
|
||||||
format(self.host, self.port, self.topic))
|
format(self.host, self.port, self.topic))
|
||||||
|
|
||||||
client.loop_forever()
|
self._client.loop_forever()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.logger.info('Received STOP event on MqttBackend')
|
||||||
|
if self._client:
|
||||||
|
self._client.disconnect()
|
||||||
|
self._client.loop_stop()
|
||||||
|
self._client = None
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue