BOSWatch/plugins/Pushover/Pushover.py

122 lines
3.5 KiB
Python
Raw Normal View History

2016-09-29 14:20:05 +02:00
#!/usr/bin/python
2016-10-02 22:45:15 +02:00
# -*- coding: UTF-8 -*-
2016-09-29 14:20:05 +02:00
"""
Pushover-Plugin to send FMS-, ZVEI- and POCSAG - messages to Pushover Clients
@author: Ricardo Krippner
@requires: Pushover-Configuration has to be set in the config.ini
"""
import logging # Global logger
import httplib #for the HTTP request
import urllib
from includes import globalVars # Global variables
2016-09-29 14:20:05 +02:00
2016-11-20 13:34:24 +01:00
'from includes.helper import timeHandler
2016-09-29 14:20:05 +02:00
from includes.helper import configHandler
##
#
# 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
"""
# nothing to do for this plugin
return
##
#
# Main function of Pushover-plugin
# will be called by the alarmHandler
#
def run(typ,freq,data):
"""
This function is the implementation of the Pushover-Plugin.
It will send the data to Pushover API
@type typ: string (FMS|ZVEI|POC)
@param typ: Typ of the dataset
@type data: map of data (structure see readme.md in plugin folder)
2016-09-29 14:20:05 +02:00
@param data: Contains the parameter
@type freq: string
@keyword freq: frequency of the SDR Stick
@requires: Pushover-Configuration has to be set in the config.ini
@return: nothing
"""
try:
if configHandler.checkConfig("Pushover"): #read and debug the config
try:
#
# Pushover-Request
#
logging.debug("send Pushover %s", typ)
if data["function"] == '1':
2016-10-03 12:02:18 +02:00
priority = globalVars.config.get("Pushover", "SubA")
2016-09-29 14:20:05 +02:00
elif data["function"] == '2':
2016-10-03 12:02:18 +02:00
priority = globalVars.config.get("Pushover", "SubB")
2016-09-29 14:20:05 +02:00
elif data["function"] == '3':
2016-10-03 12:02:18 +02:00
priority = globalVars.config.get("Pushover", "SubC")
2016-09-29 14:20:05 +02:00
elif data["function"] == '4':
2016-10-03 12:02:18 +02:00
priority = globalVars.config.get("Pushover", "SubD")
2016-09-29 14:20:05 +02:00
else:
priority = 0
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
2016-10-03 12:02:18 +02:00
"token": globalVars.config.get("Pushover", "api_key"),
"user": globalVars.config.get("Pushover", "user_key"),
2016-09-29 14:20:05 +02:00
"message": "<b>"+data["description"]+"</b><br>"+data["msg"].replace(";", "<br>"),
2016-10-03 12:02:18 +02:00
"html": globalVars.config.get("Pushover", "html"),
"title": globalVars.config.get("Pushover", "title"),
2016-09-29 14:20:05 +02:00
"priority": priority,
2016-10-03 12:02:18 +02:00
"retry": globalVars.config.get("Pushover", "retry"),
"expire": globalVars.config.get("Pushover", "expire")
2016-09-29 14:20:05 +02:00
}),{"Content-type": "application/x-www-form-urlencoded"})
except:
logging.error("cannot send Pushover request")
logging.debug("cannot send Pushover request", exc_info=True)
return
else:
try:
#
# check Pushover-Response
#
response = conn.getresponse()
if str(response.status) == "200": #Check Pushover Response and print a Log or Error
logging.debug("Pushover response: %s - %s" , str(response.status), str(response.reason))
else:
logging.warning("Pushover response: %s - %s" , str(response.status), str(response.reason))
except: #otherwise
logging.error("cannot get Pushover response")
logging.debug("cannot get Pushover response", exc_info=True)
return
finally:
logging.debug("close Pushover-Connection")
try:
request.close()
except:
pass
except:
logging.error("unknown error")
logging.debug("unknown error", exc_info=True)