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-05-27 12:21:37 +02:00
|
|
|
from platypush.message.response import Response
|
|
|
|
from platypush.plugins import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
class MqttPlugin(Plugin):
|
|
|
|
def send_message(self, topic, msg, host, port=1883, *args, **kwargs):
|
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-05-27 12:21:37 +02:00
|
|
|
publisher.single(topic, str(msg), hostname=host, port=port)
|
|
|
|
return Response(output={'state': 'ok'})
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|