2018-05-27 12:21:37 +02:00
|
|
|
import json
|
2018-11-02 16:15:48 +01:00
|
|
|
import os
|
2018-10-26 15:33:23 +02:00
|
|
|
import threading
|
2018-05-27 12:21:37 +02:00
|
|
|
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import paho.mqtt.publish as publisher
|
|
|
|
|
|
|
|
from platypush.backend import Backend
|
2018-11-02 16:15:48 +01:00
|
|
|
from platypush.context import get_plugin
|
2018-05-27 12:21:37 +02:00
|
|
|
from platypush.message import Message
|
2018-10-26 15:33:23 +02:00
|
|
|
from platypush.message.request import Request
|
2018-05-27 12:21:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MqttBackend(Backend):
|
|
|
|
"""
|
2018-06-26 00:16:39 +02:00
|
|
|
Backend that reads messages from a configured MQTT topic (default:
|
|
|
|
``platypush_bus_mq/<device_id>``) and posts them to the application bus.
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **paho-mqtt** (``pip install paho-mqtt``)
|
2018-05-27 12:21:37 +02:00
|
|
|
"""
|
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
def __init__(self, host, port=1883, topic='platypush_bus_mq', tls_cafile=None,
|
|
|
|
tls_certfile=None, tls_keyfile=None,
|
|
|
|
tls_version=None, tls_ciphers=None, username=None,
|
|
|
|
password=None, *args, **kwargs):
|
2018-05-27 12:21:37 +02:00
|
|
|
"""
|
2018-06-26 00:16:39 +02:00
|
|
|
:param host: MQTT broker host
|
|
|
|
:type host: str
|
|
|
|
|
|
|
|
:param port: MQTT broker port (default: 1883)
|
|
|
|
:type port: int
|
|
|
|
|
|
|
|
:param topic: Topic to read messages from (default: ``platypush_bus_mq/<device_id>``)
|
|
|
|
:type topic: str
|
2018-11-02 16:15:48 +01:00
|
|
|
|
|
|
|
:param tls_cafile: If TLS/SSL is enabled on the MQTT server and the certificate requires a certificate authority to authenticate it, `ssl_cafile` will point to the provided ca.crt file (default: None)
|
|
|
|
:type tls_cafile: str
|
|
|
|
|
|
|
|
:param tls_certfile: If TLS/SSL is enabled on the MQTT server and a client certificate it required, specify it here (default: None)
|
|
|
|
:type tls_certfile: str
|
|
|
|
|
|
|
|
:param tls_keyfile: If TLS/SSL is enabled on the MQTT server and a client certificate key it required, specify it here (default: None)
|
|
|
|
:type tls_keyfile: str
|
|
|
|
|
|
|
|
:param tls_version: If TLS/SSL is enabled on the MQTT server and it requires a certain TLS version, specify it here (default: None)
|
|
|
|
:type tls_version: str
|
|
|
|
|
|
|
|
:param tls_ciphers: If TLS/SSL is enabled on the MQTT server and an explicit list of supported ciphers is required, specify it here (default: None)
|
|
|
|
:type tls_ciphers: str
|
|
|
|
|
|
|
|
:param username: Specify it if the MQTT server requires authentication (default: None)
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:param password: Specify it if the MQTT server requires authentication (default: None)
|
|
|
|
:type password: str
|
2018-05-27 12:21:37 +02:00
|
|
|
"""
|
2018-06-26 00:16:39 +02:00
|
|
|
|
2018-05-27 12:21:37 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.topic = '{}/{}'.format(topic, self.device_id)
|
2018-11-02 16:15:48 +01:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2018-05-27 12:21:37 +02:00
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
self.tls_cafile = os.path.abspath(os.path.expanduser(tls_cafile)) \
|
|
|
|
if tls_cafile else None
|
2018-05-27 12:21:37 +02:00
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
self.tls_certfile = os.path.abspath(os.path.expanduser(tls_certfile)) \
|
|
|
|
if tls_certfile else None
|
2018-06-12 09:28:10 +02:00
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
self.tls_keyfile = os.path.abspath(os.path.expanduser(tls_keyfile)) \
|
|
|
|
if tls_keyfile else None
|
|
|
|
|
|
|
|
self.tls_version = tls_version
|
|
|
|
self.tls_ciphers = tls_ciphers
|
|
|
|
|
|
|
|
|
|
|
|
def send_message(self, msg):
|
2018-11-06 11:40:01 +01:00
|
|
|
try:
|
|
|
|
client = get_plugin('mqtt')
|
|
|
|
client.send_message(topic=self.topic, msg=msg, host=self.host,
|
|
|
|
port=self.port, username=self.username,
|
|
|
|
password=self.password, tls_cafile=self.tls_cafile,
|
|
|
|
tls_certfile=self.tls_certfile,
|
|
|
|
tls_keyfile=self.tls_keyfile,
|
|
|
|
tls_version=self.tls_version,
|
|
|
|
tls_ciphers=self.tls_ciphers)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.exception(e)
|
2018-05-27 12:21:37 +02:00
|
|
|
|
2018-10-25 20:45:58 +02:00
|
|
|
def run(self):
|
2018-05-27 12:21:37 +02:00
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
|
client.subscribe(self.topic)
|
|
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
2018-10-26 15:33:23 +02:00
|
|
|
def response_thread(msg):
|
|
|
|
response = self.get_message_response(msg)
|
2019-01-02 09:29:27 +01:00
|
|
|
if not response:
|
|
|
|
return
|
2018-10-26 15:33:23 +02:00
|
|
|
response_topic = '{}/responses/{}'.format(self.topic, msg.id)
|
|
|
|
|
|
|
|
self.logger.info('Processing response on the MQTT topic {}: {}'.
|
|
|
|
format(response_topic, response))
|
|
|
|
|
2018-11-06 11:40:01 +01:00
|
|
|
self.send_message(response)
|
2018-10-26 15:33:23 +02:00
|
|
|
|
2018-05-27 12:21:37 +02:00
|
|
|
msg = msg.payload.decode('utf-8')
|
2018-06-12 10:33:30 +02:00
|
|
|
try: msg = Message.build(json.loads(msg))
|
|
|
|
except: pass
|
2018-11-10 01:16:32 +01:00
|
|
|
if not msg: return
|
2018-05-27 12:21:37 +02:00
|
|
|
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Received message on the MQTT backend: {}'.format(msg))
|
2018-11-06 11:40:01 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.on_message(msg)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.exception(e)
|
2018-11-10 01:16:32 +01:00
|
|
|
return
|
2018-05-27 12:21:37 +02:00
|
|
|
|
2018-10-26 15:33:23 +02:00
|
|
|
if isinstance(msg, Request):
|
2019-01-10 23:08:29 +01:00
|
|
|
threading.Thread(target=response_thread,
|
|
|
|
name='PlatypushMQTTResponseProcessor',
|
|
|
|
args=(msg,)).start()
|
2018-10-26 15:33:23 +02:00
|
|
|
|
2018-10-25 20:45:58 +02:00
|
|
|
super().run()
|
2018-05-27 12:21:37 +02:00
|
|
|
client = mqtt.Client()
|
|
|
|
client.on_connect = on_connect
|
|
|
|
client.on_message = on_message
|
2018-11-02 16:15:48 +01:00
|
|
|
|
|
|
|
if self.username and self.password:
|
|
|
|
client.username_pw_set(self.username, self.password)
|
|
|
|
|
|
|
|
if self.tls_cafile:
|
|
|
|
client.tls_set(ca_certs=self.tls_cafile, certfile=self.tls_certfile,
|
|
|
|
keyfile=self.tls_keyfile, tls_version=self.tls_version,
|
|
|
|
ciphers=self.tls_ciphers)
|
|
|
|
|
2018-05-27 12:21:37 +02:00
|
|
|
client.connect(self.host, self.port, 60)
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Initialized MQTT backend on host {}:{}, topic {}'.
|
2018-05-27 12:21:37 +02:00
|
|
|
format(self.host, self.port, self.topic))
|
|
|
|
|
|
|
|
client.loop_forever()
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|