BOSWatch/plugins/FFAgent/FFAgent.py

174 lines
5.2 KiB
Python
Raw Normal View History

2016-09-29 14:21:35 +02:00
#!/usr/bin/python
2016-10-02 22:45:15 +02:00
# -*- coding: UTF-8 -*-
2016-09-29 14:21:35 +02:00
"""
FFAgent-Plugin to send FMS-, ZVEI- and POCSAG - messages to FF-Agent
@author: Ricardo Krippner
@requires: FFAgent-Configuration has to be set in the config.ini
"""
import logging # Global logger
import hmac, hashlib
import json, requests
from includes import globalVars # Global variables
2016-09-29 14:21:35 +02:00
2016-11-20 13:37:29 +01:00
#from includes.helper import timeHandler
2016-09-29 14:21:35 +02:00
from includes.helper import configHandler
# needed for Ordered Dictionaries to serve correctly ordered JSON
from collections import OrderedDict
2016-09-29 14:21:35 +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 FFAgent-plugin
# will be called by the alarmHandler
#
def run(typ,freq,data):
"""
This function is the implementation of the FFAgent-Plugin.
It will send the data to FFAgent Webservice API
Documentation here:
http://free.ff-agent.com/app/public/docs/Dokumentation_WebAPI.pdf
@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:21:35 +02:00
@param data: Contains the parameter
@type freq: string
@keyword freq: frequency of the SDR Stick
@requires: FFAgent-Configuration has to be set in the config.ini
@return: nothing
"""
try:
if configHandler.checkConfig("FFAgent"): #read and debug the config
try:
#
# FFAgent-Request
#
logging.debug("send FFAgent %s", typ)
2016-10-03 12:02:18 +02:00
if globalVars.config.get("FFAgent", "live") == "1":
2016-09-29 14:21:35 +02:00
url = "https://api.service.ff-agent.com/v1/WebService/triggerAlarm"
else:
url = "https://free.api.service.ff-agent.com/v1/WebService/triggerAlarm"
2016-10-03 12:02:18 +02:00
serverCertFile = globalVars.config.get("FFAgent", "serverCertFile")
clientCertFile = globalVars.config.get("FFAgent", "clientCertFile")
clientCertPass = globalVars.config.get("FFAgent", "clientCertPass")
webApiToken = globalVars.config.get("FFAgent", "webApiToken")
webApiKey = globalVars.config.get("FFAgent", "webApiKey")
accessToken = globalVars.config.get("FFAgent", "accessToken")
selectiveCallCode = globalVars.config.get("FFAgent", "selectiveCallCode")
2016-09-29 14:21:35 +02:00
# data["description"]
msg_split = data["msg"].split(';')
alarmData = {
"alarmDate" : "",
"keyword" : msg_split[0],
"type" : "",
"message" : data["msg"],
"note" : msg_split[5],
"operationResources" : "",
"operationSchedule" : "",
"object" : msg_split[2],
"location" : msg_split[3] + " " + msg_split[4],
"district" : msg_split[1],
"lat" : "",
"lng" : "",
"easting" : "",
"northing" : "",
"alarmMessage" : ""
}
2016-10-03 12:02:18 +02:00
if globalVars.config.get("FFAgent", "test") == "1":
2016-09-29 14:21:35 +02:00
alarmData = {
"alarmDate" : "",
"keyword" : "Test",
"type" : "Test",
"message" : data["msg"],
"note" : msg_split[5],
"operationResources" : "",
"operationSchedule" : "",
"object" : msg_split[2],
"location" : msg_split[3] + " " + msg_split[4],
"district" : msg_split[1],
"lat" : "",
"lng" : "",
"easting" : "",
"northing" : "",
"alarmMessage" : ""
}
alarmData = json.dumps(alarmData)
logging.debug(alarmData)
alarmHeaders = OrderedDict([
("Content-Type", "application/json"),
("webApiToken", webApiToken),
("accessToken", accessToken),
("selectiveCallCode", selectiveCallCode),
("hmac", hmac.new(webApiKey, webApiToken + selectiveCallCode + accessToken + alarmData, digestmod=hashlib.sha256).hexdigest())
])
logging.debug(alarmHeaders)
2016-09-29 14:21:35 +02:00
2016-10-03 12:02:18 +02:00
if globalVars.config.get("FFAgent", "live") == "1":
2018-02-13 18:31:40 +01:00
r = requests.post(url, data=alarmData, headers=alarmHeaders, verify=serverCertFile, cert=(clientCertFile, clientCertPass))
2016-09-29 14:21:35 +02:00
else:
r = requests.post(url, data=alarmData, headers=alarmHeaders, verify=serverCertFile)
except:
logging.error("cannot send FFAgent request")
logging.debug("cannot send FFAgent request", exc_info=True)
return
else:
try:
#
# check FFAgent-Response
#
if r.status_code == requests.codes.ok: #Check FFAgent Response and print a Log or Error
logging.debug("FFAgent response: %s" , str(r.status_code))
else:
logging.warning("FFAgent response: %s" , str(r.status_code))
except: #otherwise
logging.error("cannot get FFAgent response")
logging.debug("cannot get FFAgent response", exc_info=True)
return
finally:
logging.debug("close FFAgent-Connection")
try:
r.close()
except:
pass
except:
logging.error("unknown error")
logging.debug("unknown error", exc_info=True)