From b14fc23c8c012c9e92ed09adb650e3b8a36b4840 Mon Sep 17 00:00:00 2001 From: MrDuckSpace Date: Fri, 18 Dec 2020 15:15:38 +0100 Subject: [PATCH] Added Plugin MQTT --- config/config.template.ini | 10 ++++ plugins/mqtt/mqtt.py | 108 ++++++++++++++++++++++++++++++++++ plugins/mqtt/requirements.txt | 1 + 3 files changed, 119 insertions(+) create mode 100644 plugins/mqtt/mqtt.py create mode 100644 plugins/mqtt/requirements.txt diff --git a/config/config.template.ini b/config/config.template.ini index af5da01..21d50a9 100644 --- a/config/config.template.ini +++ b/config/config.template.ini @@ -182,6 +182,7 @@ yowsup = 0 hue = 0 Divera = 0 gpiocontrol = 0 +mqtt = 0 # for developing - template-module template = 0 @@ -519,6 +520,15 @@ triggertime = 180 #POC Rics that trigger PIN (empty: allow all, separator ",") activerics = 1234567,1234568 +[mqtt] +#Plugin for publish POCSAG Alarm wit MQTT + +#Adress from MQTT-Broker (Example: 192.168.178.27) +brokeraddress = + +#Example: +#topic = alarm/posac +topic = ##################### ##### Not ready yet # diff --git a/plugins/mqtt/mqtt.py b/plugins/mqtt/mqtt.py new file mode 100644 index 0000000..ed121b6 --- /dev/null +++ b/plugins/mqtt/mqtt.py @@ -0,0 +1,108 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +""" +@author: KS + +@requires: paho-mqtt +""" + +# +# Imports +# +import logging # Global logger +from includes import globalVars # Global variables + +# Helper function, uncomment to use +from includes.helper import timeHandler +from includes.helper import wildcardHandler +from includes.helper import configHandler + +import paho.mqtt.client as mqtt +import json + +## +# +# onLoad (init) function of plugin +# will be called one time by the pluginLoader on start +# +def onLoad(): + """ + While loading the plugins by pluginLoader.loadPlugins() + this onLoad() routine is called one time for initialize the plugin + + @requires: nothing + + @return: nothing + @exception: Exception if init has an fatal error so that the plugin couldn't work + + """ + try: + ########## User onLoad CODE ########## + pass + ########## User onLoad CODE ########## + except: + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) + raise + +## +# +# Main function of plugin +# will be called by the alarmHandler +# +def run(typ,freq,data): + """ + This function is the implementation of the Plugin. + + If necessary the configuration hast to be set in the config.ini. + + @type typ: string (FMS|ZVEI|POC) + @param typ: Typ of the dataset + @type data: map of data (structure see readme.md in plugin folder) + @param data: Contains the parameter for dispatch + @type freq: string + @keyword freq: frequency of the SDR Stick + + @requires: If necessary the configuration hast to be set in the config.ini. + + @return: nothing + @exception: nothing, make sure this function will never thrown an exception + """ + try: + if configHandler.checkConfig("mqtt"): #read and debug the config (let empty if no config used) + + logging.debug(globalVars.config.get("mqtt", "brokeraddress")) + logging.debug(globalVars.config.get("mqtt", "topic")) + ########## User Plugin CODE ########## + broker_address = globalVars.config.get("mqtt", "brokeraddress") + topic = globalVars.config.get("mqtt", "topic") + mqttClient = mqtt.Client() + + if typ == "FMS": + logging.warning("%s not supported", typ) + elif typ == "ZVEI": + logging.warning("%s not supported", typ) + elif typ == "POC": + functionText = "%FUNCTEXT%" + functionText = wildcardHandler.replaceWildcards(functionText, data) + x = { + "ric": data["ric"], + "function": data["function"], + "functionText": functionText, + "functionChar": data["functionChar"], + "msg": data["msg"], + "bitrate": data["bitrate"], + "description": data["description"], + "timestamp": timeHandler.curtime() + } + y = json.dumps(x) + mqttClient.connect(broker_address) + mqttClient.publish(topic,y) + else: + logging.warning("Invalid Typ: %s", typ) + ########## User Plugin CODE ########## + + except: + logging.error("unknown error") + logging.debug("unknown error", exc_info=True) diff --git a/plugins/mqtt/requirements.txt b/plugins/mqtt/requirements.txt new file mode 100644 index 0000000..8579e8b --- /dev/null +++ b/plugins/mqtt/requirements.txt @@ -0,0 +1 @@ +paho-mqtt