2019-10-15 08:42:09 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Divera-Plugin to send FMS-, ZVEI- and POCSAG - messages to Divera
|
|
|
|
|
@author: Marco Grosjohann
|
|
|
|
|
@requires: Divera-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
|
|
|
|
|
|
|
|
|
|
# from includes.helper import timeHandler
|
|
|
|
|
from includes.helper import configHandler
|
|
|
|
|
from includes.helper import wildcardHandler
|
|
|
|
|
|
2020-04-29 20:49:07 +02:00
|
|
|
def isSignal(poc_id):
|
|
|
|
|
"""
|
|
|
|
|
@type poc_id: string
|
|
|
|
|
@param poc_id: POCSAG Ric
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
|
|
|
|
|
@return: True if the Ric is Signal, other False
|
|
|
|
|
@exception: none
|
|
|
|
|
"""
|
|
|
|
|
# If RIC is Signal return True, else False
|
|
|
|
|
if globalVars.config.get("POC", "netIdent_ric"):
|
|
|
|
|
if poc_id in globalVars.config.get("POC", "netIdent_ric"):
|
|
|
|
|
logging.info("RIC %s is net ident", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logging.info("RIC %s is no net ident", poc_id)
|
|
|
|
|
return False
|
2019-10-15 08:42:09 +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
|
|
|
|
|
"""
|
|
|
|
|
# nothing to do for this plugin
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Main function of Divera-plugin
|
|
|
|
|
# will be called by the alarmHandler
|
|
|
|
|
#
|
|
|
|
|
def run(typ, freq, data):
|
|
|
|
|
"""
|
|
|
|
|
This function is the implementation of the Divera-Plugin.
|
|
|
|
|
It will send the data to Divera 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)
|
|
|
|
|
@param data: Contains the parameter
|
|
|
|
|
@type freq: string
|
|
|
|
|
@keyword freq: frequency of the SDR Stick
|
|
|
|
|
@requires: Divera-Configuration has to be set in the config.ini
|
|
|
|
|
@return: nothing
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
if configHandler.checkConfig("Divera"): # read and debug the config
|
|
|
|
|
|
|
|
|
|
if typ == "FMS":
|
|
|
|
|
#
|
|
|
|
|
# building message for FMS
|
|
|
|
|
#
|
|
|
|
|
text = globalVars.config.get("Divera", "fms_text")
|
2019-10-21 15:19:27 +02:00
|
|
|
title = globalVars.config.get("Divera", "fms_title")
|
2019-10-15 08:42:09 +02:00
|
|
|
priority = globalVars.config.get("Divera", "fms_prio")
|
2020-04-29 19:50:45 +02:00
|
|
|
vehicle = globalVars.config.get("Divera", "fms_vehicle")
|
2019-10-15 08:42:09 +02:00
|
|
|
|
|
|
|
|
elif typ == "ZVEI":
|
|
|
|
|
#
|
|
|
|
|
# building message for ZVEI
|
|
|
|
|
#
|
|
|
|
|
text = globalVars.config.get("Divera", "zvei_text")
|
2019-10-21 15:19:27 +02:00
|
|
|
title = globalVars.config.get("Divera", "zvei_title")
|
2020-04-29 19:57:44 +02:00
|
|
|
priority = globalVars.config.get("Divera","zvei_prio")
|
2020-04-29 19:50:45 +02:00
|
|
|
ric = globalVars.config.get("Divera","zvei_ric")
|
2019-10-15 08:42:09 +02:00
|
|
|
|
|
|
|
|
elif typ == "POC":
|
2020-05-01 11:38:36 +02:00
|
|
|
if isSignal(data["ric"]):
|
|
|
|
|
|
2020-05-02 13:03:18 +02:00
|
|
|
logging.debug("RIC is net ident")
|
|
|
|
|
return
|
|
|
|
|
else:
|
2020-05-01 11:38:36 +02:00
|
|
|
#
|
|
|
|
|
# building message for POC
|
|
|
|
|
#
|
|
|
|
|
if data["function"] == '1':
|
|
|
|
|
priority = globalVars.config.get("Divera", "SubA")
|
|
|
|
|
elif data["function"] == '2':
|
|
|
|
|
priority = globalVars.config.get("Divera", "SubB")
|
|
|
|
|
elif data["function"] == '3':
|
|
|
|
|
priority = globalVars.config.get("Divera", "SubC")
|
|
|
|
|
elif data["function"] == '4':
|
|
|
|
|
priority = globalVars.config.get("Divera", "SubD")
|
|
|
|
|
else:
|
|
|
|
|
priority = ''
|
2019-10-15 08:42:09 +02:00
|
|
|
|
2020-05-01 11:38:36 +02:00
|
|
|
text = globalVars.config.get("Divera", "poc_text")
|
|
|
|
|
title = globalVars.config.get("Divera", "poc_title")
|
|
|
|
|
ric = globalVars.config.get("Divera", "poc_ric")
|
2020-05-02 13:03:18 +02:00
|
|
|
|
2019-10-15 08:42:09 +02:00
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("Invalid type: %s", typ)
|
2019-10-15 10:46:38 +02:00
|
|
|
return
|
2019-10-15 10:16:12 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
#
|
|
|
|
|
# Divera-Request
|
|
|
|
|
#
|
|
|
|
|
logging.debug("send Divera for %s", typ)
|
|
|
|
|
|
|
|
|
|
# replace the wildcards
|
|
|
|
|
text = wildcardHandler.replaceWildcards(text, data)
|
2019-10-21 15:19:27 +02:00
|
|
|
title = wildcardHandler.replaceWildcards(title, data)
|
2020-05-04 18:23:22 +02:00
|
|
|
|
|
|
|
|
# replace the wildcards in FMS; RIC is used for ZVEI/POC
|
2020-04-29 19:50:45 +02:00
|
|
|
if typ == "FMS":
|
|
|
|
|
vehicle = wildcardHandler.replaceWildcards(vehicle, data)
|
|
|
|
|
else:
|
|
|
|
|
ric = wildcardHandler.replaceWildcards(ric, data)
|
|
|
|
|
|
2019-10-15 10:16:12 +02:00
|
|
|
|
|
|
|
|
# Logging data to send
|
2019-10-21 15:19:27 +02:00
|
|
|
logging.debug("Title : %s", title)
|
2020-05-04 18:23:22 +02:00
|
|
|
|
|
|
|
|
# ZVEI, POC is logged, if not FMS is used
|
2020-04-29 19:50:45 +02:00
|
|
|
if typ == "FMS":
|
|
|
|
|
logging.debug("Vehicle : %s", vehicle)
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("RIC : %s", ric)
|
2020-05-04 18:23:22 +02:00
|
|
|
|
|
|
|
|
# Logging normal
|
2019-10-15 10:16:12 +02:00
|
|
|
logging.debug("Text : %s", text)
|
|
|
|
|
logging.debug("Priority: %s", priority)
|
2019-10-15 08:42:09 +02:00
|
|
|
|
2019-10-15 10:16:12 +02:00
|
|
|
# check priority value
|
|
|
|
|
if (priority != 'false') and (priority != 'true'):
|
2019-11-04 08:19:41 +01:00
|
|
|
logging.info("No Priority set for type '%s'! Skipping Divera-Alarm!", typ)
|
|
|
|
|
return
|
2019-10-15 10:16:12 +02:00
|
|
|
|
2020-04-29 19:50:45 +02:00
|
|
|
# Check FMS
|
|
|
|
|
if typ == "FMS":
|
|
|
|
|
if (vehicle == ''):
|
|
|
|
|
logging.info("No Vehicle set!");
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
if (ric == ''):
|
|
|
|
|
logging.info("No RIC set!");
|
|
|
|
|
|
|
|
|
|
# start connection to Divera
|
|
|
|
|
if typ == "FMS":
|
|
|
|
|
# start the connection FMS
|
|
|
|
|
conn = httplib.HTTPSConnection("www.divera247.com:443")
|
|
|
|
|
conn.request("GET", "/api/fms",
|
|
|
|
|
urllib.urlencode({
|
|
|
|
|
"accesskey": globalVars.config.get("Divera", "accesskey"),
|
|
|
|
|
"vehicle_ric": vehicle,
|
2020-04-29 20:49:07 +02:00
|
|
|
"status_id": data["status"],
|
|
|
|
|
"status_note": data["directionText"],
|
2020-04-29 19:50:45 +02:00
|
|
|
"title": title,
|
|
|
|
|
"text": text,
|
|
|
|
|
"priority": priority,
|
|
|
|
|
}))
|
|
|
|
|
|
2020-04-29 20:49:07 +02:00
|
|
|
elif typ == "ZVEI":
|
2020-05-02 14:02:58 +02:00
|
|
|
# start connection ZVEI
|
2020-04-29 19:50:45 +02:00
|
|
|
conn = httplib.HTTPSConnection("www.divera247.com:443")
|
|
|
|
|
conn.request("GET", "/api/alarm",
|
|
|
|
|
urllib.urlencode({
|
|
|
|
|
"accesskey": globalVars.config.get("Divera", "accesskey"),
|
|
|
|
|
"title": title,
|
|
|
|
|
"ric": ric,
|
|
|
|
|
"text": text,
|
|
|
|
|
"priority": priority,
|
|
|
|
|
}))
|
2020-04-29 20:49:07 +02:00
|
|
|
|
|
|
|
|
elif typ == "POC":
|
|
|
|
|
# start connection POC
|
2020-05-01 11:38:36 +02:00
|
|
|
conn = httplib.HTTPSConnection("www.divera247.com:443")
|
|
|
|
|
conn.request("GET", "/api/alarm",
|
|
|
|
|
urllib.urlencode({
|
|
|
|
|
"accesskey": globalVars.config.get("Divera", "accesskey"),
|
|
|
|
|
"title": title,
|
|
|
|
|
"ric": ric,
|
|
|
|
|
"text": text,
|
|
|
|
|
"priority": priority,
|
|
|
|
|
}))
|
2020-04-30 23:48:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
loggin.debug("No Type is set", exc_info=True)
|
|
|
|
|
return
|
2019-10-15 10:16:12 +02:00
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
logging.error("cannot send Divera request")
|
|
|
|
|
logging.debug("cannot send Divera request", exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
#
|
|
|
|
|
# check Divera-Response
|
|
|
|
|
#
|
|
|
|
|
response = conn.getresponse()
|
|
|
|
|
if str(response.status) == "200": # Check Divera Response and print a Log or Error
|
|
|
|
|
logging.debug("Divera response: %s - %s", str(response.status), str(response.reason))
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("Divera response: %s - %s", str(response.status), str(response.reason))
|
|
|
|
|
except: # otherwise
|
|
|
|
|
logging.error("cannot get Divera response")
|
|
|
|
|
logging.debug("cannot get Divera response", exc_info=True)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
logging.debug("close Divera-Connection")
|
2019-10-15 08:42:09 +02:00
|
|
|
try:
|
2019-10-15 10:16:12 +02:00
|
|
|
request.close()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2019-10-15 08:42:09 +02:00
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
logging.error("unknown error")
|
|
|
|
|
logging.debug("unknown error", exc_info=True)
|