2015-07-19 20:29:22 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
notifyMyAndroid-Plugin to dispatch FMS-, ZVEI- and POCSAG-messages via UDP/TCP
|
|
|
|
|
|
|
|
|
|
@author: Jens Herrmann
|
|
|
|
|
|
|
|
|
|
@requires: notifyMyAndroid-Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging # Global logger
|
|
|
|
|
|
|
|
|
|
import socket # for connection
|
|
|
|
|
import json # for data-transfer
|
|
|
|
|
|
|
|
|
|
from includes import globals # Global variables
|
|
|
|
|
|
|
|
|
|
from includes.helper import configHandler
|
|
|
|
|
from includes.helper import timeHandler
|
|
|
|
|
from includes.helper import uft8Converter # UTF-8 converter
|
|
|
|
|
from includes.pynma import pynma
|
|
|
|
|
|
2015-07-28 20:24:57 +02:00
|
|
|
# local variables
|
|
|
|
|
application = "BOSWatch"
|
|
|
|
|
APIKey = None
|
|
|
|
|
|
2015-07-19 20:29:22 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
"""
|
2015-07-28 20:24:57 +02:00
|
|
|
# local variables
|
|
|
|
|
global application
|
|
|
|
|
global APIKey
|
|
|
|
|
|
|
|
|
|
# load config:
|
|
|
|
|
configHandler.checkConfig("notifyMyAndroid")
|
|
|
|
|
APIKey = globals.config.get("notifyMyAndroid","APIKey")
|
|
|
|
|
application = globals.config.get("notifyMyAndroid","appName")
|
|
|
|
|
|
2015-07-19 20:29:22 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Main function of jsonSocket-plugin
|
|
|
|
|
# will be called by the alarmHandler
|
|
|
|
|
#
|
|
|
|
|
def run(typ,freq,data):
|
|
|
|
|
"""
|
|
|
|
|
This function is the implementation of the notifyMyAndroid-Plugin.
|
|
|
|
|
|
|
|
|
|
The configuration is set in the config.ini.
|
|
|
|
|
|
|
|
|
|
@type typ: string (FMS|ZVEI|POC)
|
|
|
|
|
@param typ: Typ of the dataset for sending via UDP/TCP
|
|
|
|
|
@type data: map of data (structure see interface.txt)
|
|
|
|
|
@param data: Contains the parameter for dispatch to UDP.
|
|
|
|
|
@type freq: string
|
|
|
|
|
@keyword freq: frequency of the SDR Stick
|
|
|
|
|
|
|
|
|
|
@requires: notifyMyAndroid-Configuration has to be set in the config.ini
|
|
|
|
|
|
|
|
|
|
@return: nothing
|
|
|
|
|
"""
|
2015-07-28 20:24:57 +02:00
|
|
|
# local variables
|
|
|
|
|
global application
|
|
|
|
|
global APIKey
|
|
|
|
|
|
2015-07-19 20:29:22 +02:00
|
|
|
try:
|
2015-07-28 20:24:57 +02:00
|
|
|
try:
|
|
|
|
|
#
|
|
|
|
|
# initialize to pyNMA
|
|
|
|
|
#
|
|
|
|
|
nma = pynma.PyNMA(APIKey)
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
logging.error("cannot initialize pyNMA")
|
|
|
|
|
logging.debug("cannot initialize %s-socket", exc_info=True)
|
|
|
|
|
# Without class, plugin couldn't work
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# toDo is equals for all types, so only check if typ is supported
|
|
|
|
|
supportedTypes = ["FMS", "ZVEI", "POC"]
|
|
|
|
|
if typ in supportedTypes:
|
|
|
|
|
logging.debug("Start %s to NMA", typ)
|
|
|
|
|
try:
|
|
|
|
|
# send data
|
|
|
|
|
event = data['description']
|
|
|
|
|
msg = timeHandler.curtime()
|
|
|
|
|
if len(data['msg']) > 0:
|
|
|
|
|
msg += "\n" + data['msg']
|
|
|
|
|
response = nma.push(application, uft8Converter.convertToUTF8(event), uft8Converter.convertToUTF8(msg), priority=globals.config.getint("notifyMyAndroid","priority"))
|
|
|
|
|
except:
|
|
|
|
|
logging.error("%s to NMA failed", typ)
|
|
|
|
|
logging.debug("%s to NMA failed", typ, exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
else:
|
2015-07-19 20:29:22 +02:00
|
|
|
try:
|
2015-07-28 20:24:57 +02:00
|
|
|
#
|
|
|
|
|
# check HTTP-Response
|
|
|
|
|
#
|
|
|
|
|
if str(response[APIKey]['code']) == "200": #Check HTTP Response an print a Log or Error
|
|
|
|
|
logging.debug("NMA response: %s" , str(response[APIKey]['code']))
|
|
|
|
|
if int(response[APIKey]['remaining']) == 0:
|
|
|
|
|
logging.error("NMA remaining msgs: %s" , str(response[APIKey]['remaining']))
|
|
|
|
|
if int(response[APIKey]['remaining']) < 20:
|
|
|
|
|
logging.warning("NMA remaining msgs: %s" , str(response[APIKey]['remaining']))
|
2015-07-19 20:29:22 +02:00
|
|
|
else:
|
2015-07-28 20:24:57 +02:00
|
|
|
logging.debug("NMA remaining msgs: %s" , str(response[APIKey]['remaining']))
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("NMA response: %s - %s" , str(response[APIKey]['code']), str(response[APIKey]['message']))
|
|
|
|
|
except: #otherwise
|
|
|
|
|
logging.error("cannot read pynma response")
|
|
|
|
|
logging.debug("cannot read pynma response", exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("Invalid Typ: %s", typ)
|
2015-07-19 20:29:22 +02:00
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
# something very mysterious
|
|
|
|
|
logging.error("unknown error")
|
2015-07-28 20:24:57 +02:00
|
|
|
logging.debug("unknown error", exc_info=True)
|