2018-06-12 10:33:30 +02:00
|
|
|
import json
|
2018-11-02 16:32:12 +01:00
|
|
|
import os
|
2018-05-27 12:21:37 +02:00
|
|
|
import paho.mqtt.publish as publisher
|
|
|
|
|
2018-06-12 10:33:30 +02:00
|
|
|
from platypush.message import Message
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-05-27 12:21:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MqttPlugin(Plugin):
|
2018-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
This plugin allows you to send custom message to a message queue compatible
|
|
|
|
with the MQTT protocol, see http://mqtt.org/
|
|
|
|
"""
|
|
|
|
|
2018-11-02 16:32:12 +01:00
|
|
|
def __init__(self, host=None, port=1883, tls_cafile=None,
|
|
|
|
tls_certfile=None, tls_keyfile=None,
|
|
|
|
tls_version=None, tls_ciphers=None, username=None,
|
|
|
|
password=None, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
:param host: If set, MQTT messages will by default routed to this host unless overridden in `send_message` (default: None)
|
|
|
|
:type host: str
|
|
|
|
|
|
|
|
:param port: If a default host is set, specify the listen port (default: 1883)
|
|
|
|
:type port: int
|
|
|
|
|
|
|
|
:param tls_cafile: If a default host is set and requires TLS/SSL, specify the certificate authority file (default: None)
|
|
|
|
:type tls_cafile: str
|
|
|
|
|
|
|
|
:param tls_certfile: If a default host is set and requires TLS/SSL, specify the certificate file (default: None)
|
|
|
|
:type tls_certfile: str
|
|
|
|
|
|
|
|
:param tls_keyfile: If a default host is set and requires TLS/SSL, specify the key file (default: None)
|
|
|
|
:type tls_keyfile: str
|
|
|
|
|
|
|
|
:param tls_version: If a default host is set and requires TLS/SSL, specify the minimum TLS supported version (default: None)
|
|
|
|
:type tls_version: str
|
|
|
|
|
|
|
|
:param tls_ciphers: If a default host is set and requires TLS/SSL, specify the supported ciphers (default: None)
|
|
|
|
:type tls_ciphers: str
|
|
|
|
|
|
|
|
:param username: If a default host is set and requires user authentication, specify the username ciphers (default: None)
|
|
|
|
:type username: str
|
|
|
|
|
|
|
|
:param password: If a default host is set and requires user authentication, specify the password ciphers (default: None)
|
|
|
|
:type password: str
|
|
|
|
"""
|
|
|
|
|
2018-07-06 02:26:54 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-11-02 16:32:12 +01:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
self.tls_cafile = os.path.abspath(os.path.expanduser(tls_cafile)) \
|
|
|
|
if tls_cafile else None
|
|
|
|
|
|
|
|
self.tls_certfile = os.path.abspath(os.path.expanduser(tls_certfile)) \
|
|
|
|
if tls_certfile else None
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-11-02 16:32:12 +01:00
|
|
|
def send_message(self, topic, msg, host=None, port=1883, tls_cafile=None,
|
2018-11-02 16:15:48 +01:00
|
|
|
tls_certfile=None, tls_keyfile=None,
|
|
|
|
tls_version=None, tls_ciphers=None, username=None,
|
|
|
|
password=None, *args, **kwargs):
|
2018-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
Sends a message to a topic/channel.
|
|
|
|
|
|
|
|
:param topic: Topic/channel where the message will be delivered
|
|
|
|
:type topic: str
|
|
|
|
|
|
|
|
:param msg: Message to be sent. It can be a list, a dict, or a Message object
|
|
|
|
|
|
|
|
:param host: MQTT broker hostname/IP
|
|
|
|
:type host: str
|
|
|
|
|
|
|
|
:param port: MQTT broker port (default: 1883)
|
|
|
|
:type port: int
|
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-06-25 00:49:45 +02:00
|
|
|
"""
|
|
|
|
|
2018-11-02 16:32:12 +01:00
|
|
|
if not host and not self.host:
|
|
|
|
raise RuntimeError('No host specified and no default host configured')
|
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
publisher_args = {
|
2018-11-02 16:32:12 +01:00
|
|
|
'hostname': host or self.host,
|
|
|
|
'port': port or self.port,
|
2018-11-02 16:15:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 16:32:12 +01:00
|
|
|
if host:
|
|
|
|
if username and password:
|
|
|
|
publisher_args['auth'] = {
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
if self.username and self.password:
|
|
|
|
publisher_args['auth'] = {
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
}
|
|
|
|
|
|
|
|
if host:
|
|
|
|
if tls_cafile:
|
|
|
|
publisher_args['tls'] = { 'ca_certs': tls_cafile }
|
|
|
|
if tls_certfile:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['certfile'] = tls_certfile
|
2018-11-02 16:32:12 +01:00
|
|
|
if tls_keyfile:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['keyfile'] = tls_keyfile
|
2018-11-02 16:32:12 +01:00
|
|
|
if tls_version:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['tls_version'] = tls_version
|
2018-11-02 16:32:12 +01:00
|
|
|
if tls_ciphers:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['ciphers'] = tls_ciphers
|
2018-11-02 16:32:12 +01:00
|
|
|
else:
|
|
|
|
if self.tls_cafile:
|
|
|
|
publisher_args['tls'] = { 'ca_certs': self.tls_cafile }
|
|
|
|
if self.tls_certfile:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['certfile'] = self.tls_certfile
|
2018-11-02 16:32:12 +01:00
|
|
|
if self.tls_keyfile:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['keyfile'] = self.tls_keyfile
|
2018-11-02 16:32:12 +01:00
|
|
|
if self.tls_version:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['tls_version'] = self.tls_version
|
2018-11-02 16:32:12 +01:00
|
|
|
if self.tls_ciphers:
|
2019-03-18 00:16:54 +01:00
|
|
|
publisher_args['tls']['ciphers'] = self.tls_ciphers
|
2018-11-02 16:15:48 +01:00
|
|
|
|
2018-06-12 10:33:30 +02:00
|
|
|
try: msg = json.dumps(msg)
|
|
|
|
except: pass
|
|
|
|
|
|
|
|
try: msg = Message.build(json.loads(msg))
|
|
|
|
except: pass
|
|
|
|
|
2018-11-02 16:15:48 +01:00
|
|
|
publisher.single(topic, str(msg), **publisher_args)
|
2018-05-27 12:21:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|