Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Plugin MQTT #498

Merged
merged 8 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
##### Added
- fhemCmd-Plugin: New plugin fhemCmd to execute commands in FHEM home automation. [#457](https://github.com/Schrolli91/BOSWatch/pull/457)
- Add field "ricFuncChar" in data structure for POC messages as a combination of "ric" and "functionChar" for using in RegEx filter. [#459](https://github.com/Schrolli91/BOSWatch/pull/459)
- MQTT: Plugin für Übermittlung von POSAC an einen MQTT-Broker [#498](
Schrolli91 marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/Schrolli91/BOSWatch/pull/498)
##### Changed
- Divera Plugin: Add individual alarms for FMS, ZVEI and POC. [#451](https://github.com/Schrolli91/BOSWatch/pull/451)
- install.sh: local git repo available at /opt/boswatch (or at your own path). Updates easier with `git pull` in /opt/boswatch. [#452](https://github.com/Schrolli91/BOSWatch/pull/452)
Expand Down
6 changes: 6 additions & 0 deletions config/config.template.ini
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ hue = 0
Divera = 0
gpiocontrol = 0
fhemCmd = 0
mqtt = 0

# for developing - template-module
template = 0
Expand Down Expand Up @@ -550,6 +551,11 @@ commandFMS = set SteckdoseSchlafzimmerEinsatz on-for-timer 90
commandZVEI =
commandPOC =

[mqtt]
#Adress from MQTT-Broker
brokeraddress = 192.168.178.27
topic = alarm/data

#####################
##### Not ready yet #
#####################
Expand Down
121 changes: 121 additions & 0 deletions plugins/mqtt/mqtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/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":
x = {
"fms": data["fms"],
"status": data["status"],
"direction": data["direction"],
"directionText": data["directionText"],
"tsi": data["tsi"],
"description": data["description"],
"timestamp": timeHandler.curtime()
}
elif typ == "ZVEI":
x = {
"zvei": data["zvei"],
"description": data["description"],
"timestamp": timeHandler.curtime()
}
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()
}
else:
logging.warning("Invalid Typ: %s", typ)

y = json.dumps(x)
mqttClient.connect(broker_address)
mqttClient.publish(topic,y)
########## User Plugin CODE ##########

except:
logging.error("unknown error")
logging.debug("unknown error", exc_info=True)
1 change: 1 addition & 0 deletions plugins/mqtt/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paho-mqtt