2018-06-12 10:33:30 +02:00
|
|
|
import json
|
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-07-06 02:26:54 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-11-02 16:15:48 +01:00
|
|
|
def send_message(self, topic, msg, host, port=1883, tls_cafile=None,
|
|
|
|
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:15:48 +01:00
|
|
|
publisher_args = {
|
|
|
|
'hostname': host,
|
|
|
|
'port': port,
|
|
|
|
}
|
|
|
|
|
|
|
|
if username and password:
|
|
|
|
publisher_args['auth'] = {
|
|
|
|
'username': username,
|
|
|
|
'password': password,
|
|
|
|
}
|
|
|
|
|
|
|
|
if tls_cafile:
|
|
|
|
publisher_args['tls'] = { 'ca_certs': tls_cafile }
|
|
|
|
if tls_certfile:
|
|
|
|
publishers_args['tls']['certfile'] = tls_certfile
|
|
|
|
if tls_keyfile:
|
|
|
|
publishers_args['tls']['keyfile'] = tls_keyfile
|
|
|
|
if tls_version:
|
|
|
|
publishers_args['tls']['tls_version'] = tls_version
|
|
|
|
if tls_ciphers:
|
|
|
|
publishers_args['tls']['ciphers'] = tls_ciphers
|
|
|
|
|
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:
|
|
|
|
|